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
Update: Added generators option to func-names (fixes #9511) #10697
Merged
not-an-aardvark
merged 9 commits into
eslint:master
from
OscarBarrett:feature/func-names-generators
Sep 5, 2018
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
109faf2
Update: Added generators option to func-names (fixes #9511)
OscarBarrett 13a8c74
Updated option schema to match base and added alternative object schema
OscarBarrett 7d845ae
Removed object schema
OscarBarrett 67867f7
Lint fix: no-prototype-builtins
OscarBarrett 0950db3
Handle as-needed case for generators
OscarBarrett df0be72
Merge branch 'master' into feature/func-names-generators
OscarBarrett 2f087c5
Added additional test cases and updated docs for as-needed generators
OscarBarrett c54e1f7
Simplified logic
OscarBarrett 41b828a
Improve readability of "as-needed" and "always" checks
OscarBarrett File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -33,20 +33,55 @@ module.exports = { | |
url: "https://eslint.org/docs/rules/func-names" | ||
}, | ||
|
||
schema: [ | ||
{ | ||
enum: ["always", "as-needed", "never"] | ||
} | ||
], | ||
schema: { | ||
definitions: { | ||
value: { | ||
enum: [ | ||
"always", | ||
"as-needed", | ||
"never" | ||
] | ||
} | ||
}, | ||
items: [ | ||
{ | ||
$ref: "#/definitions/value" | ||
}, | ||
{ | ||
type: "object", | ||
properties: { | ||
generators: { | ||
$ref: "#/definitions/value" | ||
} | ||
}, | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
messages: { | ||
unnamed: "Unexpected unnamed {{name}}.", | ||
named: "Unexpected named {{name}}." | ||
} | ||
}, | ||
|
||
create(context) { | ||
const never = context.options[0] === "never"; | ||
const asNeeded = context.options[0] === "as-needed"; | ||
|
||
/** | ||
* Returns the config option for the given node. | ||
* @param {ASTNode} node - A node to get the config for. | ||
* @returns {string} The config option. | ||
*/ | ||
function getConfigForNode(node) { | ||
if ( | ||
node.generator && | ||
context.options.length > 1 && | ||
context.options[1].generators | ||
) { | ||
return context.options[1].generators; | ||
} | ||
|
||
return context.options[0] || "always"; | ||
} | ||
|
||
/** | ||
* Determines whether the current FunctionExpression node is a get, set, or | ||
|
@@ -83,6 +118,32 @@ module.exports = { | |
(parent.type === "AssignmentPattern" && parent.right === node); | ||
} | ||
|
||
/** | ||
* Reports that an unnamed function should be named | ||
* @param {ASTNode} node - The node to report in the event of an error. | ||
* @returns {void} | ||
*/ | ||
function reportUnexpectedUnnamedFunction(node) { | ||
context.report({ | ||
node, | ||
messageId: "unnamed", | ||
data: { name: astUtils.getFunctionNameWithKind(node) } | ||
}); | ||
} | ||
|
||
/** | ||
* Reports that a named function should be unnamed | ||
* @param {ASTNode} node - The node to report in the event of an error. | ||
* @returns {void} | ||
*/ | ||
function reportUnexpectedNamedFunction(node) { | ||
context.report({ | ||
node, | ||
messageId: "named", | ||
data: { name: astUtils.getFunctionNameWithKind(node) } | ||
}); | ||
} | ||
|
||
return { | ||
"FunctionExpression:exit"(node) { | ||
|
||
|
@@ -94,23 +155,15 @@ module.exports = { | |
} | ||
|
||
const hasName = Boolean(node.id && node.id.name); | ||
const name = astUtils.getFunctionNameWithKind(node); | ||
const config = getConfigForNode(node); | ||
|
||
if (never) { | ||
if (config === "never") { | ||
if (hasName) { | ||
context.report({ | ||
node, | ||
messageId: "named", | ||
data: { name } | ||
}); | ||
reportUnexpectedNamedFunction(node); | ||
} | ||
} else { | ||
if (!hasName && (asNeeded ? !hasInferredName(node) : !isObjectOrClassMethod(node))) { | ||
context.report({ | ||
node, | ||
messageId: "unnamed", | ||
data: { name } | ||
}); | ||
if (!hasName && (config === "as-needed" ? !hasInferredName(node) : !isObjectOrClassMethod(node))) { | ||
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. Could this be simplified to something like: } else if (config === "as-needed") {
if (!hasName && !hasInferredName(node)) {
reportUnexpectedUnnamedFunction(node);
}
} else {
if (!isObjectOrClassMethod(node)) {
reportUnexpectedUnnamedFunction(node);
}
} In my opinion, the expression |
||
reportUnexpectedUnnamedFunction(node); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
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.
The use of
true
andfalse
for this case (meaning "use the base option" and "invert the base option") is a bit unintuitive to me. Since there three possible base options (always
,as-needed
, andnever
) and only two possiblegenerators
options, a booleangenerators
option isn't able to express the full space of possible behaviors here. It also seems like the wording of "require generators to follow thefunc-names
rule" is self-referential since the option itself is describing what it means for a generator to follow thefunc-names
rule.What would you think about having the
generators
option be a string with the same possible values as the regular option? This seems like it could reduce the cognitive overhead when configuring the option, because thegenerators
option would behave the same way as the base option. For example:With this scheme, the default behavior of the
generators
option would be to fall back to the base option.Another possible change could be to make the first option an object, e.g.
With this setup, it might be clearer how the
generators
andbase
options interact with each other, without reading the documentation. (Note that we would still need to support the string option for backwards compatibility.)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.
Thanks, that makes sense. I'll look at updating the schema as suggested.