-
-
Notifications
You must be signed in to change notification settings - Fork 737
feat(linter/plugins): allow JS plugins to access globals
#16512
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
Merged
overlookmotel
merged 11 commits into
oxc-project:main
from
lilnasy:globals-quick-dirty-and-inefficient
Dec 6, 2025
Merged
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dea5d8c
feat(linter/plugins): allow JS plugins to access `globals`
lilnasy d8a94c0
`to_string_lossy()` does not exist anymore?
lilnasy 23b9919
suggestions from code review
lilnasy 3a44ac0
style: import with ts extension
lilnasy 6fc57a9
style: formatting
overlookmotel 076d68b
style: move comment
overlookmotel 84b89ae
add comments
overlookmotel 1606231
add debug assert
overlookmotel 1559510
`Object.freeze` as top-level var
overlookmotel 24c13c1
avoid `@ts-expect-error`
overlookmotel f9bb9fc
style: shorten code
overlookmotel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Methods related to globals. | ||
| */ | ||
|
|
||
| import { debugAssertIsNonNull } from "../utils/asserts.js"; | ||
|
|
||
| /** | ||
| * Globals for the file being linted. | ||
| * | ||
| * Globals are deserialized from JSON, so can only contain JSON-compatible values. | ||
| * Each global variable maps to "readonly", "writable", or "off". | ||
| */ | ||
| export type Globals = Record<string, "readonly" | "writable" | "off">; | ||
|
|
||
| // Empty globals object. | ||
| // No need to freeze this object, as it's never passed to user. | ||
| export const EMPTY_GLOBALS: Globals = {}; | ||
|
|
||
| // Globals for current file. | ||
| // `globalsJSON` is set before linting a file by `setGlobalsForFile`. | ||
| // `globals` is deserialized from `globalsJSON` lazily upon first access. | ||
| let globalsJSON: string | null = null; | ||
| export let globals: Readonly<Globals> | null = null; | ||
|
|
||
| /** | ||
| * Updates the globals for the file. | ||
| * | ||
| * TODO(perf): Globals are deserialized once per file to accommodate folder level settings, | ||
| * even if the globals haven't changed. | ||
| * | ||
| * @param globalsJSONInput - Globals for the file as JSON | ||
| */ | ||
| export function setGlobalsForFile(globalsJSONInput: string): undefined { | ||
| globalsJSON = globalsJSONInput; | ||
| } | ||
|
|
||
| /** | ||
| * Deserialize globals from JSON. | ||
| */ | ||
| export function initGlobals(): void { | ||
| debugAssertIsNonNull(globalsJSON); | ||
|
|
||
| if (globalsJSON === "{}") { | ||
| globals = EMPTY_GLOBALS; | ||
| return; | ||
| } | ||
|
|
||
| globals = JSON.parse(globalsJSON); | ||
|
|
||
| // `globals` was deserialized from JSON, so we can use a simple `for..in` loop here | ||
| for (const key in globals) { | ||
| // @ts-expect-error globals is not made immutable yet | ||
| if (globals[key] === "writeable") globals[key] = "writable"; | ||
| } | ||
|
|
||
| // Freeze the globals object, to prevent any mutation of `globals` by plugins. | ||
| // No need to deep freeze since all keys are just strings. | ||
| Object.freeze(globals); | ||
| } | ||
|
|
||
| /** | ||
| * Reset globals. | ||
| */ | ||
| export function resetGlobals(): undefined { | ||
| globals = null; | ||
| globalsJSON = null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "jsPlugins": [ | ||
| "./plugin.ts" | ||
| ], | ||
| "categories": { | ||
| "correctness": "off" | ||
| }, | ||
| "rules": { | ||
| "globals-plugin/globals": "error" | ||
| }, | ||
| "globals": { | ||
| "React": "readonly", | ||
| "process": "writable", | ||
| "console": "readonly", | ||
| "window": "off" | ||
| }, | ||
| "overrides": [ | ||
| { | ||
| "files": [ | ||
| "files/nested/**" | ||
| ], | ||
| "globals": { | ||
| "React": "writable", | ||
| "process": "off", | ||
| "customGlobal": "readonly" | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| debugger; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| let x; | ||
lilnasy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Exit code | ||
| 1 | ||
|
|
||
| # stdout | ||
| ``` | ||
| x globals-plugin(globals): { | ||
| | "React": "readonly", | ||
| | "console": "readonly", | ||
| | "process": "writable", | ||
| | "window": "off" | ||
| | } | ||
| ,-[files/index.js:1:1] | ||
| 1 | debugger; | ||
| : ^ | ||
| `---- | ||
|
|
||
| x globals-plugin(globals): { | ||
| | "React": "writable", | ||
| | "console": "readonly", | ||
| | "customGlobal": "readonly", | ||
| | "process": "off", | ||
| | "window": "off" | ||
| | } | ||
| ,-[files/nested/index.js:1:1] | ||
| 1 | let x; | ||
| : ^ | ||
| `---- | ||
|
|
||
| Found 0 warnings and 2 errors. | ||
| Finished in Xms on 2 files using X threads. | ||
| ``` | ||
|
|
||
| # stderr | ||
| ``` | ||
| WARNING: JS plugins are experimental and not subject to semver. | ||
| Breaking changes are possible while JS plugins support is under development. | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { Node, Plugin } from "#oxlint"; | ||
|
|
||
| const SPAN: Node = { | ||
| start: 0, | ||
| end: 0, | ||
| range: [0, 0], | ||
| loc: { | ||
| start: { line: 0, column: 0 }, | ||
| end: { line: 0, column: 0 }, | ||
| }, | ||
| }; | ||
|
|
||
| const plugin: Plugin = { | ||
| meta: { | ||
| name: "globals-plugin", | ||
| }, | ||
| rules: { | ||
| globals: { | ||
| create(context) { | ||
| context.report({ | ||
| message: JSON.stringify(context.languageOptions.globals, null, 2), | ||
| node: SPAN, | ||
| }); | ||
| return {}; | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.