-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
es-x/no-iterator-prototype-find
rule
- Loading branch information
Showing
10 changed files
with
293 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: "es-x/no-iterator-prototype-find" | ||
description: "disallow the `Iterator.prototype.find` method" | ||
--- | ||
|
||
# es-x/no-iterator-prototype-find | ||
> disallow the `Iterator.prototype.find` 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: [no-iterator-helpers] and [no-new-in-esnext] | ||
|
||
This rule reports ES2025 [`Iterator.prototype.find`](https://github.com/tc39/proposal-iterator-helpers) as errors.\ | ||
|
||
## 💡 Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
<eslint-playground type="bad"> | ||
|
||
```js | ||
/*eslint es-x/no-iterator-prototype-find: error */ | ||
const result = naturals() | ||
.find(n => n % 2); | ||
|
||
function* naturals() { | ||
// ... | ||
} | ||
``` | ||
|
||
</eslint-playground> | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-iterator-prototype-find.js) | ||
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-iterator-prototype-find.js) | ||
|
||
[no-iterator-helpers]: ../configs/index.md#no-iterator-helpers | ||
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict" | ||
|
||
const { | ||
definePrototypeMethodHandler, | ||
} = require("../util/define-prototype-method-handler") | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow the `Iterator.prototype.find` method.", | ||
category: "ES2025", | ||
proposal: "iterator-helpers", | ||
recommended: false, | ||
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-iterator-prototype-find.html", | ||
}, | ||
fixable: null, | ||
messages: { | ||
forbidden: "ES2025 '{{name}}' method is forbidden.", | ||
}, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
aggressive: { type: "boolean" }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
type: "problem", | ||
}, | ||
create(context) { | ||
return definePrototypeMethodHandler(context, { | ||
Iterator: ["find"], | ||
}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
"use strict" | ||
|
||
const path = require("path") | ||
const RuleTester = require("../../tester") | ||
const rule = require("../../../lib/rules/no-iterator-prototype-find.js") | ||
const ruleId = "no-iterator-prototype-find" | ||
|
||
if (!RuleTester.isSupported(2025)) { | ||
//eslint-disable-next-line no-console | ||
console.log(`Skip the tests of ${ruleId}.`) | ||
return | ||
} | ||
|
||
new RuleTester({ | ||
languageOptions: { globals: { Iterator: "readonly" } }, | ||
}).run(ruleId, rule, { | ||
valid: [ | ||
"find(n => n % 2)", | ||
"foo.unknown(0)", | ||
"foo.find(n => n % 2)", | ||
{ | ||
code: "find(n => n % 2)", | ||
settings: { "es-x": { aggressive: true } }, | ||
}, | ||
{ code: "foo.unknown(0)", settings: { "es-x": { aggressive: true } } }, | ||
{ | ||
code: "foo.find(n => n % 2)", | ||
options: [{ aggressive: false }], | ||
settings: { "es-x": { aggressive: true } }, | ||
}, | ||
` | ||
function * foo(){}; | ||
function bar() { | ||
if (baz) return foo(); | ||
else return "s" | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
` | ||
function * foo(){}; | ||
function bar() { | ||
switch (baz) { | ||
case 1: return foo(); | ||
default: return "s" | ||
} | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
` | ||
function * foo(){}; | ||
function bar() { | ||
try { | ||
return foo(); | ||
} catch (e) { | ||
return Iterator.from(x) | ||
} finally { | ||
return "s" | ||
} | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: "foo.find(n => n % 2)", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
settings: { "es-x": { aggressive: true } }, | ||
}, | ||
{ | ||
code: "foo.find(n => n % 2)", | ||
options: [{ aggressive: true }], | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
settings: { "es-x": { aggressive: false } }, | ||
}, | ||
{ | ||
code: "foo().find(n => n % 2); function * foo() { }", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: "Iterator.from(foo).find(n => n % 2);", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: "Iterator.from(foo).drop(3).find(n => n % 2);", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: "function * foo(){}; function bar() { return foo() }; bar().find(n => n % 2)", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: ` | ||
function * foo(){}; | ||
function bar() { | ||
if (baz) return foo(); | ||
else return Iterator.from(x) | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: ` | ||
function * foo(){}; | ||
function bar() { | ||
while(true) return foo(); | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: ` | ||
function * foo(){}; | ||
function bar() { | ||
switch (baz) { | ||
case 1: return foo(); | ||
default: return Iterator.from(x) | ||
} | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: ` | ||
function * foo(){}; | ||
function bar() { | ||
try { | ||
return foo(); | ||
} catch (e) { | ||
return Iterator.from(x) | ||
} finally { | ||
return Iterator.from(y) | ||
} | ||
}; | ||
bar().find(n => n % 2); | ||
`, | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
code: ` | ||
function * foo(){}; | ||
const bar = () => foo(); | ||
bar().find(n => n % 2); | ||
`, | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
], | ||
}) | ||
|
||
// ----------------------------------------------------------------------------- | ||
// TypeScript | ||
// ----------------------------------------------------------------------------- | ||
const parser = require("@typescript-eslint/parser") | ||
const tsconfigRootDir = path.resolve(__dirname, "../../fixtures") | ||
const project = "tsconfig.json" | ||
const filename = path.join(tsconfigRootDir, "test.ts") | ||
|
||
new RuleTester({ | ||
languageOptions: { | ||
parser, | ||
parserOptions: { | ||
tsconfigRootDir, | ||
project, | ||
disallowAutomaticSingleRunInference: true, | ||
}, | ||
}, | ||
}).run(`${ruleId} TS Full Type Information`, rule, { | ||
valid: [ | ||
{ filename, code: "find(n => n % 2)" }, | ||
{ filename, code: "foo.unknown(0)" }, | ||
{ filename, code: "foo.find(n => n % 2)" }, | ||
{ | ||
filename, | ||
code: "let foo = {}; foo.find(n => n % 2)", | ||
}, | ||
{ | ||
filename, | ||
code: "find(n => n % 2)", | ||
settings: { "es-x": { aggressive: true } }, | ||
}, | ||
{ | ||
filename, | ||
code: "foo.unknown(0)", | ||
settings: { "es-x": { aggressive: true } }, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
filename, | ||
code: "let foo = [][Symbol.iterator]; foo().find(n => n % 2)", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
filename, | ||
code: "function f(a: Iterator<any>) { a.find(n => n % 2) }", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
filename, | ||
code: "function * foo(){}; foo().find(n => n % 2)", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
}, | ||
{ | ||
filename, | ||
code: "foo.find(n => n % 2)", | ||
errors: ["ES2025 'Iterator.prototype.find' method is forbidden."], | ||
settings: { "es-x": { aggressive: true } }, | ||
}, | ||
], | ||
}) |