Repository: eslint-community/eslint-plugin-es-x
Pull request #52 (2023-01-20 15:30:47)

Add rules for ES2020 Intl API
related to #45


Patch:
diff --git a/docs/rules/index.md b/docs/rules/index.md
index 92bf52e..d8b3b31 100644
--- a/docs/rules/index.md
+++ b/docs/rules/index.md
@@ -79,6 +79,15 @@ There are multiple configs that enable all rules in this category: `plugin:es-x/
 | [es-x/no-regexp-unicode-property-escapes-2020](./no-regexp-unicode-property-escapes-2020.md) | disallow the new values of RegExp Unicode property escape sequences in ES2020. |  |
 | [es-x/no-string-prototype-matchall](./no-string-prototype-matchall.md) | disallow the `String.prototype.matchAll` method. |  |
 
+## ES2020 Intl API
+
+There are multiple configs that enable all rules in this category: `plugin:es-x/no-new-in-es2020-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`, and `plugin:es-x/restrict-to-es2019-intl-api`
+
+| Rule ID | Description |    |
+|:--------|:------------|:--:|
+| [es-x/no-intl-locale](./no-intl-locale.md) | disallow the `Intl.Locale` object. |  |
+| [es-x/no-intl-relativetimeformat](./no-intl-relativetimeformat.md) | disallow the `Intl.RelativeTimeFormat` object. |  |
+
 ## ES2019
 
 There are multiple configs that enable all rules in this category: `plugin:es-x/no-new-in-es2019`, `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`, and `plugin:es-x/restrict-to-es2018`
diff --git a/docs/rules/no-intl-locale.md b/docs/rules/no-intl-locale.md
new file mode 100644
index 0000000..b4ee349
--- /dev/null
+++ b/docs/rules/no-intl-locale.md
@@ -0,0 +1,30 @@
+---
+title: "es-x/no-intl-locale"
+description: "disallow the `Intl.Locale` object"
+---
+
+# es-x/no-intl-locale
+> disallow the `Intl.Locale` 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-es2020-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`, and `plugin:es-x/restrict-to-es2019-intl-api`
+
+This rule reports ES2020 Intl API `Intl.Locale` object as errors.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-intl-locale: error */
+const japanese = new Intl.Locale('ja-Jpan-JP-u-ca-japanese-hc-h12');
+```
+
+</eslint-playground>
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-intl-locale.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-intl-locale.js)
diff --git a/docs/rules/no-intl-relativetimeformat.md b/docs/rules/no-intl-relativetimeformat.md
new file mode 100644
index 0000000..32e32a4
--- /dev/null
+++ b/docs/rules/no-intl-relativetimeformat.md
@@ -0,0 +1,30 @@
+---
+title: "es-x/no-intl-relativetimeformat"
+description: "disallow the `Intl.RelativeTimeFormat` object"
+---
+
+# es-x/no-intl-relativetimeformat
+> disallow the `Intl.RelativeTimeFormat` 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-es2020-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`, and `plugin:es-x/restrict-to-es2019-intl-api`
+
+This rule reports ES2020 Intl API `Intl.RelativeTimeFormat` object as errors.
+
+## 💡 Examples
+
+⛔ Examples of **incorrect** code for this rule:
+
+<eslint-playground type="bad">
+
+```js
+/*eslint es-x/no-intl-relativetimeformat: error */
+const rtf = new Intl.RelativeTimeFormat('en', { style: 'narrow' });
+```
+
+</eslint-playground>
+
+## 📚 References
+
+- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-intl-relativetimeformat.js)
+- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-intl-relativetimeformat.js)
diff --git a/lib/configs/no-new-in-es2020-intl-api.js b/lib/configs/no-new-in-es2020-intl-api.js
index f42c599..e7ee804 100644
--- a/lib/configs/no-new-in-es2020-intl-api.js
+++ b/lib/configs/no-new-in-es2020-intl-api.js
@@ -4,4 +4,10 @@
  */
 "use strict"
 
