Repository: eslint-community/eslint-plugin-es-x
Pull request #51 (2023-01-20 15:09:56)

Add rules for ES2021 Intl API
related to #45


Patch:
diff --git a/docs/rules/index.md b/docs/rules/index.md
index 88b62fa..92bf52e 100644
--- a/docs/rules/index.md
+++ b/docs/rules/index.md
@@ -52,6 +52,16 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
 | [es-x/no-string-prototype-replaceall](./no-string-prototype-replaceall.md) | disallow the `String.prototype.replaceAll` method. |  |
 | [es-x/no-weakrefs](./no-weakrefs.md) | disallow the `WeakRef` and `FinalizationRegistry` class. |  |
 
+## ES2021 Intl API
+
+There are multiple configs that enable all rules in this category: `plugin:es-x/no-new-in-es2021-intl-api`, `plugin:es-x/restrict-to-es-intl-api-1st-edition`, `plugin:es-x/restrict-to-es2015-intl-api`, `plugin:es-x/restrict-to-es2016-intl-api`, `plugin:es-x/restrict-to-es2017-intl-api`, `plugin:es-x/restrict-to-es2018-intl-api`, `plugin:es-x/restrict-to-es2019-intl-api`, and `plugin:es-x/restrict-to-es2020-intl-api`
+
+| Rule ID | Description |    |
+|:--------|:------------|:--:|
+| [es-x/no-intl-datetimeformat-prototype-formatrange](./no-intl-datetimeformat-prototype-formatrange.md) | disallow the `Intl.DateTimeFormat.prototype.formatRange` method. |  |
+| [es-x/no-intl-displaynames](./no-intl-displaynames.md) | disallow the `Intl.DisplayNames` object. |  |
+| [es-x/no-intl-listformat](./no-intl-listformat.md) | disallow the `Intl.ListFormat` object. |  |
+
 ## ES2020
 
 There are multiple configs that enable all rules in this category: `plugin:es-x/no-new-in-es2020`, `plugin:es-x/restrict-to-es3`, `plugin:es-x/restrict-to-es5`, `plugin:es-x/restrict-to-es2015`, `plugin:es-x/restrict-to-es2016`, `plugin:es-x/restrict-to-es2017`, `plugin:es-x/restrict-to-es2018`, and `plugin:es-x/restrict-to-es2019`
