{"record":{"id":"8db5a284b1c33797","repo":"swc-project/swc","slug":"syntax-error","errorCode":null,"errorMessage":"Syntax Error","messagePattern":"Syntax Error","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc-ast-explorer/src/parser.rs","lineNumber":28,"sourceCode":"    let syntax = Syntax::Typescript(TsSyntax {\n        tsx: true,\n        ..Default::default()\n    });\n    let mut errors = Vec::new();\n    let program = parse_file_as_program(file, syntax, EsVersion::latest(), None, &mut errors)\n        .map_err(|err| {\n            err.into_diagnostic(handler).emit();\n            anyhow!(\"Syntax Error\")\n        })?;\n\n    let mut has_recovered_error = false;\n    for err in errors {\n        err.into_diagnostic(handler).emit();\n        has_recovered_error = true;\n    }\n\n    if has_recovered_error {\n        bail!(\"Syntax Error\");\n    }\n\n    Ok(program)\n}\n","sourceCodeStart":10,"sourceCodeEnd":33,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc-ast-explorer/src/parser.rs#L10-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before - angle-bracket assertion inside TSX fails the TSX grammar\nconst n = <number>x;\n\n// after - use the `as` form in TSX files\nconst n = x as number;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"use swc_common::errors::Handler;\n\nmatch parse_source(&fm, &handler) {\n    Ok(program) => program,\n    Err(e) if format!(\"{e:#}\").contains(\"Syntax Error\") => {\n        // the Handler already printed exact locations; convert to a structured result\n        return Err(anyhow!(\"input is not valid TypeScript/TSX: {}\", fm.name));\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["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"],"tags":["swc-ast-explorer","parser","typescript","tsx","syntax"],"backgroundTag":"javascript-syntax-error","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}