-module.exports = { plugins: ["es-x"], rules: {} }
+module.exports = {
+    plugins: ["es-x"],
+    rules: {
+        "es-x/no-intl-locale": "error",
+        "es-x/no-intl-relativetimeformat": "error",
+    },
+}
diff --git a/lib/index.js b/lib/index.js
index bce98d7..510bc92 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -125,6 +125,8 @@ 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-locale": require("./rules/no-intl-locale"),
+        "no-intl-relativetimeformat": require("./rules/no-intl-relativetimeformat"),
         "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"),
diff --git a/lib/rules/no-intl-locale.js b/lib/rules/no-intl-locale.js
new file mode 100644
index 0000000..b162320
--- /dev/null
+++ b/lib/rules/no-intl-locale.js
@@ -0,0 +1,38 @@
+"use strict"
+
+const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "disallow the `Intl.Locale` object.",
+            category: "ES2020-Intl-API",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-intl-locale.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2020 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: {
+                        Locale: { [READ]: true },
+                    },
+                })) {
+                    context.report({
+                        node,
+                        messageId: "forbidden",
+                        data: { name: path.join(".") },
+                    })
+                }
+            },
+        }
+    },
+}
diff --git a/lib/rules/no-intl-relativetimeformat.js b/lib/rules/no-intl-relativetimeformat.js
new file mode 100644
index 0000000..4b31996
--- /dev/null
+++ b/lib/rules/no-intl-relativetimeformat.js
@@ -0,0 +1,38 @@
+"use strict"
+
+const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
+
+module.exports = {
+    meta: {
+        docs: {
+            description: "disallow the `Intl.RelativeTimeFormat` object.",
+            category: "ES2020-Intl-API",
+            recommended: false,
+            url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-intl-relativetimeformat.html",
+        },
+        fixable: null,
+        messages: {
+            forbidden: "ES2020 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: {
+                        RelativeTimeFormat: { [READ]: true },
+                    },
+                })) {
+                    context.report({
+                        node,
+                        messageId: "forbidden",
+                        data: { name: path.join(".") },
+                    })
+                }
+            },
+        }
+    },
+}


Test patch:
diff --git a/tests/lib/rules/no-intl-locale.js b/tests/lib/rules/no-intl-locale.js
new file mode 100644
index 0000000..d0f5da6
--- /dev/null
+++ b/tests/lib/rules/no-intl-locale.js
@@ -0,0 +1,19 @@
+"use strict"
+
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-intl-locale")
+
+new RuleTester().run("no-intl-locale", rule, {
+    valid: [
+        "Intl",
+        "Intl.DateTimeFormat",
+        "Intl.NumberFormat",
+        "let Intl = 0; Intl.Locale",
+    ],
+    invalid: [
+        {
+            code: "Intl.Locale",
+            errors: ["ES2020 Intl API 'Intl.Locale' object is forbidden."],
+        },
+    ],
+})
diff --git a/tests/lib/rules/no-intl-relativetimeformat.js b/tests/lib/rules/no-intl-relativetimeformat.js
new file mode 100644
index 0000000..f6258c6
--- /dev/null
+++ b/tests/lib/rules/no-intl-relativetimeformat.js
@@ -0,0 +1,21 @@
+"use strict"
+
+const RuleTester = require("../../tester")
+const rule = require("../../../lib/rules/no-intl-relativetimeformat")
+
+new RuleTester().run("no-intl-relativetimeformat", rule, {
+    valid: [
+        "Intl",
+        "Intl.DateTimeFormat",
+        "Intl.NumberFormat",
+        "let Intl = 0; Intl.RelativeTimeFormat",
+    ],
+    invalid: [
+        {
+            code: "Intl.RelativeTimeFormat",
+            errors: [
+                "ES2020 Intl API 'Intl.RelativeTimeFormat' object is forbidden.",
+            ],
+        },
+    ],
+})