Skip to content

Commit

Permalink
Port custom JSCS rules to ESLint
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Dec 8, 2016
1 parent eefd236 commit f8f080c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = {
'WeakMap': true,
},
rules: {
'require-yuidoc-access': 'error',
'no-const-outside-module-scope': 'error',

// temporarily disabled
'no-unused-vars': 'off',
'comma-dangle': 'off',
Expand Down
29 changes: 29 additions & 0 deletions lib/eslint-rules/no-const-outside-module-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = function(context) {
return {
VariableDeclaration: function(node) {
if (node.kind !== 'const') {
return;
}

if (node.parent && node.parent.type === 'Program') {
// Declaration is in root of module.
return;
}

if (node.parent && node.parent.type === 'ExportNamedDeclaration' &&
node.parent.parent && node.parent.parent.type === 'Program') {
// Declaration is a `export const foo = 'asdf'` in root of the module.
return;
}

context.report({
node: node,
message: '`const` should only be used in module scope (not inside functions/blocks).'
});
}
};
};

module.exports.schema = []; // no options
34 changes: 34 additions & 0 deletions lib/eslint-rules/require-yuidoc-access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

function isDocComment(comment) {
return comment.value[0] === '*';
}

function isModuleOnlyComment(comment) {
return comment.value.match(/^\*\n\s*@module.+\n(?:\s*@submodule.+\n)?$/);
}

function includesAccessDeclaration(comment) {
return comment.value.match(/\n\s*(@private|@public|@protected)\s/);
}

module.exports = function(context) {

var sourceCode = context.getSourceCode();

sourceCode.getAllComments().forEach(function(comment) {
if (comment.type !== 'Block') { return; }
if (!isDocComment(comment)) { return; }
if (isModuleOnlyComment(comment)) { return; }
if (includesAccessDeclaration(comment)) { return; }

context.report({
loc: comment.loc.start,
message: 'Access declaration missing, you must supply `@public`, `@private`, or `@protected` for doc comments.'
});
});

return {};
};

module.exports.schema = []; // no options

0 comments on commit f8f080c

Please sign in to comment.