Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add ES8/ES2017 target (#10768)
  • Loading branch information
andrejbaran committed Oct 5, 2016
commit 2c46f9b6870348bd14973c1e5abb2f3ba893c084
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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