()
| 670 | } |
| 671 | |
| 672 | private isStartOfRegex(): boolean { |
| 673 | if (this.tokens.length === 0) { |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | const prevToken = this.tokens[this.tokens.length - 1]; |
| 678 | |
| 679 | // If a slash is preceded by a `!` operator, we need to distinguish whether it's a |
| 680 | // negation or a non-null assertion. Regexes can only be precded by negations. |
| 681 | if (prevToken.isOperator('!')) { |
| 682 | const beforePrevToken = this.tokens.length > 1 ? this.tokens[this.tokens.length - 2] : null; |
| 683 | const isNegation = |
| 684 | beforePrevToken === null || |
| 685 | (beforePrevToken.type !== TokenType.Identifier && |
| 686 | !beforePrevToken.isCharacter(chars.$RPAREN) && |
| 687 | !beforePrevToken.isCharacter(chars.$RBRACKET)); |
| 688 | |
| 689 | return isNegation; |
| 690 | } |
| 691 | |
| 692 | // Only consider the slash a regex if it's preceded either by: |
| 693 | // - Any operator, aside from `!` which is special-cased above. |
| 694 | // - Opening paren (e.g. `(/a/)`). |
| 695 | // - Opening bracket (e.g. `[/a/]`). |
| 696 | // - A comma (e.g. `[1, /a/]`). |
| 697 | // - A colon (e.g. `{foo: /a/}`). |
| 698 | return ( |
| 699 | prevToken.type === TokenType.Operator || |
| 700 | prevToken.isCharacter(chars.$LPAREN) || |
| 701 | prevToken.isCharacter(chars.$LBRACKET) || |
| 702 | prevToken.isCharacter(chars.$COMMA) || |
| 703 | prevToken.isCharacter(chars.$COLON) |
| 704 | ); |
| 705 | } |
| 706 | |
| 707 | private scanRegex(tokenStart: number): Token { |
| 708 | this.advance(); |
no test coverage detected