Skip to content

Commit a1288f8

Browse files
crisbetothePunderWoman
authored andcommitted
refactor(compiler): add output AST node for regular expressions (angular#63857)
Adds the `RegularExpressionLiteral` node to the output AST. PR Close angular#63857
1 parent 328a2bf commit a1288f8

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

packages/compiler/src/output/abstract_emitter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex
353353
return null;
354354
}
355355

356+
visitRegularExpressionLiteral(ast: o.RegularExpressionLiteral, ctx: EmitterVisitorContext): any {
357+
ctx.print(ast, `/${ast.body}/${ast.flags || ''}`);
358+
return null;
359+
}
360+
356361
visitLocalizedString(ast: o.LocalizedString, ctx: EmitterVisitorContext): any {
357362
const head = ast.serializeI18nHead();
358363
ctx.print(ast, '$localize `' + head.raw);

packages/compiler/src/output/output_ast.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,32 @@ export class InstantiateExpr extends Expression {
538538
}
539539
}
540540

541+
export class RegularExpressionLiteral extends Expression {
542+
constructor(
543+
public body: string,
544+
public flags: string | null,
545+
sourceSpan?: ParseSourceSpan | null,
546+
) {
547+
super(null, sourceSpan);
548+
}
549+
550+
override isEquivalent(e: Expression): boolean {
551+
return e instanceof RegularExpressionLiteral && this.body === e.body && this.flags === e.flags;
552+
}
553+
554+
override isConstant() {
555+
return true;
556+
}
557+
558+
override visitExpression(visitor: ExpressionVisitor, context: any): any {
559+
return visitor.visitRegularExpressionLiteral(this, context);
560+
}
561+
562+
override clone(): RegularExpressionLiteral {
563+
return new RegularExpressionLiteral(this.body, this.flags, this.sourceSpan);
564+
}
565+
}
566+
541567
export class LiteralExpr extends Expression {
542568
constructor(
543569
public value: number | string | boolean | null | undefined,
@@ -1400,6 +1426,7 @@ export interface ExpressionVisitor {
14001426
visitVoidExpr(ast: VoidExpr, context: any): any;
14011427
visitArrowFunctionExpr(ast: ArrowFunctionExpr, context: any): any;
14021428
visitParenthesizedExpr(ast: ParenthesizedExpr, context: any): any;
1429+
visitRegularExpressionLiteral(ast: RegularExpressionLiteral, context: any): any;
14031430
}
14041431

14051432
export const NULL_EXPR = new LiteralExpr(null, null, null);
@@ -1627,6 +1654,9 @@ export class RecursiveAstVisitor implements StatementVisitor, ExpressionVisitor
16271654
visitLiteralExpr(ast: LiteralExpr, context: any): any {
16281655
return this.visitExpression(ast, context);
16291656
}
1657+
visitRegularExpressionLiteral(ast: RegularExpressionLiteral, context: any): any {
1658+
return this.visitExpression(ast, context);
1659+
}
16301660
visitLocalizedString(ast: LocalizedString, context: any): any {
16311661
return this.visitExpression(ast, context);
16321662
}

0 commit comments

Comments
 (0)