Skip to content
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

Move RegExp flag version mapping to LanguageFeatureMinimumTarget #58311

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 16 additions & 17 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 @@ -293,15 +294,12 @@ 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.DotAll, LanguageFeatureMinimumTarget.RegularExpressionFlagsDotAll],
[RegularExpressionFlags.Unicode, LanguageFeatureMinimumTarget.RegularExpressionFlagsUnicode],
[RegularExpressionFlags.UnicodeSets, LanguageFeatureMinimumTarget.RegularExpressionFlagsUnicodeSets],
[RegularExpressionFlags.Sticky, LanguageFeatureMinimumTarget.RegularExpressionFlagsSticky],
]);

/*
Expand Down Expand Up @@ -2461,10 +2459,7 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
else {
regExpFlags |= flag;
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag)!;
if (languageVersion < availableFrom) {
error(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, p, 1, getNameOfScriptTarget(availableFrom));
}
checkRegularExpressionFlagAvailable(flag, p);
}
}
p++;
Expand Down Expand Up @@ -2742,10 +2737,7 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
else {
currFlags |= flag;
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag)!;
if (languageVersion < availableFrom) {
error(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, pos, 1, getNameOfScriptTarget(availableFrom));
}
checkRegularExpressionFlagAvailable(flag, pos);
}
pos++;
}
Expand Down Expand Up @@ -3446,6 +3438,13 @@ export function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean
}
});
}

function checkRegularExpressionFlagAvailable(flag: RegularExpressionFlags, pos: number) {
const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag) as ScriptTarget | undefined;
if (availableFrom && languageVersion < availableFrom) {
error(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, pos, 1, getNameOfScriptTarget(availableFrom));
}
}
}

function appendIfCommentDirective(
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8231,6 +8231,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 +8245,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,6 +8262,7 @@ 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,
Expand All @@ -8269,6 +8273,7 @@ export const enum LanguageFeatureMinimumTarget {
// transformers/esnext.ts, commandLineParser.ts, and the contents of each lib/esnext.*.d.ts file.
UsingAndAwaitUsing = ScriptTarget.ESNext, // `using x = y`, `await using x = y`
ClassAndClassElementDecorators = ScriptTarget.ESNext, // `@dec class C {}`, `class C { @dec m() {} }`
RegularExpressionFlagsUnicodeSets = ScriptTarget.ESNext,
}

// dprint-ignore
Expand Down