Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ESLint selectors in custom rules #17572

Merged
merged 4 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions tools/eslint-rules/buffer-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@
const msg = 'Use of the Buffer() constructor has been deprecated. ' +
'Please use either Buffer.alloc(), Buffer.allocUnsafe(), ' +
'or Buffer.from()';

function test(context, node) {
if (node.callee.name === 'Buffer') {
context.report(node, msg);
}
}
const astSelector = 'NewExpression[callee.name="Buffer"],' +
'CallExpression[callee.name="Buffer"]';

module.exports = function(context) {
return {
'NewExpression': (node) => test(context, node),
'CallExpression': (node) => test(context, node)
[astSelector]: (node) => context.report(node, msg)
};
};
46 changes: 14 additions & 32 deletions tools/eslint-rules/no-let-in-for-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,28 @@
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const message = 'Use of `let` as the loop variable in a for-loop is ' +
'not recommended. Please use `var` instead.';
const forSelector = 'ForStatement[init.kind="let"]';
const forInOfSelector = 'ForOfStatement[left.kind="let"],' +
'ForInStatement[left.kind="let"]';

module.exports = {
create(context) {
const sourceCode = context.getSourceCode();
const msg = 'Use of `let` as the loop variable in a for-loop is ' +
'not recommended. Please use `var` instead.';

/**
* Report function to test if the for-loop is declared using `let`.
*/
function testForLoop(node) {
if (node.init && node.init.kind === 'let') {
context.report({
node: node.init,
message: msg,
fix: (fixer) =>
fixer.replaceText(sourceCode.getFirstToken(node.init), 'var')
});
}
}

/**
* Report function to test if the for-in or for-of loop
* is declared using `let`.
*/
function testForInOfLoop(node) {
if (node.left && node.left.kind === 'let') {
context.report({
node: node.left,
message: msg,
fix: (fixer) =>
fixer.replaceText(sourceCode.getFirstToken(node.left), 'var')
});
}
function report(node) {
context.report({
node,
message,
fix: (fixer) =>
fixer.replaceText(sourceCode.getFirstToken(node), 'var')
});
}

return {
'ForStatement': testForLoop,
'ForInStatement': testForInOfLoop,
'ForOfStatement': testForInOfLoop
[forSelector]: (node) => report(node.init),
[forInOfSelector]: (node) => report(node.left),
};
}
};
27 changes: 8 additions & 19 deletions tools/eslint-rules/prefer-assert-methods.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
'use strict';

function isAssert(node) {
return node.expression &&
node.expression.type === 'CallExpression' &&
node.expression.callee &&
node.expression.callee.name === 'assert';
}

function getFirstArg(expression) {
return expression.arguments && expression.arguments[0];
}
const astSelector = 'ExpressionStatement[expression.type="CallExpression"]' +
'[expression.callee.name="assert"]' +
'[expression.arguments.0.type="BinaryExpression"]';

function parseError(method, op) {
return `'assert.${method}' should be used instead of '${op}'`;
Expand All @@ -24,15 +17,11 @@ const preferedAssertMethod = {

module.exports = function(context) {
return {
ExpressionStatement(node) {
if (isAssert(node)) {
const arg = getFirstArg(node.expression);
if (arg && arg.type === 'BinaryExpression') {
const assertMethod = preferedAssertMethod[arg.operator];
if (assertMethod) {
context.report(node, parseError(assertMethod, arg.operator));
}
}
[astSelector]: function(node) {
const arg = node.expression.arguments[0];
const assertMethod = preferedAssertMethod[arg.operator];
if (assertMethod) {
context.report(node, parseError(assertMethod, arg.operator));
}
}
};
Expand Down
35 changes: 13 additions & 22 deletions tools/eslint-rules/prefer-common-mustnotcall.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,21 @@

const msg = 'Please use common.mustNotCall(msg) instead of ' +
'common.mustCall(fn, 0) or common.mustCall(0).';

function isCommonMustCall(node) {
return node &&
node.callee &&
node.callee.object &&
node.callee.object.name === 'common' &&
node.callee.property &&
node.callee.property.name === 'mustCall';
}

function isArgZero(argument) {
return argument &&
typeof argument.value === 'number' &&
argument.value === 0;
}
const mustCallSelector = 'CallExpression[callee.object.name="common"]' +
'[callee.property.name="mustCall"]';
const arg0Selector = `${mustCallSelector}[arguments.0.value=0]`;
const arg1Selector = `${mustCallSelector}[arguments.1.value=0]`;

module.exports = function(context) {
function report(node) {
context.report(node, msg);
}

return {
CallExpression(node) {
if (isCommonMustCall(node) &&
(isArgZero(node.arguments[0]) || // catch common.mustCall(0)
isArgZero(node.arguments[1]))) { // catch common.mustCall(fn, 0)
context.report(node, msg);
}
}
// Catch common.mustCall(0)
[arg0Selector]: report,

// Catch common.mustCall(fn, 0)
[arg1Selector]: report
};
};