Skip to content

feat(node-builtins): Add support for process.getBuiltinModule() #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/unsupported-features/node-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const NodeBuiltinModules = {
...require("./node-builtins-modules/sea.js"),
...require("./node-builtins-modules/stream.js"),
...require("./node-builtins-modules/string_decoder.js"),
...require("./node-builtins-modules/sqlite.js"),
...require("./node-builtins-modules/test.js"),
...require("./node-builtins-modules/timers.js"),
...require("./node-builtins-modules/tls.js"),
Expand Down
7 changes: 7 additions & 0 deletions lib/util/check-unsupported-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const getSemverRange = require("./get-semver-range")
const unprefixNodeColon = require("./unprefix-node-colon")
const semverRangeSubset = require("semver/ranges/subset")
const { getScope } = require("../util/eslint-compat")
const {
iterateProcessGetBuiltinModuleReferences,
} = require("./iterate-process-get-builtin-module-references")

/**
* Parses the options.
Expand Down Expand Up @@ -158,6 +161,10 @@ module.exports.checkUnsupportedBuiltins = function checkUnsupportedBuiltins(
const tracker = new ReferenceTracker(scope, { mode: "legacy" })
const references = [
...tracker.iterateCjsReferences(traceMap.modules ?? {}),
...iterateProcessGetBuiltinModuleReferences(
tracker,
traceMap.modules ?? {}
),
...tracker.iterateEsmReferences(traceMap.modules ?? {}),
...tracker.iterateGlobalReferences(traceMap.globals ?? {}),
]
Expand Down
56 changes: 56 additions & 0 deletions lib/util/iterate-process-get-builtin-module-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use strict"
const {
CALL,
getStringIfConstant,
READ,
} = require("@eslint-community/eslint-utils")
const processGetBuiltinModuleCall = {
process: {
getBuiltinModule: {
[CALL]: true,
},
},
}
/**
* Iterate the references of process.getBuiltinModule() modules.
* @template Info
* @param {import("@eslint-community/eslint-utils").ReferenceTracker} tracker The reference tracker.
* @param {import("@eslint-community/eslint-utils").TraceMap<Info>} traceMap The trace map.
* @returns {IterableIterator<import("@eslint-community/eslint-utils").Reference<Info>>} The iterator.
*/
function* iterateProcessGetBuiltinModuleReferences(tracker, traceMap) {
for (const { node } of tracker.iterateGlobalReferences(
processGetBuiltinModuleCall
)) {
if (node.type !== "CallExpression") continue
const key = node.arguments[0] && getStringIfConstant(node.arguments[0])
if (key == null) {
continue
}
const nextTraceMap = Object.hasOwn(traceMap, key) && traceMap[key]
if (!nextTraceMap) {
continue
}

if (nextTraceMap[READ]) {
yield {
node,
path: [key],
type: READ,
info: nextTraceMap[READ],
}
}

for (const ref of tracker.iteratePropertyReferences(
node,
nextTraceMap
)) {
yield {
...ref,
path: [key, ...ref.path],
}
}
}
}

module.exports = { iterateProcessGetBuiltinModuleReferences }
143 changes: 143 additions & 0 deletions tests/lib/rules/no-unsupported-features/node-builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -5626,5 +5626,148 @@ new RuleTester({ languageOptions: { sourceType: "module" } }).run(
},
],
},

//----------------------------------------------------------------------
// sqlite
//----------------------------------------------------------------------
{
valid: [
{
code: `
import { DatabaseSync } from 'node:sqlite';
const database = new DatabaseSync(':memory:');
`,
options: [
{
version: ">=22.5.0",
allowExperimental: true,
},
],
languageOptions: { ecmaVersion: "latest" },
},
{
code: `
import { DatabaseSync } from 'node:sqlite';
const database = new DatabaseSync(':memory:');
`,
options: [
{
version: ">=22.3.0",
ignores: ["sqlite", "sqlite.DatabaseSync"],
},
],
languageOptions: { ecmaVersion: "latest" },
},
{
code: `
const { DatabaseSync } = require('node:sqlite');
const database = new DatabaseSync(':memory:');
`,
options: [
{
version: ">=22.5.0",
allowExperimental: true,
},
],
languageOptions: { ecmaVersion: "latest" },
},
{
code: `
const { DatabaseSync } = process.getBuiltinModule('node:sqlite');
const database = new DatabaseSync(':memory:');
`,
options: [
{
version: ">=22.5.0",
allowExperimental: true,
},
],
languageOptions: { ecmaVersion: "latest" },
},
],
invalid: [
{
code: `
import { DatabaseSync } from 'node:sqlite';
const database = new DatabaseSync(':memory:');
`,
options: [{ version: ">=22.3.0", allowExperimental: true }],
languageOptions: { ecmaVersion: "latest" },

errors: [
{
messageId: "not-experimental-till",
data: {
name: "sqlite",
experimental: "22.5.0",
version: ">=22.3.0",
},
},
{
messageId: "not-supported-till",
data: {
name: "sqlite.DatabaseSync",
supported: "22.5.0",
version: ">=22.3.0",
},
},
],
},
{
code: `
const { DatabaseSync } = require('node:sqlite');
const database = new DatabaseSync(':memory:');
`,
options: [{ version: ">=22.3.0", allowExperimental: true }],
languageOptions: { ecmaVersion: "latest" },

errors: [
{
messageId: "not-supported-till",
data: {
name: "sqlite.DatabaseSync",
supported: "22.5.0",
version: ">=22.3.0",
},
},
{
messageId: "not-experimental-till",
data: {
name: "sqlite",
experimental: "22.5.0",
version: ">=22.3.0",
},
},
],
},
{
code: `
const { DatabaseSync } = process.getBuiltinModule('node:sqlite');
const database = new DatabaseSync(':memory:');
`,
options: [{ version: ">=22.3.0", allowExperimental: true }],
languageOptions: { ecmaVersion: "latest" },

errors: [
{
messageId: "not-supported-till",
data: {
name: "sqlite.DatabaseSync",
supported: "22.5.0",
version: ">=22.3.0",
},
},
{
messageId: "not-experimental-till",
data: {
name: "sqlite",
experimental: "22.5.0",
version: ">=22.3.0",
},
},
],
},
],
},
])
)