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-cli): allow '==' to compare nullable types #16731

Merged
merged 1 commit into from May 16, 2017
Merged
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
28 changes: 26 additions & 2 deletions packages/compiler-cli/src/diagnostics/expression_type.ts
Expand Up @@ -51,8 +51,29 @@ export class AstType implements AstVisitor {
return kind;
}

const leftType = this.getType(ast.left);
const rightType = this.getType(ast.right);
const getType = (ast: AST, operation: string): Symbol => {
const type = this.getType(ast);
if (type.nullable) {
switch (operation) {
case '&&':
case '||':
case '==':
case '!=':
case '===':
case '!==':
// Nullable allowed.
break;
default:
this.reportError(`The expression might be null`, ast);
break;
}
return this.query.getNonNullableType(type);
}
return type;
};

const leftType = getType(ast.left, ast.operation);
const rightType = getType(ast.right, ast.operation);
const leftRawKind = this.query.getTypeKind(leftType);
const rightRawKind = this.query.getTypeKind(rightType);
const leftKind = normalize(leftRawKind, rightRawKind);
Expand Down Expand Up @@ -166,6 +187,9 @@ export class AstType implements AstVisitor {

visitConditional(ast: Conditional) {
// The type of a conditional is the union of the true and false conditions.
if (this.diagnostics) {
visitAstChildren(ast, this);
}
return this.query.getTypeUnion(this.getType(ast.trueExp), this.getType(ast.falseExp));
}

Expand Down
Expand Up @@ -175,7 +175,16 @@ describe('expression diagnostics', () => {
it('should reject an uncalled event handler',
() => reject(
'<div (click)="click">{{person.name.first}}</div>', 'Unexpected callable expression'));

describe('with comparisions between nullable and non-nullable', () => {
it('should accept ==', () => accept(`<div>{{e == 1 ? 'a' : 'b'}}</div>`));
it('should accept ===', () => accept(`<div>{{e === 1 ? 'a' : 'b'}}</div>`));
it('should accept !=', () => accept(`<div>{{e != 1 ? 'a' : 'b'}}</div>`));
it('should accept !==', () => accept(`<div>{{e !== 1 ? 'a' : 'b'}}</div>`));
it('should accept &&', () => accept(`<div>{{e && 1 ? 'a' : 'b'}}</div>`));
it('should accept ||', () => accept(`<div>{{e || 1 ? 'a' : 'b'}}</div>`));
it('should reject >',
() => reject(`<div>{{e > 1 ? 'a' : 'b'}}</div>`, 'The expression might be null'));
});
});

const FILES: Directory = {
Expand Down Expand Up @@ -216,6 +225,7 @@ const FILES: Directory = {
promised_people: Promise<Person[]>;
private private_person: Person;
private private_people: Person[];
e?: number;

getPerson(): Person { return this.person; }
click() {}
Expand Down