{"record":{"id":"63e512c33dacb5ea","repo":"swc-project/swc","slug":"unexpectedtokeninstartphase","errorCode":"UnexpectedTokenInStartPhase","errorMessage":"Unexpected token in start phase","messagePattern":"Unexpected token in start phase","errorType":"validation","errorClass":"swc_xml_parser::error::Error","httpStatus":null,"severity":"error","filePath":"crates/swc_xml_parser/src/parser/mod.rs","lineNumber":272,"sourceCode":"\n                    self.append_node(self.document.as_ref().unwrap(), element.clone());\n                    self.open_elements_stack.items.push(element);\n                    self.phase = Phase::MainPhase;\n                }\n                Token::EmptyTag { .. } => {\n                    let element = self.create_element_for_token(token_and_info.clone());\n\n                    self.append_node(self.document.as_ref().unwrap(), element);\n                    self.phase = Phase::EndPhase;\n                }\n                Token::Comment { .. } => {\n                    self.append_comment_to_doc(token_and_info)?;\n                }\n                Token::ProcessingInstruction { .. } => {\n                    self.append_processing_instruction_to_doc(token_and_info)?;\n                }\n                Token::Cdata { .. } => {\n                    self.errors.push(Error::new(\n                        token_and_info.span,\n                        ErrorKind::UnexpectedTokenInStartPhase,\n                    ));\n\n                    self.append_cdata_to_doc(token_and_info)?;\n                }\n                Token::Character { value, .. } => {\n                    if !is_whitespace(*value) {\n                        self.errors.push(Error::new(\n                            token_and_info.span,\n                            ErrorKind::UnexpectedCharacter,\n                        ));\n                    }\n                }\n                Token::Eof => {\n                    self.errors.push(Error::new(\n                        token_and_info.span,\n                        ErrorKind::UnexpectedEofInStartPhase,","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_xml_parser/src/parser/mod.rs#L254-L290","documentation":"swc_xml_parser processes the document in a start phase before the root element where only the doctype, comments, processing instructions and whitespace are legal. When a CDATA token arrives in that phase, `UnexpectedTokenInStartPhase` is recorded (crates/swc_xml_parser/src/parser/mod.rs:272) — CDATA sections are only allowed inside element content. The parser still appends the CDATA node to the document and moves on, so the diagnostic is recoverable.","triggerScenarios":"Parsing a document whose prolog contains a CDATA section, e.g. `<![CDATA[raw]]><root/>` — the CDATA token hits the start-phase match before the root element is created.","commonSituations":"Concatenating a CDATA payload ahead of the root element in generated files; fragments pasted above `<root>`; malformed exports that put escaped/legacy content before the document element.","solutions":["Move the CDATA section inside the root element","If the file is a fragment, wrap it in a root element before parsing","Convert leading metadata text to a comment or processing instruction","Pre-validate with xmllint so prolog errors surface before your pipeline"],"exampleFix":"<!-- before -->\n<![CDATA[raw]]><root><a/></root>\n<!-- after -->\n<root><![CDATA[raw]]><a/></root>","handlingStrategy":"validation","validationCode":"// Reject CDATA or stray tags before the root element\nfunction assertCleanProlog(xml: string): void {\n  const rootIdx = xml.search(/<[a-zA-Z_:]/);\n  const prolog = rootIdx === -1 ? xml : xml.slice(0, rootIdx);\n  const cleaned = prolog.replace(/<\\?[\\s\\S]*?\\?>|<!--([\\s\\S]*?)-->|<!DOCTYPE[^>]*>/g, '');\n  if (/\\S|<!\\[CDATA\\[/.test(cleaned)) throw new Error('illegal content before root element');\n}","typeGuard":null,"tryCatchPattern":"for err in parser.take_errors() {\n    if matches!(err.kind, ErrorKind::UnexpectedTokenInStartPhase) {\n        // CDATA was still appended to the document; strip it downstream if unwanted\n    }\n}","preventionTips":["Keep CDATA sections strictly inside the root element","Wrap fragments in a root element before parsing them as documents","Pre-validate with xmllint so prolog violations fail early in CI"],"tags":["xml","cdata","prolog","well-formedness","parser"],"backgroundTag":"xml-wellformedness-error","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}