-
-
Notifications
You must be signed in to change notification settings - Fork 899
feat(linter/plugins): add oxlint-plugin-eslint package
#20009
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
graphite-app
merged 1 commit into
main
from
om/03-04-linter_introduce_oxlint-plugin-eslint_package
Mar 9, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| /node_modules/ | ||
| /dist/ | ||
| /dist-pkg-plugins/ | ||
| /dist-pkg-plugin-eslint/ | ||
| /src-js/generated/plugin-eslint/ | ||
| *.node |
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,170 @@ | ||
| /** | ||
| * Generates the `oxlint-plugin-eslint` package source files. | ||
| * | ||
| * This script produces: | ||
| * | ||
| * 1. `rules/<name>.cjs` - One file for each ESLint core rule, that re-exports the rule's `create` function. | ||
| * 2. `index.ts` - Exports all rules as a `Record<string, CreateRule>`. | ||
| * This is the `rules` property of the `oxlint-plugin-eslint` plugin. | ||
| * 3. `rule_names.ts` - Exports a list of all rule names, which is used in TSDown config. | ||
| * | ||
| * `index.ts` uses a split eager/lazy strategy so that `registerPlugin` can read each rule's `meta` | ||
| * without loading the rule module itself: | ||
| * | ||
| * - `meta` is serialized and inlined at build time. | ||
| * `registerPlugin` needs it at plugin registration time (for `fixable`, `hasSuggestions`, `schema`, | ||
| * `defaultOptions`, `messages`), so it must be available immediately without requiring the rule module. | ||
| * | ||
| * - `create` is deferred via a cached `require` call. | ||
| * The rule module is only loaded the first time `create` is called (i.e. when the rule actually runs at lint time). | ||
| * A top-level variable per rule caches the loaded function so subsequent calls skip the `require` call. | ||
| * | ||
| * Build-time validations: | ||
| * - Each rule object must only have `meta` and `create` properties. | ||
| * - `meta` values are walked to ensure they contain no functions | ||
| * (which would be serialized as executable code by `serialize-javascript`). | ||
| */ | ||
|
|
||
| import { readdirSync, mkdirSync, writeFileSync, rmSync } from "node:fs"; | ||
| import { join as pathJoin, basename, relative as pathRelative } from "node:path"; | ||
| import { createRequire } from "node:module"; | ||
| import { execFileSync } from "node:child_process"; | ||
| import serialize from "serialize-javascript"; | ||
|
|
||
| import type { CreateRule } from "../src-js/plugins/load.ts"; | ||
| import type { RuleMeta } from "../src-js/plugins/rule_meta.ts"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
|
|
||
| const oxlintDirPath = pathJoin(import.meta.dirname, ".."); | ||
| const rootDirPath = pathJoin(oxlintDirPath, "../.."); | ||
| const eslintRulesDir = pathJoin(require.resolve("eslint/package.json"), "../lib/rules"); | ||
| const generatedDirPath = pathJoin(oxlintDirPath, "src-js/generated/plugin-eslint"); | ||
| const generatedRulesDirPath = pathJoin(generatedDirPath, "rules"); | ||
|
|
||
| export default function generatePluginEslint(): void { | ||
| // Get all ESLint rule names (exclude `index.js` which is the registry, not a rule) | ||
| const ruleNames = readdirSync(eslintRulesDir) | ||
| .filter((filename) => filename.endsWith(".js") && filename !== "index.js") | ||
| .map((filename) => basename(filename, ".js")) | ||
| .sort(); | ||
|
|
||
| // oxlint-disable-next-line no-console | ||
| console.log(`Found ${ruleNames.length} ESLint rules`); | ||
|
|
||
| // Wipe and recreate generated directories | ||
| rmSync(generatedDirPath, { recursive: true, force: true }); | ||
| mkdirSync(generatedRulesDirPath, { recursive: true }); | ||
|
|
||
| // Generate a CJS wrapper file for each rule | ||
| for (const ruleName of ruleNames) { | ||
| const relPath = pathRelative(generatedRulesDirPath, pathJoin(eslintRulesDir, `${ruleName}.js`)); | ||
| const content = `module.exports = require(${JSON.stringify(relPath)}).create;\n`; | ||
| writeFileSync(pathJoin(generatedRulesDirPath, `${ruleName}.cjs`), content); | ||
| } | ||
|
|
||
| // Generate the plugin rules index. | ||
| // `meta` is inlined so it's available at registration time without loading the rule module. | ||
| // `create` is deferred via a cached `require` so the rule module is only loaded on first use. | ||
| const indexLines = [ | ||
| ` | ||
| import { createRequire } from "node:module"; | ||
|
|
||
| import type { CreateRule } from "../../plugins/load.ts"; | ||
|
|
||
| type CreateFn = CreateRule["create"]; | ||
|
|
||
| var require = createRequire(import.meta.url); | ||
| `, | ||
| ]; | ||
|
|
||
| // Generate a `let` declaration for each rule's cached `create` function. | ||
| // These are initially `null` and populated on first call. | ||
| for (let i = 0; i < ruleNames.length; i++) { | ||
| indexLines.push(`var create${i}: CreateFn | null = null;`); | ||
| } | ||
|
|
||
| indexLines.push("", "export default {"); | ||
|
|
||
| for (let i = 0; i < ruleNames.length; i++) { | ||
| const ruleName = ruleNames[i]; | ||
| const rulePath = pathJoin(eslintRulesDir, `${ruleName}.js`); | ||
| const rule: CreateRule = require(rulePath); | ||
|
|
||
| // Validate that the rule only has expected top-level properties. | ||
| // If ESLint adds new properties in a future version, we want to find out at build time. | ||
| const unexpectedKeys = Object.keys(rule).filter((key) => key !== "meta" && key !== "create"); | ||
| if (unexpectedKeys.length > 0) { | ||
| throw new Error( | ||
| `Unexpected properties on rule \`${ruleName}\`: ${unexpectedKeys.join(", ")}. ` + | ||
| "Expected only `meta` and `create`.", | ||
| ); | ||
| } | ||
|
|
||
| // Reduce `meta` to only the properties Oxlint uses, with consistent shape and property order. | ||
| // We discard e.g. `deprecated` and `docs` properties. This reduces code size. | ||
| // Default values match what `registerPlugin` assumes when a property is absent. | ||
| const { meta } = rule; | ||
| const reducedMeta: RuleMeta = { | ||
| messages: meta?.messages ?? undefined, | ||
| fixable: meta?.fixable ?? null, | ||
| hasSuggestions: meta?.hasSuggestions ?? false, | ||
| schema: meta?.schema ?? undefined, | ||
| defaultOptions: meta?.defaultOptions ?? undefined, | ||
| }; | ||
|
|
||
| // Check for function values in `reducedMeta`, which would be unexpected and likely a bug. | ||
| // `serialize-javascript` would serialize them as executable code, so catch this at build time. | ||
| assertNoFunctions(reducedMeta, `eslint/lib/rules/${ruleName}.js`, "meta"); | ||
|
|
||
| const metaCode = serialize(reducedMeta, { unsafe: true }); | ||
|
|
||
| indexLines.push(` | ||
| ${JSON.stringify(ruleName)}: { | ||
| meta: ${metaCode}, | ||
| create(context) { | ||
| if (create${i} === null) create${i} = require("./rules/${ruleName}.cjs") as CreateFn; | ||
| return create${i}(context); | ||
| }, | ||
| }, | ||
| `); | ||
| } | ||
| indexLines.push("} satisfies Record<string, CreateRule>;\n"); | ||
|
|
||
| const indexFilePath = pathJoin(generatedDirPath, "index.ts"); | ||
| writeFileSync(indexFilePath, indexLines.join("\n")); | ||
|
|
||
| // Format generated index file with oxfmt to clean up unnecessary quotes around property names. | ||
| // This isn't necessary, as it gets minified and bundled anyway, but it makes generated code easier to read | ||
| // when debugging. | ||
| execFileSync("pnpm", ["exec", "oxfmt", "--write", indexFilePath], { cwd: rootDirPath }); | ||
|
|
||
| // Generate the rule_names.ts file for use in tsdown config | ||
| const ruleNamesCode = [ | ||
| "export default [", | ||
| ...ruleNames.map((name) => ` ${JSON.stringify(name)},`), | ||
| "] as const;\n", | ||
| ].join("\n"); | ||
|
|
||
| writeFileSync(pathJoin(generatedDirPath, "rule_names.ts"), ruleNamesCode); | ||
|
|
||
| // oxlint-disable-next-line no-console | ||
| console.log("Generated plugin-eslint files."); | ||
| } | ||
|
|
||
| /** | ||
| * Walk an object tree and throw if any function values are found. | ||
| */ | ||
| function assertNoFunctions(value: unknown, rulePath: string, path: string): void { | ||
| if (typeof value === "function") { | ||
| throw new Error( | ||
| `Unexpected function value in \`${path}\` of rule \`${rulePath}\`. ` + | ||
| "Rule meta objects must be static data.", | ||
| ); | ||
| } | ||
| if (typeof value === "object" && value !== null) { | ||
| for (const [key, child] of Object.entries(value)) { | ||
| assertNoFunctions(child, rulePath, `${path}.${key}`); | ||
| } | ||
| } | ||
| } | ||
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,10 @@ | ||
| // oxlint-disable-next-line typescript/ban-ts-comment | ||
| // @ts-ignore - file is generated and not checked in to git | ||
| import rules from "../generated/plugin-eslint/index.ts"; | ||
|
|
||
| export default { | ||
| meta: { | ||
| name: "eslint-js", | ||
| }, | ||
| rules, | ||
| }; |
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,16 @@ | ||
| { | ||
| "categories": { | ||
| "correctness": "off" | ||
| }, | ||
| "jsPlugins": ["../../../dist-pkg-plugin-eslint/index.js"], | ||
| "rules": { | ||
| "eslint-js/array-bracket-newline": ["error", "consistent"], | ||
| "eslint-js/no-restricted-syntax": [ | ||
| "error", | ||
| { | ||
| "selector": "ThrowStatement > CallExpression[callee.name=/Error$/]", | ||
| "message": "Use `new` keyword when throwing an `Error`." | ||
| } | ||
| ] | ||
| } | ||
| } |
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,6 @@ | ||
| // Violation: array-bracket-newline (opening has newline, closing does not) | ||
| const a = [ | ||
| 1, 2, 3]; | ||
|
|
||
| // Violation: no-restricted-syntax (throw Error without `new`) | ||
| throw TypeError("bad"); |
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,27 @@ | ||
| # Exit code | ||
| 1 | ||
|
|
||
| # stdout | ||
| ``` | ||
| x eslint-js(array-bracket-newline): A linebreak is required before ']'. | ||
| ,-[files/index.js:3:10] | ||
| 2 | const a = [ | ||
| 3 | 1, 2, 3]; | ||
| : ^ | ||
| 4 | | ||
| `---- | ||
|
|
||
| x eslint-js(no-restricted-syntax): Use `new` keyword when throwing an `Error`. | ||
| ,-[files/index.js:6:7] | ||
| 5 | // Violation: no-restricted-syntax (throw Error without `new`) | ||
| 6 | throw TypeError("bad"); | ||
| : ^^^^^^^^^^^^^^^^ | ||
| `---- | ||
|
|
||
| Found 0 warnings and 2 errors. | ||
| Finished in Xms on 1 file with 2 rules using X threads. | ||
| ``` | ||
|
|
||
| # stderr | ||
| ``` | ||
| ``` |
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,3 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this package will be documented in this file. |
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.