Skip to content

always check statements in case clause #5639

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

Merged
merged 1 commit into from
Nov 12, 2015
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12886,13 +12886,13 @@ namespace ts {
// In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from the type of the 'switch' expression.
const caseType = checkExpression(caseClause.expression);

// Permit 'number[] | "foo"' to be asserted to 'string'.
if (expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) {
return;
}
const expressionTypeIsAssignableToCaseType =
// Permit 'number[] | "foo"' to be asserted to 'string'.
(expressionTypeIsStringLike && someConstituentTypeHasKind(caseType, TypeFlags.StringLike)) ||
isTypeAssignableTo(expressionType, caseType);

if (!isTypeAssignableTo(expressionType, caseType)) {
// check 'expressionType isAssignableTo caseType' failed, try the reversed check and report errors if it fails
if (!expressionTypeIsAssignableToCaseType) {
// 'expressionType is not assignable to caseType', try the reversed check and report errors if it fails
checkTypeAssignableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [checkSwitchStatementIfCaseTypeIsString.ts]
declare function use(a: any): void;

class A {
doIt(x: Array<string>): void {
x.forEach((v) => {
switch(v) {
case "test": use(this);
}
});
}
}

//// [checkSwitchStatementIfCaseTypeIsString.js]
var A = (function () {
function A() {
}
A.prototype.doIt = function (x) {
var _this = this;
x.forEach(function (v) {
switch (v) {
case "test": use(_this);
}
});
};
return A;
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/checkSwitchStatementIfCaseTypeIsString.ts ===
declare function use(a: any): void;
>use : Symbol(use, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 0, 0))
>a : Symbol(a, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 0, 21))

class A {
>A : Symbol(A, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 0, 35))

doIt(x: Array<string>): void {
>doIt : Symbol(doIt, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 2, 9))
>x : Symbol(x, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 3, 9))
>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

x.forEach((v) => {
>x.forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 3, 9))
>forEach : Symbol(Array.forEach, Decl(lib.d.ts, --, --))
>v : Symbol(v, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 4, 19))

switch(v) {
>v : Symbol(v, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 4, 19))

case "test": use(this);
>use : Symbol(use, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 0, 0))
>this : Symbol(A, Decl(checkSwitchStatementIfCaseTypeIsString.ts, 0, 35))
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/checkSwitchStatementIfCaseTypeIsString.ts ===
declare function use(a: any): void;
>use : (a: any) => void
>a : any

class A {
>A : A

doIt(x: Array<string>): void {
>doIt : (x: string[]) => void
>x : string[]
>Array : T[]

x.forEach((v) => {
>x.forEach((v) => { switch(v) { case "test": use(this); } }) : void
>x.forEach : (callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void
>x : string[]
>forEach : (callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void
>(v) => { switch(v) { case "test": use(this); } } : (v: string) => void
>v : string

switch(v) {
>v : string

case "test": use(this);
>"test" : string
>use(this) : void
>use : (a: any) => void
>this : this
}
});
}
}
11 changes: 11 additions & 0 deletions tests/cases/compiler/checkSwitchStatementIfCaseTypeIsString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare function use(a: any): void;

class A {
doIt(x: Array<string>): void {
x.forEach((v) => {
switch(v) {
case "test": use(this);
}
});
}
}