-
-
Notifications
You must be signed in to change notification settings - Fork 720
fix(linter/plugins): do not call before hook if empty visitor
#14401
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
fix(linter/plugins): do not call before hook if empty visitor
#14401
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes the linter plugin system by preventing the execution of before and after hooks when a rule's visitor is empty (contains no AST node visitor functions). This improves forward compatibility with planned Rust-side optimizations that may skip JavaScript execution entirely for files that don't contain relevant AST nodes.
- Adds logic to detect empty visitors and substitute a no-op
beforehook that always returns false - Updates test fixtures to ensure rules with only hooks have at least one AST visitor function to maintain their expected behavior
- Removes expected test output for rules that now correctly skip hook execution
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apps/oxlint/src-js/plugins/load.ts | Implements the core optimization logic to detect empty visitors and prevent hook execution |
| apps/oxlint/test/fixtures/*/plugin.ts | Updates test rules to include AST visitor functions to maintain expected test behavior |
| apps/oxlint/test/fixtures/*/output.snap.md | Removes expected output for hooks that are now correctly skipped |
| apps/oxlint/test/fixtures/*/.oxlintrc.json | Adds configuration for new test rule |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Merge activity
|
If rule is defined using `createOnce`, and it returns an empty visitor (`before` / `after` hooks only), then don't run the hooks - the rule is a no-op.
Reason this is important is forward-compatibility with an optimization we could make where we calculate on Rust side whether AST contains any nodes that rules act on, and if it doesn't, skip calling into JS entirely. In that case, the `before` hook wouldn't run on those files.
We can't emulate that behavior entirely, but we can at least avoid users creating rules which *only* contain a `before` hook, and then finding that when we apply that optimization later on, it stops behaving as expected.
i.e. we make the `before` hook never be called for a rule defined like this:
```js
export default {
createOnce(context) {
return {
before() {
if (content.filename.endsWith('.tsx') {
context.report({ message: 'No TSX!', node: context.sourceCode.ast });
}
},
};
},
};
```
0f004ab to
96663fb
Compare
## [1.21.0] - 2025-10-08 ### 🚀 Features - 576be20 linter/plugins: Support selectors DSL (#14435) (overlookmotel) - b2de44f linter/plugins: Support interpolation in normal diagnostic `message` (#14419) (overlookmotel) - 382c5be linter/plugins: Support placeholders in messageIds (#14416) (camc314) - 529e88e linter/plugins: Support `messageId`s (#14415) (camc314) - 83e7824 linter: Add `vue/require-default-export` rule (#14351) (Sysix) - ff98536 linter: Add `vue/no-import-compiler-macros` rule (#14335) (Sysix) - 0ec0847 ci: Run napi tests on windows (#14383) (camc314) ### 🐛 Bug Fixes - 11e0440 linter/jsx-handler-name: Improve handler name position in error messages (#14174) (Takuji Shimokawa) - 493082c language_server: Use the first Span of the message as the primary Diagnostic range (#14057) (Sysix) - 88ec1bd linter/plugins: Fix error messages (#14423) (overlookmotel) - 18616c2 oxlint: Ignore fixtures dir for vitest (#14414) (camc314) - ec02fe8 oxlint: Normalize path separators in snapshot tests (#14406) (camc314) - 6e8d2f6 language_server: Ignore JS plugins (#14379) (overlookmotel) - 96663fb linter/plugins: Do not call `before` hook if empty visitor (#14401) (overlookmotel) - 52f04bd linter: Use `pathToFileURL` for importing plugins to ensure correct URL format (#14394) (camc314) - 1ea0d46 oxlint: Resolve tsdown deprecation warning (#14389) (camc314) - bb679b5 linter: Promise/prefer-await-to-then strict option not reading from config (#14382) (camc314) ### 🚜 Refactor - 3374b8e linter/language_server: Move all lsp relevant code to `oxc_language_server` crate (#14430) (Sysix) - d24b74e linter/language_server: `oxc_linter::TsgoLinter::run_source` returns `Message` (#14429) (Sysix) - e5b7fb2 linter/language_server: `oxc_linter::Runtime::run_source` returns `Message` (#14428) (Sysix) - 3b26bf3 linter/plugins: Split adding visit function to compiler visitor into multiple functions (#14433) (overlookmotel) - af3a75e linter/plugins: Track ancestors while walking AST (#14432) (overlookmotel) - f279f0b linter/plugins: Do not lazy-load visitor keys (#14431) (overlookmotel) - 5e99ed3 linter/plugins: Allow nullish values as `message` or `messageId` (#14422) (overlookmotel) - dc30938 linter/plugins: Remove default value from `Context` constructor (#14421) (overlookmotel) - 28cfae0 oxlint: Use `vitest`s built in file snapshot comparison (#14392) (camc314) - 06b0e9f linter/plugins: Convert generated files to TS (#14385) (overlookmotel) - 52f35c6 napi/parser, linter/plugins: Rename `types.js` to `type_ids.js` (#14384) (overlookmotel) ### ⚡ Performance - 26435a1 linter/plugins: Small perf optimizations (#14420) (overlookmotel) - d8a8be1 linter/plugins: Avoid private methods (#14418) (overlookmotel) ### 🧪 Testing - d8da4a4 linter/plugins: Clarify tests for message placeholders (#14417) (overlookmotel) Co-authored-by: camc314 <18101008+camc314@users.noreply.github.com>

If rule is defined using
createOnce, and it returns an empty visitor (before/afterhooks only), then don't run the hooks - the rule is a no-op.Reason this is important is forward-compatibility with an optimization we could make where we calculate on Rust side whether AST contains any nodes that rules act on, and if it doesn't, skip calling into JS entirely. In that case, the
beforehook wouldn't run on those files.We can't emulate that behavior entirely, but we can at least avoid users creating rules which only contain a
beforehook, and then finding that when we apply that optimization later on, it stops behaving as expected.i.e. we make the
beforehook never be called for a rule defined like this:To get a rule to always run on every file:
export default { createOnce(context) { return { - before() { + Program(_node) { if (content.filename.endsWith('.tsx') { context.report({ message: 'No TSX!', node: context.sourceCode.ast }); } }, }; }, };