Skip to content

Commit fca7cf4

Browse files
authored
feat(no-deprecated-api): Add support for process.getBuiltinModule() (#435)
1 parent bb21bd0 commit fca7cf4

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

lib/rules/no-deprecated-api.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const getSemverRange = require("../util/get-semver-range")
1616
const extendTrackmapWithNodePrefix = require("../util/extend-trackmap-with-node-prefix")
1717
const unprefixNodeColon = require("../util/unprefix-node-colon")
1818
const { getScope } = require("../util/eslint-compat")
19+
const {
20+
iterateProcessGetBuiltinModuleReferences,
21+
} = require("../util/iterate-process-get-builtin-module-references")
1922

2023
/** @typedef {import('../unsupported-features/types.js').DeprecatedInfo} DeprecatedInfo */
2124
/**
@@ -842,6 +845,10 @@ module.exports = {
842845
}
843846
for (const report of [
844847
...tracker.iterateCjsReferences(modules),
848+
...iterateProcessGetBuiltinModuleReferences(
849+
tracker,
850+
modules
851+
),
845852
...tracker.iterateEsmReferences(modules),
846853
]) {
847854
const { node, path, type, info } = report
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use strict"
2+
const {
3+
CALL,
4+
getStringIfConstant,
5+
READ,
6+
} = require("@eslint-community/eslint-utils")
7+
const processGetBuiltinModuleCall = {
8+
process: {
9+
getBuiltinModule: {
10+
[CALL]: true,
11+
},
12+
},
13+
}
14+
/**
15+
* Iterate the references of process.getBuiltinModule() modules.
16+
* @template Info
17+
* @param {import("@eslint-community/eslint-utils").ReferenceTracker} tracker The reference tracker.
18+
* @param {import("@eslint-community/eslint-utils").TraceMap<Info>} traceMap The trace map.
19+
* @returns {IterableIterator<import("@eslint-community/eslint-utils").Reference<Info>>} The iterator.
20+
*/
21+
function* iterateProcessGetBuiltinModuleReferences(tracker, traceMap) {
22+
for (const { node } of tracker.iterateGlobalReferences(
23+
processGetBuiltinModuleCall
24+
)) {
25+
if (node.type !== "CallExpression") continue
26+
const key = node.arguments[0] && getStringIfConstant(node.arguments[0])
27+
if (key == null) {
28+
continue
29+
}
30+
const nextTraceMap = Object.hasOwn(traceMap, key) && traceMap[key]
31+
if (!nextTraceMap) {
32+
continue
33+
}
34+
35+
if (nextTraceMap[READ]) {
36+
yield {
37+
node,
38+
path: [key],
39+
type: READ,
40+
info: nextTraceMap[READ],
41+
}
42+
}
43+
44+
for (const ref of tracker.iteratePropertyReferences(
45+
node,
46+
nextTraceMap
47+
)) {
48+
yield {
49+
...ref,
50+
path: [key, ...ref.path],
51+
}
52+
}
53+
}
54+
}
55+
56+
module.exports = { iterateProcessGetBuiltinModuleReferences }

tests/lib/rules/no-deprecated-api.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ ruleTester.run("no-deprecated-api", rule, {
4242
code: "import {request} from 'http'; request()",
4343
languageOptions: { sourceType: "module" },
4444
},
45+
{
46+
code: "const {Buffer} = process.getBuiltinModule('another-buffer'); new Buffer()",
47+
},
48+
{
49+
code: "const {request} = process.getBuiltinModule('http'); request()",
50+
},
4551

4652
// On Node v6.8.0, fs.existsSync revived.
4753
{
@@ -764,6 +770,92 @@ ruleTester.run("no-deprecated-api", rule, {
764770
],
765771
},
766772

773+
// process.getBuiltinModule()
774+
{
775+
code: "const b = process.getBuiltinModule('buffer'); new b.Buffer()",
776+
options: [{ version: "6.0.0" }],
777+
errors: [
778+
"'new buffer.Buffer()' was deprecated since v6.0.0. Use 'buffer.Buffer.alloc()' or 'buffer.Buffer.from()' instead.",
779+
],
780+
},
781+
{
782+
code: "const b = process.getBuiltinModule('node:buffer'); new b.Buffer()",
783+
options: [{ version: "6.0.0" }],
784+
errors: [
785+
"'new buffer.Buffer()' was deprecated since v6.0.0. Use 'buffer.Buffer.alloc()' or 'buffer.Buffer.from()' instead.",
786+
],
787+
},
788+
{
789+
code: "const {Buffer} = process.getBuiltinModule('buffer'); new Buffer()",
790+
options: [{ version: "6.0.0" }],
791+
errors: [
792+
"'new buffer.Buffer()' was deprecated since v6.0.0. Use 'buffer.Buffer.alloc()' or 'buffer.Buffer.from()' instead.",
793+
],
794+
},
795+
{
796+
code: "const {Buffer:b} = process.getBuiltinModule('buffer'); new b()",
797+
options: [{ version: "6.0.0" }],
798+
errors: [
799+
"'new buffer.Buffer()' was deprecated since v6.0.0. Use 'buffer.Buffer.alloc()' or 'buffer.Buffer.from()' instead.",
800+
],
801+
},
802+
{
803+
code: "const b = process.getBuiltinModule('buffer'); b.SlowBuffer",
804+
options: [{ version: "6.0.0" }],
805+
errors: [
806+
"'buffer.SlowBuffer' was deprecated since v6.0.0. Use 'buffer.Buffer.allocUnsafeSlow()' instead.",
807+
],
808+
},
809+
{
810+
code: "const domain = process.getBuiltinModule('domain');",
811+
options: [{ version: "4.0.0" }],
812+
languageOptions: { sourceType: "module" },
813+
errors: ["'domain' module was deprecated since v4.0.0."],
814+
},
815+
816+
{
817+
code: "new (process.getBuiltinModule('buffer').Buffer)()",
818+
options: [
819+
{
820+
//
821+
ignoreModuleItems: ["buffer.Buffer()"],
822+
ignoreGlobalItems: ["Buffer()", "new Buffer()"],
823+
version: "6.0.0",
824+
},
825+
],
826+
errors: [
827+
"'new buffer.Buffer()' was deprecated since v6.0.0. Use 'buffer.Buffer.alloc()' or 'buffer.Buffer.from()' instead.",
828+
],
829+
},
830+
{
831+
code: "process.getBuiltinModule('buffer').Buffer()",
832+
options: [
833+
{
834+
//
835+
ignoreModuleItems: ["new buffer.Buffer()"],
836+
ignoreGlobalItems: ["Buffer()", "new Buffer()"],
837+
version: "6.0.0",
838+
},
839+
],
840+
errors: [
841+
"'buffer.Buffer()' was deprecated since v6.0.0. Use 'buffer.Buffer.alloc()' or 'buffer.Buffer.from()' instead.",
842+
],
843+
},
844+
{
845+
code: "process.getBuiltinModule('module').createRequireFromPath()",
846+
options: [{ version: "12.0.0" }],
847+
errors: [
848+
"'module.createRequireFromPath' was deprecated since v12.2.0.",
849+
],
850+
},
851+
{
852+
code: "process.getBuiltinModule('module').createRequireFromPath()",
853+
options: [{ version: "12.2.0" }],
854+
errors: [
855+
"'module.createRequireFromPath' was deprecated since v12.2.0. Use 'module.createRequire()' instead.",
856+
],
857+
},
858+
767859
//----------------------------------------------------------------------
768860
// Global Variables
769861
//----------------------------------------------------------------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy