Skip to content

Commit 97d7aa5

Browse files
authored
Merge pull request microsoft#9042 from Microsoft/ES6ModulesES5Target
Fix microsoft#6319: Add support for `--t: es5` and `--m es6`
2 parents 71b7d44 + f0a430a commit 97d7aa5

File tree

50 files changed

+850
-32
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+850
-32
lines changed

src/compiler/diagnosticMessages.json

-4
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,6 @@
635635
"category": "Error",
636636
"code": 1203
637637
},
638-
"Cannot compile modules into 'es2015' when targeting 'ES5' or lower.": {
639-
"category": "Error",
640-
"code": 1204
641-
},
642638
"Decorators are not valid here.": {
643639
"category": "Error",
644640
"code": 1206

src/compiler/emitter.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -5613,7 +5613,11 @@ const _super = (function (geti, seti) {
56135613
}
56145614

56155615
function emitClassLikeDeclarationBelowES6(node: ClassLikeDeclaration) {
5616+
const isES6ExportedClass = isES6ExportedDeclaration(node);
56165617
if (node.kind === SyntaxKind.ClassDeclaration) {
5618+
if (isES6ExportedClass && !(node.flags & NodeFlags.Default)) {
5619+
write("export ");
5620+
}
56175621
// source file level classes in system modules are hoisted so 'var's for them are already defined
56185622
if (!shouldHoistDeclarationInSystemJsModule(node)) {
56195623
write("var ");
@@ -5683,9 +5687,15 @@ const _super = (function (geti, seti) {
56835687
}
56845688
emitEnd(node);
56855689

5686-
if (node.kind === SyntaxKind.ClassDeclaration) {
5690+
if (node.kind === SyntaxKind.ClassDeclaration && !isES6ExportedClass) {
56875691
emitExportMemberAssignment(<ClassDeclaration>node);
56885692
}
5693+
else if (isES6ExportedClass && (node.flags & NodeFlags.Default)) {
5694+
writeLine();
5695+
write("export default ");
5696+
emitDeclarationName(node);
5697+
write(";");
5698+
}
56895699
}
56905700

56915701
function emitClassMemberPrefix(node: ClassLikeDeclaration, member: Node) {

src/compiler/program.ts

-5
Original file line numberDiff line numberDiff line change
@@ -2160,11 +2160,6 @@ namespace ts {
21602160
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none));
21612161
}
21622162

2163-
// Cannot specify module gen target of es6 when below es6
2164-
if (options.module === ModuleKind.ES6 && languageVersion < ScriptTarget.ES6) {
2165-
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_es2015_when_targeting_ES5_or_lower));
2166-
}
2167-
21682163
// Cannot specify module gen that isn't amd or system with --out
21692164
if (outFile) {
21702165
if (options.module && !(options.module === ModuleKind.AMD || options.module === ModuleKind.System)) {

tests/baselines/reference/es5andes6module.errors.txt

-19
This file was deleted.

tests/baselines/reference/es5andes6module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ var A = (function () {
2323
};
2424
return A;
2525
}());
26-
exports.default = A;
26+
export default A;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/es5andes6module.ts ===
2+
3+
export default class A
4+
>A : Symbol(A, Decl(es5andes6module.ts, 0, 0))
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : Symbol(A.B, Decl(es5andes6module.ts, 6, 5))
13+
{
14+
return 42;
15+
}
16+
}
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/es5andes6module.ts ===
2+
3+
export default class A
4+
>A : A
5+
{
6+
constructor ()
7+
{
8+
9+
}
10+
11+
public B()
12+
>B : () => number
13+
{
14+
return 42;
15+
>42 : number
16+
}
17+
}
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//// [es6modulekindWithES5Target.ts]
2+
3+
export class C {
4+
static s = 0;
5+
p = 1;
6+
method() { }
7+
}
8+
export { C as C2 };
9+
10+
declare function foo(...args: any[]): any;
11+
@foo
12+
export class D {
13+
static s = 0;
14+
p = 1;
15+
method() { }
16+
}
17+
export { D as D2 };
18+
19+
class E { }
20+
export {E};
21+
22+
23+
//// [es6modulekindWithES5Target.js]
24+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
25+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
26+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
27+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
28+
return c > 3 && r && Object.defineProperty(target, key, r), r;
29+
};
30+
export var C = (function () {
31+
function C() {
32+
this.p = 1;
33+
}
34+
C.prototype.method = function () { };
35+
C.s = 0;
36+
return C;
37+
}());
38+
export { C as C2 };
39+
export var D = (function () {
40+
function D() {
41+
this.p = 1;
42+
}
43+
D.prototype.method = function () { };
44+
D.s = 0;
45+
D = __decorate([
46+
foo
47+
], D);
48+
return D;
49+
}());
50+
export { D as D2 };
51+
var E = (function () {
52+
function E() {
53+
}
54+
return E;
55+
}());
56+
export { E };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
=== tests/cases/compiler/es6modulekindWithES5Target.ts ===
2+
3+
export class C {
4+
>C : Symbol(C, Decl(es6modulekindWithES5Target.ts, 0, 0))
5+
6+
static s = 0;
7+
>s : Symbol(C.s, Decl(es6modulekindWithES5Target.ts, 1, 16))
8+
9+
p = 1;
10+
>p : Symbol(C.p, Decl(es6modulekindWithES5Target.ts, 2, 17))
11+
12+
method() { }
13+
>method : Symbol(C.method, Decl(es6modulekindWithES5Target.ts, 3, 10))
14+
}
15+
export { C as C2 };
16+
>C : Symbol(C2, Decl(es6modulekindWithES5Target.ts, 6, 8))
17+
>C2 : Symbol(C2, Decl(es6modulekindWithES5Target.ts, 6, 8))
18+
19+
declare function foo(...args: any[]): any;
20+
>foo : Symbol(foo, Decl(es6modulekindWithES5Target.ts, 6, 19))
21+
>args : Symbol(args, Decl(es6modulekindWithES5Target.ts, 8, 21))
22+
23+
@foo
24+
>foo : Symbol(foo, Decl(es6modulekindWithES5Target.ts, 6, 19))
25+
26+
export class D {
27+
>D : Symbol(D, Decl(es6modulekindWithES5Target.ts, 8, 42))
28+
29+
static s = 0;
30+
>s : Symbol(D.s, Decl(es6modulekindWithES5Target.ts, 10, 16))
31+
32+
p = 1;
33+
>p : Symbol(D.p, Decl(es6modulekindWithES5Target.ts, 11, 17))
34+
35+
method() { }
36+
>method : Symbol(D.method, Decl(es6modulekindWithES5Target.ts, 12, 10))
37+
}
38+
export { D as D2 };
39+
>D : Symbol(D2, Decl(es6modulekindWithES5Target.ts, 15, 8))
40+
>D2 : Symbol(D2, Decl(es6modulekindWithES5Target.ts, 15, 8))
41+
42+
class E { }
43+
>E : Symbol(E, Decl(es6modulekindWithES5Target.ts, 15, 19))
44+
45+
export {E};
46+
>E : Symbol(E, Decl(es6modulekindWithES5Target.ts, 18, 8))
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
=== tests/cases/compiler/es6modulekindWithES5Target.ts ===
2+
3+
export class C {
4+
>C : C
5+
6+
static s = 0;
7+
>s : number
8+
>0 : number
9+
10+
p = 1;
11+
>p : number
12+
>1 : number
13+
14+
method() { }
15+
>method : () => void
16+
}
17+
export { C as C2 };
18+
>C : typeof C
19+
>C2 : typeof C
20+
21+
declare function foo(...args: any[]): any;
22+
>foo : (...args: any[]) => any
23+
>args : any[]
24+
25+
@foo
26+
>foo : (...args: any[]) => any
27+
28+
export class D {
29+
>D : D
30+
31+
static s = 0;
32+
>s : number
33+
>0 : number
34+
35+
p = 1;
36+
>p : number
37+
>1 : number
38+
39+
method() { }
40+
>method : () => void
41+
}
42+
export { D as D2 };
43+
>D : typeof D
44+
>D2 : typeof D
45+
46+
class E { }
47+
>E : E
48+
49+
export {E};
50+
>E : typeof E
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/compiler/es6modulekindWithES5Target10.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
2+
tests/cases/compiler/es6modulekindWithES5Target10.ts(2,20): error TS2307: Cannot find module 'mod'.
3+
tests/cases/compiler/es6modulekindWithES5Target10.ts(7,1): error TS1203: Export assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'export default' or another module format instead.
4+
5+
6+
==== tests/cases/compiler/es6modulekindWithES5Target10.ts (3 errors) ====
7+
8+
import i = require("mod"); // Error;
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS1202: Import assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'import * as ns from "mod"', 'import {a} from "mod"', 'import d from "mod"', or another module format instead.
11+
~~~~~
12+
!!! error TS2307: Cannot find module 'mod'.
13+
14+
15+
namespace N {
16+
}
17+
export = N; // Error
18+
~~~~~~~~~~~
19+
!!! error TS1203: Export assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'export default' or another module format instead.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [es6modulekindWithES5Target10.ts]
2+
3+
import i = require("mod"); // Error;
4+
5+
6+
namespace N {
7+
}
8+
export = N; // Error
9+
10+
//// [es6modulekindWithES5Target10.js]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [es6modulekindWithES5Target11.ts]
2+
3+
declare function foo(...args: any[]): any;
4+
@foo
5+
export default class C {
6+
static x() { return C.y; }
7+
static y = 1
8+
p = 1;
9+
method() { }
10+
}
11+
12+
//// [es6modulekindWithES5Target11.js]
13+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17+
return c > 3 && r && Object.defineProperty(target, key, r), r;
18+
};
19+
var C = (function () {
20+
function C() {
21+
this.p = 1;
22+
}
23+
C.x = function () { return C.y; };
24+
C.prototype.method = function () { };
25+
C.y = 1;
26+
C = __decorate([
27+
foo
28+
], C);
29+
return C;
30+
}());
31+
export default C;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/compiler/es6modulekindWithES5Target11.ts ===
2+
3+
declare function foo(...args: any[]): any;
4+
>foo : Symbol(foo, Decl(es6modulekindWithES5Target11.ts, 0, 0))
5+
>args : Symbol(args, Decl(es6modulekindWithES5Target11.ts, 1, 21))
6+
7+
@foo
8+
>foo : Symbol(foo, Decl(es6modulekindWithES5Target11.ts, 0, 0))
9+
10+
export default class C {
11+
>C : Symbol(C, Decl(es6modulekindWithES5Target11.ts, 1, 42))
12+
13+
static x() { return C.y; }
14+
>x : Symbol(C.x, Decl(es6modulekindWithES5Target11.ts, 3, 24))
15+
>C.y : Symbol(C.y, Decl(es6modulekindWithES5Target11.ts, 4, 30))
16+
>C : Symbol(C, Decl(es6modulekindWithES5Target11.ts, 1, 42))
17+
>y : Symbol(C.y, Decl(es6modulekindWithES5Target11.ts, 4, 30))
18+
19+
static y = 1
20+
>y : Symbol(C.y, Decl(es6modulekindWithES5Target11.ts, 4, 30))
21+
22+
p = 1;
23+
>p : Symbol(C.p, Decl(es6modulekindWithES5Target11.ts, 5, 16))
24+
25+
method() { }
26+
>method : Symbol(C.method, Decl(es6modulekindWithES5Target11.ts, 6, 10))
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/es6modulekindWithES5Target11.ts ===
2+
3+
declare function foo(...args: any[]): any;
4+
>foo : (...args: any[]) => any
5+
>args : any[]
6+
7+
@foo
8+
>foo : (...args: any[]) => any
9+
10+
export default class C {
11+
>C : C
12+
13+
static x() { return C.y; }
14+
>x : () => number
15+
>C.y : number
16+
>C : typeof C
17+
>y : number
18+
19+
static y = 1
20+
>y : number
21+
>1 : number
22+
23+
p = 1;
24+
>p : number
25+
>1 : number
26+
27+
method() { }
28+
>method : () => void
29+
}

0 commit comments

Comments
 (0)