diff --git a/docs/rules/no-intl-datetimeformat-prototype-formatrange.md b/docs/rules/no-intl-datetimeformat-prototype-formatrange.md
new file mode 100644
index 0000000..157fde9
--- /dev/null
+++ b/docs/rules/no-intl-datetimeformat-prototype-formatrange.md
@@ -0,0 +1,47 @@
+---
+title: "es-x/no-intl-datetimeformat-prototype-formatrange"
+description: "disallow the `Intl.DateTimeFormat.prototype.formatRange` method"
+---
+
+# es-x/no-intl-datetimeformat-prototype-formatrange
+> disallow the `Intl.DateTimeFormat.prototype.formatRange` method
+
+- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
+- ✅ The following configurations enable this rule: `plugin:es-x/no-new-in-es2021-intl-api`, `plugin:es-x/restrict-to-es-intl-api-1st-edition`, `plugin:es-x/restrict-to-es2015-intl-api`, `plugin:es-x/restrict-to-es2016-intl-api`, `plugin:es-x/restrict-to-es2017-intl-api`, `plugin:es-x/restrict-to-es2018-intl-api`, `plugin:es-x/restrict-to-es2019-intl-api`, and `plugin:es-x/restrict-to-es2020-intl-api`
+
+This rule reports ES2021 Intl API `Intl.DateTimeFormat.prototype.formatRange` as errors.
+
+This rule is silent by default because it's hard to know types. You need to configure [the aggressive mode](../#the-aggressive-mode) or TypeScript in order to enable this rule.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-intl-datetimeformat-prototype-formatrange: [error, { aggressive: true }] */
+const dateTimeFormat = new Intl.DateTimeFormat('en', options1);
+console.log(dateTimeFormat.formatRange(startDate, endDate));
+```
+
+</eslint-playground>
+
+## 🔧 Options
+
+This rule has an option.
+
+```yml
+rules:
+  es-x/no-intl-datetimeformat-prototype-formatrange: [error, { aggressive: false }]
+```
+
+### aggressive: boolean
+
+Configure the aggressive mode for only this rule.
+This is prior to the `settings['es-x'].aggressive` setting.
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-intl-datetimeformat-prototype-formatrange.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-intl-datetimeformat-prototype-formatrange.js)
diff --git a/docs/rules/no-intl-displaynames.md b/docs/rules/no-intl-displaynames.md
new file mode 100644
index 0000000..b5779f4
--- /dev/null
+++ b/docs/rules/no-intl-displaynames.md
@@ -0,0 +1,30 @@
+---
+title: "es-x/no-intl-displaynames"
+description: "disallow the `Intl.DisplayNames` object"
+---
+
+# es-x/no-intl-displaynames
+> disallow the `Intl.DisplayNames` object
+
+- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
+- ✅ The following configurations enable this rule: `plugin:es-x/no-new-in-es2021-intl-api`, `plugin:es-x/restrict-to-es-intl-api-1st-edition`, `plugin:es-x/restrict-to-es2015-intl-api`, `plugin:es-x/restrict-to-es2016-intl-api`, `plugin:es-x/restrict-to-es2017-intl-api`, `plugin:es-x/restrict-to-es2018-intl-api`, `plugin:es-x/restrict-to-es2019-intl-api`, and `plugin:es-x/restrict-to-es2020-intl-api`
+
+This rule reports ES2021 Intl API `Intl.DisplayNames` object as errors.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-intl-displaynames: error */
+const dn = new Intl.DisplayNames('en', {type: 'calendar'});
+```
+
+</eslint-playground>
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-intl-displaynames.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-intl-displaynames.js)
diff --git a/docs/rules/no-intl-listformat.md b/docs/rules/no-intl-listformat.md
new file mode 100644
index 0000000..5e2235b
--- /dev/null
+++ b/docs/rules/no-intl-listformat.md
@@ -0,0 +1,30 @@
+---
+title: "es-x/no-intl-listformat"
+description: "disallow the `Intl.ListFormat` object"
+---
+
+# es-x/no-intl-listformat
+> disallow the `Intl.ListFormat` object
+
+- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
+- ✅ The following configurations enable this rule: `plugin:es-x/no-new-in-es2021-intl-api`, `plugin:es-x/restrict-to-es-intl-api-1st-edition`, `plugin:es-x/restrict-to-es2015-intl-api`, `plugin:es-x/restrict-to-es2016-intl-api`, `plugin:es-x/restrict-to-es2017-intl-api`, `plugin:es-x/restrict-to-es2018-intl-api`, `plugin:es-x/restrict-to-es2019-intl-api`, and `plugin:es-x/restrict-to-es2020-intl-api`
+
+This rule reports ES2021 Intl API `Intl.ListFormat` object as errors.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-intl-listformat: error */
+const formatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
+```
+
+</eslint-playground>
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-intl-listformat.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-intl-listformat.js)
diff --git a/lib/configs/no-new-in-es2021-intl-api.js b/lib/configs/no-new-in-es2021-intl-api.js
index f42c599..766910f 100644
--- a/lib/configs/no-new-in-es2021-intl-api.js
+++ b/lib/configs/no-new-in-es2021-intl-api.js
@@ -4,4 +4,11 @@
  */
 "use strict"
 
-module.exports = { plugins: ["es-x"], rules: {} }
+module.exports = {
+    plugins: ["es-x"],
+    rules: {
+        "es-x/no-intl-datetimeformat-prototype-formatrange": "error",
+        "es-x/no-intl-displaynames": "error",
+        "es-x/no-intl-listformat": "error",
+    },
+}
diff --git a/lib/index.js b/lib/index.js
index 8667538..bce98d7 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -125,6 +125,9 @@ module.exports = {
         "no-hashbang": require("./rules/no-hashbang"),
         "no-import-meta": require("./rules/no-import-meta"),
         "no-initializers-in-for-in": require("./rules/no-initializers-in-for-in"),
+        "no-intl-datetimeformat-prototype-formatrange": require("./rules/no-intl-datetimeformat-prototype-formatrange"),
+        "no-intl-displaynames": require("./rules/no-intl-displaynames"),
+        "no-intl-listformat": require("./rules/no-intl-listformat"),
         "no-intl-segmenter": require("./rules/no-intl-segmenter"),
         "no-intl-supportedvaluesof": require("./rules/no-intl-supportedvaluesof"),
         "no-json": require("./rules/no-json"),
diff --git a/lib/rules/no-intl-datetimeformat-prototype-formatrange.js b/lib/rules/no-intl-datetimeformat-prototype-formatrange.js
new file mode 100644
index 0000000..e29035f
--- /dev/null
+++ b/lib/rules/no-intl-datetimeformat-prototype-formatrange.js
@@ -0,0 +1,36 @@
+"use strict"
+
+const {
+    definePrototypeMethodHandler,
+} = require("../util/define-prototype-method-handler")
+
+module.exports = {
+    meta: {
+        docs: {
+            description:
+                "disallow the `Intl.DateTimeFormat.prototype.formatRange` method.",
+            category: "ES2021-Intl-API",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-intl-datetimeformat-prototype-formatrange.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2021 Intl API '{{name}}' method is forbidden.",
+        },
+        schema: [
+            {
+                type: "object",
+                properties: {
+                    aggressive: { type: "boolean" },
+                },
+                additionalProperties: false,
+            },
+        ],
+        type: "problem",
+    },
+    create(context) {
+        return definePrototypeMethodHandler(context, {
+            "Intl.DateTimeFormat": ["formatRange"],
+        })
+    },
+}
diff --git a/lib/rules/no-intl-displaynames.js b/lib/rules/no-intl-displaynames.js
new file mode 100644
index 0000000..9ece5e0
--- /dev/null
+++ b/lib/rules/no-intl-displaynames.js
@@ -0,0 +1,38 @@
+"use strict"
+
+const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "disallow the `Intl.DisplayNames` object.",
+            category: "ES2021-Intl-API",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-intl-displaynames.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2021 Intl API '{{name}}' object is forbidden.",
+        },
+        schema: [],
+        type: "problem",
+    },
+    create(context) {
+        return {
+            "Program:exit"() {
+                const tracker = new ReferenceTracker(context.getScope())
+                for (const { node, path } of tracker.iterateGlobalReferences({
+                    Intl: {
+                        DisplayNames: { [READ]: true },
+                    },
+                })) {
+                    context.report({
+                        node,
+                        messageId: "forbidden",
+                        data: { name: path.join(".") },
+                    })
+                }
+            },
+        }
+    },
+}
diff --git a/lib/rules/no-intl-listformat.js b/lib/rules/no-intl-listformat.js
new file mode 100644
index 0000000..8cc4db1
--- /dev/null
+++ b/lib/rules/no-intl-listformat.js
@@ -0,0 +1,38 @@
+"use strict"
+
+const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "disallow the `Intl.ListFormat` object.",
+            category: "ES2021-Intl-API",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-intl-listformat.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2021 Intl API '{{name}}' object is forbidden.",
+        },
+        schema: [],
+        type: "problem",
+    },
+    create(context) {
+        return {
+            "Program:exit"() {
+                const tracker = new ReferenceTracker(context.getScope())
+                for (const { node, path } of tracker.iterateGlobalReferences({
+                    Intl: {
+                        ListFormat: { [READ]: true },
+                    },
+                })) {
+                    context.report({
+                        node,
+                        messageId: "forbidden",
+                        data: { name: path.join(".") },
+                    })
+                }
+            },
+        }
+    },
+}
diff --git a/lib/util/define-prototype-method-handler.js b/lib/util/define-prototype-method-handler.js
index 00ee11d..386de32 100644
--- a/lib/util/define-prototype-method-handler.js
+++ b/lib/util/define-prototype-method-handler.js
@@ -202,14 +202,18 @@ function definePrototypeMethodHandler(context, nameMap, options) {
      * @returns {boolean} `true` if should disallow it.
      */
     function typeSymbolEscapedNameEquals(type, className) {
-        const escapedName = type.symbol.escapedName
-        return (
-            escapedName === className ||
-            // ReadonlyArray, ReadonlyMap, ReadonlySet
-            escapedName === `Readonly${className}` ||
-            // CallableFunction
-            (className === "Function" && escapedName === "CallableFunction")
-        )
+        const symbol = type.symbol
+        if (!className.includes(".")) {
+            const escapedName = symbol.escapedName
+            return (
+                escapedName === className ||
+                // ReadonlyArray, ReadonlyMap, ReadonlySet
+                escapedName === `Readonly${className}` ||
+                // CallableFunction
+                (className === "Function" && escapedName === "CallableFunction")
+            )
+        }
+        return checker.getFullyQualifiedName(symbol) === className
     }
 
     /**
diff --git a/package.json b/package.json
index be264b9..af7ea96 100644
--- a/package.json
+++ b/package.json
@@ -52,6 +52,7 @@
     "pretest": "npm run -s lint",
     "test": "npm run -s test:mocha",
     "test:mocha": "nyc mocha tests/**/*.js --reporter dot --timeout 60000",
+    "test:debug": "mocha tests/**/*.js --reporter dot --timeout 60000",
     "update": "run-s update:*",
     "update:url": "npm run -s lint -- --fix",
     "update:configs": "node scripts/update-lib-configs",


Test patch:
diff --git a/tests/fixtures/tsconfig.json b/tests/fixtures/tsconfig.json
index 65fe407..2f2cd23 100644
--- a/tests/fixtures/tsconfig.json
+++ b/tests/fixtures/tsconfig.json
@@ -1,7 +1,7 @@
 {
     "compilerOptions": {
         "strict": true,
-        "lib": ["ES2020"]
+        "lib": ["ES2021"]
     },
     "include": ["test.ts"]
 }
diff --git a/tests/lib/rules/no-intl-datetimeformat-prototype-formatrange.js b/tests/lib/rules/no-intl-datetimeformat-prototype-formatrange.js
new file mode 100644
index 0000000..c18b820
--- /dev/null
+++ b/tests/lib/rules/no-intl-datetimeformat-prototype-formatrange.js
@@ -0,0 +1,136 @@
+"use strict"
+
+const path = require("path")
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-intl-datetimeformat-prototype-formatrange.js")
+const ruleId = "no-intl-datetimeformat-prototype-formatrange"
+
+new RuleTester().run(ruleId, rule, {
+    valid: [
+        "formatRange(startDate, endDate)",
+        "foo.unknown(0)",
+        "foo.formatRange(startDate, endDate)",
+        {
+            code: "formatRange(startDate, endDate)",
+            settings: { "es-x": { aggressive: true } },
+        },
+        { code: "foo.unknown(0)", settings: { "es-x": { aggressive: true } } },
+        {
+            code: "foo.formatRange(startDate, endDate)",
+            options: [{ aggressive: false }],
+            settings: { "es-x": { aggressive: true } },
+        },
+    ],
+    invalid: [
+        {
+            code: "foo.formatRange(startDate, endDate)",
+            errors: [
+                "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+            ],
+            settings: { "es-x": { aggressive: true } },
+        },
+        {
+            code: "foo.formatRange(startDate, endDate)",
+            options: [{ aggressive: true }],
+            errors: [
+                "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+            ],
+            settings: { "es-x": { aggressive: false } },
+        },
+    ],
+})
+
+// -----------------------------------------------------------------------------
+// TypeScript
+// -----------------------------------------------------------------------------
+const parser = require.resolve("@typescript-eslint/parser")
+const tsconfigRootDir = path.resolve(__dirname, "../../fixtures")
+const project = "tsconfig.json"
+const filename = path.join(tsconfigRootDir, "test.ts")
+
+new RuleTester({ parser }).run(`${ruleId} TS`, rule, {
+    valid: [
+        { filename, code: "formatRange(startDate, endDate)" },
+        { filename, code: "foo.unknown(0)" },
+        { filename, code: "foo.formatRange(startDate, endDate)" },
+        { filename, code: "let foo = {}; foo.formatRange(startDate, endDate)" },
+        {
+            filename,
+            code: "formatRange(startDate, endDate)",
+            settings: { "es-x": { aggressive: true } },
+        },
+        {
+            filename,
+            code: "foo.unknown(0)",
+            settings: { "es-x": { aggressive: true } },
+        },
+    ],
+    invalid: [
+        {
+            filename,
+            code: "let foo = new Unknown(); foo.formatRange(startDate, endDate)",
+            errors: [
+                "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+            ],
+            settings: { "es-x": { aggressive: true } },
+        },
+        {
+            filename,
+            code: "foo.formatRange(startDate, endDate)",
+            errors: [
+                "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+            ],
+            settings: { "es-x": { aggressive: true } },
+        },
+    ],
+})
+
+new RuleTester({ parser, parserOptions: { tsconfigRootDir, project } }).run(
+    `${ruleId} TS Full Type Information`,
+    rule,
+    {
+        valid: [
+            { filename, code: "formatRange(startDate, endDate)" },
+            { filename, code: "foo.unknown(0)" },
+            { filename, code: "foo.formatRange(startDate, endDate)" },
+            {
+                filename,
+                code: "let foo = {}; foo.formatRange(startDate, endDate)",
+            },
+            {
+                filename,
+                code: "formatRange(startDate, endDate)",
+                settings: { "es-x": { aggressive: true } },
+            },
+            {
+                filename,
+                code: "foo.unknown(0)",
+                settings: { "es-x": { aggressive: true } },
+            },
+        ],
+        invalid: [
+            {
+                filename,
+                code: "let foo = new Intl.DateTimeFormat(); foo.formatRange(startDate, endDate)",
+                errors: [
+                    "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+                ],
+            },
+            {
+                filename,
+                code: "function f(a: Intl.DateTimeFormat) { a.formatRange(startDate, endDate) }",
+                errors: [
+                    "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+                ],
+            },
+            {
+                filename,
+                code: "foo.formatRange(startDate, endDate)",
+                errors: [
+                    "ES2021 Intl API 'Intl.DateTimeFormat.prototype.formatRange' method is forbidden.",
+                ],
+                settings: { "es-x": { aggressive: true } },
+            },
+        ],
+    },
+)
diff --git a/tests/lib/rules/no-intl-displaynames.js b/tests/lib/rules/no-intl-displaynames.js
new file mode 100644
index 0000000..3f71119
--- /dev/null
+++ b/tests/lib/rules/no-intl-displaynames.js
@@ -0,0 +1,21 @@
+"use strict"
+
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-intl-displaynames")
+
+new RuleTester().run("no-intl-displaynames", rule, {
+    valid: [
+        "Intl",
+        "Intl.DateTimeFormat",
+        "Intl.NumberFormat",
+        "let Intl = 0; Intl.DisplayNames",
+    ],
+    invalid: [
+        {
+            code: "Intl.DisplayNames",
+            errors: [
+                "ES2021 Intl API 'Intl.DisplayNames' object is forbidden.",
+            ],
+        },
+    ],
+})
diff --git a/tests/lib/rules/no-intl-listformat.js b/tests/lib/rules/no-intl-listformat.js
new file mode 100644
index 0000000..01c2022
--- /dev/null
+++ b/tests/lib/rules/no-intl-listformat.js
@@ -0,0 +1,19 @@
+"use strict"
+
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-intl-listformat")
+
+new RuleTester().run("no-intl-listformat", rule, {
+    valid: [
+        "Intl",
+        "Intl.DateTimeFormat",
+        "Intl.NumberFormat",
+        "let Intl = 0; Intl.ListFormat",
+    ],
+    invalid: [
+        {
+            code: "Intl.ListFormat",
+            errors: ["ES2021 Intl API 'Intl.ListFormat' object is forbidden."],
+        },
+    ],
+})