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]>
40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
/* auto-generated by NAPI-RS */
|
|
/* eslint-disable */
|
|
export interface Diagnostic {
|
|
severity: string
|
|
message: string
|
|
ruleId?: string
|
|
category?: string
|
|
reason?: string
|
|
description?: string
|
|
loc?: DiagnosticLocation
|
|
details: Array<DiagnosticDetail>
|
|
}
|
|
|
|
export interface DiagnosticDetail {
|
|
kind: string
|
|
loc?: DiagnosticLocation
|
|
message?: string
|
|
}
|
|
|
|
export interface DiagnosticLocation {
|
|
start: DiagnosticPosition
|
|
end: DiagnosticPosition
|
|
filename?: string
|
|
identifierName?: string
|
|
}
|
|
|
|
export interface DiagnosticPosition {
|
|
line: number
|
|
column: number
|
|
index?: number
|
|
}
|
|
|
|
export declare function isReactCompilerRequired(code: Buffer, signal?: AbortSignal | undefined | null): Promise<boolean>
|
|
|
|
export declare function isReactCompilerRequiredSync(code: Buffer): boolean
|
|
|
|
export declare function lint(code: string, syntax?: Buffer | undefined | null, signal?: AbortSignal | undefined | null): Promise<Array<Diagnostic>>
|
|
|
|
export declare function lintSync(code: string, syntax?: Buffer | undefined | null): Array<Diagnostic>
|