Skip to content

Conversation

overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Sep 20, 2025

Add createOnce method for rules. This is the "alternative API" mentioned in #9905 (comment).

export default {
  meta: { /* ... */ },
  // `createOnce`, not `create`. Called only once, NOT once per file.
  createOnce(context) {
    let classCount;
    return {
      before() {
        classCount = 0;
      },
      ClassDeclaration(klass) {
        classCount++;
      },
      after() {
        if (classCount > 5) {
          context.report({
            message: 'files should not contain more than 5 class declarations, '
              + `found ${classCount}`,
            node: { start: 0, end: 0 },
          });
        }
      },
    };
  }
};

If before method returns false, the rule is skipped for the current file. This is equivalent to the following pattern in ESLint:

export default {
  meta: { /* ... */ },
  create(context) {
    if (shouldSkip(context)) return {}; // Skip
    return { /* visitor */ };
  }
};

Currently we don't capitalize on the potential perf benefits of this alternative API, but plugin authors can use it now, and once we do implement the perf optimizations this API enables, they'll get a speed boost just by upgrading Oxlint, without needing to change their plugin code.

@github-actions github-actions bot added A-linter Area - Linter A-cli Area - CLI C-enhancement Category - New feature or request labels Sep 20, 2025
Copy link
Member Author

overlookmotel commented Sep 20, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

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.

@overlookmotel overlookmotel marked this pull request as ready for review September 20, 2025 11:13
@overlookmotel overlookmotel force-pushed the 09-20-feat_linter_estree-compatible_ast_for_js_plugins branch from 55771c4 to dc47cc3 Compare September 21, 2025 10:03
@overlookmotel overlookmotel force-pushed the 09-20-feat_linter_plugins_add_createonce_api branch 2 times, most recently from 988eab7 to 710d205 Compare September 21, 2025 10:06
@overlookmotel overlookmotel force-pushed the 09-20-feat_linter_estree-compatible_ast_for_js_plugins branch 2 times, most recently from 234a600 to a089a79 Compare September 21, 2025 10:31
@overlookmotel overlookmotel force-pushed the 09-20-feat_linter_plugins_add_createonce_api branch from 710d205 to a0a8490 Compare September 21, 2025 10:31
@overlookmotel overlookmotel force-pushed the 09-20-feat_linter_plugins_add_createonce_api branch from a0a8490 to d903879 Compare September 21, 2025 15:00
@overlookmotel overlookmotel force-pushed the 09-20-feat_linter_estree-compatible_ast_for_js_plugins branch from a089a79 to 16f54cc Compare September 21, 2025 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area - CLI A-linter Area - Linter C-enhancement Category - New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants