swc-project/swc · error
Syntax Error
Error message
Syntax Error
What it means
swc-ast-explorer's parse_source parses every input as TypeScript with TSX enabled (parse_file_as_program) and deliberately treats even recovered parser errors as fatal: it emits each diagnostic and then bails with the bare message 'Syntax Error'. The bail message carries no detail by design - the emitted diagnostics above it contain locations. Any syntax the TS/TSX grammar flags, including recoverable errors, aborts parsing.
Solutions
- Read the emitted diagnostic - it has the exact file, line and column of the first error
- Check for JSX/angle-bracket conflicts and use 'as' assertions in .tsx content
- Downgrade/simplify the offending syntax to confirm it is the trigger
- If the file is standard TypeScript, update or report against swc_ecma_parser with the repro
Example fix
// before - angle-bracket assertion inside TSX fails the TSX grammar const n = <number>x; // after - use the `as` form in TSX files const n = x as number;
Defensive patterns
Strategy: try-catch
Try / catch
use swc_common::errors::Handler;
match parse_source(&fm, &handler) {
Ok(program) => program,
Err(e) if format!("{e:#}").contains("Syntax Error") => {
// the Handler already printed exact locations; convert to a structured result
return Err(anyhow!("input is not valid TypeScript/TSX: {}", fm.name));
}
Err(e) => return Err(e),
} Prevention
- Read the emitted diagnostic, not the bare 'Syntax Error' bail - it has line/column detail
- Pre-check files with tsc (--noEmit) in pipelines that feed swc-ast-explorer
- Avoid angle-bracket type assertions in TSX content; use 'as' expressions
- Pin a swc_ecma_parser version that supports the syntax features your corpus uses
When it happens
Trigger: Passing a file whose syntax is invalid under TypeScript+TSX rules: JSX where TSX forbids it, angle-bracket type assertions conflicting with JSX, ECMAScript proposals the bundled parser does not support, or truncated files.
Common situations: Exploring ASTs for files that use very new syntax not yet supported by the pinned swc_ecma_parser version; .tsx files mixing type assertions and JSX; files with syntax valid in Babel but rejected by SWC's TS parser.
Related errors
- Expected
- Expected function or '('
- Expected ident
- Expected ident (exclude the keywords 'only', 'not', 'and'…
- Expected identifier value
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/8db5a284b1c33797.
Report an issue: GitHub.
Appendix: source
Thrown at crates/swc-ast-explorer/src/parser.rs:28
let syntax = Syntax::Typescript(TsSyntax {
tsx: true,
..Default::default()
});
let mut errors = Vec::new();
let program = parse_file_as_program(file, syntax, EsVersion::latest(), None, &mut errors)
.map_err(|err| {
err.into_diagnostic(handler).emit();
anyhow!("Syntax Error")
})?;
let mut has_recovered_error = false;
for err in errors {
err.into_diagnostic(handler).emit();
has_recovered_error = true;
}
if has_recovered_error {
bail!("Syntax Error");
}
Ok(program)
}
View on GitHub (pinned to 5176682b65)