Repository: eslint/eslint
Pull request #12037 (2019-07-30 11:12:33)

Docs: no use eslint.linter in code example
`eslint.linter` exists for backwards compatibility, but we do not recommend using it because any mutations to it are shared among every module that uses `eslint`. Instead, please create your own instance of `eslint.Linter`.

<!--
    ESLint adheres to the [JS Foundation Code of Conduct](https://js.foundation/community/code-of-conduct).
-->

**What is the purpose of this pull request? (put an "X" next to item)**

[x] Documentation update


<!--
    If the item you've checked above has a template, please paste the template questions below and answer them. (If this pull request is addressing an issue, you can just paste a link to the issue here instead.)
-->

<!--
    Please ensure your pull request is ready:

    - Read the pull request guide (https://eslint.org/docs/developer-guide/contributing/pull-requests)
    - Include tests for this change
    - Update documentation for this change (if appropriate)
-->

<!--
    The following is required for all pull requests:
-->

**What changes did you make? (Give an overview)**


**Is there anything you'd like reviewers to focus on?**

no.



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/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
+            }]
         }
     ]
 });