Merge pull request #176 from Microsoft/errorOnWithStatement

Give a semantic error on with statements
This commit is contained in:
Jason Freeman 2014-07-23 11:55:58 -07:00
commit bc26d480f7
17 changed files with 147 additions and 33 deletions

View File

@ -5284,7 +5284,7 @@ module ts {
function checkWithStatement(node: WithStatement) {
checkExpression(node.expression);
checkSourceElement(node.statement);
error(node.expression, Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any);
}
function checkSwitchStatement(node: SwitchStatement) {

View File

@ -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." },

View File

@ -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

View File

@ -1,4 +1,4 @@
==== tests/cases/compiler/ambientWithStatements.ts (14 errors) ====
==== tests/cases/compiler/ambientWithStatements.ts (15 errors) ====
declare module M {
break;
~~~~~
@ -52,5 +52,7 @@
with (x) {
~~~~
!!! Statements are not allowed in ambient contexts.
~
!!! All symbols within a 'with' block will be resolved to 'any'.
}
}

View File

@ -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;
}
@ -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

View File

@ -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) {
@ -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) {

View File

@ -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;
}
}
}

View File

@ -11,8 +11,7 @@ function x() {
function x() {
with ({}) {
function f() {
var _this = this;
(function () { return _this; });
(function () { return this; });
}
}
}

View File

@ -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'.
}

View File

@ -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'.
}

View File

@ -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;

View File

@ -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();

View File

@ -1,4 +1,4 @@
==== tests/cases/compiler/superCallsInConstructor.ts (2 errors) ====
==== tests/cases/compiler/superCallsInConstructor.ts (1 errors) ====
class C {
foo() {}
bar() {}
@ -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(); }

View File

@ -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() {}

View File

@ -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

View File

@ -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);
}

View File

@ -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'.
}