Add new ESLint rule: prefer-primordials#35448
Add new ESLint rule: prefer-primordials#35448Leko wants to merge 1 commit intonodejs:masterfrom Leko:improve-primordials-linter
Conversation
|
I'm +1 on this. It should be doable after #35499 |
This comment has been minimized.
This comment has been minimized.
Codecov Report
@@ Coverage Diff @@
## master #35448 +/- ##
==========================================
- Coverage 96.87% 96.40% -0.48%
==========================================
Files 212 220 +8
Lines 69641 73735 +4094
==========================================
+ Hits 67466 71085 +3619
- Misses 2175 2650 +475
Continue to review full report at Codecov.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
DerekNonGeneric
left a comment
There was a problem hiding this comment.
Just wanted to say thanks for doing this. Definitely +1, but don't have time for a proper review at the moment. Very happy to see this!
This comment has been minimized.
This comment has been minimized.
targos
left a comment
There was a problem hiding this comment.
LGTM based on the added unit tests and changes in lib. I am not familiar with writing ESLint rules.
|
@nodejs/collaborators Could you help me with reviewing this PR? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive.
|
I found a new lint error while landing. I fixed the error. |
|
Landed in cef1444 |
I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive. PR-URL: #35448 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
| Int16Array, | ||
| Int32Array, | ||
| Int8Array, | ||
| JSON, |
There was a problem hiding this comment.
JSON, Math and Reflect don’t actually exist on primordials, only their methods.
I’m fixing that in #36016.
Refs: nodejs#35448 Refs: nodejs#36003 Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
Refs: #35448 Refs: #36003 Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #36016 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive. PR-URL: #35448 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
Refs: #35448 Refs: #36003 Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #36016 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive. PR-URL: #35448 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive. PR-URL: #35448 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
Refs: #35448 Refs: #36003 Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #36016 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
I added a new custom ESLint rule to fix these problems. We have a lot of replaceable codes with primordials. Accessing built-in objects is restricted by existing rule (no-restricted-globals), but accessing property in the built-in objects is not restricted right now. We manually review codes that can be replaced by primordials, but there's a lot of code that actually needs to be fixed. We have often made pull requests to replace the primordials with. Restrict accessing global built-in objects such as `Promise`. Restrict calling static methods such as `Array.from` or `Symbol.for`. Don't restrict prototype methods to prevent false-positive. PR-URL: #35448 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Ben Coe <bencoe@gmail.com>
Refs: #35448 Refs: #36003 Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #36016 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Fixed #35449
Rule option
A rule option has a required key and optional keys
stringNumber,Symbol,Arraystring[]stackTraceLimitstringNumber(ex. If this rule find code that is usingparseIntglobally, it will reportconst { NumberParseInt } = primordials; ...)Ignored properties of Error
I marked some of properties of Error as ignored.
Error.stackTraceLimit, Error.captureStackTrace
A lot of tests are failed when I replace these props with primordials.
Error.prepareStackTrace
View failed tests output
actual - expected
'Uncaught Error: Whoops!\n' +
' at REPL1::\n' +
' at d (REPL1::)\n' +
' at c (REPL1::)\n' +
' at b (REPL1::)\n' +
' at a (REPL1::)\n'
node:internal/main/runmain_module:17:47--->
Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12)--->
Function.Module._load (node:internal/modules/cjs/loader:778:14)--->
Module.load (node:internal/modules/cjs/loader:937:32)--->
Object.Module._extensions..js (node:internal/modules/cjs/loader:1101:10)--->
Module._compile (node:internal/modules/cjs/loader:1072:30)--->
Object. (/Users/leko/ghq/github.com/Leko/node/test/parallel/test-repl-pretty-custom-stack.js:74:7)--->
Array.forEach ()--->
run (/Users/leko/ghq/github.com/Leko/node/test/parallel/test-repl-pretty-custom-stack.js:27:10)] {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: 'Uncaught Error: Whoops!\n' +
' at REPL1::\n' +
' at d (REPL1::)\n' +
' at c (REPL1::)\n' +
' at b (REPL1::)\n' +
' at a (REPL1::)\n',
expected: 'Uncaught Error: Whoops!--->\n' +
'REPL1::--->\n' +
'd (REPL1::)--->\n' +
'c (REPL1::)--->\n' +
'b (REPL1::)--->\n' +
'a (REPL1::_)\n',
operator: 'strictEqual'
}
Error.isPrototypeOf
View failed tests output
=== release test-assert-async === Path: parallel/test-assert-async node:internal/process/promises:218 triggerUncaughtException(err, true /* fromPromise */); ^AssertionError [ERR_ASSERTION]: TypeError is not instance of AssertionError
at Object.handler2 (/Users/leko/ghq/github.com/Leko/node/test/parallel/test-assert-async.js:202:5)
at Object. (/Users/leko/ghq/github.com/Leko/node/test/common/index.js:361:15)
at expectedException (node:assert:656:26)
at expectsError (node:assert:781:3)
at Function.rejects (node:assert:834:3)
at async Promise.all (index 20) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: false,
expected: true,
operator: '=='
}
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes