swc-project/swc · error · anyhow::Error
Syntax Error
Error message
Syntax Error
What it means
Thrown by swc-ast-explorer's parse_source (crates/swc-ast-explorer/src/parser.rs). The file is always parsed as TypeScript with TSX enabled; the error fires both for fatal parse errors and for recovered errors - by design, recovered diagnostics are treated as fatal so the CLI never prints a partial AST for invalid input. The actual diagnostic is emitted to the handler before bailing, so the message itself is intentionally generic.
Source
Thrown at crates/swc-ast-explorer/src/parser.rs:18
use anyhow::{anyhow, bail, Result};
use swc_common::{errors::Handler, SourceFile};
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_parser::{parse_file_as_program, Syntax, TsSyntax};
/// Parses a source file as JavaScript, TypeScript, or TSX and returns the SWC
/// program tree. Recovered parser errors are treated as fatal so the CLI does
/// not print partial ASTs for invalid input.
pub fn parse_source(file: &SourceFile, handler: &Handler) -> Result<Program> {
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 d7d7434666)
Solutions
- Look at the diagnostic printed above the error - it has the exact line/column - and fix the syntax at that spot
- Verify the file parses elsewhere first (tsc --noEmit, or swc CLI with matching syntax) before exploring it
- Update swc-ast-explorer so its parser supports the newest syntax used in the file
- If the file relies on JS-only syntax the TS grammar misreads, preprocess or rename to an appropriate setup before exploring
Example fix
// before: ambiguous type-cast syntax breaks the TS grammar const n = <number>value; // after: use the as-form the TSX-capable grammar accepts const n = value as number;
Defensive patterns
Strategy: try-catch
Validate before calling
// Skip files that do not even tokenize/parse before exploring
// (cheap pre-check with the same parser family, e.g. run swc transform once)
import { transformSync } from '@swc/core';
function isParseable(filename, code) {
try {
transformSync(code, { filename, jsc: { parser: { syntax: 'typescript', tsx: true } } });
return true;
} catch {
return false;
}
} Try / catch
// CLI context: check exit status and stderr
// $ swc-ast-explore file.ts 2>diag.txt || handle 'Syntax Error'
// Library context (Rust):
// let program = parse_source(&fm, &handler).unwrap_or_else(|e| {
// eprintln!("explorer cannot parse this file: {e}");
// std::process::exit(1);
// }); Prevention
- Run tsc --noEmit or an equivalent parse check before pointing the explorer at a file
- Remember the explorer always parses as TS+TSX: avoid syntax legal only in non-TS grammars, and prefer `x as T` casts over `<T>x`
- Keep swc-ast-explorer updated so recent proposal syntax is understood
When it happens
Trigger: Running the swc-ast-explorer CLI on a file with any syntax error; feeding TSX/JSX into a context where the TS+TSX grammar still rejects it (e.g. ambiguous '<Type>expr' casts in .ts-mode content, invalid decorators, unfinished template literals); exploring files with proposal syntax the vendored parser version does not support.
Common situations: Pointing the explorer at draft or WIP files that do not compile; using an older explorer build against newer language syntax; files that are valid JS but rejected by the stricter TS grammar used unconditionally here.
Related errors
- Failed to parse the input
- Syntax Error
- Bundle command is not yet implemented
- Lint command is not yet implemented
- Minify command is not yet implemented
AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16).
Data as JSON: /api/errors/8db5a284b1c33797.
Report an issue: GitHub.