Commit Graph

4 Commits

Author SHA1 Message Date
Antoine du Hamel
7fd6c2036d tools: refactor prefer-primordials
Use optional chaining to improve code readability and remove use of
`Array.prototype.reduce`.

PR-URL: https://github.com/nodejs/node/pull/36018
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
2021-02-16 17:58:31 +01:00
Antoine du Hamel
5c3944fa20 lib,tools: enforce access to prototype from primordials
PR-URL: https://github.com/nodejs/node/pull/36025
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2020-11-14 05:56:39 -08:00
Antoine du Hamel
831f4c755d tools,lib: recommend using safe primordials
Make the linter recommend replacing `globalThis.Map` by
`primordials.SafeMap`, and similar for `Set`, `WeakSet`, and `WeakMap`.

PR-URL: https://github.com/nodejs/node/pull/36026
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
2020-11-09 19:05:41 +00:00
Leko
cef144421c tools: add new ESLint rule: prefer-primordials
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: https://github.com/nodejs/node/pull/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>
2020-11-07 18:28:14 +08:00