mirror of
https://github.com/hanzoai/swc.git
synced 2026-10-30 19:42:53 +00:00
**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]>
60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
|
|
import type { EsParserConfig, TsParserConfig } from '@swc/types'
|
|
|
|
import * as binding from './binding'
|
|
|
|
export type {
|
|
Diagnostic,
|
|
DiagnosticDetail,
|
|
DiagnosticLocation,
|
|
DiagnosticPosition,
|
|
} from './binding'
|
|
|
|
/**
|
|
* The `syntax` shapes `lint`/`lintSync` accept. Deliberately narrower than
|
|
* `@swc/types`'s `ParserConfig` (which also includes `FlowParserConfig`):
|
|
* this crate only enables `swc_ecma_parser`'s `typescript` feature, not
|
|
* `flow`, so the compiled `Syntax` enum has no `Flow` variant — passing a
|
|
* Flow config would type-check against the wider `ParserConfig` but fail
|
|
* at runtime with a confusing `unknown variant 'flow'` error. Excluding it
|
|
* here turns that into a compile-time error instead.
|
|
*/
|
|
export type LintSyntax = TsParserConfig | EsParserConfig
|
|
|
|
/**
|
|
* TODO
|
|
*/
|
|
export async function isReactCompilerRequired(code: Buffer) {
|
|
return await binding.isReactCompilerRequired(code)
|
|
}
|
|
|
|
|
|
/**
|
|
* TODO
|
|
*/
|
|
export function isReactCompilerRequiredSync(code: Buffer): boolean {
|
|
return binding.isReactCompilerRequiredSync(code)
|
|
}
|
|
|
|
function encodeSyntax(syntax?: LintSyntax): Buffer | undefined {
|
|
return syntax === undefined ? undefined : Buffer.from(JSON.stringify(syntax))
|
|
}
|
|
|
|
/**
|
|
* Lints `code` for React Compiler rule violations without compiling it.
|
|
*
|
|
* `syntax` defaults to plain ECMAScript (no JSX/TSX/decorators) when
|
|
* omitted, matching `@swc/core`'s own default — pass an explicit `syntax`
|
|
* for TypeScript, JSX, or decorator syntax.
|
|
*/
|
|
export async function lint(code: string, syntax?: LintSyntax): Promise<binding.Diagnostic[]> {
|
|
return await binding.lint(code, encodeSyntax(syntax))
|
|
}
|
|
|
|
/**
|
|
* Synchronous variant of {@link lint}.
|
|
*/
|
|
export function lintSync(code: string, syntax?: LintSyntax): binding.Diagnostic[] {
|
|
return binding.lintSync(code, encodeSyntax(syntax))
|
|
}
|