{"record":{"id":"b0dc8888180314e7","repo":"swc-project/swc","slug":"failed-to-parse-the-input","errorCode":null,"errorMessage":"Failed to parse the input","messagePattern":"Failed to parse the input","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"bindings/binding_es_ast_viewer/src/lib.rs","lineNumber":97,"sourceCode":"        parser.parse_commonjs().map(Program::Script)\n    } else {\n        parser.parse_program()\n    };\n\n    let mut ast = try_with_handler(\n        cm,\n        HandlerOpts {\n            color: ColorConfig::Never,\n            ..Default::default()\n        },\n        |handler| {\n            for err in parser.take_errors() {\n                err.into_diagnostic(handler).emit();\n            }\n\n            program.map_err(|err| {\n                err.into_diagnostic(handler).emit();\n                anyhow::anyhow!(\"Failed to parse the input\")\n            })\n        },\n    )\n    .map_err(|err| err.to_pretty_string())?;\n\n    let tokens = parser.input_mut().iter_mut().take();\n\n    GLOBALS.set(&Globals::default(), || {\n        let unresolved_mark = Mark::new();\n        let top_level_mark = Mark::new();\n        ast.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, is_ts));\n\n        Ok(vec![format!(\"{ast:#?}\"), format!(\"{tokens:#?}\")])\n    })\n}\n\n#[wasm_bindgen]\npub fn version() -> String {","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/bindings/binding_es_ast_viewer/src/lib.rs#L79-L115","documentation":"The ast-viewer wasm binding (bindings/binding_es_ast_viewer) parses user-submitted code with swc's parser (parse_program / parse_module / parse_commonjs depending on the is_esm/is_cjs flags). When the parser returns an Err, the handler first pretty-prints the real diagnostic (with file/line/column) and then this generic anyhow 'Failed to parse the input' is attached so the JS side receives a rejected promise. The useful information is the emitted diagnostic above this message, not the message itself.","triggerScenarios":"Calling the ast-viewer's parse function (or the ast-viewer website) with code that fails to parse under the chosen syntax flags: TypeScript syntax fed with is_ts=false, JSX fed with is_jsx=false, ESM syntax with is_cjs=true, or genuinely malformed code.","commonSituations":"Pasting TS/TSX into the viewer without the TS/JSX toggles enabled; a `target`/syntax flag combination that forbids decorators or early TS features; truncated code snippets cut off mid-token.","solutions":["Read the pretty-printed diagnostic that accompanies the error — it contains the exact line/column and reason; fix the code there","Set the correct syntax flags for the input: enable is_ts for TypeScript, is_jsx for JSX, and pick esm vs cjs to match the module goal","If the snippet is TS-only syntax, convert or strip type annotations before parsing as plain JS"],"exampleFix":"// before: TS code parsed as plain ESM\nparse(code /* `const x: number = 1` */, { isTs: false, isEsm: true });\n\n// after\nparse(code, { isTs: true, isJsx: true, isEsm: true });","handlingStrategy":"validation","validationCode":"// Choose flags from the file before calling the viewer's parse\nfunction flagsFor(file, code) {\n  return {\n    isTs: /\\.(ts|mts|cts|tsx)$/.test(file),\n    isJsx: /\\.tsx$/.test(file) || /<[A-Za-z][^>]*>/.test(code),\n    isEsm: !/\\.cjs$/.test(file) && /^\\s*(import|export)\\s/m.test(code),\n    isCjs: /\\.cjs$/.test(file),\n  };\n}","typeGuard":null,"tryCatchPattern":"try {\n  parse(code, flags);\n} catch (e) {\n  // the pretty diagnostic above 'Failed to parse the input' has line/column; surface both\n  showDiagnostic(e.message);\n}","preventionTips":["Derive is_ts/is_jsx/esm/cjs flags from the file extension rather than defaults","Feed complete files, not truncated snippets, to the parser","Run tsc --noEmit first when parsing TypeScript to catch syntax errors with better messages"],"tags":["parse-error","ast-viewer","wasm-binding","syntax-flags","typescript"],"backgroundTag":"syntax-parse-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"}