8000 feat(compiler): add support for the `typeof` keyword in template expressions by JeanMeche · Pull Request #58183 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

feat(compiler): add support for the typeof keyword in template expressions #58183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! feat(compiler): add support for the typeof keyword in templa…
…te expressions.
  • Loading branch information
JeanMeche committed Oct 15, 2024
commit 1780abd4ed18b543bac4dab89c52d08364fd7c25
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ describe('type check blocks', () => {
);
});

it('should handle typeof expressions', () => {
expect(tcb('{{typeof a}}')).toContain('typeof (((this).a))');
expect(tcb('{{!(typeof a)}}')).toContain('!(typeof (((this).a)))');
expect(tcb('{{!(typeof a === "object")}}')).toContain(
'!((typeof (((this).a))) === ("object"))',
);
});

it('should handle attribute values for directive inputs', () => {
const TEMPLATE = `<div dir inputA="value"></div>`;
const DIRECTIVES: TestDeclaration[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ MyApp.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "0.0.0-
{{ (1 % 2) + 3 / 4 * 5 }}
{{ +1 }}
{{ typeof {} === 'object' }}
{{ !(typeof {} === 'object') }}
`, isInline: true });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDER", ngImport: i0, type: MyApp, decorators: [{
type: Component,
Expand All @@ -89,6 +90,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "0.0.0-PLACEHOLDE
{{ (1 % 2) + 3 / 4 * 5 }}
{{ +1 }}
{{ typeof {} === 'object' }}
{{ !(typeof {} === 'object') }}
`,
standalone: false
}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Component, NgModule} from '@angular/core';
{{ (1 % 2) + 3 / 4 * 5 }}
{{ +1 }}
{{ typeof {} === 'object' }}
{{ !(typeof {} === 'object') }}
`,
standalone: false
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ template: function MyApp_Template(rf, $ctx$) {
if (rf & 1) {
$i0$.ɵɵtext(0);
} if (rf & 2) {
i0.ɵɵtextInterpolate4(" ", 1 + 2, " ", 1 % 2 + 3 / 4 * 5, " ", +1, " ", typeof i0.ɵɵpureFunction0(4, _c0) === "object","\n");
i0.ɵɵtextInterpolate5(" ", 1 + 2, " ", 1 % 2 + 3 / 4 * 5, " ", +1, " ", typeof i0.ɵɵpureFunction0(5, _c0) === "object", " ", !(typeof i0.ɵɵpureFunction0(6, _c0) === "object"), "\n");
}
}

41 changes: 41 additions & 0 deletions packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,47 @@ runInEachFileSystem(() => {
expect(diags[0].messageText).toContain(`Property 'input' does not exist on type 'TestCmp'.`);
});

it('should error on non valid typeof expressions', () => {
env.write(
'test.ts',
`
import {Component} from '@angular/core';

@Component({
standalone: true,
template: \` {{typeof {} === 'foobar'}} \`,
})
class TestCmp {
}
`,
);

const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].messageText).toContain(`This comparison appears to be unintentional`);
});

it('should error on misused logical not in typeof expressions', () => {
env.write(
'test.ts',
`
import {Component} from '@angular/core';

@Component({
standalone: true,
// should be !(typeof {} === 'object')
template: \` {{!typeof {} === 'object'}} \`,
})
class TestCmp {
}
`,
);

const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].messageText).toContain(`This comparison appears to be unintentional`);
});

describe('strictInputTypes', () => {
beforeEach(() => {
env.write(
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/expression_parser/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ export class AstTransformer implements AstVisitor {
return new PrefixNot(ast.span, ast.sourceSpan, ast.expression.visit(this));
}

visitTypeofExpresion(ast: PrefixNot, context: any): AST {
visitTypeofExpresion(ast: TypeofExpression, context: any): AST {
return new TypeofExpression(ast.span, ast.sourceSpan, ast.expression.visit(this));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/expression_parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ class _ParseAST {
result = this.parsePrefix();
return new PrefixNot(this.span(start), this.sourceSpan(start), result);
}
} else if (this.next.type == TokenType.Keyword && this.next.strValue === 'typeof') {
} else if (this.next.isKeywordTypeof()) {
this.advance();
const start = this.inputIndex;
let result = this.parsePrefix();
Expand Down
24 changes: 24 additions & 0 deletions packages/core/test/acceptance/control_flow_if_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,30 @@ describe('control flow - if', () => {
expect(fixture.nativeElement.textContent).toBe('Something');
});

it('should support a condition with the a typeof expression', () => {
@Component({
standalone: true,
template: `
@if (typeof value === 'string') {
{{value.length}}
} @else {
{{value}}
}
`,
})
class TestComponent {
value: string | number = 'string';
}

const fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
expect(fixture.nativeElement.textContent.trim()).toBe('6');

fixture.componentInstance.value = 42;
fixture.detectChanges();
expect(fixture.nativeElement.textContent.trim()).toBe('42');
});

describe('content projection', () => {
it('should project an @if with a single root node into the root node slot', () => {
@Component({
Expand Down
0