Repository: eslint/eslint
Pull request #12038 (2019-07-30 11:14:05)

Docs: add a case to func-names
**What is the purpose of this pull request? (put an "X" next to item)**

X [ ] Documentation update

**What changes did you make? (Give an overview)**
Add these cases to docs:

```js
// Both of the two functions has implied names by spec.
const cat = {
  meow: function() {}
}

const cat = {
  meow() {}
}
```



Patch:
diff --git a/docs/developer-guide/nodejs-api.md b/docs/developer-guide/nodejs-api.md
index a5194004d..e6df77222 100644
--- a/docs/developer-guide/nodejs-api.md
+++ b/docs/developer-guide/nodejs-api.md
@@ -586,8 +586,9 @@ Once you have the configuration information, you can pass it into the `linter` o
 
 ```js
 const CLIEngine = require("eslint").CLIEngine,
-    linter = require("eslint").linter;
+    Linter = require("eslint").Linter;
 
+const linter = new Linter();
 const cli = new CLIEngine({
     envs: ["browser", "mocha"],
     useEslintrc: false,
diff --git a/docs/rules/func-names.md b/docs/rules/func-names.md
index cd6994006..b051d9b9b 100644
--- a/docs/rules/func-names.md
+++ b/docs/rules/func-names.md
@@ -38,6 +38,10 @@ Examples of **incorrect** code for this rule with the default `"always"` option:
 
 Foo.prototype.bar = function() {};
 
+const cat = {
+  meow: function() {}
+}
+
 (function() {
     // ...
 }())
@@ -50,6 +54,10 @@ Examples of **correct** code for this rule with the default `"always"` option:
 
 Foo.prototype.bar = function bar() {};
 
+const cat = {
+  meow() {}
+}
+
 (function bar() {
     // ...
 }())
@@ -78,6 +86,10 @@ Examples of **correct** code for this rule with the `"as-needed"` option:
 
 var bar = function() {};
 
+const cat = {
+  meow: function() {}
+}
+
 (function bar() {
     // ...
 }())
diff --git a/lib/rules/func-names.js b/lib/rules/func-names.js
index 01beb9e2e..ff3a1f4b5 100644
--- a/lib/rules/func-names.js
+++ b/lib/rules/func-names.js
@@ -69,6 +69,8 @@ module.exports = {
 
     create(context) {
 
+        const sourceCode = context.getSourceCode();
+
         /**
          * Returns the config option for the given node.
          * @param {ASTNode} node - A node to get the config for.
@@ -130,6 +132,7 @@ module.exports = {
             context.report({
                 node,
                 messageId: "unnamed",
+                loc: astUtils.getFunctionHeadLoc(node, sourceCode),
                 data: { name: astUtils.getFunctionNameWithKind(node) }
             });
         }
@@ -143,6 +146,7 @@ module.exports = {
             context.report({
                 node,
                 messageId: "named",
+                loc: astUtils.getFunctionHeadLoc(node, sourceCode),
                 data: { name: astUtils.getFunctionNameWithKind(node) }
             });
         }


Test patch:
diff --git a/tests/lib/rules/func-names.js b/tests/lib/rules/func-names.js
index 3fcfc50a3..1976d565c 100644
--- a/tests/lib/rules/func-names.js
+++ b/tests/lib/rules/func-names.js
@@ -17,7 +17,6 @@ const rule = require("../../../lib/rules/func-names"),
 //------------------------------------------------------------------------------
 
 const ruleTester = new RuleTester();
-const unnamedError = { messageId: "unnamed", type: "FunctionExpression" };
 
 ruleTester.run("func-names", rule, {
     valid: [
@@ -254,73 +253,166 @@ ruleTester.run("func-names", rule, {
     invalid: [
         {
             code: "Foo.prototype.bar = function() {};",
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 21,
+                endColumn: 29
+            }]
         },
         {
             code: "(function(){}())",
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 10
+            }]
         },
         {
             code: "f(function(){})",
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 3,
+                endColumn: 11
+            }]
         },
         {
             code: "var a = new Date(function() {});",
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 18,
+                endColumn: 26
+            }]
         },
         {
             code: "var test = function(d, e, f) {};",
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 12,
+                endColumn: 20
+            }]
         },
         {
             code: "new function() {}",
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 5,
+                endColumn: 13
+            }]
         },
         {
             code: "Foo.prototype.bar = function() {};",
             options: ["as-needed"],
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 21,
+                endColumn: 29
+            }]
         },
         {
             code: "(function(){}())",
             options: ["as-needed"],
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 10
+            }]
         },
         {
             code: "f(function(){})",
             options: ["as-needed"],
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 3,
+                endColumn: 11
+            }]
         },
         {
             code: "var a = new Date(function() {});",
             options: ["as-needed"],
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 18,
+                endColumn: 26
+            }]
         },
         {
             code: "new function() {}",
             options: ["as-needed"],
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 5,
+                endColumn: 13
+            }]
         },
         {
             code: "var {foo} = function(){};",
             options: ["as-needed"],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 13,
+                endColumn: 21
+            }]
         },
         {
             code: "var x = function foo() {};",
             options: ["never"],
-            errors: [{ messageId: "named", data: { name: "function 'foo'" }, type: "FunctionExpression" }]
+            errors: [{
+                messageId: "named",
+                data: { name: "function 'foo'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 9,
+                endColumn: 21
+            }]
         },
         {
             code: "Foo.prototype.bar = function foo() {};",
             options: ["never"],
-            errors: [{ messageId: "named", data: { name: "function 'foo'" }, type: "FunctionExpression" }]
+            errors: [{
+                messageId: "named",
+                data: { name: "function 'foo'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 21,
+                endColumn: 33
+            }]
         },
         {
             code: "({foo: function foo() {}})",
             options: ["never"],
-            errors: [{ messageId: "named", data: { name: "method 'foo'" }, type: "FunctionExpression" }]
+            errors: [{
+                messageId: "named",
+                data: { name: "method 'foo'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 3,
+                endColumn: 20
+            }]
         },
 
         // generators
@@ -328,121 +420,241 @@ ruleTester.run("func-names", rule, {
             code: "var foo = bar(function *() {});",
             options: ["always"],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "var foo = function*() {};",
             options: ["always"],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 11,
+                endColumn: 20
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["always"],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["always", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "var foo = function*() {};",
             options: ["always", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 11,
+                endColumn: 20
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["always", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["always", { generators: "as-needed" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["always", { generators: "as-needed" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["as-needed"],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["as-needed"],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["as-needed", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "var foo = function*() {};",
             options: ["as-needed", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 11,
+                endColumn: 20
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["as-needed", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["as-needed", { generators: "as-needed" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["as-needed", { generators: "as-needed" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["never", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "var foo = function*() {};",
             options: ["never", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 11,
+                endColumn: 20
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["never", { generators: "always" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
         {
             code: "var foo = bar(function *() {});",
             options: ["never", { generators: "as-needed" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 25
+            }]
         },
         {
             code: "(function*() {}())",
             options: ["never", { generators: "as-needed" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [unnamedError]
+            errors: [{
+                messageId: "unnamed",
+                type: "FunctionExpression",
+                line: 1,
+                column: 2,
+                endColumn: 11
+            }]
         },
 
         {
@@ -450,26 +662,52 @@ ruleTester.run("func-names", rule, {
             options: ["never"],
             parserOptions: { ecmaVersion: 6 },
             errors: [{
-                message: "Unexpected named generator function 'baz'."
+                messageId: "named",
+                data: { name: "generator function 'baz'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 28
             }]
         },
         {
             code: "var foo = bar(function *baz() {});",
             options: ["never", { generators: "never" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [{ messageId: "named", data: { name: "generator function 'baz'" }, type: "FunctionExpression" }]
+            errors: [{
+                messageId: "named",
+                data: { name: "generator function 'baz'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 28
+            }]
         },
         {
             code: "var foo = bar(function *baz() {});",
             options: ["always", { generators: "never" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [{ messageId: "named", data: { name: "generator function 'baz'" }, type: "FunctionExpression" }]
+            errors: [{
+                messageId: "named",
+                data: { name: "generator function 'baz'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 28
+            }]
         },
         {
             code: "var foo = bar(function *baz() {});",
             options: ["as-needed", { generators: "never" }],
             parserOptions: { ecmaVersion: 6 },
-            errors: [{ messageId: "named", data: { name: "generator function 'baz'" }, type: "FunctionExpression" }]
+            errors: [{
+                messageId: "named",
+                data: { name: "generator function 'baz'" },
+                type: "FunctionExpression",
+                line: 1,
+                column: 15,
+                endColumn: 28
+            }]
         }
     ]
 });