Skip to content

Breaking: Update fixer-return and prefer-replace-text rules to also apply to suggestion fixer functions #194

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

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
12 changes: 1 addition & 11 deletions lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,12 @@ module.exports = {

// Stacks this function's information.
onCodePathStart (codePath, node) {
const parent = node.parent;

// Whether we are inside the fixer function we care about.
const shouldCheck = ['FunctionExpression', 'ArrowFunctionExpression'].includes(node.type) &&
parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.type === 'CallExpression' &&
contextIdentifiers.has(parent.parent.parent.callee.object) &&
parent.parent.parent.callee.property.name === 'report' &&
utils.getReportInfo(parent.parent.parent.arguments).fix === node;

funcInfo = {
upper: funcInfo,
codePath,
hasYieldWithFixer: false,
hasReturnWithFixer: false,
shouldCheck,
shouldCheck: utils.isAutoFixerFunction(node, contextIdentifiers) || utils.isSuggestionFixerFunction(node, contextIdentifiers),
node,
};
},
Expand Down
10 changes: 1 addition & 9 deletions lib/rules/prefer-replace-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,10 @@ module.exports = {

// Stacks this function's information.
onCodePathStart (codePath, node) {
const parent = node.parent;
const shouldCheck = (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') &&
parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.type === 'CallExpression' &&
contextIdentifiers.has(parent.parent.parent.callee.object) &&
parent.parent.parent.callee.property.name === 'report' &&
utils.getReportInfo(parent.parent.parent.arguments, context).fix === node;

funcInfo = {
upper: funcInfo,
codePath,
shouldCheck,
shouldCheck: utils.isAutoFixerFunction(node, contextIdentifiers) || utils.isSuggestionFixerFunction(node, contextIdentifiers),
node,
};
},
Expand Down
40 changes: 40 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,44 @@ module.exports = {
),
];
},

/**
* Whether the provided node represents an autofixer function.
* @param {Node} node
* @param {Node[]} contextIdentifiers
* @returns {boolean}
*/
isAutoFixerFunction (node, contextIdentifiers) {
const parent = node.parent;
return ['FunctionExpression', 'ArrowFunctionExpression'].includes(node.type) &&
parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.type === 'CallExpression' &&
contextIdentifiers.has(parent.parent.parent.callee.object) &&
parent.parent.parent.callee.property.name === 'report' &&
module.exports.getReportInfo(parent.parent.parent.arguments).fix === node;
},

/**
* Whether the provided node represents a suggestion fixer function.
* @param {Node} node
* @param {Node[]} contextIdentifiers
* @returns {boolean}
*/
isSuggestionFixerFunction (node, contextIdentifiers) {
const parent = node.parent;
return (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') &&
parent.type === 'Property' &&
parent.key.type === 'Identifier' &&
parent.key.name === 'fix' &&
parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.type === 'ArrayExpression' &&
parent.parent.parent.parent.type === 'Property' &&
parent.parent.parent.parent.key.type === 'Identifier' &&
parent.parent.parent.parent.key.name === 'suggest' &&
parent.parent.parent.parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.parent.parent.parent.type === 'CallExpression' &&
contextIdentifiers.has(parent.parent.parent.parent.parent.parent.callee.object) &&
parent.parent.parent.parent.parent.parent.callee.property.name === 'report' &&
module.exports.getReportInfo(parent.parent.parent.parent.parent.parent.arguments).suggest === parent.parent.parent;
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a few tests for it, in tests/lib/utils.js?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests.

};
87 changes: 87 additions & 0 deletions tests/lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,55 @@ ruleTester.run('fixer-return', rule, {
}
};
`,

// Suggestion
`
module.exports = {
create: function(context) {
context.report( {
suggest: [
{
fix: function(fixer) {
return fixer.foo();
}
}
]
});
}
};
`,
// Suggestion but wrong `suggest` key
`
module.exports = {
create: function(context) {
context.report( {
notSuggest: [
{
fix: function(fixer) {
fixer.foo();
}
}
]
});
}
};
`,
// Suggestion but wrong `fix` key
`
module.exports = {
create: function(context) {
context.report( {
suggest: [
{
notFix: function(fixer) {
fixer.foo();
}
}
]
});
}
};
`,
],

invalid: [
Expand All @@ -239,6 +288,25 @@ ruleTester.run('fixer-return', rule, {
`,
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 5, column: 24 }],
},
{
// Fix but missing return (suggestion)
code: `
module.exports = {
create: function(context) {
context.report({
suggest: [
{
fix(fixer) {
fixer.foo();
}
}
]
});
}
};
`,
errors: [{ messageId: 'missingFix', type: 'FunctionExpression', line: 7, column: 36 }],
},
{
// Fix but missing return (arrow function, report on arrow)
code: `
Expand All @@ -254,6 +322,25 @@ ruleTester.run('fixer-return', rule, {
`,
errors: [{ messageId: 'missingFix', type: 'ArrowFunctionExpression', line: 5, endLine: 5, column: 34, endColumn: 36 }],
},
{
// Fix but missing return (arrow function, report on arrow, suggestion)
code: `
module.exports = {
create: function(context) {
context.report({
suggest: [
{
fix: (fixer) => {
fixer.foo();
}
}
]
});
}
};
`,
errors: [{ messageId: 'missingFix', type: 'ArrowFunctionExpression', line: 7, endLine: 7, column: 46, endColumn: 48 }],
},
{
// With no autofix (arrow function, explicit return, report on arrow)
code: `
Expand Down
49 changes: 42 additions & 7 deletions tests/lib/rules/prefer-replace-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const RuleTester = require('eslint').RuleTester;
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
const ERROR = { messageId: 'useReplaceText', type: 'CallExpression' };


ruleTester.run('prefer-placeholders', rule, {
valid: [
Expand Down Expand Up @@ -52,6 +50,23 @@ ruleTester.run('prefer-placeholders', rule, {
`
fixer.replaceTextRange([node.range[0], node.range[1]], '');
`,

// Suggestion
`
module.exports = {
create(context) {
context.report({
suggest: [
{
fix(fixer) {
return fixer.replaceTextRange([start, end], '');
}
}
]
});
}
};
`,
],

invalid: [
Expand All @@ -67,7 +82,7 @@ ruleTester.run('prefer-placeholders', rule, {
}
};
`,
errors: [ERROR],
errors: [{ messageId: 'useReplaceText', type: 'CallExpression' }],
},
{
code: `
Expand All @@ -81,7 +96,7 @@ ruleTester.run('prefer-placeholders', rule, {
}
};
`,
errors: [ERROR],
errors: [{ messageId: 'useReplaceText', type: 'CallExpression' }],
},
{
code: `
Expand All @@ -95,7 +110,7 @@ ruleTester.run('prefer-placeholders', rule, {
}
};
`,
errors: [ERROR],
errors: [{ messageId: 'useReplaceText', type: 'CallExpression' }],
},
{
code: `
Expand All @@ -107,7 +122,7 @@ ruleTester.run('prefer-placeholders', rule, {
}
};
`,
errors: [ERROR],
errors: [{ messageId: 'useReplaceText', type: 'CallExpression' }],
},
{
code: `
Expand All @@ -121,7 +136,27 @@ ruleTester.run('prefer-placeholders', rule, {
}
};
`,
errors: [ERROR],
errors: [{ messageId: 'useReplaceText', type: 'CallExpression' }],
},

