Skip to content

Give a semantic error on with statements #176

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 4 commits into from
Jul 23, 2014
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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5284,7 +5284,7 @@ module ts {

function checkWithStatement(node: WithStatement) {
checkExpression(node.expression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to talk about how we should still typecheck the expression (since there's no with craziness there) and how the error message is inaccurate (since symbolless expressions like 3 > '' ought to error anyway), but this is actually inline with the old compiler so I'm inclined to say it really doesn't matter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we've always just thrown up our hands on everything in a 'with' block

checkSourceElement(node.statement);
error(node.expression, Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong. The statement of the 'with' block should not be checked, but the expression of the with block should still be checked. Yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am open to discussing checking the expression, but as @RyanCavanaugh says, skipping it is consistent with the old compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small tweak. can you just underline the 'while' token (i think you can get it with the scanner). it seems weird to underling the expression since you're complaining about the 'while' block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Should be the with keyword.

}

function checkSwitchStatement(node: SwitchStatement) {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module ts {
Setters_cannot_return_a_value: { code: 2122, category: DiagnosticCategory.Error, key: "Setters cannot return a value." },
Invalid_left_hand_side_of_assignment_expression: { code: 2130, category: DiagnosticCategory.Error, key: "Invalid left-hand side of assignment expression." },
Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2: { code: 2134, category: DiagnosticCategory.Error, key: "Subsequent variable declarations must have the same type. Variable '{0}' must be of type '{1}', but here has type '{2}'." },
All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2135, category: DiagnosticCategory.Error, key: "All symbols within a 'with' block will be resolved to 'any'." },
The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer: { code: 2139, category: DiagnosticCategory.Error, key: "The operand of an increment or decrement operator must be a variable, property or indexer." },
Overload_signatures_must_all_be_public_or_private: { code: 2150, category: DiagnosticCategory.Error, key: "Overload signatures must all be public or private." },
Overload_signatures_must_all_be_exported_or_not_exported: { code: 2151, category: DiagnosticCategory.Error, key: "Overload signatures must all be exported or not exported." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@
"category": "Error",
"code": 2134
},
"All symbols within a 'with' block will be resolved to 'any'.": {
"category": "Error",
"code": 2135
},
"The operand of an increment or decrement operator must be a variable, property or indexer.": {
"category": "Error",
"code": 2139
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/ambientWithStatements.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
==== tests/cases/compiler/ambientWithStatements.ts (14 errors) ====
==== tests/cases/compiler/ambientWithStatements.ts (15 errors) ====
declare module M {
break;
~~~~~
Expand Down Expand Up @@ -52,5 +52,7 @@
with (x) {
~~~~
!!! Statements are not allowed in ambient contexts.
~
!!! All symbols within a 'with' block will be resolved to 'any'.
}
}
8 changes: 5 additions & 3 deletions tests/baselines/reference/arrowFunctionContexts.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
==== tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts (9 errors) ====
==== tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts (10 errors) ====

// Arrow function used in with statement
with (window) {
~~~~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
~~~~~~
!!! Cannot find name 'window'.
var p = () => this;
}
Expand Down Expand Up @@ -52,10 +54,10 @@
// Arrow function used in with statement
with (window) {
~~~~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
~~~~~~
!!! Cannot find name 'window'.
var p = () => this;
~~~~
!!! 'this' cannot be referenced in a module body.
}

// Arrow function as argument to super call
Expand Down
6 changes: 2 additions & 4 deletions tests/baselines/reference/arrowFunctionContexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ var __extends = this.__extends || function (d, b) {
__.prototype = b.prototype;
d.prototype = new __();
};
var _this = this;
with (window) {
var p = function () { return _this; };
var p = function () { return this; };
}
var Base = (function () {
function Base(n) {
Expand Down Expand Up @@ -137,9 +136,8 @@ var M;
})(M || (M = {}));
var M2;
(function (M2) {
var _this = this;
with (window) {
var p = function () { return _this; };
var p = function () { return this; };
}
var Base = (function () {
function Base(n) {
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/functionExpressionInWithBlock.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
==== tests/cases/compiler/functionExpressionInWithBlock.ts (1 errors) ====
function x() {
with({}) {
~~
!!! All symbols within a 'with' block will be resolved to 'any'.
function f() {
() => this;
}
}
}
3 changes: 1 addition & 2 deletions tests/baselines/reference/functionExpressionInWithBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ function x() {
function x() {
with ({}) {
function f() {
var _this = this;
(function () { return _this; });
(function () { return this; });
}
}
}
4 changes: 3 additions & 1 deletion tests/baselines/reference/parserStrictMode14.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
==== tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts (1 errors) ====
==== tests/cases/conformance/parser/ecmascript5/StrictMode/parserStrictMode14.ts (2 errors) ====
"use strict";
with (a) {
~
!!! All symbols within a 'with' block will be resolved to 'any'.
~
!!! Cannot find name 'a'.
}
4 changes: 3 additions & 1 deletion tests/baselines/reference/parserWithStatement1.d.errors.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
==== tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts (2 errors) ====
==== tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement1.d.ts (3 errors) ====
with (foo) {
~~~~
!!! Statements are not allowed in ambient contexts.
~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
~~~
!!! Cannot find name 'foo'.
}
5 changes: 5 additions & 0 deletions tests/baselines/reference/parserWithStatement2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
==== tests/cases/conformance/parser/ecmascript5/Statements/parserWithStatement2.ts (1 errors) ====
with (1)
~
!!! All symbols within a 'with' block will be resolved to 'any'.
return;
86 changes: 86 additions & 0 deletions tests/baselines/reference/sourceMapValidationStatements.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
==== tests/cases/compiler/sourceMapValidationStatements.ts (1 errors) ====
function f() {
var y;
var x = 0;
for (var i = 0; i < 10; i++) {
x += i;
x *= 0;
}
if (x > 17) {
x /= 9;
} else {
x += 10;
x++;
}
var a = [
1,
2,
3
];
var obj = {
z: 1,
q: "hello"
};
for (var j in a) {
obj.z = a[j];
var v = 10;
}
try {
obj.q = "ohhh";
} catch (e) {
if (obj.z < 10) {
obj.z = 12;
} else {
obj.q = "hmm";
}
}
try {
throw new Error();
} catch (e1) {
var b = e1;
} finally {
y = 70;
}
with (obj) {
~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
i = 2;
z = 10;
}
switch (obj.z) {
case 0: {
x++;
break;

}
case 1: {
x--;
break;

}
default: {
x *= 2;
x = 50;
break;

}
}
while (x < 10) {
x++;
}
do {
x--;
} while (x > 4)
x = y;
var z = (x == 1) ? x + 1 : x - 1;
(x == 1) ? x + 1 : x - 1;
x === 1;
x = z = 40;
eval("y");
return;
}
var b = function () {
var x = 10;
x = x + 1;
};
f();
8 changes: 3 additions & 5 deletions tests/baselines/reference/superCallsInConstructor.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
==== tests/cases/compiler/superCallsInConstructor.ts (2 errors) ====
==== tests/cases/compiler/superCallsInConstructor.ts (1 errors) ====
class C {
foo() {}
bar() {}
Expand All @@ -11,13 +11,11 @@
class Derived extends Base {
constructor() {
with(new C()) {
~~~~~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
foo();
~~~
!!! Cannot find name 'foo'.
super();
bar();
~~~
!!! Cannot find name 'bar'.
}

try {} catch(e) { super(); }
Expand Down
8 changes: 3 additions & 5 deletions tests/baselines/reference/withStatement.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
==== tests/cases/compiler/withStatement.ts (2 errors) ====
==== tests/cases/compiler/withStatement.ts (1 errors) ====
declare var ooo:any;

with (ooo.eee.oo.ah_ah.ting.tang.walla.walla) { // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
bing = true; // no error
~~~~
!!! Cannot find name 'bing'.
bang = true; // no error
~~~~
!!! Cannot find name 'bang'.

function bar() {}

Expand Down
8 changes: 3 additions & 5 deletions tests/baselines/reference/withStatementErrors.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
==== tests/cases/compiler/withStatementErrors.ts (4 errors) ====
==== tests/cases/compiler/withStatementErrors.ts (3 errors) ====
declare var ooo:any;

with (ooo.eee.oo.ah_ah.ting.tang.walla.walla) { // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! All symbols within a 'with' block will be resolved to 'any'.
bing = true; // no error
~~~~
!!! Cannot find name 'bing'.
bang = true; // no error
~~~~
!!! Cannot find name 'bang'.

function bar() {} // no error

Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/withStatementNestedScope.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
==== tests/cases/compiler/withStatementNestedScope.ts (1 errors) ====
var x = 1;
with (x) {
~
!!! All symbols within a 'with' block will be resolved to 'any'.
function f(a: number) {
return 1;
}
// should be any
var r = f(1);
}
8 changes: 3 additions & 5 deletions tests/baselines/reference/withStatements.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
==== tests/cases/conformance/statements/withStatements/withStatements.ts (2 errors) ====
==== tests/cases/conformance/statements/withStatements/withStatements.ts (1 errors) ====
var x = 12;
with (x) {
~
!!! All symbols within a 'with' block will be resolved to 'any'.
name = 'twelve'
~~~~
!!! Cannot find name 'name'.
id = 12
~~
!!! Cannot find name 'id'.
}