add grammar check for labeled declaration (#25317)

* add grammar check for labeled function declaration

* fix debug failed on labeled class declaration

* move labeled statement check to binder and add more pattern for check

* update diagnostic message

* update baseline
This commit is contained in:
Wenlu Wang
2018-07-18 15:37:27 -06:00
committed by Wesley Wigham
parent 2701f785ed
commit ed8b76424e
18 changed files with 608 additions and 1 deletions
+11
View File
@@ -1918,6 +1918,15 @@ namespace ts {
}
}
function checkStrictModeLabeledStatement(node: LabeledStatement) {
// Grammar checking for labeledStatement
if (inStrictMode && options.target! >= ScriptTarget.ES2015) {
if (isDeclarationStatement(node.statement) || isVariableStatement(node.statement)) {
errorOnFirstToken(node.label, Diagnostics.A_label_is_not_allowed_here);
}
}
}
function errorOnFirstToken(node: Node, message: DiagnosticMessage, arg0?: any, arg1?: any, arg2?: any) {
const span = getSpanOfTokenAtPosition(file, node.pos);
file.bindDiagnostics.push(createFileDiagnostic(file, span.start, span.length, message, arg0, arg1, arg2));
@@ -2104,6 +2113,8 @@ namespace ts {
return checkStrictModePrefixUnaryExpression(<PrefixUnaryExpression>node);
case SyntaxKind.WithStatement:
return checkStrictModeWithStatement(<WithStatement>node);
case SyntaxKind.LabeledStatement:
return checkStrictModeLabeledStatement(<LabeledStatement>node);
case SyntaxKind.ThisType:
seenThisKeyword = true;
return;
+4
View File
@@ -983,6 +983,10 @@
"category": "Error",
"code": 1343
},
"'A label is not allowed here.": {
"category": "Error",
"code": 1344
},
"Duplicate identifier '{0}'.": {
"category": "Error",
+1 -1
View File
@@ -2207,7 +2207,7 @@ namespace ts {
const statement = unwrapInnermostStatementOfLabel(node, convertedLoopState && recordLabel);
return isIterationStatement(statement, /*lookInLabeledStatements*/ false)
? visitIterationStatement(statement, /*outermostLabeledStatement*/ node)
: restoreEnclosingLabel(visitNode(statement, visitor, isStatement), node, convertedLoopState && resetLabel);
: restoreEnclosingLabel(visitNode(statement, visitor, isStatement, liftToBlock), node, convertedLoopState && resetLabel);
}
function visitIterationStatement(node: IterationStatement, outermostLabeledStatement: LabeledStatement) {
@@ -0,0 +1,23 @@
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(11,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts (2 errors) ====
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
~~~~~~
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
label: namespace N {}
~~~~~~~~~
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
label: type T = {}
@@ -0,0 +1,80 @@
//// [labeledStatementWithLabel.ts]
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
label: namespace N {}
label: type T = {}
//// [labeledStatementWithLabel.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
label: function fn() { }
label: function gen() { return __generator(this, function (_a) {
return [2 /*return*/];
}); }
label: function gen1() {
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); });
}
label: {
var E;
(function (E) {
})(E || (E = {}));
}
label:
label: {
var C = /** @class */ (function () {
function C() {
}
return C;
}());
}
label: var a = 1;
label: var b = 1;
label: var c = 1;
label:
label:
label:
@@ -0,0 +1,37 @@
=== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts ===
label: function fn() { }
>fn : Symbol(fn, Decl(labeledStatementWithLabel.ts, 0, 6))
label: function* gen() { }
>gen : Symbol(gen, Decl(labeledStatementWithLabel.ts, 1, 6))
label: async function gen1() { }
>gen1 : Symbol(gen1, Decl(labeledStatementWithLabel.ts, 2, 6))
label: enum E {}
>E : Symbol(E, Decl(labeledStatementWithLabel.ts, 3, 6))
label: interface I {}
>I : Symbol(I, Decl(labeledStatementWithLabel.ts, 4, 6))
label: class C { }
>C : Symbol(C, Decl(labeledStatementWithLabel.ts, 5, 6))
label: var a = 1;
>a : Symbol(a, Decl(labeledStatementWithLabel.ts, 6, 10))
label: let b = 1;
>b : Symbol(b, Decl(labeledStatementWithLabel.ts, 7, 10))
label: const c = 1;
>c : Symbol(c, Decl(labeledStatementWithLabel.ts, 8, 12))
label: module M { }
>M : Symbol(M, Decl(labeledStatementWithLabel.ts, 10, 6))
label: namespace N {}
>N : Symbol(N, Decl(labeledStatementWithLabel.ts, 11, 6))
label: type T = {}
>T : Symbol(T, Decl(labeledStatementWithLabel.ts, 12, 6))
@@ -0,0 +1,52 @@
=== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel.ts ===
label: function fn() { }
>label : any
>fn : () => void
label: function* gen() { }
>label : any
>gen : () => IterableIterator<any>
label: async function gen1() { }
>label : any
>gen1 : () => Promise<void>
label: enum E {}
>label : any
>E : E
label: interface I {}
>label : any
>I : I
label: class C { }
>label : any
>C : C
label: var a = 1;
>label : any
>a : number
>1 : 1
label: let b = 1;
>label : any
>b : number
>1 : 1
label: const c = 1;
>label : any
>c : 1
>1 : 1
label: module M { }
>label : any
>M : any
label: namespace N {}
>label : any
>N : any
label: type T = {}
>label : any
>T : {}
@@ -0,0 +1,23 @@
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(11,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts (2 errors) ====
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
~~~~~~
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
label: namespace N {}
~~~~~~~~~
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
label: type T = {}
@@ -0,0 +1,44 @@
//// [labeledStatementWithLabel_es2015.ts]
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
label: namespace N {}
label: type T = {}
//// [labeledStatementWithLabel_es2015.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
label: function fn() { }
label: function* gen() { }
label: function gen1() {
return __awaiter(this, void 0, void 0, function* () { });
}
label: {
var E;
(function (E) {
})(E || (E = {}));
}
label:
label: class C {
}
label: var a = 1;
label: let b = 1;
label: const c = 1;
label:
label:
label:
@@ -0,0 +1,37 @@
=== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts ===
label: function fn() { }
>fn : Symbol(fn, Decl(labeledStatementWithLabel_es2015.ts, 0, 6))
label: function* gen() { }
>gen : Symbol(gen, Decl(labeledStatementWithLabel_es2015.ts, 1, 6))
label: async function gen1() { }
>gen1 : Symbol(gen1, Decl(labeledStatementWithLabel_es2015.ts, 2, 6))
label: enum E {}
>E : Symbol(E, Decl(labeledStatementWithLabel_es2015.ts, 3, 6))
label: interface I {}
>I : Symbol(I, Decl(labeledStatementWithLabel_es2015.ts, 4, 6))
label: class C { }
>C : Symbol(C, Decl(labeledStatementWithLabel_es2015.ts, 5, 6))
label: var a = 1;
>a : Symbol(a, Decl(labeledStatementWithLabel_es2015.ts, 6, 10))
label: let b = 1;
>b : Symbol(b, Decl(labeledStatementWithLabel_es2015.ts, 7, 10))
label: const c = 1;
>c : Symbol(c, Decl(labeledStatementWithLabel_es2015.ts, 8, 12))
label: module M { }
>M : Symbol(M, Decl(labeledStatementWithLabel_es2015.ts, 10, 6))
label: namespace N {}
>N : Symbol(N, Decl(labeledStatementWithLabel_es2015.ts, 11, 6))
label: type T = {}
>T : Symbol(T, Decl(labeledStatementWithLabel_es2015.ts, 12, 6))
@@ -0,0 +1,52 @@
=== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_es2015.ts ===
label: function fn() { }
>label : any
>fn : () => void
label: function* gen() { }
>label : any
>gen : () => IterableIterator<any>
label: async function gen1() { }
>label : any
>gen1 : () => Promise<void>
label: enum E {}
>label : any
>E : E
label: interface I {}
>label : any
>I : I
label: class C { }
>label : any
>C : C
label: var a = 1;
>label : any
>a : number
>1 : 1
label: let b = 1;
>label : any
>b : number
>1 : 1
label: const c = 1;
>label : any
>c : 1
>1 : 1
label: module M { }
>label : any
>M : any
label: namespace N {}
>label : any
>N : any
label: type T = {}
>label : any
>T : {}
@@ -0,0 +1,60 @@
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(2,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(3,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(4,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(5,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(6,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(7,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(8,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(9,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(10,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(12,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,1): error TS1344: 'A label is not allowed here.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(13,8): error TS1235: A namespace declaration is only allowed in a namespace or module.
tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts(14,1): error TS1344: 'A label is not allowed here.
==== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts (14 errors) ====
"use strict"
label: function fn() { }
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: function* gen() { }
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: async function gen1() { }
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: enum E {}
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: interface I {}
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: class C { }
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: var a = 1;
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: let b = 1;
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: const c = 1;
~~~~~
!!! error TS1344: 'A label is not allowed here.
label: module M { }
~~~~~
!!! error TS1344: 'A label is not allowed here.
~~~~~~
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
label: namespace N {}
~~~~~
!!! error TS1344: 'A label is not allowed here.
~~~~~~~~~
!!! error TS1235: A namespace declaration is only allowed in a namespace or module.
label: type T = {}
~~~~~
!!! error TS1344: 'A label is not allowed here.
@@ -0,0 +1,46 @@
//// [labeledStatementWithLabel_strict.ts]
"use strict"
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
label: namespace N {}
label: type T = {}
//// [labeledStatementWithLabel_strict.js]
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
label: function fn() { }
label: function* gen() { }
label: function gen1() {
return __awaiter(this, void 0, void 0, function* () { });
}
label: {
var E;
(function (E) {
})(E || (E = {}));
}
label:
label: class C {
}
label: var a = 1;
label: let b = 1;
label: const c = 1;
label:
label:
label:
@@ -0,0 +1,38 @@
=== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts ===
"use strict"
label: function fn() { }
>fn : Symbol(fn, Decl(labeledStatementWithLabel_strict.ts, 1, 6))
label: function* gen() { }
>gen : Symbol(gen, Decl(labeledStatementWithLabel_strict.ts, 2, 6))
label: async function gen1() { }
>gen1 : Symbol(gen1, Decl(labeledStatementWithLabel_strict.ts, 3, 6))
label: enum E {}
>E : Symbol(E, Decl(labeledStatementWithLabel_strict.ts, 4, 6))
label: interface I {}
>I : Symbol(I, Decl(labeledStatementWithLabel_strict.ts, 5, 6))
label: class C { }
>C : Symbol(C, Decl(labeledStatementWithLabel_strict.ts, 6, 6))
label: var a = 1;
>a : Symbol(a, Decl(labeledStatementWithLabel_strict.ts, 7, 10))
label: let b = 1;
>b : Symbol(b, Decl(labeledStatementWithLabel_strict.ts, 8, 10))
label: const c = 1;
>c : Symbol(c, Decl(labeledStatementWithLabel_strict.ts, 9, 12))
label: module M { }
>M : Symbol(M, Decl(labeledStatementWithLabel_strict.ts, 11, 6))
label: namespace N {}
>N : Symbol(N, Decl(labeledStatementWithLabel_strict.ts, 12, 6))
label: type T = {}
>T : Symbol(T, Decl(labeledStatementWithLabel_strict.ts, 13, 6))
@@ -0,0 +1,55 @@
=== tests/cases/conformance/statements/labeledStatements/labeledStatementWithLabel_strict.ts ===
"use strict"
>"use strict" : "use strict"
label: function fn() { }
>label : any
>fn : () => void
label: function* gen() { }
>label : any
>gen : () => IterableIterator<any>
label: async function gen1() { }
>label : any
>gen1 : () => Promise<void>
label: enum E {}
>label : any
>E : E
label: interface I {}
>label : any
>I : I
label: class C { }
>label : any
>C : C
label: var a = 1;
>label : any
>a : number
>1 : 1
label: let b = 1;
>label : any
>b : number
>1 : 1
label: const c = 1;
>label : any
>c : 1
>1 : 1
label: module M { }
>label : any
>M : any
label: namespace N {}
>label : any
>N : any
label: type T = {}
>label : any
>T : {}
@@ -0,0 +1,15 @@
// @lib: es2015
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
label: namespace N {}
label: type T = {}
@@ -0,0 +1,15 @@
// @target: es2015
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
label: namespace N {}
label: type T = {}
@@ -0,0 +1,15 @@
// @target: es2015
"use strict"
label: function fn() { }
label: function* gen() { }
label: async function gen1() { }
label: enum E {}
label: interface I {}
label: class C { }
label: var a = 1;
label: let b = 1;
label: const c = 1;
label: module M { }
label: namespace N {}
label: type T = {}