Skip to content

Commit 2e0482a

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

File tree

6 files changed

+37
-17
lines changed

6 files changed

+37
-17
lines changed

src/compiler/commandLineParser.ts

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 1 deletion
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 (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions && (languageVersion < ScriptTarget.ES8)) {
21962199
writeLines(awaiterHelper);
21972200
if (languageVersion < ScriptTarget.ES6) {
21982201
writeLines(generatorHelper);

src/compiler/transformer.ts

Lines changed: 4 additions & 2 deletions
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

Lines changed: 23 additions & 12 deletions
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+
// ES8 async keyword
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:
@@ -2235,7 +2238,7 @@ namespace ts {
22352238
*/
22362239
function visitArrowFunction(node: ArrowFunction) {
22372240
const func = createArrowFunction(
2238-
/*modifiers*/ undefined,
2241+
visitNodes(node.modifiers, visitor, isModifier),
22392242
/*typeParameters*/ undefined,
22402243
visitNodes(node.parameters, visitor, isParameter),
22412244
/*type*/ undefined,
@@ -2250,7 +2253,7 @@ namespace ts {
22502253
}
22512254

22522255
function transformFunctionBody(node: MethodDeclaration | AccessorDeclaration | FunctionDeclaration | FunctionExpression): FunctionBody {
2253-
if (isAsyncFunctionLike(node)) {
2256+
if (isAsyncFunctionLike(node) && languageVersion < ScriptTarget.ES8) {
22542257
return <FunctionBody>transformAsyncFunctionBody(node);
22552258
}
22562259

@@ -2270,7 +2273,7 @@ namespace ts {
22702273
}
22712274

22722275
function transformConciseBody(node: ArrowFunction): ConciseBody {
2273-
if (isAsyncFunctionLike(node)) {
2276+
if (isAsyncFunctionLike(node) && languageVersion < ScriptTarget.ES8) {
22742277
return transformAsyncFunctionBody(node);
22752278
}
22762279

@@ -2453,14 +2456,22 @@ namespace ts {
24532456
* @param node The await expression node.
24542457
*/
24552458
function visitAwaitExpression(node: AwaitExpression): Expression {
2456-
return setOriginalNode(
2457-
createYield(
2458-
/*asteriskToken*/ undefined,
2459-
visitNode(node.expression, visitor, isExpression),
2460-
/*location*/ node
2461-
),
2462-
node
2463-
);
2459+
return languageVersion >= ScriptTarget.ES8
2460+
? setOriginalNode(
2461+
createAwait(
2462+
visitNode(node.expression, visitor, isExpression),
2463+
/*location*/ node
2464+
),
2465+
node
2466+
)
2467+
: setOriginalNode(
2468+
createYield(
2469+
/*asteriskToken*/ undefined,
2470+
visitNode(node.expression, visitor, isExpression),
2471+
/*location*/ node
2472+
),
2473+
node
2474+
);
24642475
}
24652476

24662477
/**

src/compiler/types.ts

Lines changed: 3 additions & 1 deletion
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)