Skip to content

Commit 3bd2226

Browse files
committed
Fix misleading error TS2410 from issue microsoft#10601
1 parent 60ab007 commit 3bd2226

21 files changed

+77
-71
lines changed

src/compiler/checker.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -16499,7 +16499,13 @@ namespace ts {
1649916499
}
1650016500

1650116501
checkExpression(node.expression);
16502-
error(node.expression, Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any);
16502+
16503+
const sourceFile = getSourceFileOfNode(node);
16504+
if (!hasParseDiagnostics(sourceFile)) {
16505+
const start = getSpanOfTokenAtPosition(sourceFile, node.pos).start;
16506+
const end = node.statement.pos;
16507+
grammarErrorAtPos(sourceFile, start, end - start, Diagnostics.Unsupported_with_statement_all_symbols_within_a_with_block_will_be_resolved_to_any);
16508+
}
1650316509
}
1650416510

1650516511
function checkSwitchStatement(node: SwitchStatement) {

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@
12791279
"category": "Error",
12801280
"code": 2409
12811281
},
1282-
"All symbols within a 'with' block will be resolved to 'any'.": {
1282+
"Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.": {
12831283
"category": "Error",
12841284
"code": 2410
12851285
},

tests/baselines/reference/ambientWithStatements.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/ambientWithStatements.ts(2,5): error TS1036: Statements are
22
tests/cases/compiler/ambientWithStatements.ts(3,5): error TS1104: A 'continue' statement can only be used within an enclosing iteration statement.
33
tests/cases/compiler/ambientWithStatements.ts(7,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
44
tests/cases/compiler/ambientWithStatements.ts(11,5): error TS1108: A 'return' statement can only be used within a function body.
5-
tests/cases/compiler/ambientWithStatements.ts(25,11): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
5+
tests/cases/compiler/ambientWithStatements.ts(25,5): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
66

77

88
==== tests/cases/compiler/ambientWithStatements.ts (5 errors) ====
@@ -39,7 +39,7 @@ tests/cases/compiler/ambientWithStatements.ts(25,11): error TS2410: All symbols
3939
finally {
4040
}
4141
with (x) {
42-
~
43-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
42+
~~~~~~~~
43+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
4444
}
4545
}

tests/baselines/reference/arrowFunctionContexts.errors.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(3,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
12
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(3,7): error TS2304: Cannot find name 'window'.
2-
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(3,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
33
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(19,1): error TS2304: Cannot find name 'window'.
44
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(31,9): error TS2322: Type '() => number' is not assignable to type 'E'.
55
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(32,16): error TS2332: 'this' cannot be referenced in current location.
6+
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(44,5): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
67
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(44,11): error TS2304: Cannot find name 'window'.
7-
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(44,11): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
88
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(60,5): error TS2304: Cannot find name 'window'.
99
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(72,13): error TS2322: Type '() => number' is not assignable to type 'E'.
1010
tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(73,20): error TS2332: 'this' cannot be referenced in current location.
@@ -14,10 +14,10 @@ tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(73,20): e
1414

1515
// Arrow function used in with statement
1616
with (window) {
17+
~~~~~~~~~~~~~
18+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
1719
~~~~~~
1820
!!! error TS2304: Cannot find name 'window'.
19-
~~~~~~
20-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
2121
var p = () => this;
2222
}
2323

@@ -65,10 +65,10 @@ tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts(73,20): e
6565
module M2 {
6666
// Arrow function used in with statement
6767
with (window) {
68+
~~~~~~~~~~~~~
69+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
6870
~~~~~~
6971
!!! error TS2304: Cannot find name 'window'.
70-
~~~~~~
71-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
7272
var p = () => this;
7373
}
7474

tests/baselines/reference/constDeclarations-invalidContexts.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/constDeclarations-invalidContexts.ts(4,5): error TS1156: 'c
22
tests/cases/compiler/constDeclarations-invalidContexts.ts(6,5): error TS1156: 'const' declarations can only be declared inside a block.
33
tests/cases/compiler/constDeclarations-invalidContexts.ts(9,5): error TS1156: 'const' declarations can only be declared inside a block.
44
tests/cases/compiler/constDeclarations-invalidContexts.ts(12,5): error TS1156: 'const' declarations can only be declared inside a block.
5-
tests/cases/compiler/constDeclarations-invalidContexts.ts(16,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
5+
tests/cases/compiler/constDeclarations-invalidContexts.ts(16,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
66
tests/cases/compiler/constDeclarations-invalidContexts.ts(20,5): error TS1156: 'const' declarations can only be declared inside a block.
77
tests/cases/compiler/constDeclarations-invalidContexts.ts(23,5): error TS1156: 'const' declarations can only be declared inside a block.
88
tests/cases/compiler/constDeclarations-invalidContexts.ts(26,12): error TS1156: 'const' declarations can only be declared inside a block.
@@ -34,8 +34,8 @@ tests/cases/compiler/constDeclarations-invalidContexts.ts(29,29): error TS1156:
3434

3535
var obj;
3636
with (obj)
37-
~~~
38-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
37+
~~~~~~~~~~
38+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
3939
const c5 = 0; // No Error will be reported here since we turn off all type checking
4040

4141
for (var i = 0; i < 10; i++)

tests/baselines/reference/constDeclarations-scopes.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/constDeclarations-scopes.ts(13,5): error TS7027: Unreachable code detected.
22
tests/cases/compiler/constDeclarations-scopes.ts(22,1): error TS7027: Unreachable code detected.
3-
tests/cases/compiler/constDeclarations-scopes.ts(28,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
3+
tests/cases/compiler/constDeclarations-scopes.ts(28,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
44

55

66
==== tests/cases/compiler/constDeclarations-scopes.ts (3 errors) ====
@@ -36,8 +36,8 @@ tests/cases/compiler/constDeclarations-scopes.ts(28,7): error TS2410: All symbol
3636

3737
var obj;
3838
with (obj) {
39-
~~~
40-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
39+
~~~~~~~~~~
40+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
4141
const c = 0;
4242
n = c;
4343
}

tests/baselines/reference/constDeclarations-validContexts.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/constDeclarations-validContexts.ts(20,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1+
tests/cases/compiler/constDeclarations-validContexts.ts(20,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
22

33

44
==== tests/cases/compiler/constDeclarations-validContexts.ts (1 errors) ====
@@ -22,8 +22,8 @@ tests/cases/compiler/constDeclarations-validContexts.ts(20,7): error TS2410: All
2222

2323
var obj;
2424
with (obj) {
25-
~~~
26-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
25+
~~~~~~~~~~
26+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
2727
const c5 = 0;
2828
}
2929

tests/baselines/reference/es5-asyncFunctionWithStatements.errors.txt

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(4,5): error TS1300: 'with' statements are not allowed in an async function block.
2-
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(4,11): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
2+
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(4,5): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
33
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(10,5): error TS1300: 'with' statements are not allowed in an async function block.
4-
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(10,11): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
4+
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(10,5): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
55
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(16,5): error TS1300: 'with' statements are not allowed in an async function block.
6-
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(16,11): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
6+
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(16,5): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
77
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,5): error TS1300: 'with' statements are not allowed in an async function block.
8-
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,11): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
8+
tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,5): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
99

1010

1111
==== tests/cases/compiler/es5-asyncFunctionWithStatements.ts (8 errors) ====
@@ -15,8 +15,8 @@ tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,11): error TS2410: Al
1515
with (x) {
1616
~~~~
1717
!!! error TS1300: 'with' statements are not allowed in an async function block.
18-
~
19-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
18+
~~~~~~~~
19+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
2020
y;
2121
}
2222
}
@@ -25,8 +25,8 @@ tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,11): error TS2410: Al
2525
with (await x) {
2626
~~~~
2727
!!! error TS1300: 'with' statements are not allowed in an async function block.
28-
~~~~~~~
29-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
28+
~~~~~~~~~~~~~~
29+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
3030
y;
3131
}
3232
}
@@ -35,8 +35,8 @@ tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,11): error TS2410: Al
3535
with (x) {
3636
~~~~
3737
!!! error TS1300: 'with' statements are not allowed in an async function block.
38-
~
39-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
38+
~~~~~~~~
39+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
4040
a;
4141
await y;
4242
b;
@@ -47,8 +47,8 @@ tests/cases/compiler/es5-asyncFunctionWithStatements.ts(24,11): error TS2410: Al
4747
with (x) {
4848
~~~~
4949
!!! error TS1300: 'with' statements are not allowed in an async function block.
50-
~
51-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
50+
~~~~~~~~
51+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
5252
with (z) {
5353
a;
5454
await y;

tests/baselines/reference/functionExpressionInWithBlock.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/compiler/functionExpressionInWithBlock.ts(2,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1+
tests/cases/compiler/functionExpressionInWithBlock.ts(2,2): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
22

33

44
==== tests/cases/compiler/functionExpressionInWithBlock.ts (1 errors) ====
55
function x() {
66
with({}) {
7-
~~
8-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
7+
~~~~~~~~
8+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
99
function f() {
1010
() => this;
1111
}

tests/baselines/reference/letDeclarations-invalidContexts.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ tests/cases/compiler/letDeclarations-invalidContexts.ts(5,5): error TS1157: 'let
22
tests/cases/compiler/letDeclarations-invalidContexts.ts(7,5): error TS1157: 'let' declarations can only be declared inside a block.
33
tests/cases/compiler/letDeclarations-invalidContexts.ts(10,5): error TS1157: 'let' declarations can only be declared inside a block.
44
tests/cases/compiler/letDeclarations-invalidContexts.ts(13,5): error TS1157: 'let' declarations can only be declared inside a block.
5-
tests/cases/compiler/letDeclarations-invalidContexts.ts(17,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
5+
tests/cases/compiler/letDeclarations-invalidContexts.ts(17,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
66
tests/cases/compiler/letDeclarations-invalidContexts.ts(21,5): error TS1157: 'let' declarations can only be declared inside a block.
77
tests/cases/compiler/letDeclarations-invalidContexts.ts(24,5): error TS1157: 'let' declarations can only be declared inside a block.
88
tests/cases/compiler/letDeclarations-invalidContexts.ts(27,12): error TS1157: 'let' declarations can only be declared inside a block.
@@ -35,8 +35,8 @@ tests/cases/compiler/letDeclarations-invalidContexts.ts(30,29): error TS1157: 'l
3535

3636
var obj;
3737
with (obj)
38-
~~~
39-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
38+
~~~~~~~~~~
39+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
4040
let l5 = 0;
4141

4242
for (var i = 0; i < 10; i++)

tests/baselines/reference/letDeclarations-scopes.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/letDeclarations-scopes.ts(29,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1+
tests/cases/compiler/letDeclarations-scopes.ts(29,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
22

33

44
==== tests/cases/compiler/letDeclarations-scopes.ts (1 errors) ====
@@ -31,8 +31,8 @@ tests/cases/compiler/letDeclarations-scopes.ts(29,7): error TS2410: All symbols
3131

3232
var obj;
3333
with (obj) {
34-
~~~
35-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
34+
~~~~~~~~~~
35+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
3636
let l = 0;
3737
n = l;
3838
}

tests/baselines/reference/letDeclarations-validContexts.errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/letDeclarations-validContexts.ts(21,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1+
tests/cases/compiler/letDeclarations-validContexts.ts(21,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
22

33

44
==== tests/cases/compiler/letDeclarations-validContexts.ts (1 errors) ====
@@ -23,8 +23,8 @@ tests/cases/compiler/letDeclarations-validContexts.ts(21,7): error TS2410: All s
2323

2424
var obj;
2525
with (obj) {
26-
~~~
27-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
26+
~~~~~~~~~~
27+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
2828
let l5 = 0;
2929
}
3030

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts(2,1): error TS1101: 'with' statements are not allowed in strict mode.
2+
tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts(2,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
23
tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts(2,7): error TS2304: Cannot find name 'a'.
3-
tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts(2,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
44

55

66
==== tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts (3 errors) ====
77
"use strict";
88
with (a) {
99
~~~~
1010
!!! error TS1101: 'with' statements are not allowed in strict mode.
11+
~~~~~~~~
12+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
1113
~
1214
!!! error TS2304: Cannot find name 'a'.
13-
~
14-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1515
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts(1,1): error TS1036: Statements are not allowed in ambient contexts.
2+
tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts(1,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
23
tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts(1,7): error TS2304: Cannot find name 'foo'.
3-
tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts(1,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
44

55

66
==== tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts (3 errors) ====
77
with (foo) {
88
~~~~
99
!!! error TS1036: Statements are not allowed in ambient contexts.
10+
~~~~~~~~~~
11+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
1012
~~~
1113
!!! error TS2304: Cannot find name 'foo'.
12-
~~~
13-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1414
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts(1,7): error TS2410: All symbols within a 'with' block will be resolved to 'any'.
1+
tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts(1,1): error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
22

33

44
==== tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts (1 errors) ====
55
with (1)
6-
~
7-
!!! error TS2410: All symbols within a 'with' block will be resolved to 'any'.
6+
~~~~~~~~
7+
!!! error TS2410: Unsupported 'with' statement, all symbols within a 'with' block will be resolved to 'any'.
88
return;

0 commit comments

Comments
 (0)