Skip to content

Commit 5cb8450

Browse files
Merge pull request #605 from microsoft/main
Create a new pull request by comparing changes across two branches
2 parents e894bbd + 717d05c commit 5cb8450

File tree

5 files changed

+113
-9
lines changed

5 files changed

+113
-9
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,9 +1318,11 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
13181318
case SyntaxKind.ExclamationEqualsToken:
13191319
case SyntaxKind.EqualsEqualsEqualsToken:
13201320
case SyntaxKind.ExclamationEqualsEqualsToken:
1321-
return isNarrowableOperand(expr.left) || isNarrowableOperand(expr.right) ||
1322-
isNarrowingTypeofOperands(expr.right, expr.left) || isNarrowingTypeofOperands(expr.left, expr.right) ||
1323-
(isBooleanLiteral(expr.right) && isNarrowingExpression(expr.left) || isBooleanLiteral(expr.left) && isNarrowingExpression(expr.right));
1321+
const left = skipParentheses(expr.left);
1322+
const right = skipParentheses(expr.right);
1323+
return isNarrowableOperand(left) || isNarrowableOperand(right) ||
1324+
isNarrowingTypeofOperands(right, left) || isNarrowingTypeofOperands(left, right) ||
1325+
(isBooleanLiteral(right) && isNarrowingExpression(left) || isBooleanLiteral(left) && isNarrowingExpression(right));
13241326
case SyntaxKind.InstanceOfKeyword:
13251327
return isNarrowableOperand(expr.left);
13261328
case SyntaxKind.InKeyword:

src/server/session.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3058,20 +3058,21 @@ export class Session<TMessage = string> implements EventSender {
30583058
codeActions = project.getLanguageService().getCodeFixesAtPosition(file, startPosition, endPosition, args.errorCodes, this.getFormatOptions(file), this.getPreferences(file));
30593059
}
30603060
catch (e) {
3061+
const error = e instanceof Error ? e : new Error(e);
3062+
30613063
const ls = project.getLanguageService();
30623064
const existingDiagCodes = [
30633065
...ls.getSyntacticDiagnostics(file),
30643066
...ls.getSemanticDiagnostics(file),
30653067
...ls.getSuggestionDiagnostics(file),
3066-
].map(d =>
3067-
decodedTextSpanIntersectsWith(startPosition, endPosition - startPosition, d.start!, d.length!)
3068-
&& d.code
3069-
);
3068+
]
3069+
.filter(d => decodedTextSpanIntersectsWith(startPosition, endPosition - startPosition, d.start!, d.length!))
3070+
.map(d => d.code);
30703071
const badCode = args.errorCodes.find(c => !existingDiagCodes.includes(c));
30713072
if (badCode !== undefined) {
3072-
e.message = `BADCLIENT: Bad error code, ${badCode} not found in range ${startPosition}..${endPosition} (found: ${existingDiagCodes.join(", ")}); could have caused this error:\n${e.message}`;
3073+
error.message += `\nAdditional information: BADCLIENT: Bad error code, ${badCode} not found in range ${startPosition}..${endPosition} (found: ${existingDiagCodes.join(", ")})`;
30733074
}
3074-
throw e;
3075+
throw error;
30753076
}
30763077
return simplifiedResult ? codeActions.map(codeAction => this.mapCodeFixAction(codeAction)) : codeActions;
30773078
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/narrowingTypeofParenthesized1.ts] ////
2+
3+
=== narrowingTypeofParenthesized1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/42203
5+
6+
declare const foo: string;
7+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
8+
9+
if ((typeof foo) === "string") {
10+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
11+
12+
foo;
13+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
14+
15+
} else {
16+
foo;
17+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
18+
}
19+
20+
if (typeof foo === ("string")) {
21+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
22+
23+
foo;
24+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
25+
26+
} else {
27+
foo;
28+
>foo : Symbol(foo, Decl(narrowingTypeofParenthesized1.ts, 2, 13))
29+
}
30+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/compiler/narrowingTypeofParenthesized1.ts] ////
2+
3+
=== narrowingTypeofParenthesized1.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/42203
5+
6+
declare const foo: string;
7+
>foo : string
8+
> : ^^^^^^
9+
10+
if ((typeof foo) === "string") {
11+
>(typeof foo) === "string" : boolean
12+
> : ^^^^^^^
13+
>(typeof foo) : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
14+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
>typeof foo : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
16+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
>foo : string
18+
> : ^^^^^^
19+
>"string" : "string"
20+
> : ^^^^^^^^
21+
22+
foo;
23+
>foo : string
24+
> : ^^^^^^
25+
26+
} else {
27+
foo;
28+
>foo : never
29+
> : ^^^^^
30+
}
31+
32+
if (typeof foo === ("string")) {
33+
>typeof foo === ("string") : boolean
34+
> : ^^^^^^^
35+
>typeof foo : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
36+
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37+
>foo : string
38+
> : ^^^^^^
39+
>("string") : "string"
40+
> : ^^^^^^^^
41+
>"string" : "string"
42+
> : ^^^^^^^^
43+
44+
foo;
45+
>foo : string
46+
> : ^^^^^^
47+
48+
} else {
49+
foo;
50+
>foo : never
51+
> : ^^^^^
52+
}
53+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/42203
5+
6+
declare const foo: string;
7+
8+
if ((typeof foo) === "string") {
9+
foo;
10+
} else {
11+
foo;
12+
}
13+
14+
if (typeof foo === ("string")) {
15+
foo;
16+
} else {
17+
foo;
18+
}

0 commit comments

Comments
 (0)