Skip to content

Commit d52c224

Browse files
committed
Disallow generators in an ambient context
1 parent 124fdb6 commit d52c224

21 files changed

+142
-0
lines changed

src/compiler/checker.ts

+3
Original file line numberDiff line numberDiff line change
@@ -12550,6 +12550,9 @@ module ts {
1255012550
node.kind === SyntaxKind.FunctionDeclaration ||
1255112551
node.kind === SyntaxKind.FunctionExpression ||
1255212552
node.kind === SyntaxKind.MethodDeclaration);
12553+
if (isInAmbientContext(node)) {
12554+
return grammarErrorOnNode(node.asteriskToken, Diagnostics.Generators_are_not_allowed_in_an_ambient_context);
12555+
}
1255312556
if (languageVersion < ScriptTarget.ES6) {
1255412557
return grammarErrorOnNode(node.asteriskToken, Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_6_or_higher);
1255512558
}

src/compiler/diagnosticInformationMap.generated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ module ts {
175175
Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." },
176176
Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." },
177177
Generators_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 1219, category: DiagnosticCategory.Error, key: "Generators are only available when targeting ECMAScript 6 or higher." },
178+
Generators_are_not_allowed_in_an_ambient_context: { code: 1220, category: DiagnosticCategory.Error, key: "Generators are not allowed in an ambient context." },
178179
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
179180
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
180181
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,10 @@
687687
"category": "Error",
688688
"code": 1219
689689
},
690+
"Generators are not allowed in an ambient context.": {
691+
"category": "Error",
692+
"code": 1220
693+
},
690694

691695

692696
"Duplicate identifier '{0}'.": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext1.ts(2,5): error TS1219: Generators are not allowed in an ambient context.
2+
3+
4+
==== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext1.ts (1 errors) ====
5+
declare class C {
6+
*generator(): any;
7+
~
8+
!!! error TS1219: Generators are not allowed in an ambient context.
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [generatorInAmbientContext1.ts]
2+
declare class C {
3+
*generator(): any;
4+
}
5+
6+
//// [generatorInAmbientContext1.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext2.ts(2,14): error TS1219: Generators are not allowed in an ambient context.
2+
3+
4+
==== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext2.ts (1 errors) ====
5+
declare module M {
6+
function *generator(): any;
7+
~
8+
!!! error TS1219: Generators are not allowed in an ambient context.
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [generatorInAmbientContext2.ts]
2+
declare module M {
3+
function *generator(): any;
4+
}
5+
6+
//// [generatorInAmbientContext2.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext3.d.ts(2,5): error TS1219: Generators are not allowed in an ambient context.
2+
3+
4+
==== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext3.d.ts (1 errors) ====
5+
declare class C {
6+
*generator(): any;
7+
~
8+
!!! error TS1219: Generators are not allowed in an ambient context.
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext4.d.ts(2,14): error TS1219: Generators are not allowed in an ambient context.
2+
3+
4+
==== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext4.d.ts (1 errors) ====
5+
declare module M {
6+
function *generator(): any;
7+
~
8+
!!! error TS1219: Generators are not allowed in an ambient context.
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [generatorInAmbientContext5.ts]
2+
class C {
3+
*generator(): any { }
4+
}
5+
6+
//// [generatorInAmbientContext5.js]
7+
class C {
8+
*generator() { }
9+
}
10+
11+
12+
//// [generatorInAmbientContext5.d.ts]
13+
declare class C {
14+
generator(): any;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext5.ts ===
2+
class C {
3+
>C : Symbol(C, Decl(generatorInAmbientContext5.ts, 0, 0))
4+
5+
*generator(): any { }
6+
>generator : Symbol(generator, Decl(generatorInAmbientContext5.ts, 0, 9))
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext5.ts ===
2+
class C {
3+
>C : C
4+
5+
*generator(): any { }
6+
>generator : () => any
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [generatorInAmbientContext6.ts]
2+
module M {
3+
export function *generator(): any { }
4+
}
5+
6+
//// [generatorInAmbientContext6.js]
7+
var M;
8+
(function (M) {
9+
function* generator() { }
10+
M.generator = generator;
11+
})(M || (M = {}));
12+
13+
14+
//// [generatorInAmbientContext6.d.ts]
15+
declare module M {
16+
function generator(): any;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext6.ts ===
2+
module M {
3+
>M : Symbol(M, Decl(generatorInAmbientContext6.ts, 0, 0))
4+
5+
export function *generator(): any { }
6+
>generator : Symbol(generator, Decl(generatorInAmbientContext6.ts, 0, 10))
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/es6/yieldExpressions/generatorInAmbientContext6.ts ===
2+
module M {
3+
>M : typeof M
4+
5+
export function *generator(): any { }
6+
>generator : () => any
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@target: ES6
2+
declare class C {
3+
*generator(): any;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@target: ES6
2+
declare module M {
3+
function *generator(): any;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@target: ES6
2+
declare class C {
3+
*generator(): any;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@target: ES6
2+
declare module M {
3+
function *generator(): any;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@target: ES6
2+
//@declaration: true
3+
class C {
4+
*generator(): any { }
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@target: ES6
2+
//@declaration: true
3+
module M {
4+
export function *generator(): any { }
5+
}

0 commit comments

Comments
 (0)