Skip to content

Commit 193d3cc

Browse files
authored
Track minimum language version for various language features (#57670)
1 parent 1e982d8 commit 193d3cc

8 files changed

Lines changed: 151 additions & 43 deletions

File tree

src/compiler/checker.ts

Lines changed: 54 additions & 37 deletions
Large diffs are not rendered by default.

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ export const inverseJsxOptionMap = new Map(mapIterator(jsxOptionMap.entries(), (
144144
// order in the generated program (see `getDefaultLibPriority` in program.ts). This
145145
// order also affects overload resolution when a type declared in one lib is
146146
// augmented in another lib.
147+
// NOTE: We must reevaluate the target for upcoming features when each successive TC39 edition is ratified in
148+
// June of each year. This includes changes to `LanguageFeatureMinimumTarget`, `ScriptTarget`,
149+
// transformers/esnext.ts, commandLineParser.ts, and the contents of each lib/esnext.*.d.ts file.
147150
const libEntries: [string, string][] = [
148151
// JavaScript only
149152
["es5", "lib.es5.d.ts"],

src/compiler/transformers/esnext.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ const enum UsingKind {
7171

7272
/** @internal */
7373
export function transformESNext(context: TransformationContext): (x: SourceFile | Bundle) => SourceFile | Bundle {
74+
// NOTE: We must reevaluate the target for upcoming features when each successive TC39 edition is ratified in
75+
// June of each year. This includes changes to `LanguageFeatureMinimumTarget`, `ScriptTarget`,
76+
// transformers/esnext.ts, commandLineParser.ts, and the contents of each lib/esnext.*.d.ts file.
77+
7478
const {
7579
factory,
7680
getEmitHelperFactory: emitHelpers,

src/compiler/types.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7277,6 +7277,9 @@ export const enum ScriptKind {
72777277
Deferred = 7,
72787278
}
72797279

7280+
// NOTE: We must reevaluate the target for upcoming features when each successive TC39 edition is ratified in
7281+
// June of each year. This includes changes to `LanguageFeatureMinimumTarget`, `ScriptTarget`,
7282+
// transformers/esnext.ts, commandLineParser.ts, and the contents of each lib/esnext.*.d.ts file.
72807283
export const enum ScriptTarget {
72817284
/** @deprecated */
72827285
ES3 = 0,
@@ -8035,6 +8038,65 @@ export type UniqueNameHandler = (baseName: string, checkFn?: (name: string) => b
80358038

80368039
export type EmitHelperUniqueNameCallback = (name: string) => string;
80378040

8041+
/**
8042+
* Indicates the minimum `ScriptTarget` (inclusive) after which a specific language feature is no longer transpiled.
8043+
*
8044+
* @internal
8045+
*/
8046+
export const enum LanguageFeatureMinimumTarget {
8047+
// ES2015 Features
8048+
Classes = ScriptTarget.ES2015,
8049+
ForOf = ScriptTarget.ES2015,
8050+
Generators = ScriptTarget.ES2015,
8051+
Iteration = ScriptTarget.ES2015,
8052+
SpreadElements = ScriptTarget.ES2015,
8053+
RestElements = ScriptTarget.ES2015,
8054+
TaggedTemplates = ScriptTarget.ES2015,
8055+
DestructuringAssignment = ScriptTarget.ES2015,
8056+
BindingPatterns = ScriptTarget.ES2015,
8057+
ArrowFunctions = ScriptTarget.ES2015,
8058+
BlockScopedVariables = ScriptTarget.ES2015,
8059+
ObjectAssign = ScriptTarget.ES2015,
8060+
8061+
// ES2016 Features
8062+
Exponentiation = ScriptTarget.ES2016, // `x ** y`
8063+
8064+
// ES2017 Features
8065+
AsyncFunctions = ScriptTarget.ES2017, // `async function f() {}`
8066+
8067+
// ES2018 Features
8068+
ForAwaitOf = ScriptTarget.ES2018, // `for await (const x of y)`
8069+
AsyncGenerators = ScriptTarget.ES2018, // `async function * f() { }`
8070+
AsyncIteration = ScriptTarget.ES2018, // `Symbol.asyncIterator`
8071+
ObjectSpreadRest = ScriptTarget.ES2018, // `{ ...obj }`
8072+
8073+
// ES2019 Features
8074+
BindinglessCatch = ScriptTarget.ES2019, // `try { } catch { }`
8075+
8076+
// ES2020 Features
8077+
BigInt = ScriptTarget.ES2020, // `0n`
8078+
NullishCoalesce = ScriptTarget.ES2020, // `a ?? b`
8079+
OptionalChaining = ScriptTarget.ES2020, // `a?.b`
8080+
8081+
// ES2021 Features
8082+
LogicalAssignment = ScriptTarget.ES2021, // `a ||= b`, `a &&= b`, `a ??= b`
8083+
8084+
// ES2022 Features
8085+
TopLevelAwait = ScriptTarget.ES2022,
8086+
ClassFields = ScriptTarget.ES2022,
8087+
PrivateNamesAndClassStaticBlocks = ScriptTarget.ES2022, // `class C { static {} #x = y, #m() {} }`, `#x in y`
8088+
8089+
// ES2023 Features
8090+
ShebangComments = ScriptTarget.ESNext,
8091+
8092+
// Upcoming Features
8093+
// NOTE: We must reevaluate the target for upcoming features when each successive TC39 edition is ratified in
8094+
// June of each year. This includes changes to `LanguageFeatureMinimumTarget`, `ScriptTarget`,
8095+
// transformers/esnext.ts, commandLineParser.ts, and the contents of each lib/esnext.*.d.ts file.
8096+
UsingAndAwaitUsing = ScriptTarget.ESNext, // `using x = y`, `await using x = y`
8097+
ClassAndClassElementDecorators = ScriptTarget.ESNext, // `@dec class C {}`, `class C { @dec m() {} }`
8098+
}
8099+
80388100
// dprint-ignore
80398101
/**
80408102
* Used by the checker, this enum keeps track of external emit helpers that should be type

tests/baselines/reference/awaitUsingDeclarationsWithImportHelpers.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ async function f() {
88
}
99

1010
//// [awaitUsingDeclarationsWithImportHelpers.js]
11+
import { __addDisposableResource, __disposeResources } from "tslib";
1112
async function f() {
12-
await using a = null;
13+
const env_1 = { stack: [], error: void 0, hasError: false };
14+
try {
15+
const a = __addDisposableResource(env_1, null, true);
16+
}
17+
catch (e_1) {
18+
env_1.error = e_1;
19+
env_1.hasError = true;
20+
}
21+
finally {
22+
const result_1 = __disposeResources(env_1);
23+
if (result_1)
24+
await result_1;
25+
}
1326
}
14-
export {};

tests/baselines/reference/usingDeclarationsWithImportHelpers.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ export {};
88
}
99

1010
//// [usingDeclarationsWithImportHelpers.js]
11+
import { __addDisposableResource, __disposeResources } from "tslib";
1112
{
12-
using a = null;
13+
const env_1 = { stack: [], error: void 0, hasError: false };
14+
try {
15+
const a = __addDisposableResource(env_1, null, false);
16+
}
17+
catch (e_1) {
18+
env_1.error = e_1;
19+
env_1.hasError = true;
20+
}
21+
finally {
22+
__disposeResources(env_1);
23+
}
1324
}
14-
export {};

tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsWithImportHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @target: esnext
1+
// @target: es2022
22
// @module: esnext
33
// @lib: esnext
44
// @importHelpers: true

tests/cases/conformance/statements/VariableStatements/usingDeclarations/usingDeclarationsWithImportHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// @target: esnext
1+
// @target: es2022
22
// @module: esnext
33
// @lib: esnext
44
// @importHelpers: true

0 commit comments

Comments
 (0)