Skip to content
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

fix(compiler): support event bindings in fullTemplateTypeCheck #20490

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 42 additions & 5 deletions packages/compiler-cli/test/diagnostics/check_types_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import * as ts from 'typescript';

import {TestSupport, expectNoDiagnostics, setup} from '../test_support';

type MockFiles = {
[fileName: string]: string
};

describe('ng type checker', () => {
let errorSpy: jasmine.Spy&((s: string) => void);
let testSupport: TestSupport;

function compileAndCheck(
mockDirs: {[fileName: string]: string}[],
overrideOptions: ng.CompilerOptions = {}): ng.Diagnostics {
mockDirs: MockFiles[], overrideOptions: ng.CompilerOptions = {}): ng.Diagnostics {
testSupport.writeFiles(...mockDirs);
const fileNames: string[] = [];
mockDirs.forEach((dir) => {
Expand All @@ -40,13 +43,12 @@ describe('ng type checker', () => {
testSupport = setup();
});

function accept(
files: {[fileName: string]: string} = {}, overrideOptions: ng.CompilerOptions = {}) {
function accept(files: MockFiles = {}, overrideOptions: ng.CompilerOptions = {}) {
expectNoDiagnostics({}, compileAndCheck([QUICKSTART, files], overrideOptions));
}

function reject(
message: string | RegExp, location: RegExp, files: {[fileName: string]: string},
message: string | RegExp, location: RegExp, files: MockFiles,
overrideOptions: ng.CompilerOptions = {}) {
const diagnostics = compileAndCheck([QUICKSTART, files], overrideOptions);
if (!diagnostics || !diagnostics.length) {
Expand Down Expand Up @@ -79,6 +81,41 @@ describe('ng type checker', () => {
});
});

describe('regressions ', () => {
const a = (files: MockFiles, options: object = {}) => {
accept(files, {fullTemplateTypeCheck: true, ...options});
};

// #19905
it('should accept an event binding', () => {
a({
'src/app.component.ts': '',
'src/lib.ts': '',
'src/app.module.ts': `
import {NgModule, Component, Directive, HostListener} from '@angular/core';

@Component({
selector: 'comp',
template: '<div someDir></div>'
})
export class MainComp {}

@Directive({
selector: '[someDir]'
})
export class SomeDirective {
@HostListener('click', ['$event'])
onClick(event: any) {}
}

@NgModule({
declarations: [MainComp, SomeDirective],
})
export class MainModule {}`
});
});
});

describe('with modified quickstart (fullTemplateTypeCheck: false)', () => {
addTests({fullTemplateTypeCheck: false});
});
Expand Down
17 changes: 15 additions & 2 deletions packages/compiler/src/view_compiler/type_check_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ interface Expression {

const DYNAMIC_VAR_NAME = '_any';

class TypeCheckLocalResolver implements LocalResolver {
getLocal(name: string): o.Expression|null {
if (name === EventHandlerVars.event.name) {
// References to the event should not be type-checked.
// TODO(chuckj): determine a better type for the event.
return o.variable(DYNAMIC_VAR_NAME);
}
return null;
}
}

const defaultResolver = new TypeCheckLocalResolver();

class ViewBuilder implements TemplateAstVisitor, LocalResolver {
private refOutputVars = new Map<string, OutputVarType>();
private variables: VariableAst[] = [];
Expand Down Expand Up @@ -112,7 +125,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {
this.updates.forEach((expression) => {
const {sourceSpan, context, value} = this.preprocessUpdateExpression(expression);
const bindingId = `${bindingCount++}`;
const nameResolver = context === this.component ? this : null;
const nameResolver = context === this.component ? this : defaultResolver;
const {stmts, currValExpr} = convertPropertyBinding(
nameResolver, o.variable(this.getOutputVar(context)), value, bindingId);
stmts.push(new o.ExpressionStatement(currValExpr));
Expand All @@ -122,7 +135,7 @@ class ViewBuilder implements TemplateAstVisitor, LocalResolver {

this.actions.forEach(({sourceSpan, context, value}) => {
const bindingId = `${bindingCount++}`;
const nameResolver = context === this.component ? this : null;
const nameResolver = context === this.component ? this : defaultResolver;
const {stmts} = convertActionBinding(
nameResolver, o.variable(this.getOutputVar(context)), value, bindingId);
viewStmts.push(...stmts.map(
Expand Down