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

Add ES2017 target #11407

Merged
merged 15 commits into from
Oct 15, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ namespace ts {
"es3": ScriptTarget.ES3,
"es5": ScriptTarget.ES5,
"es6": ScriptTarget.ES6,
"es8": ScriptTarget.ES8,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Officially there is no ES8, it is ES2017, so i would not add ES8 here or anywhere else. i would just leave it to ES2017

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add ES2016. it is a bit funny that we skip one ES 2016 (though not very useful but should be there for consistency).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to update getDefaultLibFileName to inject the right library based on the target.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"es2015": ScriptTarget.ES2015,
"es2017": ScriptTarget.ES2017,
}),
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015,
paramType: Diagnostics.VERSION,
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ namespace ts {
export function getEmitModuleKind(compilerOptions: CompilerOptions) {
return typeof compilerOptions.module === "number" ?
compilerOptions.module :
getEmitScriptTarget(compilerOptions) === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS;
getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS;
}

/* @internal */
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2192,7 +2192,10 @@ const _super = (function (geti, seti) {
helpersEmitted = true;
}

if (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions) {
// Only emit __awaiter function when target ES5/ES6.
// Only emit __generator function when target ES5.
// For target ES8 and above, we can emit async/await as is.
if ((languageVersion < ScriptTarget.ES8) && (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions)) {
writeLines(awaiterHelper);
if (languageVersion < ScriptTarget.ES6) {
writeLines(generatorHelper);
Expand Down
6 changes: 4 additions & 2 deletions src/compiler/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ namespace ts {
transformers.push(transformJsx);
}

transformers.push(transformES7);
if (languageVersion < ScriptTarget.ES8) {
transformers.push(transformES7);
}

if (languageVersion < ScriptTarget.ES6) {
transformers.push(transformES6);
Expand Down Expand Up @@ -339,4 +341,4 @@ namespace ts {
return statements;
}
}
}
}
41 changes: 33 additions & 8 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,14 @@ namespace ts {
// ES6 export and default modifiers are elided when inside a namespace.
return currentNamespace ? undefined : node;

case SyntaxKind.AsyncKeyword:
// Async keyword is not elided for target ES8
return languageVersion < ScriptTarget.ES8 ? undefined : node;

case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.AbstractKeyword:
case SyntaxKind.AsyncKeyword:
case SyntaxKind.ConstKeyword:
case SyntaxKind.DeclareKeyword:
case SyntaxKind.ReadonlyKeyword:
Expand Down Expand Up @@ -2223,6 +2226,14 @@ namespace ts {
/*location*/ node
);

// Add ES8 async function expression modifier
// Not sure this is the right place? Might be better to move this
// into createFunctionExpression itself.
if ((languageVersion >= ScriptTarget.ES8) && isAsyncFunctionLike(node)) {
const funcModifiers = visitNodes(node.modifiers, visitor, isModifier);
func.modifiers = createNodeArray(funcModifiers);
}

setOriginalNode(func, node);

return func;
Expand All @@ -2235,7 +2246,7 @@ namespace ts {
*/
function visitArrowFunction(node: ArrowFunction) {
const func = createArrowFunction(
/*modifiers*/ undefined,
visitNodes(node.modifiers, visitor, isModifier),
/*typeParameters*/ undefined,
visitNodes(node.parameters, visitor, isParameter),
/*type*/ undefined,
Expand All @@ -2250,7 +2261,7 @@ namespace ts {
}

function transformFunctionBody(node: MethodDeclaration | AccessorDeclaration | FunctionDeclaration | FunctionExpression): FunctionBody {
if (isAsyncFunctionLike(node)) {
if (isAsyncFunctionLike(node) && languageVersion < ScriptTarget.ES8) {
return <FunctionBody>transformAsyncFunctionBody(node);
}

Expand All @@ -2270,7 +2281,7 @@ namespace ts {
}

function transformConciseBody(node: ArrowFunction): ConciseBody {
if (isAsyncFunctionLike(node)) {
if (isAsyncFunctionLike(node) && languageVersion < ScriptTarget.ES8) {
return transformAsyncFunctionBody(node);
}

Expand Down Expand Up @@ -2453,14 +2464,28 @@ namespace ts {
* @param node The await expression node.
*/
function visitAwaitExpression(node: AwaitExpression): Expression {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async/await should be split into its own transform, es2017.ts, then we do not need to run it in this case, just like ES7 transform.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove this function altogether.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const targetAtLeastES8 = languageVersion >= ScriptTarget.ES8;
return setOriginalNode(
createYield(
targetAtLeastES8 ? createAwaitExpression() : createYieldExpression(),
node
);

function createAwaitExpression() {
const awaitExpression = createAwait(
visitNode(node.expression, visitor, isExpression),
/*location*/ node
);
return awaitExpression;
}

function createYieldExpression() {
const yieldExpression = createYield(
/*asteriskToken*/ undefined,
visitNode(node.expression, visitor, isExpression),
/*location*/ node
),
node
);
);
return yieldExpression;
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2831,8 +2831,10 @@ namespace ts {
ES3 = 0,
ES5 = 1,
ES6 = 2,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is minor, but can you make ES2015=2 and ES6=ES2015 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe even delete ES6 if it's not used any more. I can't remember if this has anything to do with command line parsing, so it might be hard to tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only place where ES6 is used (other than comments) is in command line/compiler options parsing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed ES6 completely

ES8 = 3,
ES2015 = ES6,
Latest = ES6,
ES2017 = ES8,
Latest = ES8,
}

export const enum LanguageVariant {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/unittests/commandLineParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ namespace ts {
start: undefined,
length: undefined,
}, {
messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'",
messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es8', 'es2015', 'es2017'",
category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category,
code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code,

Expand Down
2 changes: 1 addition & 1 deletion src/harness/unittests/convertCompilerOptionsFromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace ts {
file: undefined,
start: 0,
length: 0,
messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'",
messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es8', 'es2015', 'es2017'",
code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code,
category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category
}]
Expand Down
87 changes: 87 additions & 0 deletions tests/baselines/reference/es8-async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//// [es8-async.ts]

async (): Promise<void> => {
await 0;
}

async function asyncFunc() {
await 0;
}

const asycnArrowFunc = async (): Promise<void> => {
await 0;
}

async function asyncIIFE() {
await 0;

await (async function(): Promise<void> {
await 1;
})();

await (async function asyncNamedFunc(): Promise<void> {
await 1;
})();

await (async (): Promise<void> => {
await 1;
})();
}

class AsyncClass {
asyncPropFunc = async function(): Promise<void> {
await 2;
}

asyncPropNamedFunc = async function namedFunc(): Promise<void> {
await 2;
}

asyncPropArrowFunc = async (): Promise<void> => {
await 2;
}

async asyncMethod(): Promise<void> {
await 2;
}
}


//// [es8-async.js]
async () => {
await 0;
};
async function asyncFunc() {
await 0;
}
const asycnArrowFunc = async () => {
await 0;
};
async function asyncIIFE() {
await 0;
await (async function () {
await 1;
})();
await (async function asyncNamedFunc() {
await 1;
})();
await (async () => {
await 1;
})();
}
class AsyncClass {
constructor() {
this.asyncPropFunc = async function () {
await 2;
};
this.asyncPropNamedFunc = async function namedFunc() {
await 2;
};
this.asyncPropArrowFunc = async () => {
await 2;
};
}
async asyncMethod() {
await 2;
}
}
79 changes: 79 additions & 0 deletions tests/baselines/reference/es8-async.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
=== tests/cases/compiler/es8-async.ts ===

async (): Promise<void> => {
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 0;
}

async function asyncFunc() {
>asyncFunc : Symbol(asyncFunc, Decl(es8-async.ts, 3, 1))

await 0;
}

const asycnArrowFunc = async (): Promise<void> => {
>asycnArrowFunc : Symbol(asycnArrowFunc, Decl(es8-async.ts, 9, 5))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 0;
}

async function asyncIIFE() {
>asyncIIFE : Symbol(asyncIIFE, Decl(es8-async.ts, 11, 1))

await 0;

await (async function(): Promise<void> {
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 1;
})();

await (async function asyncNamedFunc(): Promise<void> {
>asyncNamedFunc : Symbol(asyncNamedFunc, Decl(es8-async.ts, 20, 11))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 1;
})();

await (async (): Promise<void> => {
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 1;
})();
}

class AsyncClass {
>AsyncClass : Symbol(AsyncClass, Decl(es8-async.ts, 27, 1))

asyncPropFunc = async function(): Promise<void> {
>asyncPropFunc : Symbol(AsyncClass.asyncPropFunc, Decl(es8-async.ts, 29, 18))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 2;
}

asyncPropNamedFunc = async function namedFunc(): Promise<void> {
>asyncPropNamedFunc : Symbol(AsyncClass.asyncPropNamedFunc, Decl(es8-async.ts, 32, 5))
>namedFunc : Symbol(namedFunc, Decl(es8-async.ts, 34, 24))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 2;
}

asyncPropArrowFunc = async (): Promise<void> => {
>asyncPropArrowFunc : Symbol(AsyncClass.asyncPropArrowFunc, Decl(es8-async.ts, 36, 5))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 2;
}

async asyncMethod(): Promise<void> {
>asyncMethod : Symbol(AsyncClass.asyncMethod, Decl(es8-async.ts, 40, 5))
>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --))

await 2;
}
}

Loading