Skip to content

Commit 992769f

Browse files
committed
fixup! feat(compiler): add support for the typeof keyword in template expressions.
1 parent d2f3f00 commit 992769f

File tree

9 files changed

+23
-21
lines changed

9 files changed

+23
-21
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/expression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
LiteralPrimitive,
2626
NonNullAssert,
2727
PrefixNot,
28-
PrefixTypeof,
28+
TypeofExpression,
2929
PropertyRead,
3030
PropertyWrite,
3131
SafeCall,
@@ -276,7 +276,7 @@ class AstTranslator implements AstVisitor {
276276
return node;
277277
}
278278

279-
visitPrefixTypeof(ast: PrefixTypeof): ts.Expression {
279+
visitTypeofExpresion(ast: TypeofExpression): ts.Expression {
280280
const expression = wrapForDiagnostics(this.translate(ast.expression));
281281
const node = ts.factory.createTypeOfExpression(expression);
282282
addParseSpanInfo(node, ast.sourceSpan);
@@ -549,7 +549,7 @@ class VeSafeLhsInferenceBugDetector implements AstVisitor {
549549
visitPrefixNot(ast: PrefixNot): boolean {
550550
return ast.expression.visit(this);
551551
}
552-
visitPrefixTypeof(ast: PrefixNot): boolean {
552+
visitTypeofExpresion(ast: PrefixNot): boolean {
553553
return ast.expression.visit(this);
554554
}
555555
visitNonNullAssert(ast: PrefixNot): boolean {

packages/compiler/src/expression_parser/ast.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ export class PrefixNot extends AST {
373373
}
374374
}
375375

376-
export class PrefixTypeof extends AST {
376+
export class TypeofExpression extends AST {
377377
constructor(
378378
span: ParseSpan,
379379
sourceSpan: AbsoluteSourceSpan,
@@ -382,7 +382,7 @@ export class PrefixTypeof extends AST {
382382
super(span, sourceSpan);
383383
}
384384
override visit(visitor: AstVisitor, context: any = null): any {
385-
return visitor.visitPrefixTypeof(this, context);
385+
return visitor.visitTypeofExpresion(this, context);
386386
}
387387
}
388388

@@ -547,7 +547,7 @@ export interface AstVisitor {
547547
visitLiteralPrimitive(ast: LiteralPrimitive, context: any): any;
548548
visitPipe(ast: BindingPipe, context: any): any;
549549
visitPrefixNot(ast: PrefixNot, context: any): any;
550-
visitPrefixTypeof(ast: PrefixTypeof, context: any): any;
550+
visitTypeofExpresion(ast: TypeofExpression, context: any): any;
551551
visitNonNullAssert(ast: NonNullAssert, context: any): any;
552552
visitPropertyRead(ast: PropertyRead, context: any): any;
553553
visitPropertyWrite(ast: PropertyWrite, context: any): any;
@@ -615,7 +615,7 @@ export class RecursiveAstVisitor implements AstVisitor {
615615
visitPrefixNot(ast: PrefixNot, context: any): any {
616616
this.visit(ast.expression, context);
617617
}
618-
visitPrefixTypeof(ast: PrefixTypeof, context: any) {
618+
visitTypeofExpresion(ast: TypeofExpression, context: any) {
619619
this.visit(ast.expression, context);
620620
}
621621
visitNonNullAssert(ast: NonNullAssert, context: any): any {
@@ -732,8 +732,8 @@ export class AstTransformer implements AstVisitor {
732732
return new PrefixNot(ast.span, ast.sourceSpan, ast.expression.visit(this));
733733
}
734734

735-
visitPrefixTypeof(ast: PrefixNot, context: any): AST {
736-
return new PrefixTypeof(ast.span, ast.sourceSpan, ast.expression.visit(this));
735+
visitTypeofExpresion(ast: PrefixNot, context: any): AST {
736+
return new TypeofExpression(ast.span, ast.sourceSpan, ast.expression.visit(this));
737737
}
738738

739739
visitNonNullAssert(ast: NonNullAssert, context: any): AST {
@@ -912,10 +912,10 @@ export class AstMemoryEfficientTransformer implements AstVisitor {
912912
return ast;
913913
}
914914

915-
visitPrefixTypeof(ast: PrefixTypeof, context: any): AST {
915+
visitTypeofExpresion(ast: TypeofExpression, context: any): AST {
916916
const expression = ast.expression.visit(this);
917917
if (expression !== ast.expression) {
918-
return new PrefixTypeof(ast.span, ast.sourceSpan, expression);
918+
return new TypeofExpression(ast.span, ast.sourceSpan, expression);
919919
}
920920
return ast;
921921
}

packages/compiler/src/expression_parser/lexer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class Token {
112112
}
113113

114114
isKeywordTypeof(): boolean {
115-
return this.type == TokenType.Keyword && this.strValue == 'typeof';
115+
return this.type === TokenType.Keyword && this.strValue === 'typeof';
116116
}
117117

118118
isError(): boolean {

packages/compiler/src/expression_parser/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737
ParserError,
3838
ParseSpan,
3939
PrefixNot,
40-
PrefixTypeof,
40+
TypeofExpression,
4141
PropertyRead,
4242
PropertyWrite,
4343
RecursiveAstVisitor,
@@ -965,7 +965,7 @@ class _ParseAST {
965965
this.advance();
966966
const start = this.inputIndex;
967967
let result = this.parsePrefix();
968-
return new PrefixTypeof(this.span(start), this.sourceSpan(start), result);
968+
return new TypeofExpression(this.span(start), this.sourceSpan(start), result);
969969
}
970970
return this.parseCallChain();
971971
}

packages/compiler/src/template/pipeline/src/ingest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ function convertAst(
11501150
convertAst(ast.expression, job, baseSourceSpan),
11511151
convertSourceSpan(ast.span, baseSourceSpan),
11521152
);
1153-
} else if (ast instanceof e.PrefixTypeof) {
1153+
} else if (ast instanceof e.TypeofExpression) {
11541154
return o.typeofExpr(convertAst(ast.expression, job, baseSourceSpan));
11551155
} else {
11561156
throw new Error(

packages/compiler/test/expression_parser/parser_spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ describe('parser', () => {
100100

101101
it('should parse typeof expression', () => {
102102
checkAction(`typeof {} === "object"`);
103+
checkAction('(!(typeof {} === "number"))', '!typeof {} === "number"');
103104
});
104105

105106
it('should parse grouped expressions', () => {

packages/compiler/test/expression_parser/utils/unparser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
LiteralPrimitive,
2525
NonNullAssert,
2626
PrefixNot,
27+
TypeofExpression,
2728
PropertyRead,
2829
PropertyWrite,
2930
RecursiveAstVisitor,
@@ -192,7 +193,7 @@ class Unparser implements AstVisitor {
192193
this._visit(ast.expression);
193194
}
194195

195-
visitPrefixTypeof(ast: PrefixNot, context: any) {
196+
visitTypeofExpresion(ast: TypeofExpression, context: any) {
196197
this._expression += 'typeof ';
197198
this._visit(ast.expression);
198199
}

packages/compiler/test/expression_parser/utils/validator.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
LiteralPrimitive,
2323
ParseSpan,
2424
PrefixNot,
25-
PrefixTypeof,
25+
TypeofExpression,
2626
PropertyRead,
2727
PropertyWrite,
2828
RecursiveAstVisitor,
@@ -113,8 +113,8 @@ class ASTValidator extends RecursiveAstVisitor {
113113
this.validate(ast, () => super.visitPrefixNot(ast, context));
114114
}
115115

116-
override visitPrefixTypeof(ast: PrefixTypeof, context: any): any {
117-
this.validate(ast, () => super.visitPrefixTypeof(ast, context));
116+
override visitTypeofExpresion(ast: TypeofExpression, context: any): any {
117+
this.validate(ast, () => super.visitTypeofExpresion(ast, context));
118118
}
119119

120120
override visitPropertyRead(ast: PropertyRead, context: any): any {

packages/compiler/test/render3/util/expression.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ class ExpressionSourceHumanizer extends e.RecursiveAstVisitor implements t.Visit
8787
this.recordAst(ast);
8888
super.visitPrefixNot(ast, null);
8989
}
90-
override visitPrefixTypeof(ast: e.PrefixTypeof) {
90+
override visitTypeofExpresion(ast: e.TypeofExpression) {
9191
this.recordAst(ast);
92-
super.visitPrefixTypeof(ast, null);
92+
super.visitTypeofExpresion(ast, null);
9393
}
9494
override visitPropertyRead(ast: e.PropertyRead) {
9595
this.recordAst(ast);

0 commit comments

Comments
 (0)