-
-
Notifications
You must be signed in to change notification settings - Fork 30
New: fixer-return #15
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
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e4e805b
New: fixer-return
aladdin-add 2252f3b
wip
aladdin-add a30976f
Update: utils.
aladdin-add 7c98305
Update: check context.report({fix})
aladdin-add 8835fce
Update: support old-style context.report calls.
aladdin-add 646d28f
Docs: fixer-return.md
aladdin-add d4fac84
Docs: fixer-return in readme.md
aladdin-add 1aed6f9
Fix: accept review suggestions.
aladdin-add File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Enforces always return from a fixer function (fixer-return) | ||
|
||
In a fixable rule, missing return from a fixer function will not apply fixes. | ||
|
||
## Rule Details | ||
|
||
This rule enforces always return from a fixer function. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/fixer-return: error */ | ||
module.exports = { | ||
create: function(context) { | ||
context.report( { | ||
fix: function(fixer) { | ||
fixer.foo(); | ||
} | ||
}); | ||
} | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/fixer-return: error */ | ||
module.exports = { | ||
create: function(context) { | ||
context.report( { | ||
fix: function(fixer) { | ||
return fixer.foo(); | ||
} | ||
}); | ||
} | ||
}; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't want to enforce always return from a fixer function, do not enable this rule. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* @fileoverview Enforces always return from a fixer function | ||
* @author 薛定谔的猫<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Expected fixer function to always return a value.', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
fixable: null, | ||
}, | ||
|
||
create (context) { | ||
const message = 'Expected fixer function to always return a value.'; | ||
let funcInfo = { | ||
upper: null, | ||
codePath: null, | ||
hasReturn: false, | ||
shouldCheck: false, | ||
node: null, | ||
}; | ||
let contextIdentifiers; | ||
|
||
/** | ||
* Checks whether or not the last code path segment is reachable. | ||
* Then reports this function if the segment is reachable. | ||
* | ||
* If the last code path segment is reachable, there are paths which are not | ||
* returned or thrown. | ||
* | ||
* @param {ASTNode} node - A node to check. | ||
* @returns {void} | ||
*/ | ||
function checkLastSegment (node) { | ||
if (funcInfo.shouldCheck && funcInfo.codePath.currentSegments.some(segment => segment.reachable)) { | ||
context.report({ | ||
node, | ||
loc: (node.id || node).loc.start, | ||
message, | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
Program (node) { | ||
contextIdentifiers = utils.getContextIdentifiers(context, node); | ||
}, | ||
|
||
// Stacks this function's information. | ||
onCodePathStart (codePath, node) { | ||
const parent = node.parent; | ||
const shouldCheck = node.type === 'FunctionExpression' && | ||
parent.parent.type === 'ObjectExpression' && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will fail for the old-style context.report(node, 'foo', {}, fixer => {}); You can use I think the best way to do it would be to use something like this: const shouldCheck = node.type === 'FunctionExpression' &&
- node.body.type === 'BlockStatement' &&
- // check if it is context.report({fix})
- utils.getKeyName(parent) === 'fix' &&
parent.parent.type === 'ObjectExpression' &&
parent.parent.parent.type === 'CallExpression' &&
contextIdentifiers.has(parent.parent.parent.callee.object) &&
- parent.parent.parent.callee.property.name === 'report';
+ parent.parent.parent.callee.property.name === 'report' &&
+ utils.getReportInfo(parent.parent.parent.arguments).fix === node; |
||
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, | ||
hasReturn: false, | ||
shouldCheck, | ||
node, | ||
}; | ||
}, | ||
|
||
// Pops this function's information. | ||
onCodePathEnd () { | ||
funcInfo = funcInfo.upper; | ||
}, | ||
|
||
// Checks the return statement is valid. | ||
ReturnStatement (node) { | ||
if (funcInfo.shouldCheck) { | ||
funcInfo.hasReturn = true; | ||
|
||
if (!node.argument) { | ||
context.report({ | ||
node, | ||
message, | ||
}); | ||
} | ||
} | ||
}, | ||
|
||
// Reports a given function if the last path is reachable. | ||
'FunctionExpression:exit': checkLastSegment, | ||
}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @fileoverview enforces always return from a fixer function | ||
* @author 薛定谔的猫<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/fixer-return'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const ERROR = { message: 'Expected fixer function to always return a value.' }; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | ||
ruleTester.run('fixer-return', rule, { | ||
valid: [ | ||
` | ||
module.exports = { | ||
create: function(context) { | ||
context.report( { | ||
fix: function(fixer) { | ||
return fixer.foo(); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
` | ||
module.exports = { | ||
create: function(context) { | ||
context.report({ | ||
fix: fixer => fixer.foo() | ||
}); | ||
} | ||
}; | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
module.exports = { | ||
create: function(context) { | ||
context.report({ | ||
fix(fixer) { | ||
fixer.foo(); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
errors: [ERROR], | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: I think it would be better to word this as "This rule enforces that fixer functions always return a value."