{"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/d7d74346665e36e692aa07e13d4ee3ee692ee35c/bindings/binding_es_ast_viewer/src/lib.rs#L79-L115","documentation":"Thrown by the wasm-bindgen `parse` entry point of the AST viewer binding (bindings/binding_es_ast_viewer). The SWC parser (parse_module / parse_commonjs / parse_program, chosen from the file_name extension) returned an Err, so no Program could be built. The real parser diagnostic is first emitted into the swc error handler, and this anyhow message is the wrapper that reaches JS as a pretty-printed string. Syntax (TS vs ES, JSX, d.ts, module/goal) is inferred purely from the extension of `file_name` (default \"main.mtsx\"), so a mismatch between content and extension also lands here.","triggerScenarios":"Calling parse() on syntactically invalid JS/TS; passing TSX or JSX content while file_name's extension is not one of jsx/tsx/mjsx/cjsx/mtsx/ctsx (so jsx: false disables JSX parsing); passing TypeScript-only syntax with a .js/.mjs/.cjs file_name (Syntax::Es is selected); passing a .cjs file where parse_commonjs is used on ESM content, or a .mjs file on CJS content.","commonSituations":"Using the default file name (main.mtsx) for plain-JS-with-decorators or proposals the TS parser rejects; typo'd extensions like 'tss' or 'jsx.'; exploring draft/partial files or non-source text pasted into an AST viewer UI; older @swc/wasm-ast-viewer packages with a stricter TS parser.","solutions":["Read the emitted diagnostic inside the returned error string - it contains file, line, and column of the real syntax error - and fix that location in the input","Pass a second file_name argument whose extension matches the content, e.g. parse(code, 'input.tsx') for TSX or parse(code, 'input.js') for plain ES","If the content is TypeScript, never name it .js/.mjs/.cjs; if it is JSX-free TS, a .ts name avoids JSX-only parsing quirks","Verify the snippet elsewhere first (e.g. tsc --noEmit or swc CLI) before feeding it to the viewer","Upgrade @swc/wasm-ast-viewer if the input is valid but the parser version is stale"],"exampleFix":"// before: TSX content, default file name 'main.mtsx' actually works for tsx,\n// but plain JS with class properties fails under the TS grammar\nconst out = parse('class A { #x = 1; }');\n\n// after: name it as JavaScript so Syntax::Es (jsx off) is selected\nconst out = parse('class A { #x = 1; }', 'input.mjs');","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// wasm-bindgen Result<_, String> surfaces as a thrown string in JS\ntry {\n  const [ast, tokens] = parse(code, fileName);\n} catch (e) {\n  // e is a pretty-printed string containing the real parser diagnostic\n  console.error(String(e));\n  throw new Error('AST viewer could not parse input: ' + e);\n}","preventionTips":["Always pass an explicit file_name whose extension matches the content's language (tsx/ts/mjs/cjs)","Lint or type-check files before feeding them to the viewer","Treat the returned string as the source of truth: it embeds the emitted diagnostics with line/column"],"tags":["wasm","parser","syntax-error","ast-viewer","typescript","file-extension"],"backgroundTag":null,"analyzedSha":"d7d74346665e36e692aa07e13d4ee3ee692ee35c","analyzedAt":"2026-08-16T12:41:13.160Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}