swc-project/swc · error · SyntaxError

Unterminated regexp literal

Error message

Unterminated regexp literal

What it means

Lexer error from read_regexp: while scanning a regular expression literal the lexer hit a line terminator before the closing '/', which is illegal because regexp literals cannot span lines (behavior ported from Babel). The span starts at the opening '/'.

Source

Thrown at crates/swc_ecma_lexer/src/common/lexer/mod.rs:1605

        debug_assert_eq!(self.cur(), Some(b'/'));

        let start = self.cur_pos();

        self.bump(); // bump '/'

        let slice_start = self.cur_pos();

        let (mut escaped, mut in_class) = (false, false);

        while let Some(c) = self.cur() {
            let c = c as char;
            // This is ported from babel.
            // Seems like regexp literal cannot contain linebreak.
            if c.is_line_terminator() {
                let span = self.span(start);

                return Err(crate::error::Error::new(
                    span,
                    SyntaxError::UnterminatedRegExp,
                ));
            }

            if escaped {
                escaped = false;
            } else {
                match c {
                    '[' => in_class = true,
                    ']' if in_class => in_class = false,
                    // Terminates content part of regex literal
                    '/' if !in_class => break,
                    _ => {}
                }

                escaped = c == '\\';
            }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Close the regexp literal with '/' on the same line
  2. Move the pattern onto one line or build the RegExp via new RegExp with a string
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_ecma_lexer/src/common/lexer/mod.rs:1605 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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