8000 Move RegExp flag version mapping to LanguageFeatureMinimumTarget by jakebailey · Pull Request #58311 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

Move RegExp flag version mapping to LanguageFeatureMinimumTarget #58311

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

Merged
merged 6 commits into from
Apr 25, 2024
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
Move RegExp flag version mapping to LanguageFeatureMinimumTarget
  • Loading branch information
jakebailey committed Apr 24, 2024
commit e96f25b44c0d186424bc4547705564b7360cea89
23 changes: 12 additions & 11 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
JSDocSyntaxKind,
JsxTokenSyntaxKind,
KeywordSyntaxKind,
LanguageFeatureMinimumTarget,
LanguageVariant,
last,
LineAndCharacter,
Expand Down Expand Up @@ -291,15 +292,15 @@ const charToRegExpFlag = new Map(Object.entries({
y: RegularExpressionFlags.Sticky,
}));

const regExpFlagToFirstAvailableLanguageVersion = new Map([
[RegularExpressionFlags.HasIndices, ScriptTarget.ES2022],
[RegularExpressionFlags.Global, ScriptTarget.ES3],
[RegularExpressionFlags.IgnoreCase, ScriptTarget.ES3],
[RegularExpressionFlags.Multiline, ScriptTarget.ES3],
[RegularExpressionFlags.DotAll, ScriptTarget.ES2018],
[RegularExpressionFlags.Unicode, ScriptTarget.ES2015],
[RegularExpressionFlags.UnicodeSets, ScriptTarget.ESNext],
[RegularExpressionFlags.Sticky, ScriptTarget.ES2015],
const regExpFlagToFirstAvailableLanguageVersion = new Map<RegularExpressionFlags, LanguageFeatureMinimumTarget>([
[RegularExpressionFlags.HasIndices, LanguageFeatureMinimumTarget.RegularExpressionFlagsHasIndices],
[RegularExpressionFlags.Global, LanguageFeatureMinimumTarget.RegularExpressionFlagsGlobal],
[RegularExpressionFlags.IgnoreCase, LanguageFeatureMinimumTarget.RegularExpressionFlagsIgnoreCase],
[RegularExpressionFlags.Multiline, LanguageFeatureMinimumTarget.RegularExpressionFlagsMultiline],
[RegularExpressionFlags.DotAll, LanguageFeatureMinimumTarget.RegularExpressionFlagsDotAll],
[RegularExpressionFlags.Unicode, LanguageFeatureMinimumTarget.RegularExpressionFlagsUnicode],
[RegularExpressionFlags.UnicodeSets, LanguageFeatureMinimumTarget.RegularExpressionFlagsUnicodeSets],
[RegularExpressionFlags.Sticky, LanguageFeatureMinimumTarget.RegularExpressionFlagsSticky],
]);

/*
Expand Down Expand Up @@ -2456,7 +2457,7 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
else {
regExpFlags |= flag;
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag)!;
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag)! as unknown as ScriptTarget;
if (languageVersion < availableFrom) {
error(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, p, 1, getNameOfScriptTarget(availableFrom));
}
Expand Down Expand Up @@ -2724,7 +2725,7 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
else {
currFlags |= flag;
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag)!;
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag)! as unknown as ScriptTarget;
if (languageVersion < availableFrom) {
error(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, pos, 1, getNameOfScriptTarget(availableFrom));
}
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8218,6 +8218,11 @@ export type EmitHelperUniqueNameCallback = (name: string) => string;
* @internal
*/
export const enum LanguageFeatureMinimumTarget {
// ES3 Features
RegularExpressionFlagsGlobal = ScriptTarget.ES3,
RegularExpressionFlagsIgnoreCase = ScriptTarget.ES3,
RegularExpressionFlagsMultiline = ScriptTarget.ES3,

// ES2015 Features
Classes = ScriptTarget.ES2015,
ForOf = ScriptTarget.ES2015,
Expand All @@ -8231,6 +8236,8 @@ export const enum LanguageFeatureMinimumTarget {
ArrowFunctions = ScriptTarget.ES2015,
BlockScopedVariables = ScriptTarget.ES2015,
ObjectAssign = ScriptTarget.ES2015,
RegularExpressionFlagsUnicode = ScriptTarget.ES2015,
RegularExpressionFlagsSticky = ScriptTarget.ES2015,

// ES2016 Features
Exponentiation = ScriptTarget.ES2016, // `x ** y`
Expand All @@ -8243,6 +8250,7 @@ export const enum LanguageFeatureMinimumTarget {
AsyncGenerators = ScriptTarget.ES2018, // `async function * f() { }`
AsyncIteration = ScriptTarget.ES2018, // `Symbol.asyncIterator`
ObjectSpreadRest = ScriptTarget.ES2018, // `{ ...obj }`
RegularExpressionFlagsDotAll = ScriptTarget.ES2018,

// ES2019 Features
BindinglessCatch = ScriptTarget.ES2019, // `try { } catch { }`
Expand All @@ -8259,9 +8267,11 @@ export const enum LanguageFeatureMinimumTarget {
TopLevelAwait = ScriptTarget.ES2022,
ClassFields = ScriptTarget.ES2022,
PrivateNamesAndClassStaticBlocks = ScriptTarget.ES2022, // `class C { static {} #x = y, #m() {} }`, `#x in y`
RegularExpressionFlagsHasIndices = ScriptTarget.ES2022,

// ES2023 Features
ShebangComments = ScriptTarget.ESNext,
RegularExpressionFlagsUnicodeSets = ScriptTarget.ESNext,

// Upcoming Features
// NOTE: We must reevaluate the target for upcoming features when each successive TC39 edition is ratified in
Expand Down
0