From 2b09f555500e41809046b06992a0344990cf4cd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Sat, 7 Mar 2026 16:07:20 +0100 Subject: [PATCH 1/2] Enforce ECMAScript-only globals in test files via ESLint Add an allowlist-based ESLint config using globals.es2025 plus the CTS harness globals (assert, loadAddon, mustCall, mustNotCall, gcUntil). Any runtime-specific API (setTimeout, process, Buffer, etc.) now triggers a no-undef error in tests/**/*.js files. Co-Authored-By: Claude Opus 4.6 --- eslint.config.js | 19 +++++++++++++++---- package-lock.json | 20 +++++++++++++++++--- package.json | 1 + 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 65fa512..12c300f 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,17 +1,28 @@ import { defineConfig, globalIgnores } from "eslint/config"; +import globals from "globals"; export default defineConfig([ globalIgnores(["node"]), { files: ["tests/**/*.js"], + languageOptions: { + // Only allow ECMAScript built-ins and CTS harness globals. + // This causes no-undef to flag any runtime-specific API (setTimeout, process, Buffer, etc.). + globals: { + ...globals.es2025, + // CTS harness globals + assert: "readonly", + loadAddon: "readonly", + mustCall: "readonly", + mustNotCall: "readonly", + gcUntil: "readonly", + }, + }, rules: { - // Test files are expected to be self-contained + "no-undef": "error", "no-restricted-imports": ["error", { patterns: ["*"], }], - "no-restricted-globals": ["error", - { name: "require", message: "Test files are expected to be self-contained" } - ] }, }, ]); diff --git a/package-lock.json b/package-lock.json index d8ec84f..2f8e71a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "devDependencies": { "@types/node": "^24.10.1", "eslint": "^9.39.1", + "globals": "^17.4.0", "node-api-headers": "^1.7.0" } }, @@ -123,6 +124,19 @@ "url": "https://opencollective.com/eslint" } }, + "node_modules/@eslint/eslintrc/node_modules/globals": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-14.0.0.tgz", + "integrity": "sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/@eslint/eslintrc/node_modules/strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -688,9 +702,9 @@ } }, "node_modules/globals": { - "version": "14.0.0", - "resolved": "https://registry.npmjs.org/globals/-/globals-14.0.0.tgz", - "integrity": "sha512-oahGvuMGQlPw/ivIYBjVSrWAfWLBeku5tpPE2fOPLi+WHffIWbuh2tCjhyQhTBPMf5E9jDEH4FOmTYgYwbKwtQ==", + "version": "17.4.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-17.4.0.tgz", + "integrity": "sha512-hjrNztw/VajQwOLsMNT1cbJiH2muO3OROCHnbehc8eY5JyD2gqz4AcMHPqgaOR59DjgUjYAYLeH699g/eWi2jw==", "dev": true, "license": "MIT", "engines": { diff --git a/package.json b/package.json index 1590d7b..0288ad9 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "devDependencies": { "@types/node": "^24.10.1", "eslint": "^9.39.1", + "globals": "^17.4.0", "node-api-headers": "^1.7.0" }, "dependencies": { From 031b1e3dd615a7eed1b636122b80ace3a464457b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Sun, 8 Mar 2026 13:57:12 +0100 Subject: [PATCH 2/2] Add experimentalFeatures global and ban globalThis/global access in tests - Add experimentalFeatures to the allowed CTS harness globals - Add no-restricted-syntax rule to flag globalThis.* and global.* access, ensuring tests use CTS harness globals (e.g. gcUntil) instead Co-Authored-By: Claude Opus 4.6 --- eslint.config.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/eslint.config.js b/eslint.config.js index 12c300f..fff73c6 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -16,6 +16,7 @@ export default defineConfig([ mustCall: "readonly", mustNotCall: "readonly", gcUntil: "readonly", + experimentalFeatures: "readonly", }, }, rules: { @@ -23,6 +24,10 @@ export default defineConfig([ "no-restricted-imports": ["error", { patterns: ["*"], }], + "no-restricted-syntax": ["error", + { selector: "MemberExpression[object.name='globalThis']", message: "Avoid globalThis access in test files — use CTS harness globals instead" }, + { selector: "MemberExpression[object.name='global']", message: "Avoid global access in test files — use CTS harness globals instead" } + ], }, }, ]);