Skip to content

Commit

Permalink
Add no-iterator-helpers rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Oct 31, 2024
1 parent 239a2a7 commit 08eef01
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 4 deletions.
28 changes: 28 additions & 0 deletions docs/configs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,34 @@ export default [
}
```

## no-iterator-helpers

disallow proposal ES2025 [Iterator Helpers](https://github.com/tc39/proposal-iterator-helpers)\
⚠️ This config will be changed in the minor versions of this plugin.

This configs includes rules for [es-x/no-iterator](../rules/no-iterator.md).

### [Config (Flat Config)]

eslint.config.js:

```js
import pluginESx from "eslint-plugin-es-x"
export default [
pluginESx.configs['flat/no-iterator-helpers']
]
```

### [Legacy Config]

.eslintrc.*:

```json
{
"extends": ["plugin:es-x/no-iterator-helpers"],
}
```

## no-set-methods

disallow proposal ES2025 [Set Methods for JavaScript](https://github.com/tc39/proposal-set-methods)\
Expand Down
1 change: 1 addition & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
|:--------|:------------|:--:|
| [es-x/no-dynamic-import-options](./no-dynamic-import-options.md) | disallow second parameter to `import()`. | |
| [es-x/no-import-attributes](./no-import-attributes.md) | disallow Import Attributes. | |
| [es-x/no-iterator](./no-iterator.md) | disallow the `Iterator` class. | |
| [es-x/no-regexp-duplicate-named-capturing-groups](./no-regexp-duplicate-named-capturing-groups.md) | disallow RegExp duplicate named capturing groups. | |
| [es-x/no-regexp-modifiers](./no-regexp-modifiers.md) | disallow RegExp Modifiers. | |
| [es-x/no-set-prototype-difference](./no-set-prototype-difference.md) | disallow the `Set.prototype.difference` method. | |
Expand Down
34 changes: 34 additions & 0 deletions docs/rules/no-iterator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: "es-x/no-iterator"
description: "disallow the `Iterator` class"
---

# es-x/no-iterator
> disallow the `Iterator` class
- ❗ <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: [no-iterator-helpers] and [no-new-in-esnext]

This rule reports ES2025 `Iterator` class as errors.\
The `Iterator` class was added in the ES2025 [Iterator Helpers proposal](https://github.com/tc39/proposal-iterator-helpers).

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad">

```js
/*eslint es-x/no-iterator: error */
const wrapper = Iterator.from(iter);
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-iterator.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-iterator.js)

[no-iterator-helpers]: ../configs/index.md#no-iterator-helpers
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext
14 changes: 14 additions & 0 deletions lib/configs/flat/no-iterator-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* DON'T EDIT THIS FILE.
* This file was generated by "scripts/update-lib-flat-configs.js" script.
*/
"use strict"

