Skip to content

Commit 2c46f9b

Browse files
committed
Add ES8/ES2017 target (microsoft#10768)
1 parent ebb17e8 commit 2c46f9b

File tree

6 files changed

+47
-13
lines changed

6 files changed

+47
-13
lines changed

src/compiler/commandLineParser.ts

+2
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ namespace ts {
262262
"es3": ScriptTarget.ES3,
263263
"es5": ScriptTarget.ES5,
264264
"es6": ScriptTarget.ES6,
265+
"es8": ScriptTarget.ES8,
265266
"es2015": ScriptTarget.ES2015,
267+
"es2017": ScriptTarget.ES2017,
266268
}),
267269
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015,
268270
paramType: Diagnostics.VERSION,

src/compiler/core.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ namespace ts {
11911191
export function getEmitModuleKind(compilerOptions: CompilerOptions) {
11921192
return typeof compilerOptions.module === "number" ?
11931193
compilerOptions.module :
1194-
getEmitScriptTarget(compilerOptions) === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS;
1194+
getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS;
11951195
}
11961196

11971197
/* @internal */

src/compiler/emitter.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,10 @@ const _super = (function (geti, seti) {
21922192
helpersEmitted = true;
21932193
}
21942194

2195-
if (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions) {
2195+
// Only emit __awaiter function when target ES5/ES6.
2196+
// Only emit __generator function when target ES5.
2197+
// For target ES8 and above, we can emit async/await as is.
2198+
if ((languageVersion < ScriptTarget.ES8) && (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions)) {
21962199
writeLines(awaiterHelper);
21972200
if (languageVersion < ScriptTarget.ES6) {
21982201
writeLines(generatorHelper);

src/compiler/transformer.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ namespace ts {
115115
transformers.push(transformJsx);
116116
}
117117

118-
transformers.push(transformES7);
118+
if (languageVersion < ScriptTarget.ES8) {
119+
transformers.push(transformES7);
120+
}
119121

120122
if (languageVersion < ScriptTarget.ES6) {
121123
transformers.push(transformES6);
@@ -339,4 +341,4 @@ namespace ts {
339341
return statements;
340342
}
341343
}
342-
}
344+
}

src/compiler/transformers/ts.ts

+33-8
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,14 @@ namespace ts {
240240
// ES6 export and default modifiers are elided when inside a namespace.
241241
return currentNamespace ? undefined : node;
242242

243+
case SyntaxKind.AsyncKeyword:
244+
// Async keyword is not elided for target ES8
245+
return languageVersion < ScriptTarget.ES8 ? undefined : node;
246+
243247
case SyntaxKind.PublicKeyword:
244248
case SyntaxKind.PrivateKeyword:
245249
case SyntaxKind.ProtectedKeyword:
246250
case SyntaxKind.AbstractKeyword:
247-
case SyntaxKind.AsyncKeyword:
248251
case SyntaxKind.ConstKeyword:
249252
case SyntaxKind.DeclareKeyword:
250253
case SyntaxKind.ReadonlyKeyword:
@@ -2223,6 +2226,14 @@ namespace ts {
22232226
/*location*/ node
22242227
);
22252228

2229+
// Add ES8 async function expression modifier
2230+
// Not sure this is the right place? Might be better to move this
2231+
// into createFunctionExpression itself.
2232+
if ((languageVersion >= ScriptTarget.ES8) && isAsyncFunctionLike(node)) {
2233+
const funcModifiers = visitNodes(node.modifiers, visitor, isModifier);
2234+
func.modifiers = createNodeArray(funcModifiers);
2235+
}
2236+
22262237
setOriginalNode(func, node);
22272238

22282239
return func;
@@ -2235,7 +2246,7 @@ namespace ts {
22352246
*/
22362247
function visitArrowFunction(node: ArrowFunction) {
22372248
const func = createArrowFunction(
2238-
/*modifiers*/ undefined,
2249+
visitNodes(node.modifiers, visitor, isModifier),
22392250
/*typeParameters*/ undefined,
22402251
visitNodes(node.parameters, visitor, isParameter),
22412252
/*type*/ undefined,
@@ -2250,7 +2261,7 @@ namespace ts {
22502261
}
22512262

22522263
function transformFunctionBody(node: MethodDeclaration | AccessorDeclaration | FunctionDeclaration | FunctionExpression): FunctionBody {
2253-
if (isAsyncFunctionLike(node)) {
2264+
if (isAsyncFunctionLike(node) && languageVersion < ScriptTarget.ES8) {
22542265
return <FunctionBody>transformAsyncFunctionBody(node);
22552266
}
22562267

@@ -2270,7 +2281,7 @@ namespace ts {
22702281
}
22712282

22722283
function transformConciseBody(node: ArrowFunction): ConciseBody {
2273-
if (isAsyncFunctionLike(node)) {
2284+
if (isAsyncFunctionLike(node) && languageVersion < ScriptTarget.ES8) {
22742285
return transformAsyncFunctionBody(node);
22752286
}
22762287

@@ -2453,14 +2464,28 @@ namespace ts {
24532464
* @param node The await expression node.
24542465
*/
24552466
function visitAwaitExpression(node: AwaitExpression): Expression {
2467+
const targetAtLeastES8 = languageVersion >= ScriptTarget.ES8;
24562468
return setOriginalNode(
2457-
createYield(
2469+
targetAtLeastES8 ? createAwaitExpression() : createYieldExpression(),
2470+
node
2471+
);
2472+
2473+
function createAwaitExpression() {
2474+
const awaitExpression = createAwait(
2475+
visitNode(node.expression, visitor, isExpression),
2476+
/*location*/ node
2477+
);
2478+
return awaitExpression;
2479+
}
2480+
2481+
function createYieldExpression() {
2482+
const yieldExpression = createYield(
24582483
/*asteriskToken*/ undefined,
24592484
visitNode(node.expression, visitor, isExpression),
24602485
/*location*/ node
2461-
),
2462-
node
2463-
);
2486+
);
2487+
return yieldExpression;
2488+
}
24642489
}
24652490

24662491
/**

src/compiler/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2831,8 +2831,10 @@ namespace ts {
28312831
ES3 = 0,
28322832
ES5 = 1,
28332833
ES6 = 2,
2834+
ES8 = 3,
28342835
ES2015 = ES6,
2835-
Latest = ES6,
2836+
ES2017 = ES8,
2837+
Latest = ES8,
28362838
}
28372839

28382840
export const enum LanguageVariant {

0 commit comments

Comments
 (0)