Skip to content

Commit 5427ce8

Browse files
kraenhansenclaude
andauthored
feat: enforce ECMAScript-only globals in test files via ESLint (#30)
* 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ebd91a5 commit 5427ce8

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

eslint.config.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
import { defineConfig, globalIgnores } from "eslint/config";
2+
import globals from "globals";
23

34
export default defineConfig([
45
globalIgnores(["node"]),
56
{
67
files: ["tests/**/*.js"],
8+
languageOptions: {
9+
// Only allow ECMAScript built-ins and CTS harness globals.
10+
// This causes no-undef to flag any runtime-specific API (setTimeout, process, Buffer, etc.).
11+
globals: {
12+
...globals.es2025,
13+
// CTS harness globals
14+
assert: "readonly",
15+
loadAddon: "readonly",
16+
mustCall: "readonly",
17+
mustNotCall: "readonly",
18+
gcUntil: "readonly",
19+
experimentalFeatures: "readonly",
20+
},
21+
},
722
rules: {
8-
// Test files are expected to be self-contained
23+
"no-undef": "error",
924
"no-restricted-imports": ["error", {
1025
patterns: ["*"],
1126
}],
12-
"no-restricted-globals": ["error",
13-
{ name: "require", message: "Test files are expected to be self-contained" }
14-
]
27+
"no-restricted-syntax": ["error",
28+
{ selector: "MemberExpression[object.name='globalThis']", message: "Avoid globalThis access in test files — use CTS harness globals instead" },
29+
{ selector: "MemberExpression[object.name='global']", message: "Avoid global access in test files — use CTS harness globals instead" }
30+
],
1531
},
1632
},
1733
]);

package-lock.json

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"devDependencies": {
1414
"@types/node": "^24.10.1",
1515
"eslint": "^9.39.1",
16+
"globals": "^17.4.0",
1617
"node-api-headers": "^1.7.0"
1718
},
1819
"dependencies": {

0 commit comments

Comments
 (0)