module.exports = {
plugins: {
get "es-x"() {
return require("../../index.js")
},
},
rules: { "es-x/no-iterator": "error" },
}
1 change: 1 addition & 0 deletions lib/configs/flat/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
rules: {
"es-x/no-dynamic-import-options": "error",
"es-x/no-import-attributes": "error",
"es-x/no-iterator": "error",
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
"es-x/no-regexp-modifiers": "error",
"es-x/no-set-prototype-difference": "error",
Expand Down
7 changes: 7 additions & 0 deletions lib/configs/no-iterator-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* DON'T EDIT THIS FILE.
* This file was generated by "scripts/update-lib-configs.js" script.
*/
"use strict"

module.exports = { plugins: ["es-x"], rules: { "es-x/no-iterator": "error" } }
1 change: 1 addition & 0 deletions lib/configs/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
rules: {
"es-x/no-dynamic-import-options": "error",
"es-x/no-import-attributes": "error",
"es-x/no-iterator": "error",
"es-x/no-regexp-duplicate-named-capturing-groups": "error",
"es-x/no-regexp-modifiers": "error",
"es-x/no-set-prototype-difference": "error",
Expand Down
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
"flat/no-import-attributes": require("./configs/flat/no-import-attributes"),
"flat/no-intl-numberformat-v3": require("./configs/flat/no-intl-numberformat-v3"),
"flat/no-is-usv-string": require("./configs/flat/no-is-usv-string"),
"flat/no-iterator-helpers": require("./configs/flat/no-iterator-helpers"),
"flat/no-new-in-es5": require("./configs/flat/no-new-in-es5"),
"flat/no-new-in-es2015": require("./configs/flat/no-new-in-es2015"),
"flat/no-new-in-es2015-intl-api": require("./configs/flat/no-new-in-es2015-intl-api"),
Expand Down Expand Up @@ -68,6 +69,7 @@ module.exports = {
"no-import-attributes": require("./configs/no-import-attributes"),
"no-intl-numberformat-v3": require("./configs/no-intl-numberformat-v3"),
"no-is-usv-string": require("./configs/no-is-usv-string"),
"no-iterator-helpers": require("./configs/no-iterator-helpers"),
"no-new-in-es5": require("./configs/no-new-in-es5"),
"no-new-in-es2015": require("./configs/no-new-in-es2015"),
"no-new-in-es2015-intl-api": require("./configs/no-new-in-es2015-intl-api"),
Expand Down Expand Up @@ -222,6 +224,7 @@ module.exports = {
"no-intl-relativetimeformat": require("./rules/no-intl-relativetimeformat"),
"no-intl-segmenter": require("./rules/no-intl-segmenter"),
"no-intl-supportedvaluesof": require("./rules/no-intl-supportedvaluesof"),
"no-iterator": require("./rules/no-iterator"),
"no-json": require("./rules/no-json"),
"no-json-superset": require("./rules/no-json-superset"),
"no-keyword-properties": require("./rules/no-keyword-properties"),
Expand Down
41 changes: 41 additions & 0 deletions lib/rules/no-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict"

const { READ, ReferenceTracker } = require("@eslint-community/eslint-utils")
const { getSourceCode } = require("eslint-compat-utils")

module.exports = {
meta: {
docs: {
description: "disallow the `Iterator` class.",
category: "ES2025",
proposal: "iterator-helpers",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-iterator.html",
},
fixable: null,
messages: {
forbidden: "ES2025 '{{name}}' class is forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
return {
"Program:exit"(program) {
const sourceCode = getSourceCode(context)
const tracker = new ReferenceTracker(
sourceCode.getScope(program),
)
for (const { node, path } of tracker.iterateGlobalReferences({
Iterator: { [READ]: true },
})) {
context.report({
node,
messageId: "forbidden",
data: { name: path.join(".") },
})
}
},
}
},
}
12 changes: 8 additions & 4 deletions scripts/proposals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = {
title: "Class Fields",
link: "https://github.com/tc39/proposal-class-fields",
},
"import-attributes": {
title: "Import Attributes",
link: "https://github.com/tc39/proposal-import-attributes",
},
"intl-numberformat-v3": {
title: "Intl.NumberFormat V3",
link: "https://github.com/tc39/proposal-intl-numberformat-v3",
Expand All @@ -21,6 +25,10 @@ module.exports = {
title: "Well-Formed Unicode Strings",
link: "https://github.com/tc39/proposal-is-usv-string",
},
"iterator-helpers": {
title: "Iterator Helpers",
link: "https://github.com/tc39/proposal-iterator-helpers",
},
"relative-indexing-method": {
title: "An .at() method on all the built-in indexables",
link: "https://github.com/tc39/proposal-relative-indexing-method",
Expand All @@ -29,8 +37,4 @@ module.exports = {
title: "Set Methods for JavaScript",
link: "https://github.com/tc39/proposal-set-methods",
},
"import-attributes": {
title: "Import Attributes",
link: "https://github.com/tc39/proposal-import-attributes",
},
}
29 changes: 29 additions & 0 deletions tests/lib/rules/no-iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-iterator.js")

if (!RuleTester.isSupported(2025)) {
//eslint-disable-next-line no-console
console.log("Skip the tests of no-iterator.")
return
}

new RuleTester({
languageOptions: { globals: { Iterator: "readonly" } },
}).run("no-iterator", rule, {
valid: [
"Array.from(object)",
"const Iterator = Array; Iterator.from(object)",
],
invalid: [
{
code: "Iterator.from(object)",
errors: ["ES2025 'Iterator' class is forbidden."],
},
{
code: "Iterator",
errors: ["ES2025 'Iterator' class is forbidden."],
},
],
})

0 comments on commit 08eef01

Please sign in to comment.