{"record":{"id":"8bd16c31c8508883","repo":"swc-project/swc","slug":"syntax-error-8bd16c","errorCode":null,"errorMessage":"Syntax Error","messagePattern":"Syntax Error","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/swc_compiler_base/src/lib.rs","lineNumber":113,"sourceCode":"            IsModule::CommonJS => {\n                parse_file_as_commonjs(&fm, syntax, target, comments, &mut errors)\n                    .map(Program::Script)\n            }\n            IsModule::Unknown => parse_file_as_program(&fm, syntax, target, comments, &mut errors),\n        };\n\n        for e in errors {\n            e.into_diagnostic(handler).emit();\n            error = true;\n        }\n\n        let program = program_result.map_err(|e| {\n            e.into_diagnostic(handler).emit();\n            Error::msg(\"Syntax Error\")\n        })?;\n\n        if error {\n            return Err(anyhow::anyhow!(\"Syntax Error\"));\n        }\n\n        Ok(program)\n    })();\n\n    if env::var(\"SWC_DEBUG\").unwrap_or_default() == \"1\" {\n        res = res.with_context(|| format!(\"Parser config: {syntax:?}\"));\n    }\n\n    res\n}\n\npub struct PrintArgs<'a> {\n    pub source_root: Option<&'a str>,\n    pub source_file_name: Option<&'a str>,\n    pub output_path: Option<PathBuf>,\n    pub inline_sources_content: bool,\n    pub source_map: SourceMapsConfig,","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/swc-project/swc/blob/d7d74346665e36e692aa07e13d4ee3ee692ee35c/crates/swc_compiler_base/src/lib.rs#L95-L131","documentation":"Generic terminal error from swc_compiler_base's parse machinery: the source failed to parse, or recoverable parser errors were emitted, so the parsed program cannot be trusted and is not returned. The real information is in the diagnostics emitted to the handler just before this error is raised; the message itself is only a marker. If SWC_DEBUG=1 is set, the error is additionally annotated with the parser config used.","triggerScenarios":"Calling parse/transform APIs in swc_compiler_base with source text that is invalid under the configured Syntax (e.g. TypeScript decorators or JSX while Syntax is set to plain Es), actual syntax errors in input, or a parser config/es version mismatch with the file contents.","commonSituations":"Wrong jsc.parser.syntax in .swcrc (tsx code parsed as es), decorator proposals not enabled, using swc to parse non-JS files (JSON/HTML fed as source), or input truncated mid-token.","solutions":["Read the diagnostics printed above the error - they carry file, line, and column of the actual syntax problem; fix the input at that location","Align jsc.parser.syntax with the file type (typescript + tsx flags for .tsx, decorators flag for decorators)","Set SWC_DEBUG=1 to get 'Parser config: ...' appended and confirm the syntax actually in use","If input may be non-JS, gate it before calling swc (extension checks) so it never reaches the parser"],"exampleFix":"// .swcrc before (tsx file parsed with plain es syntax)\n\"jsc\": { \"parser\": { \"syntax\": \"ecmascript\" }, ... }\n\n// .swcrc after\n\"jsc\": { \"parser\": { \"syntax\": \"typescript\", \"tsx\": true } }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Capture the emitted diagnostics alongside the error\nuse swc_common::errors::{Handler, HandlerFlags};\nlet handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(source_map.clone()));\nlet result = swc_compiler_base::parse_js(..., &handler, syntax, target, is_module, comments);\nif let Err(e) = &result {\n    // diagnostics were already emitted to `handler`; fail with both\n    return Err(anyhow::anyhow!(\"parse failed: {e}\"));\n}","preventionTips":["Drive jsc.parser.syntax from the file extension (.ts/.tsx -> typescript)","Pre-validate third-party input with a cheap lint pass before handing it to swc","Keep SWC_DEBUG=1 enabled in dev builds to get the parser config in the error chain"],"tags":["swc","parser","syntax-error","config"],"backgroundTag":null,"analyzedSha":"d7d74346665e36e692aa07e13d4ee3ee692ee35c","analyzedAt":"2026-08-16T12:41:13.160Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}