Skip to content

Commit

Permalink
Add es6 target
Browse files Browse the repository at this point in the history
  • Loading branch information
mhegazy committed Oct 11, 2014
1 parent bdac6ca commit 873c1df
Show file tree
Hide file tree
Showing 27 changed files with 602 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/compiler/commandLineParser.ts
Expand Up @@ -102,10 +102,10 @@ module ts {
{
name: "target",
shortName: "t",
type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5 },
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5,
type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5 , "es6": ScriptTarget.ES6 },
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES6_experimental,
paramType: Diagnostics.VERSION,
error: Diagnostics.Argument_for_target_option_must_be_es3_or_es5
error: Diagnostics.Argument_for_target_option_must_be_es3_es5_or_es6
},
{
name: "version",
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/diagnosticInformationMap.generated.ts
Expand Up @@ -357,7 +357,7 @@ module ts {
Watch_input_files: { code: 6005, category: DiagnosticCategory.Message, key: "Watch input files." },
Redirect_output_structure_to_the_directory: { code: 6006, category: DiagnosticCategory.Message, key: "Redirect output structure to the directory." },
Do_not_emit_comments_to_output: { code: 6009, category: DiagnosticCategory.Message, key: "Do not emit comments to output." },
Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: 'ES3' (default), or 'ES5'" },
Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES6_experimental: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES6' (experimental)" },
Specify_module_code_generation_Colon_commonjs_or_amd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs' or 'amd'" },
Print_this_message: { code: 6017, category: DiagnosticCategory.Message, key: "Print this message." },
Print_the_compiler_s_version: { code: 6019, category: DiagnosticCategory.Message, key: "Print the compiler's version." },
Expand All @@ -379,7 +379,7 @@ module ts {
Compiler_option_0_expects_an_argument: { code: 6044, category: DiagnosticCategory.Error, key: "Compiler option '{0}' expects an argument." },
Unterminated_quoted_string_in_response_file_0: { code: 6045, category: DiagnosticCategory.Error, key: "Unterminated quoted string in response file '{0}'." },
Argument_for_module_option_must_be_commonjs_or_amd: { code: 6046, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
Argument_for_target_option_must_be_es3_or_es5: { code: 6047, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },
Argument_for_target_option_must_be_es3_es5_or_es6: { code: 6047, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3', 'es5', or 'es6'." },
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: { code: 6048, category: DiagnosticCategory.Error, key: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'." },
Unsupported_locale_0: { code: 6049, category: DiagnosticCategory.Error, key: "Unsupported locale '{0}'." },
Unable_to_open_file_0: { code: 6050, category: DiagnosticCategory.Error, key: "Unable to open file '{0}'." },
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -1424,7 +1424,7 @@
"category": "Message",
"code": 6009
},
"Specify ECMAScript target version: 'ES3' (default), or 'ES5'": {
"Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES6' (experimental)": {
"category": "Message",
"code": 6015
},
Expand Down Expand Up @@ -1512,7 +1512,7 @@
"category": "Error",
"code": 6046
},
"Argument for '--target' option must be 'es3' or 'es5'.": {
"Argument for '--target' option must be 'es3', 'es5', or 'es6'.": {

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Oct 19, 2014

Contributor

The casing is not consistent between these two messages (6015 and 6047)

"category": "Error",
"code": 6047
},
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/types.ts
Expand Up @@ -1062,6 +1062,8 @@ module ts {
export enum ScriptTarget {
ES3,
ES5,
ES6,

This comment has been minimized.

Copy link
@ahejlsberg

ahejlsberg Oct 17, 2014

Member

Awesome that we now officially have an ES6 target. We'll need to update our class and arrow function emit to not rewrite these constructs in ES6 mode. Once this goes in we should make sure to file bugs to that effect.

This comment has been minimized.

Copy link
@mhegazy

mhegazy Oct 17, 2014

Author Contributor

yes. that is the intention.

Latest = ES6,
}

export interface ParsedCommandLine {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/fourslash.ts
Expand Up @@ -2139,7 +2139,7 @@ module FourSlash {
var host = Harness.Compiler.createCompilerHost([{ unitName: Harness.Compiler.fourslashFilename, content: undefined },
{ unitName: fileName, content: content }],
(fn, contents) => result = contents,
ts.ScriptTarget.ES5,
ts.ScriptTarget.Latest,
sys.useCaseSensitiveFileNames);
var program = ts.createProgram([Harness.Compiler.fourslashFilename, fileName], { out: "fourslashTestOutput.js" }, host);
var checker = ts.createTypeChecker(program, /*fullTypeCheckMode*/ true);
Expand Down
4 changes: 3 additions & 1 deletion src/harness/harness.ts
Expand Up @@ -532,7 +532,7 @@ module Harness {
}

export var defaultLibFileName = 'lib.d.ts';
export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.ES5, /*version:*/ "0");
export var defaultLibSourceFile = ts.createSourceFile(defaultLibFileName, IO.readFile(libFolder + 'lib.core.d.ts'), /*languageVersion*/ ts.ScriptTarget.Latest, /*version:*/ "0");

// Cache these between executions so we don't have to re-parse them for every test
export var fourslashFilename = 'fourslash.ts';
Expand Down Expand Up @@ -685,6 +685,8 @@ module Harness {
options.target = ts.ScriptTarget.ES3;
} else if (setting.value.toLowerCase() === 'es5') {
options.target = ts.ScriptTarget.ES5;
} else if (setting.value.toLowerCase() === 'es6') {
options.target = ts.ScriptTarget.ES6;
} else {
throw new Error('Unknown compile target ' + setting.value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harnessLanguageService.ts
Expand Up @@ -260,7 +260,7 @@ module Harness.LanguageService {

/** Parse file given its source text */
public parseSourceText(fileName: string, sourceText: TypeScript.IScriptSnapshot): TypeScript.SourceUnitSyntax {
return TypeScript.Parser.parse(fileName, TypeScript.SimpleText.fromScriptSnapshot(sourceText), ts.ScriptTarget.ES5, TypeScript.isDTSFile(fileName)).sourceUnit();
return TypeScript.Parser.parse(fileName, TypeScript.SimpleText.fromScriptSnapshot(sourceText), ts.ScriptTarget.Latest, TypeScript.isDTSFile(fileName)).sourceUnit();
}

/** Parse a file on disk given its fileName */
Expand Down
2 changes: 1 addition & 1 deletion src/services/compiler/precompile.ts
Expand Up @@ -184,7 +184,7 @@ module TypeScript {

export function preProcessFile(fileName: string, sourceText: IScriptSnapshot, readImportFiles = true): IPreProcessedFileInfo {
var text = SimpleText.fromScriptSnapshot(sourceText);
var scanner = Scanner.createScanner(ts.ScriptTarget.ES5, text, reportDiagnostic);
var scanner = Scanner.createScanner(ts.ScriptTarget.Latest, text, reportDiagnostic);

var firstToken = scanner.scan(/*allowRegularExpression:*/ false);

Expand Down
12 changes: 6 additions & 6 deletions src/services/services.ts
Expand Up @@ -77,7 +77,7 @@ module ts {
update(scriptSnapshot: TypeScript.IScriptSnapshot, version: string, isOpen: boolean, textChangeRange: TypeScript.TextChangeRange): SourceFile;
}

var scanner: Scanner = createScanner(ScriptTarget.ES5, /*skipTrivia*/ true);
var scanner: Scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ true);

var emptyArray: any[] = [];

Expand Down Expand Up @@ -1456,9 +1456,9 @@ module ts {
}

export function getDefaultCompilerOptions(): CompilerOptions {
// Set "ES5" target by default for language service
// Set "ScriptTarget.Latest" target by default for language service
return {
target: ScriptTarget.ES5,
target: ScriptTarget.Latest,
module: ModuleKind.None,
};
}
Expand Down Expand Up @@ -3794,8 +3794,8 @@ module ts {
// before and after it have to be a non-identifier char).
var endPosition = position + symbolNameLength;

if ((position === 0 || !isIdentifierPart(text.charCodeAt(position - 1), ScriptTarget.ES5)) &&
(endPosition === sourceLength || !isIdentifierPart(text.charCodeAt(endPosition), ScriptTarget.ES5))) {
if ((position === 0 || !isIdentifierPart(text.charCodeAt(position - 1), ScriptTarget.Latest)) &&
(endPosition === sourceLength || !isIdentifierPart(text.charCodeAt(endPosition), ScriptTarget.Latest))) {
// Found a real match. Keep searching.
positions.push(position);
}
Expand Down Expand Up @@ -5217,7 +5217,7 @@ module ts {

/// Classifier
export function createClassifier(host: Logger): Classifier {
var scanner = createScanner(ScriptTarget.ES5, /*skipTrivia*/ false);
var scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ false);

/// We do not have a full parser support to know when we should parse a regex or not
/// If we consider every slash token to be a regex, we could be missing cases like "1/2/3", where
Expand Down
3 changes: 3 additions & 0 deletions src/services/shims.ts
Expand Up @@ -174,6 +174,7 @@ module ts {
export enum LanguageVersion {
EcmaScript3 = 0,
EcmaScript5 = 1,
EcmaScript6 = 2,
}

export enum ModuleGenTarget {
Expand Down Expand Up @@ -213,6 +214,7 @@ module ts {
switch (languageVersion) {
case LanguageVersion.EcmaScript3: return ScriptTarget.ES3
case LanguageVersion.EcmaScript5: return ScriptTarget.ES5;
case LanguageVersion.EcmaScript6: return ScriptTarget.ES6;
default: throw Error("unsupported LanguageVersion value: " + languageVersion);
}
}
Expand All @@ -234,6 +236,7 @@ module ts {
switch (scriptTarget) {
case ScriptTarget.ES3: return LanguageVersion.EcmaScript3;
case ScriptTarget.ES5: return LanguageVersion.EcmaScript5;
case ScriptTarget.ES6: return LanguageVersion.EcmaScript6;

This comment has been minimized.

Copy link
@JsonFreeman

JsonFreeman Oct 19, 2014

Contributor

What is the difference between ScriptTarget and LanguageVersion?

default: throw Error("unsupported ScriptTarget value: " + scriptTarget);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/syntax/scanner.ts
Expand Up @@ -186,7 +186,7 @@ module TypeScript.Scanner {
var lastTokenInfo = { leadingTriviaWidth: -1, width: -1 };
var lastTokenInfoTokenID: number = -1;

var triviaScanner = createScannerInternal(ts.ScriptTarget.ES5, SimpleText.fromString(""), () => { });
var triviaScanner = createScannerInternal(ts.ScriptTarget.Latest, SimpleText.fromString(""), () => { });

interface IScannerToken extends ISyntaxToken {
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/syntax/unicode.ts
Expand Up @@ -84,7 +84,7 @@ module TypeScript {
if (languageVersion === ts.ScriptTarget.ES3) {
return Unicode.lookupInUnicodeMap(code, Unicode.unicodeES3IdentifierStart);
}
else if (languageVersion === ts.ScriptTarget.ES5) {
else if (languageVersion >= ts.ScriptTarget.ES5) {
return Unicode.lookupInUnicodeMap(code, Unicode.unicodeES5IdentifierStart);
}
else {
Expand All @@ -96,7 +96,7 @@ module TypeScript {
if (languageVersion === ts.ScriptTarget.ES3) {
return Unicode.lookupInUnicodeMap(code, Unicode.unicodeES3IdentifierPart);
}
else if (languageVersion === ts.ScriptTarget.ES5) {
else if (languageVersion >= ts.ScriptTarget.ES5) {
return Unicode.lookupInUnicodeMap(code, Unicode.unicodeES5IdentifierPart);
}
else {
Expand Down
31 changes: 31 additions & 0 deletions tests/baselines/reference/es6-amd.js
@@ -0,0 +1,31 @@
//// [es6-amd.ts]

class A
{
constructor ()
{

}

public B()
{
return 42;
}
}

//// [es6-amd.js]
var A = (function () {
function A() {
}
A.prototype.B = function () {
return 42;
};
return A;
})();

This comment has been minimized.

Copy link
@KirillGrishin

KirillGrishin Mar 11, 2015

Do I understand correctly that currently (TS1.4.1) even with target ES6 the output will not be ES6 classes?

This comment has been minimized.

Copy link
@ahejlsberg

ahejlsberg Mar 11, 2015

Member

That is correct, but 1.5 will support proper ES6 output (i.e. output a class as a class).

//# sourceMappingURL=es6-amd.js.map

//// [es6-amd.d.ts]
declare class A {
constructor();
B(): number;
}
2 changes: 2 additions & 0 deletions tests/baselines/reference/es6-amd.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions tests/baselines/reference/es6-amd.sourcemap.txt
@@ -0,0 +1,128 @@
===================================================================
JsFile: es6-amd.js
mapUrl: es6-amd.js.map
sourceRoot:
sources: es6-amd.ts
===================================================================
-------------------------------------------------------------------
emittedFile:tests/cases/compiler/es6-amd.js
sourceFile:es6-amd.ts
-------------------------------------------------------------------
>>>var A = (function () {
1 >
2 >^^^^
3 > ^
4 > ^^^^^^^^^^^^^^->
1 >
>
2 >class
3 > A
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
2 >Emitted(1, 5) Source(2, 7) + SourceIndex(0)
3 >Emitted(1, 6) Source(2, 8) + SourceIndex(0)
---
>>> function A() {
1->^^^^
2 > ^^^^^^^^^
3 > ^
1->
>{
>
2 >
3 > A
1->Emitted(2, 5) Source(4, 5) + SourceIndex(0) name (A)
2 >Emitted(2, 14) Source(2, 7) + SourceIndex(0) name (A)
3 >Emitted(2, 15) Source(2, 8) + SourceIndex(0) name (A)
---
>>> }
1 >^^^^
2 > ^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
>{
> constructor ()
> {
>
>
2 > }
1 >Emitted(3, 5) Source(7, 5) + SourceIndex(0) name (A.constructor)
2 >Emitted(3, 6) Source(7, 6) + SourceIndex(0) name (A.constructor)
---
>>> A.prototype.B = function () {
1->^^^^
2 > ^^^^^^^^^^^^^
3 > ^^^
1->
>
> public
2 > B
3 >
1->Emitted(4, 5) Source(9, 12) + SourceIndex(0) name (A)
2 >Emitted(4, 18) Source(9, 13) + SourceIndex(0) name (A)
3 >Emitted(4, 21) Source(9, 5) + SourceIndex(0) name (A)
---
>>> return 42;
1 >^^^^^^^^
2 > ^^^^^^
3 > ^
4 > ^^
5 > ^
1 >public B()
> {
>
2 > return
3 >
4 > 42
5 > ;
1 >Emitted(5, 9) Source(11, 9) + SourceIndex(0) name (A.B)
2 >Emitted(5, 15) Source(11, 15) + SourceIndex(0) name (A.B)
3 >Emitted(5, 16) Source(11, 16) + SourceIndex(0) name (A.B)
4 >Emitted(5, 18) Source(11, 18) + SourceIndex(0) name (A.B)
5 >Emitted(5, 19) Source(11, 19) + SourceIndex(0) name (A.B)
---
>>> };
1 >^^^^
2 > ^
3 > ^^^^^^^^^->
1 >
>
2 > }
1 >Emitted(6, 5) Source(12, 5) + SourceIndex(0) name (A.B)
2 >Emitted(6, 6) Source(12, 6) + SourceIndex(0) name (A.B)
---
>>> return A;
1->^^^^
2 > ^^^^^^^^
1->
>
2 > }
1->Emitted(7, 5) Source(13, 1) + SourceIndex(0) name (A)
2 >Emitted(7, 13) Source(13, 2) + SourceIndex(0) name (A)
---
>>>})();
1 >
2 >^
3 >
4 > ^^^^
5 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >}
3 >
4 > class A
> {
> constructor ()
> {
>
> }
>
> public B()
> {
> return 42;
> }
> }
1 >Emitted(8, 1) Source(13, 1) + SourceIndex(0) name (A)
2 >Emitted(8, 2) Source(13, 2) + SourceIndex(0) name (A)
3 >Emitted(8, 2) Source(2, 1) + SourceIndex(0)
4 >Emitted(8, 6) Source(13, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=es6-amd.js.map
16 changes: 16 additions & 0 deletions tests/baselines/reference/es6-amd.types
@@ -0,0 +1,16 @@
=== tests/cases/compiler/es6-amd.ts ===

class A
>A : A
{
constructor ()
{

}

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

0 comments on commit 873c1df

Please sign in to comment.