Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #6319: Add support for --t: es5 and --m es6 #9042

Merged
merged 7 commits into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,6 @@
"category": "Error",
"code": 1203
},
"Cannot compile modules into 'es2015' when targeting 'ES5' or lower.": {
"category": "Error",
"code": 1204
},
"Decorators are not valid here.": {
"category": "Error",
"code": 1206
Expand Down
12 changes: 11 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5559,7 +5559,11 @@ const _super = (function (geti, seti) {
}

function emitClassLikeDeclarationBelowES6(node: ClassLikeDeclaration) {
const isES6ExportedClass = isES6ExportedDeclaration(node);
if (node.kind === SyntaxKind.ClassDeclaration) {
if (isES6ExportedClass && !(node.flags & NodeFlags.Default)) {
write("export ");
}
// source file level classes in system modules are hoisted so 'var's for them are already defined
if (!shouldHoistDeclarationInSystemJsModule(node)) {
write("var ");
Expand Down Expand Up @@ -5629,9 +5633,15 @@ const _super = (function (geti, seti) {
}
emitEnd(node);

if (node.kind === SyntaxKind.ClassDeclaration) {
if (node.kind === SyntaxKind.ClassDeclaration && !isES6ExportedClass) {
emitExportMemberAssignment(<ClassDeclaration>node);
}
else if (isES6ExportedClass && (node.flags & NodeFlags.Default)) {
writeLine();
write("export default ");
emitDeclarationName(node);
write(";");
}
}

function emitClassMemberPrefix(node: ClassLikeDeclaration, member: Node) {
Expand Down
5 changes: 0 additions & 5 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2099,11 +2099,6 @@ namespace ts {
programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none));
}

// Cannot specify module gen target of es6 when below es6
if (options.module === ModuleKind.ES6 && languageVersion < ScriptTarget.ES6) {
programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_es2015_when_targeting_ES5_or_lower));
}

// Cannot specify module gen that isn't amd or system with --out
if (outFile) {
if (options.module && !(options.module === ModuleKind.AMD || options.module === ModuleKind.System)) {
Expand Down
19 changes: 0 additions & 19 deletions tests/baselines/reference/es5andes6module.errors.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/baselines/reference/es5andes6module.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ var A = (function () {
};
return A;
}());
exports.default = A;
export default A;
17 changes: 17 additions & 0 deletions tests/baselines/reference/es5andes6module.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/es5andes6module.ts ===

export default class A
>A : Symbol(A, Decl(es5andes6module.ts, 0, 0))
{
constructor ()
{

}

public B()
>B : Symbol(A.B, Decl(es5andes6module.ts, 6, 5))
{
return 42;
}
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/es5andes6module.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/es5andes6module.ts ===

export default class A
>A : A
{
constructor ()
{

}

public B()
>B : () => number
{
return 42;
>42 : number
}
}

56 changes: 56 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//// [es6modulekindWithES5Target.ts]

export class C {
static s = 0;
p = 1;
method() { }
}
export { C as C2 };

declare function foo(...args: any[]): any;
@foo
export class D {
static s = 0;
p = 1;
method() { }
}
export { D as D2 };

class E { }
export {E};


//// [es6modulekindWithES5Target.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
export var C = (function () {
function C() {
this.p = 1;
}
C.prototype.method = function () { };
C.s = 0;
return C;
}());
export { C as C2 };
export var D = (function () {
function D() {
this.p = 1;
}
D.prototype.method = function () { };
D.s = 0;
D = __decorate([
foo
], D);
return D;
}());
export { D as D2 };
var E = (function () {
function E() {
}
return E;
}());
export { E };
47 changes: 47 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
=== tests/cases/compiler/es6modulekindWithES5Target.ts ===

export class C {
>C : Symbol(C, Decl(es6modulekindWithES5Target.ts, 0, 0))

static s = 0;
>s : Symbol(C.s, Decl(es6modulekindWithES5Target.ts, 1, 16))

p = 1;
>p : Symbol(C.p, Decl(es6modulekindWithES5Target.ts, 2, 17))

method() { }
>method : Symbol(C.method, Decl(es6modulekindWithES5Target.ts, 3, 10))
}
export { C as C2 };
>C : Symbol(C2, Decl(es6modulekindWithES5Target.ts, 6, 8))
>C2 : Symbol(C2, Decl(es6modulekindWithES5Target.ts, 6, 8))

declare function foo(...args: any[]): any;
>foo : Symbol(foo, Decl(es6modulekindWithES5Target.ts, 6, 19))
>args : Symbol(args, Decl(es6modulekindWithES5Target.ts, 8, 21))

@foo
>foo : Symbol(foo, Decl(es6modulekindWithES5Target.ts, 6, 19))

export class D {
>D : Symbol(D, Decl(es6modulekindWithES5Target.ts, 8, 42))

static s = 0;
>s : Symbol(D.s, Decl(es6modulekindWithES5Target.ts, 10, 16))

p = 1;
>p : Symbol(D.p, Decl(es6modulekindWithES5Target.ts, 11, 17))

method() { }
>method : Symbol(D.method, Decl(es6modulekindWithES5Target.ts, 12, 10))
}
export { D as D2 };
>D : Symbol(D2, Decl(es6modulekindWithES5Target.ts, 15, 8))
>D2 : Symbol(D2, Decl(es6modulekindWithES5Target.ts, 15, 8))

class E { }
>E : Symbol(E, Decl(es6modulekindWithES5Target.ts, 15, 19))

export {E};
>E : Symbol(E, Decl(es6modulekindWithES5Target.ts, 18, 8))

51 changes: 51 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/compiler/es6modulekindWithES5Target.ts ===

export class C {
>C : C

static s = 0;
>s : number
>0 : number

p = 1;
>p : number
>1 : number

method() { }
>method : () => void
}
export { C as C2 };
>C : typeof C
>C2 : typeof C

declare function foo(...args: any[]): any;
>foo : (...args: any[]) => any
>args : any[]

@foo
>foo : (...args: any[]) => any

export class D {
>D : D

static s = 0;
>s : number
>0 : number

p = 1;
>p : number
>1 : number

method() { }
>method : () => void
}
export { D as D2 };
>D : typeof D
>D2 : typeof D

class E { }
>E : E

export {E};
>E : typeof E

19 changes: 19 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target10.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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.
tests/cases/compiler/es6modulekindWithES5Target10.ts(2,20): error TS2307: Cannot find module 'mod'.
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.


==== tests/cases/compiler/es6modulekindWithES5Target10.ts (3 errors) ====

import i = require("mod"); // Error;
~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! 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.
~~~~~
!!! error TS2307: Cannot find module 'mod'.


namespace N {
}
export = N; // Error
~~~~~~~~~~~
!!! error TS1203: Export assignment cannot be used when targeting ECMAScript 6 modules. Consider using 'export default' or another module format instead.
10 changes: 10 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [es6modulekindWithES5Target10.ts]

import i = require("mod"); // Error;


namespace N {
}
export = N; // Error

//// [es6modulekindWithES5Target10.js]
31 changes: 31 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target11.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [es6modulekindWithES5Target11.ts]

declare function foo(...args: any[]): any;
@foo
export default class C {
static x() { return C.y; }
static y = 1
p = 1;
method() { }
}

//// [es6modulekindWithES5Target11.js]
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var C = (function () {
function C() {
this.p = 1;
}
C.x = function () { return C.y; };
C.prototype.method = function () { };
C.y = 1;
C = __decorate([
foo
], C);
return C;
}());
export default C;
27 changes: 27 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target11.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/es6modulekindWithES5Target11.ts ===

declare function foo(...args: any[]): any;
>foo : Symbol(foo, Decl(es6modulekindWithES5Target11.ts, 0, 0))
>args : Symbol(args, Decl(es6modulekindWithES5Target11.ts, 1, 21))

@foo
>foo : Symbol(foo, Decl(es6modulekindWithES5Target11.ts, 0, 0))

export default class C {
>C : Symbol(C, Decl(es6modulekindWithES5Target11.ts, 1, 42))

static x() { return C.y; }
>x : Symbol(C.x, Decl(es6modulekindWithES5Target11.ts, 3, 24))
>C.y : Symbol(C.y, Decl(es6modulekindWithES5Target11.ts, 4, 30))
>C : Symbol(C, Decl(es6modulekindWithES5Target11.ts, 1, 42))
>y : Symbol(C.y, Decl(es6modulekindWithES5Target11.ts, 4, 30))

static y = 1
>y : Symbol(C.y, Decl(es6modulekindWithES5Target11.ts, 4, 30))

p = 1;
>p : Symbol(C.p, Decl(es6modulekindWithES5Target11.ts, 5, 16))

method() { }
>method : Symbol(C.method, Decl(es6modulekindWithES5Target11.ts, 6, 10))
}
29 changes: 29 additions & 0 deletions tests/baselines/reference/es6modulekindWithES5Target11.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/es6modulekindWithES5Target11.ts ===

declare function foo(...args: any[]): any;
>foo : (...args: any[]) => any
>args : any[]

@foo
>foo : (...args: any[]) => any

export default class C {
>C : C

static x() { return C.y; }
>x : () => number
>C.y : number
>C : typeof C
>y : number

static y = 1
>y : number
>1 : number

p = 1;
>p : number
>1 : number

method() { }
>method : () => void
}