swc-project/swc · error · swc_xml_parser::error::Error

MissingWhitespaceBeforeQuestionInProcessingInstruction

MissingWhitespaceBeforeQuestionInProcessingInstruction

Error message

Missing whitespace before '?'

What it means

XML processing instructions require whitespace between the target and the data (`<?target data?>`). The lexer's PiTargetQuestion state handles a `?` read while scanning the target: if the next character is not `>`, the target runs directly into `?`-prefixed data with no separating whitespace, and `MissingWhitespaceBeforeQuestionInProcessingInstruction` is reported (crates/swc_xml_parser/src/lexer/mod.rs:1180). The `?` is then reconsumed as PI data so lexing continues.

Source

Thrown at crates/swc_xml_parser/src/lexer/mod.rs:1180

                    }
                    // Anything else
                    // Append the current input character to the processing instruction target and
                    // stay in the current state.
                    Some(c) => {
                        self.validate_input_stream_character(c);
                        self.set_processing_instruction_token(Some(c), None);
                    }
                }
            }
            State::PiTargetQuestion => {
                // Consume the next input character:
                match self.consume_next_char() {
                    // U+003E GREATER-THAN SIGN (>)
                    Some('>') => {
                        self.reconsume_in_state(State::PiEnd);
                    }
                    _ => {
                        self.errors.push(Error::new(
                            Span::new(self.cur_pos - BytePos(1), self.input.cur_pos() - BytePos(1)),
                            ErrorKind::MissingWhitespaceBeforeQuestionInProcessingInstruction,
                        ));
                        self.set_processing_instruction_token(None, Some('?'));
                        self.reconsume_in_state(State::PiData);
                    }
                }
            }
            State::PiTargetAfter => {
                // Consume the next input character:
                match self.consume_next_char() {
                    // U+0009 CHARACTER TABULATION (Tab)
                    // U+000A LINE FEED (LF)
                    // U+0020 SPACE (Space)
                    // Stay in the current state.
                    Some(c) if is_whitespace(c) => {
                        self.skip_next_lf(c);
                    }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Insert whitespace between target and data: `<?target data?>`
  2. If there is no data, terminate cleanly as `<?target?>`
  3. Escape or drop stray `?` characters inside the target portion
  4. Emit PIs with a serializer/template helper that always inserts the required space

Example fix

<!-- before -->
<?target?data?>
<!-- after -->
<?target data?>
Defensive patterns

Strategy: validation

Validate before calling

// Validate processing instructions before parsing
function assertValidPi(pi: string): void {
  // <?target data?> — require whitespace or ?> right after the target
  const m = pi.match(/^<\?([A-Za-z_:][-\w.:]*)(\s[\s\S]*?)?\?>$/);
  if (!m) throw new Error(`malformed processing instruction: ${pi}`);
  if (m[2] !== undefined && !/^\s/.test(m[2])) throw new Error('missing whitespace between PI target and data');
}

Try / catch

for err in parser.take_errors() {
    if matches!(err.kind, ErrorKind::MissingWhitespaceBeforeQuestionInProcessingInstruction) {
        // lexer recovered by treating ? as PI data; fix the source PI
    }
}

Prevention

When it happens

Trigger: Parsing PIs shaped like `<?target?data?>` or `<?php?echo?>` — any PI where a `?` appears at/after the target without whitespace and is not immediately followed by `>` terminating the PI.

Common situations: PHP-style processing instructions pasted into XML/XHTML templates; PIs generated by string concatenation without inserting the mandatory space; stylesheet targets that accidentally contain `?` (e.g. `<?xml-stylesheet?href=...?>`).

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/94a4aa5ef037efef. Report an issue: GitHub.