Skip to content

Commit

Permalink
Merge pull request #2107 from Marsup/func-names-es6-class
Browse files Browse the repository at this point in the history
Fix: func-names with ES6 classes (fixes #2103)
  • Loading branch information
nzakas committed Mar 19, 2015
2 parents 7bdb3d8 + 2a27f1e commit 1626126
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/rules/func-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ module.exports = function(context) {

/**
* Determines whether the current FunctionExpression node is a get, set, or
* shorthand method in an object literal.
* shorthand method in an object literal or a class.
* @returns {boolean} True if the node is a get, set, or shorthand method.
*/
function isObjectMethod() {
function isObjectOrClassMethod() {
var parent = context.getAncestors().pop();
return (parent.type === "Property" && (
parent.method ||
parent.kind === "get" ||
parent.kind === "set"

return (parent.type === "MethodDefinition" || (
parent.type === "Property" && (
parent.method ||
parent.kind === "get" ||
parent.kind === "set"
)
));
}

Expand All @@ -32,7 +35,7 @@ module.exports = function(context) {

var name = node.id && node.id.name;

if (!name && !isObjectMethod()) {
if (!name && !isObjectOrClassMethod()) {
context.report(node, "Missing function expression name.");
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/func-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ eslintTester.addRuleTest("lib/rules/func-names", {
{
code: "({ foo() { return 1; } });",
ecmaFeatures: { objectLiteralShorthandMethods: true }
},
{
code: "class A { constructor(){} foo(){} get bar(){} set baz(value){} static qux(){}}",
ecmaFeatures: { classes: true }
}
],
invalid: [
Expand Down

0 comments on commit 1626126

Please sign in to comment.