]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/tests/lib/rules/func-name-matching.js
import 8.41.0 source
[pve-eslint.git] / eslint / tests / lib / rules / func-name-matching.js
index 72ee66b41ed6e3d3172da7fb46c12f48007ea296..c020066e75291b7cf4b150731e72151b53748e03 100644 (file)
@@ -10,7 +10,8 @@
 //------------------------------------------------------------------------------
 
 const rule = require("../../../lib/rules/func-name-matching"),
-    { RuleTester } = require("../../../lib/rule-tester");
+    { RuleTester } = require("../../../lib/rule-tester"),
+    FlatRuleTester = require("../../../lib/rule-tester/flat-rule-tester");
 
 //------------------------------------------------------------------------------
 // Tests
@@ -29,6 +30,9 @@ ruleTester.run("func-name-matching", rule, {
         "foo = function foo() {};",
         { code: "foo = function foo() {};", options: ["always"] },
         { code: "foo = function bar() {};", options: ["never"] },
+        { code: "foo &&= function foo() {};", parserOptions: { ecmaVersion: 2021 } },
+        { code: "obj.foo ||= function foo() {};", parserOptions: { ecmaVersion: 2021 } },
+        { code: "obj['foo'] ??= function foo() {};", parserOptions: { ecmaVersion: 2021 } },
         "obj.foo = function foo() {};",
         { code: "obj.foo = function foo() {};", options: ["always"] },
         { code: "obj.foo = function bar() {};", options: ["never"] },
@@ -259,7 +263,250 @@ ruleTester.run("func-name-matching", rule, {
         {
             code: "foo({ value: function value() {} })",
             options: ["always", { considerPropertyDescriptor: true }]
-        }
+        },
+
+        // class fields, private names are ignored
+        {
+            code: "class C { x = function () {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { x = function () {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 'x' = function () {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 'x' = function () {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x = function () {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x = function () {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [x] = function () {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [x] = function () {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { ['x'] = function () {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { ['x'] = function () {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { x = function x() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { x = function y() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 'x' = function x() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 'x' = function y() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x = function x() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x = function x() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x = function y() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x = function y() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [x] = function x() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [x] = function x() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [x] = function y() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [x] = function y() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { ['x'] = function x() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { ['x'] = function y() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 'xy ' = function foo() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 'xy ' = function xy() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { ['xy '] = function foo() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { ['xy '] = function xy() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 1 = function x0() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { 1 = function x1() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [1] = function x0() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [1] = function x1() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [f()] = function g() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { [f()] = function f() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { static x = function x() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { static x = function y() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { x = (function y() {})(); }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { x = (function x() {})(); }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "(class { x = function x() {}; })",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "(class { x = function y() {}; })",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { this.#x = function x() {}; } }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { this.#x = function x() {}; } }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { this.#x = function y() {}; } }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { this.#x = function y() {}; } }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { a.b.#x = function x() {}; } }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { a.b.#x = function x() {}; } }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { a.b.#x = function y() {}; } }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        {
+            code: "class C { #x; foo() { a.b.#x = function y() {}; } }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 }
+        },
+        "var obj = { '\\u1885': function foo() {} };" // not a valid identifier in es5
     ],
     invalid: [
         {
@@ -284,6 +531,27 @@ ruleTester.run("func-name-matching", rule, {
                 { messageId: "matchVariable", data: { funcName: "bar", name: "foo" } }
             ]
         },
+        {
+            code: "foo &&= function bar() {};",
+            parserOptions: { ecmaVersion: 2021 },
+            errors: [
+                { messageId: "matchVariable", data: { funcName: "bar", name: "foo" } }
+            ]
+        },
+        {
+            code: "obj.foo ||= function bar() {};",
+            parserOptions: { ecmaVersion: 2021 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
+            ]
+        },
+        {
+            code: "obj['foo'] ??= function bar() {};",
+            parserOptions: { ecmaVersion: 2021 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
+            ]
+        },
         {
             code: "obj.foo = function bar() {};",
             parserOptions: { ecmaVersion: 6 },
@@ -457,6 +725,194 @@ ruleTester.run("func-name-matching", rule, {
             errors: [
                 { messageId: "matchProperty", data: { funcName: "bar", name: "value" } }
             ]
+        },
+
+        // Optional chaining
+        {
+            code: "(obj?.aaa).foo = function bar() {};",
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "bar", name: "foo" } }
+            ]
+        },
+        {
+            code: "Object?.defineProperty(foo, 'bar', { value: function baz() {} })",
+            options: ["always", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
+            ]
+        },
+        {
+            code: "(Object?.defineProperty)(foo, 'bar', { value: function baz() {} })",
+            options: ["always", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
+            ]
+        },
+        {
+            code: "Object?.defineProperty(foo, 'bar', { value: function bar() {} })",
+            options: ["never", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
+            ]
+        },
+        {
+            code: "(Object?.defineProperty)(foo, 'bar', { value: function bar() {} })",
+            options: ["never", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
+            ]
+        },
+        {
+            code: "Object?.defineProperties(foo, { bar: { value: function baz() {} } })",
+            options: ["always", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
+            ]
+        },
+        {
+            code: "(Object?.defineProperties)(foo, { bar: { value: function baz() {} } })",
+            options: ["always", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "baz", name: "bar" } }
+            ]
+        },
+        {
+            code: "Object?.defineProperties(foo, { bar: { value: function bar() {} } })",
+            options: ["never", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
+            ]
+        },
+        {
+            code: "(Object?.defineProperties)(foo, { bar: { value: function bar() {} } })",
+            options: ["never", { considerPropertyDescriptor: true }],
+            parserOptions: { ecmaVersion: 2020 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "bar", name: "bar" } }
+            ]
+        },
+
+        // class fields
+        {
+            code: "class C { x = function y() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "y", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { x = function x() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "x", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { 'x' = function y() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "y", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { 'x' = function x() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "x", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { ['x'] = function y() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "y", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { ['x'] = function x() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "x", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { static x = function y() {}; }",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "y", name: "x" } }
+            ]
+        },
+        {
+            code: "class C { static x = function x() {}; }",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "x", name: "x" } }
+            ]
+        },
+        {
+            code: "(class { x = function y() {}; })",
+            options: ["always"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "y", name: "x" } }
+            ]
+        },
+        {
+            code: "(class { x = function x() {}; })",
+            options: ["never"],
+            parserOptions: { ecmaVersion: 2022 },
+            errors: [
+                { messageId: "notMatchProperty", data: { funcName: "x", name: "x" } }
+            ]
+        },
+        {
+            code: "var obj = { '\\u1885': function foo() {} };", // valid identifier in es2015
+            parserOptions: { ecmaVersion: 6 },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "foo", name: "\u1885" } }
+            ]
+        }
+    ]
+});
+
+const flatRuleTester = new FlatRuleTester();
+
+flatRuleTester.run("func-name-matching", rule, {
+    valid: [
+        {
+            code: "var obj = { '\\u1885': function foo() {} };", // not a valid identifier in es5
+            languageOptions: {
+                ecmaVersion: 5,
+                sourceType: "script"
+            }
+        }
+    ],
+
+    invalid: [
+        {
+            code: "var obj = { '\\u1885': function foo() {} };", // valid identifier in es2015
+            languageOptions: {
+                ecmaVersion: 2015
+            },
+            errors: [
+                { messageId: "matchProperty", data: { funcName: "foo", name: "\u1885" } }
+            ]
         }
     ]
 });