@@ -20,14 +20,17 @@ const FOR_LOOP_EXPRESSION_PATTERN = /^\s*([0-9A-Za-z_$]*)\s+of\s+([\S\s]*)/;
2020const FOR_LOOP_TRACK_PATTERN = / ^ t r a c k \s + ( [ \S \s ] * ) / ;
2121
2222/** Pattern for the `as` expression in a conditional block. */
23- const CONDITIONAL_ALIAS_PATTERN = / ^ ( a s \s ) + ( .* ) / ;
23+ const CONDITIONAL_ALIAS_PATTERN = / ^ ( a s \s + ) ( .* ) / ;
2424
2525/** Pattern used to identify an `else if` block. */
2626const ELSE_IF_PATTERN = / ^ e l s e [ ^ \S \r \n ] + i f / ;
2727
2828/** Pattern used to identify a `let` parameter. */
2929const FOR_LOOP_LET_PATTERN = / ^ l e t \s + ( [ \S \s ] * ) / ;
3030
31+ /** Pattern used to validate a JavaScript identifier. */
32+ const IDENTIFIER_PATTERN = / ^ [ $ A - Z _ ] [ 0 - 9 A - Z _ $ ] * $ / i;
33+
3134/**
3235 * Pattern to group a string into leading whitespace, non whitespace, and trailing whitespace.
3336 * Useful for getting the variable name span when a span can contain leading and trailing space.
@@ -630,9 +633,16 @@ function parseConditionalBlockParameters(
630633 ) ;
631634 } else {
632635 const name = aliasMatch [ 2 ] . trim ( ) ;
633- const variableStart = param . sourceSpan . start . moveBy ( aliasMatch [ 1 ] . length ) ;
634- const variableSpan = new ParseSourceSpan ( variableStart , variableStart . moveBy ( name . length ) ) ;
635- expressionAlias = new t . Variable ( name , name , variableSpan , variableSpan ) ;
636+
637+ if ( IDENTIFIER_PATTERN . test ( name ) ) {
638+ const variableStart = param . sourceSpan . start . moveBy ( aliasMatch [ 1 ] . length ) ;
639+ const variableSpan = new ParseSourceSpan ( variableStart , variableStart . moveBy ( name . length ) ) ;
640+ expressionAlias = new t . Variable ( name , name , variableSpan , variableSpan ) ;
641+ } else {
642+ errors . push (
643+ new ParseError ( param . sourceSpan , '"as" expression must be a valid JavaScript identifier' ) ,
644+ ) ;
645+ }
636646 }
637647 }
638648
0 commit comments