Skip to content

Fix: allow to use generator function as fix in fixer-return #75

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
merged 1 commit into from
Dec 22, 2018
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
npm-debug.log
.vscode
.idea
yarn.lock
15 changes: 14 additions & 1 deletion lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = {
upper: null,
codePath: null,
hasReturn: false,
hasYield: false,
shouldCheck: false,
node: null,
};
Expand All @@ -48,7 +49,11 @@ module.exports = {
* @returns {void}
*/
function checkLastSegment (node) {
if (funcInfo.shouldCheck && funcInfo.codePath.currentSegments.some(segment => segment.reachable)) {
if (
funcInfo.shouldCheck &&
funcInfo.codePath.currentSegments.some(segment => segment.reachable) &&
(!node.generator || !funcInfo.hasYield)
) {
context.report({
node,
loc: (node.id || node).loc.start,
Expand All @@ -75,6 +80,7 @@ module.exports = {
funcInfo = {
upper: funcInfo,
codePath,
hasYield: false,
hasReturn: false,
shouldCheck,
node,
Expand All @@ -86,6 +92,13 @@ module.exports = {
funcInfo = funcInfo.upper;
},

// Yield in generators
YieldExpression () {
if (funcInfo.shouldCheck) {
funcInfo.hasYield = true;
}
},

// Checks the return statement is valid.
ReturnStatement (node) {
if (funcInfo.shouldCheck) {
Expand Down
39 changes: 39 additions & 0 deletions tests/lib/rules/fixer-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ ruleTester.run('fixer-return', rule, {
};
`,
`
module.exports = {
create: function(context) {
context.report( {
fix: function(fixer) {
return [
fixer.foo(),
fixer.bar()
];
}
});
}
};
`,
`
module.exports = {
create: function(context) {
context.report({
Expand All @@ -41,6 +55,17 @@ ruleTester.run('fixer-return', rule, {
}
};
`,
`
module.exports = {
create: function (context) {
context.report({
fix: function* (fixer) {
yield fixer.foo();
}
});
}
};
`,
],

invalid: [
Expand All @@ -58,5 +83,19 @@ ruleTester.run('fixer-return', rule, {
`,
errors: [ERROR],
},
{
code: `
module.exports = {
create: function(context) {
context.report({
*fix(fixer) {
fixer.foo();
}
});
}
};
`,
errors: [ERROR],
},
],
});