From 8cbe88b5011859f1710826ac8a8b5e915b2c6e37 Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Mon, 21 Jul 2014 17:09:14 -0700 Subject: [PATCH 1/2] Give a semantic error on with statements --- src/compiler/checker.ts | 3 +- .../diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 4 + .../ambientWithStatements.errors.txt | 4 +- .../arrowFunctionContexts.errors.txt | 8 +- .../reference/arrowFunctionContexts.js | 6 +- .../functionExpressionInWithBlock.errors.txt | 10 +++ .../functionExpressionInWithBlock.js | 3 +- .../reference/parserStrictMode14.errors.txt | 2 +- .../parserWithStatement1.d.errors.txt | 2 +- .../reference/parserWithStatement2.errors.txt | 5 ++ .../sourceMapValidationStatements.errors.txt | 86 +++++++++++++++++++ .../superCallsInConstructor.errors.txt | 8 +- .../reference/withStatement.errors.txt | 8 +- .../reference/withStatementErrors.errors.txt | 8 +- .../withStatementNestedScope.errors.txt | 11 +++ .../reference/withStatements.errors.txt | 8 +- 17 files changed, 141 insertions(+), 36 deletions(-) create mode 100644 tests/baselines/reference/functionExpressionInWithBlock.errors.txt create mode 100644 tests/baselines/reference/parserWithStatement2.errors.txt create mode 100644 tests/baselines/reference/sourceMapValidationStatements.errors.txt create mode 100644 tests/baselines/reference/withStatementNestedScope.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7faabfae6ab..47810bab1ce 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5195,8 +5195,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) { diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 8b47bd15e2b..c2c422c64b6 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -122,6 +122,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." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 3114b181363..ad5e89f2b38 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -481,6 +481,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 diff --git a/tests/baselines/reference/ambientWithStatements.errors.txt b/tests/baselines/reference/ambientWithStatements.errors.txt index f2018510491..9f0927d0765 100644 --- a/tests/baselines/reference/ambientWithStatements.errors.txt +++ b/tests/baselines/reference/ambientWithStatements.errors.txt @@ -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'. } } \ No newline at end of file diff --git a/tests/baselines/reference/arrowFunctionContexts.errors.txt b/tests/baselines/reference/arrowFunctionContexts.errors.txt index 35b52144102..37ff35cb23d 100644 --- a/tests/baselines/reference/arrowFunctionContexts.errors.txt +++ b/tests/baselines/reference/arrowFunctionContexts.errors.txt @@ -1,9 +1,9 @@ -==== tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts (9 errors) ==== +==== tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts (8 errors) ==== // Arrow function used in with statement with (window) { ~~~~~~ -!!! Cannot find name 'window'. +!!! All symbols within a 'with' block will be resolved to 'any'. var p = () => this; } @@ -52,10 +52,8 @@ // Arrow function used in with statement with (window) { ~~~~~~ -!!! Cannot find name 'window'. +!!! All symbols within a 'with' block will be resolved to 'any'. var p = () => this; - ~~~~ -!!! 'this' cannot be referenced in a module body. } // Arrow function as argument to super call diff --git a/tests/baselines/reference/arrowFunctionContexts.js b/tests/baselines/reference/arrowFunctionContexts.js index 635de47f402..24b928c7842 100644 --- a/tests/baselines/reference/arrowFunctionContexts.js +++ b/tests/baselines/reference/arrowFunctionContexts.js @@ -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) { diff --git a/tests/baselines/reference/functionExpressionInWithBlock.errors.txt b/tests/baselines/reference/functionExpressionInWithBlock.errors.txt new file mode 100644 index 00000000000..6a0947e95b2 --- /dev/null +++ b/tests/baselines/reference/functionExpressionInWithBlock.errors.txt @@ -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; + } + } + } \ No newline at end of file diff --git a/tests/baselines/reference/functionExpressionInWithBlock.js b/tests/baselines/reference/functionExpressionInWithBlock.js index 2fae2384313..f2353412d5c 100644 --- a/tests/baselines/reference/functionExpressionInWithBlock.js +++ b/tests/baselines/reference/functionExpressionInWithBlock.js @@ -11,8 +11,7 @@ function x() { function x() { with ({}) { function f() { - var _this = this; - (function () { return _this; }); + (function () { return this; }); } } } diff --git a/tests/baselines/reference/parserStrictMode14.errors.txt b/tests/baselines/reference/parserStrictMode14.errors.txt index 6c5793f2772..2f90123bb5c 100644 --- a/tests/baselines/reference/parserStrictMode14.errors.txt +++ b/tests/baselines/reference/parserStrictMode14.errors.txt @@ -2,5 +2,5 @@ "use strict"; with (a) { ~ -!!! Cannot find name 'a'. +!!! All symbols within a 'with' block will be resolved to 'any'. } \ No newline at end of file diff --git a/tests/baselines/reference/parserWithStatement1.d.errors.txt b/tests/baselines/reference/parserWithStatement1.d.errors.txt index 8c6efa440cc..1d71f990311 100644 --- a/tests/baselines/reference/parserWithStatement1.d.errors.txt +++ b/tests/baselines/reference/parserWithStatement1.d.errors.txt @@ -3,5 +3,5 @@ ~~~~ !!! Statements are not allowed in ambient contexts. ~~~ -!!! Cannot find name 'foo'. +!!! All symbols within a 'with' block will be resolved to 'any'. } \ No newline at end of file diff --git a/tests/baselines/reference/parserWithStatement2.errors.txt b/tests/baselines/reference/parserWithStatement2.errors.txt new file mode 100644 index 00000000000..aa87edfd36c --- /dev/null +++ b/tests/baselines/reference/parserWithStatement2.errors.txt @@ -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; \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationStatements.errors.txt b/tests/baselines/reference/sourceMapValidationStatements.errors.txt new file mode 100644 index 00000000000..4ca4140beba --- /dev/null +++ b/tests/baselines/reference/sourceMapValidationStatements.errors.txt @@ -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(); \ No newline at end of file diff --git a/tests/baselines/reference/superCallsInConstructor.errors.txt b/tests/baselines/reference/superCallsInConstructor.errors.txt index e1d382543ac..ec6f39b7afe 100644 --- a/tests/baselines/reference/superCallsInConstructor.errors.txt +++ b/tests/baselines/reference/superCallsInConstructor.errors.txt @@ -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(); } diff --git a/tests/baselines/reference/withStatement.errors.txt b/tests/baselines/reference/withStatement.errors.txt index 518426370dd..8ffedf2abe6 100644 --- a/tests/baselines/reference/withStatement.errors.txt +++ b/tests/baselines/reference/withStatement.errors.txt @@ -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() {} diff --git a/tests/baselines/reference/withStatementErrors.errors.txt b/tests/baselines/reference/withStatementErrors.errors.txt index f6f2eaa7436..c72d3180159 100644 --- a/tests/baselines/reference/withStatementErrors.errors.txt +++ b/tests/baselines/reference/withStatementErrors.errors.txt @@ -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 diff --git a/tests/baselines/reference/withStatementNestedScope.errors.txt b/tests/baselines/reference/withStatementNestedScope.errors.txt new file mode 100644 index 00000000000..141385a1098 --- /dev/null +++ b/tests/baselines/reference/withStatementNestedScope.errors.txt @@ -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); + } \ No newline at end of file diff --git a/tests/baselines/reference/withStatements.errors.txt b/tests/baselines/reference/withStatements.errors.txt index 15e965da6c2..65577ab4c67 100644 --- a/tests/baselines/reference/withStatements.errors.txt +++ b/tests/baselines/reference/withStatements.errors.txt @@ -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'. } \ No newline at end of file From f79cba22252686aaef87a9e12397ef2d78dfa181 Mon Sep 17 00:00:00 2001 From: Jason Freeman Date: Tue, 22 Jul 2014 13:37:37 -0700 Subject: [PATCH 2/2] Type check the expression of the 'with' statement --- src/compiler/checker.ts | 1 + tests/baselines/reference/arrowFunctionContexts.errors.txt | 6 +++++- tests/baselines/reference/parserStrictMode14.errors.txt | 4 +++- tests/baselines/reference/parserWithStatement1.d.errors.txt | 4 +++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 47810bab1ce..ffe79d7b48c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5195,6 +5195,7 @@ module ts { } function checkWithStatement(node: WithStatement) { + checkExpression(node.expression); error(node.expression, Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); } diff --git a/tests/baselines/reference/arrowFunctionContexts.errors.txt b/tests/baselines/reference/arrowFunctionContexts.errors.txt index 37ff35cb23d..b768abb8575 100644 --- a/tests/baselines/reference/arrowFunctionContexts.errors.txt +++ b/tests/baselines/reference/arrowFunctionContexts.errors.txt @@ -1,9 +1,11 @@ -==== tests/cases/conformance/expressions/functions/arrowFunctionContexts.ts (8 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; } @@ -53,6 +55,8 @@ with (window) { ~~~~~~ !!! All symbols within a 'with' block will be resolved to 'any'. + ~~~~~~ +!!! Cannot find name 'window'. var p = () => this; } diff --git a/tests/baselines/reference/parserStrictMode14.errors.txt b/tests/baselines/reference/parserStrictMode14.errors.txt index 2f90123bb5c..2291ebd80b7 100644 --- a/tests/baselines/reference/parserStrictMode14.errors.txt +++ b/tests/baselines/reference/parserStrictMode14.errors.txt @@ -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'. } \ No newline at end of file diff --git a/tests/baselines/reference/parserWithStatement1.d.errors.txt b/tests/baselines/reference/parserWithStatement1.d.errors.txt index 1d71f990311..b56589d2e00 100644 --- a/tests/baselines/reference/parserWithStatement1.d.errors.txt +++ b/tests/baselines/reference/parserWithStatement1.d.errors.txt @@ -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'. } \ No newline at end of file