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

Fix: func-name-matching crash on non-string literal computed keys #8246

Merged
merged 2 commits into from Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/rules/func-name-matching.js
Expand Up @@ -132,6 +132,15 @@ module.exports = {
});
}

/**
* Determines whether a given node is a string literal
* @param {ASTNode} node The node to check
* @returns {boolean} `true` if the node is a string literal
*/
function isStringLiteral(node) {
return node.type === "Literal" && typeof node.value === "string";
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -164,13 +173,13 @@ module.exports = {
},

Property(node) {
if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && node.key.type !== "Literal") {
if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && !isStringLiteral(node.key)) {
return;
}
if (node.key.type === "Identifier" && shouldWarn(node.key.name, node.value.id.name)) {
report(node, node.key.name, node.value.id.name, true);
} else if (
node.key.type === "Literal" &&
isStringLiteral(node.key) &&
isIdentifier(node.key.value, ecmaVersion) &&
shouldWarn(node.key.value, node.value.id.name)
) {
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/func-name-matching.js
Expand Up @@ -117,6 +117,14 @@ ruleTester.run("func-name-matching", rule, {
{
code: "({[foo]: function bar() {}})",
parserOptions: { ecmaVersion: 6 }
},
{
code: "({[null]: function foo() {}})",
parserOptions: { ecmaVersion: 6 }
},
{
code: "({[1]: function foo() {}})",
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
Expand Down