Files
aba65151d3 feat(bindings): add lint / lintSync API to @swc/react-compiler (#11965)
**Description:**

Adds a dedicated `lint`/`lintSync` API to `@swc/react-compiler` for
running React Compiler's lint pass and getting back structured
diagnostics, without compiling.

This replaces the original approach in this PR (attaching
`reactCompilerDiagnostics` to `@swc/core`'s thrown
`transform`/`transformSync` error), per feedback from @kdy1 and
@magic-akari — a dedicated API scoped to React Compiler, rather than
extending `@swc/core`'s generic error shape. That change is fully
reverted here.

```ts
import { lint, lintSync } from '@swc/react-compiler'

lintSync(code: string, syntax?: ParserConfig): Diagnostic[]
lint(code: string, syntax?: ParserConfig): Promise<Diagnostic[]>
```

- `code` is the raw source text.
- `syntax` is the same `ParserConfig` shape `@swc/core`'s `jsc.parser`
already accepts (`@swc/types`), letting callers opt into
TypeScript/JSX/TSX/decorators per file. Defaults to plain ECMAScript
when omitted, matching `@swc/core`'s own default.
- Each `Diagnostic` includes `severity`, `message`, `ruleId`,
`category`, `reason`, `description`, `loc` (start/end line/column/index
+ filename/identifierName), and `details`.

Motivating use case: building fast, per-rule ESLint integrations on top
of the Rust-based compiler (one `lintSync` call per file, fanned out to
N independently-configurable ESLint rules by `ruleId`) instead of
re-implementing lint checks in JS.

React Compiler's own compile options (`compilationMode`, `target`,
`panicThreshold`, `gating`, `environment`, ...) are intentionally not
exposed yet — investigated and left as-is for now (e.g. raising
`panicThreshold` would route more results through a fallback path that
loses `ruleId`/`category`/`loc`, working against the point of this API).

Validation:

- `cargo fmt --all`
- `cargo clippy --all --all-targets -- -D warnings`
- `cargo test -p swc_ecma_react_compiler`
- `cargo test -p binding_react_compiler_node`
- `cargo check -p binding_core_node` (confirms the revert didn't leave
`@swc/core`'s napi binding referencing removed types)
- `pnpm --filter @swc/react-compiler build:dev && pnpm --filter
@swc/react-compiler test`

**BREAKING CHANGE:**

None. `lint`/`lintSync` are new; the reverted `@swc/core` changes were
never released.

**Related issue (if exists):**

None.

---------

Co-authored-by: Oz <[email protected]>
Co-authored-by: Claude Sonnet 5 <[email protected]>
Co-authored-by: DongYun Kang <[email protected]>
2026-07-06 14:28:42 +09:00

46 lines
1.3 KiB
JavaScript

const test = require("node:test");
const assert = require("node:assert/strict");
const reactCompiler = require("../index.js");
const REF_ACCESS_SOURCE = `
import { useRef } from 'react';
function App() {
const ref = useRef(1);
return ref.current;
}
`;
test("lintSync reports a rule violation with rule metadata", () => {
const diagnostics = reactCompiler.lintSync(REF_ACCESS_SOURCE);
assert.equal(diagnostics.length, 1);
assert.equal(diagnostics[0].severity, "error");
assert.equal(diagnostics[0].ruleId, "refs");
assert.equal(diagnostics[0].category, "Refs");
});
test("lintSync returns no diagnostics for non-React code", () => {
const diagnostics = reactCompiler.lintSync("const x = 1;");
assert.deepEqual(diagnostics, []);
});
test("lint (async) matches lintSync", async () => {
const syncResult = reactCompiler.lintSync(REF_ACCESS_SOURCE);
const asyncResult = await reactCompiler.lint(REF_ACCESS_SOURCE);
assert.deepEqual(asyncResult, syncResult);
});
test("lintSync accepts an explicit ecmascript syntax", () => {
const diagnostics = reactCompiler.lintSync(REF_ACCESS_SOURCE, {
syntax: "ecmascript",
jsx: true,
});
assert.equal(diagnostics.length, 1);
assert.equal(diagnostics[0].ruleId, "refs");
});