{
// Suggestion fixer
code: `
module.exports = {
create(context) {
context.report({
suggest: [
{
fix(fixer) {
return fixer.replaceTextRange([node.range[0], node.range[1]], '');
}
}
]
});
}
};
`,
errors: [{ messageId: 'useReplaceText', type: 'CallExpression' }],
},
],
});
53 changes: 53 additions & 0 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,57 @@ describe('utils', () => {
}
});
});

describe('isAutoFixerFunction / isSuggestionFixerFunction', () => {
const CASES = {
// isAutoFixerFunction
'context.report({ fix(fixer) {} });' (ast) {
return { expected: true, node: ast.body[0].expression.arguments[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isAutoFixerFunction };
},
'context.notReport({ fix(fixer) {} });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isAutoFixerFunction };
},
'context.report({ notFix(fixer) {} });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isAutoFixerFunction };
},
'notContext.report({ notFix(fixer) {} });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value, context: undefined, fn: utils.isAutoFixerFunction };
},

// isSuggestionFixerFunction
'context.report({ suggest: [{ fix(fixer) {} }] });' (ast) {
return { expected: true, node: ast.body[0].expression.arguments[0].properties[0].value.elements[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isSuggestionFixerFunction };
},
'context.notReport({ suggest: [{ fix(fixer) {} }] });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value.elements[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isSuggestionFixerFunction };
},
'context.report({ notSuggest: [{ fix(fixer) {} }] });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value.elements[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isSuggestionFixerFunction };
},
'context.report({ suggest: [{ notFix(fixer) {} }] });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value.elements[0].properties[0].value, context: ast.body[0].expression.callee.object, fn: utils.isSuggestionFixerFunction };
},
'notContext.report({ suggest: [{ fix(fixer) {} }] });' (ast) {
return { expected: false, node: ast.body[0].expression.arguments[0].properties[0].value, context: undefined, fn: utils.isSuggestionFixerFunction };
},
};

Object.keys(CASES).forEach(ruleSource => {
it(ruleSource, () => {
const ast = espree.parse(ruleSource, { ecmaVersion: 6, range: true });

// Add parent to each node.
estraverse.traverse(ast, {
enter (node, parent) {
node.parent = parent;
},
});

const testCase = CASES[ruleSource](ast);
const contextIdentifiers = new Set([testCase.context]);
const result = testCase.fn(testCase.node, contextIdentifiers);
assert.strictEqual(result, testCase.expected);
});
});
});
});