From b574864abc989d0f8b15367baea1058819e126ba Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Thu, 25 Apr 2024 05:14:28 +0000 Subject: [PATCH] Update LKG --- lib/tsc.js | 183 +++++++++++++++++++++++++++++++++------------- lib/typescript.js | 183 +++++++++++++++++++++++++++++++++------------- 2 files changed, 266 insertions(+), 100 deletions(-) diff --git a/lib/tsc.js b/lib/tsc.js index 768ac687b0b5b..06f7f5ec973cc 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -6206,8 +6206,8 @@ var Diagnostics = { Unicode_property_value_expressions_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v_flag_is_set: diag(1530, 1 /* Error */, "Unicode_property_value_expressions_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v__1530", "Unicode property value expressions are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set."), _0_must_be_followed_by_a_Unicode_property_value_expression_enclosed_in_braces: diag(1531, 1 /* Error */, "_0_must_be_followed_by_a_Unicode_property_value_expression_enclosed_in_braces_1531", "'\\{0}' must be followed by a Unicode property value expression enclosed in braces."), There_is_no_capturing_group_named_0_in_this_regular_expression: diag(1532, 1 /* Error */, "There_is_no_capturing_group_named_0_in_this_regular_expression_1532", "There is no capturing group named '{0}' in this regular expression."), - A_decimal_escape_must_refer_to_an_existent_capturing_group_There_are_only_0_capturing_groups_in_this_regular_expression: diag(1533, 1 /* Error */, "A_decimal_escape_must_refer_to_an_existent_capturing_group_There_are_only_0_capturing_groups_in_this_1533", "A decimal escape must refer to an existent capturing group. There are only {0} capturing groups in this regular expression."), - Decimal_escapes_are_invalid_when_there_are_no_capturing_groups_in_a_regular_expression: diag(1534, 1 /* Error */, "Decimal_escapes_are_invalid_when_there_are_no_capturing_groups_in_a_regular_expression_1534", "Decimal escapes are invalid when there are no capturing groups in a regular expression."), + This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_regular_expression: diag(1533, 1 /* Error */, "This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_r_1533", "This backreference refers to a group that does not exist. There are only {0} capturing groups in this regular expression."), + This_backreference_is_invalid_because_the_containing_regular_expression_contains_no_capturing_groups: diag(1534, 1 /* Error */, "This_backreference_is_invalid_because_the_containing_regular_expression_contains_no_capturing_groups_1534", "This backreference is invalid because the containing regular expression contains no capturing groups."), This_character_cannot_be_escaped_in_a_regular_expression: diag(1535, 1 /* Error */, "This_character_cannot_be_escaped_in_a_regular_expression_1535", "This character cannot be escaped in a regular expression."), The_types_of_0_are_incompatible_between_these_types: diag(2200, 1 /* Error */, "The_types_of_0_are_incompatible_between_these_types_2200", "The types of '{0}' are incompatible between these types."), The_types_returned_by_0_are_incompatible_between_these_types: diag(2201, 1 /* Error */, "The_types_returned_by_0_are_incompatible_between_these_types_2201", "The types returned by '{0}' are incompatible between these types."), @@ -9012,7 +9012,9 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan tokenFlags |= 2048 /* ContainsInvalidEscape */; if (isRegularExpression || shouldEmitInvalidEscapeError) { const code = parseInt(text.substring(start2 + 1, pos), 8); - error(Diagnostics.Octal_escape_sequences_are_not_allowed_Use_the_syntax_0, start2, pos - start2, "\\x" + code.toString(16).padStart(2, "0")); + if (isRegularExpression !== "annex-b") { + error(Diagnostics.Octal_escape_sequences_are_not_allowed_Use_the_syntax_0, start2, pos - start2, "\\x" + code.toString(16).padStart(2, "0")); + } return String.fromCharCode(code); } return text.substring(start2, pos); @@ -9043,7 +9045,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan case 117 /* u */: if ((!isRegularExpression || shouldEmitInvalidEscapeError) && pos < end && text.charCodeAt(pos) === 123 /* openBrace */) { pos -= 2; - return scanExtendedUnicodeEscape(isRegularExpression || shouldEmitInvalidEscapeError); + return scanExtendedUnicodeEscape(!!isRegularExpression || shouldEmitInvalidEscapeError); } for (; pos < start2 + 6; pos++) { if (!(pos < end && isHexDigit(text.charCodeAt(pos)))) { @@ -9093,7 +9095,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan case 8233 /* paragraphSeparator */: return ""; default: - if (isRegularExpression && (shouldEmitInvalidEscapeError || isIdentifierPart(ch, languageVersion))) { + if (isRegularExpression === true && (shouldEmitInvalidEscapeError || isIdentifierPart(ch, languageVersion))) { error(Diagnostics.This_character_cannot_be_escaped_in_a_regular_expression, pos - 2, 2); } return String.fromCharCode(ch); @@ -9788,7 +9790,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan pos = tokenStart + 1; return token = 64 /* EqualsToken */; } - function reScanSlashToken() { + function reScanSlashToken(reportErrors2) { if (token === 44 /* SlashToken */ || token === 69 /* SlashEqualsToken */) { let p = tokenStart + 1; let inEscape = false; @@ -9827,38 +9829,54 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan if (!isIdentifierPart(ch, languageVersion)) { break; } - const flag = characterToRegularExpressionFlag(String.fromCharCode(ch)); - if (flag === void 0) { - error(Diagnostics.Unknown_regular_expression_flag, p, 1); - } else if (regExpFlags & flag) { - error(Diagnostics.Duplicate_regular_expression_flag, p, 1); - } else if (((regExpFlags | flag) & 96 /* UnicodeMode */) === 96 /* UnicodeMode */) { - error(Diagnostics.The_Unicode_u_flag_and_the_Unicode_Sets_v_flag_cannot_be_set_simultaneously, p, 1); - } 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)); + if (reportErrors2) { + const flag = characterToRegularExpressionFlag(String.fromCharCode(ch)); + if (flag === void 0) { + error(Diagnostics.Unknown_regular_expression_flag, p, 1); + } else if (regExpFlags & flag) { + error(Diagnostics.Duplicate_regular_expression_flag, p, 1); + } else if (((regExpFlags | flag) & 96 /* UnicodeMode */) === 96 /* UnicodeMode */) { + error(Diagnostics.The_Unicode_u_flag_and_the_Unicode_Sets_v_flag_cannot_be_set_simultaneously, p, 1); + } 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)); + } } } p++; } - pos = tokenStart + 1; - const saveTokenPos = tokenStart; - const saveTokenFlags = tokenFlags; - scanRegularExpressionWorker(text, endOfBody, regExpFlags, isUnterminated); - if (!isUnterminated) { + if (reportErrors2) { + pos = tokenStart + 1; + const saveTokenPos = tokenStart; + const saveTokenFlags = tokenFlags; + scanRegularExpressionWorker( + text, + endOfBody, + regExpFlags, + isUnterminated, + /*annexB*/ + true + ); + if (!isUnterminated) { + pos = p; + } + tokenStart = saveTokenPos; + tokenFlags = saveTokenFlags; + } else { pos = p; } - tokenStart = saveTokenPos; - tokenFlags = saveTokenFlags; tokenValue = text.substring(tokenStart, pos); token = 14 /* RegularExpressionLiteral */; } return token; - function scanRegularExpressionWorker(text2, end2, regExpFlags, isUnterminated) { - const unicodeMode = !!(regExpFlags & 96 /* UnicodeMode */); + function scanRegularExpressionWorker(text2, end2, regExpFlags, isUnterminated, annexB) { const unicodeSetsMode = !!(regExpFlags & 64 /* UnicodeSets */); + const unicodeMode = !!(regExpFlags & 96 /* UnicodeMode */); + if (unicodeMode) { + annexB = false; + } let mayContainStrings = false; let numberOfCapturingGroups = 0; const groupSpecifiers = /* @__PURE__ */ new Set(); @@ -9909,7 +9927,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan case 61 /* equals */: case 33 /* exclamation */: pos++; - isPreviousTermQuantifiable = false; + isPreviousTermQuantifiable = annexB; break; case 60 /* lessThan */: const groupNameStart = pos; @@ -10088,7 +10106,10 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan break; } default: - Debug.assert(scanCharacterClassEscape() || scanDecimalEscape() || scanCharacterEscape()); + Debug.assert(scanCharacterClassEscape() || scanDecimalEscape() || scanCharacterEscape( + /*atomEscape*/ + true + )); break; } } @@ -10103,7 +10124,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan } return false; } - function scanCharacterEscape() { + function scanCharacterEscape(atomEscape) { Debug.assertEqual(text2.charCodeAt(pos - 1), 92 /* backslash */); let ch = text2.charCodeAt(pos); switch (ch) { @@ -10116,6 +10137,9 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan } if (unicodeMode) { error(Diagnostics.c_must_be_followed_by_an_ASCII_letter, pos - 2, 2); + } else if (atomEscape && annexB) { + pos--; + return "\\"; } return String.fromCharCode(ch); case 94 /* caret */: @@ -10145,7 +10169,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan /*shouldEmitInvalidEscapeError*/ unicodeMode, /*isRegularExpression*/ - true + annexB ? "annex-b" : true ); } } @@ -10185,12 +10209,12 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan if (isClassContentExit(ch2)) { return; } - if (!minCharacter) { + if (!minCharacter && !annexB) { error(Diagnostics.A_character_class_range_must_not_be_bounded_by_another_character_class, minStart, pos - 1 - minStart); } const maxStart = pos; const maxCharacter = scanClassAtom(); - if (!maxCharacter) { + if (!maxCharacter && !annexB) { error(Diagnostics.A_character_class_range_must_not_be_bounded_by_another_character_class, maxStart, pos - maxStart); continue; } @@ -10473,7 +10497,10 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan pos++; return String.fromCharCode(ch2); default: - return scanCharacterEscape(); + return scanCharacterEscape( + /*atomEscape*/ + false + ); } } else if (ch === text2.charCodeAt(pos + 1)) { switch (ch) { @@ -10530,7 +10557,10 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan if (scanCharacterClassEscape()) { return ""; } - return scanCharacterEscape(); + return scanCharacterEscape( + /*atomEscape*/ + false + ); } } else { return scanSourceCharacter(); @@ -10645,11 +10675,11 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan } }); forEach(decimalEscapes, (escape) => { - if (escape.value > numberOfCapturingGroups) { + if (!annexB && escape.value > numberOfCapturingGroups) { if (numberOfCapturingGroups) { - error(Diagnostics.A_decimal_escape_must_refer_to_an_existent_capturing_group_There_are_only_0_capturing_groups_in_this_regular_expression, escape.pos, escape.end - escape.pos, numberOfCapturingGroups); + error(Diagnostics.This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_regular_expression, escape.pos, escape.end - escape.pos, numberOfCapturingGroups); } else { - error(Diagnostics.Decimal_escapes_are_invalid_when_there_are_no_capturing_groups_in_a_regular_expression, escape.pos, escape.end - escape.pos); + error(Diagnostics.This_backreference_is_invalid_because_the_containing_regular_expression_contains_no_capturing_groups, escape.pos, escape.end - escape.pos); } } }); @@ -45252,6 +45282,7 @@ function createTypeChecker(host) { var requestedExternalEmitHelperNames = /* @__PURE__ */ new Set(); var requestedExternalEmitHelpers; var externalHelpersModule; + var scanner; var Symbol12 = objectAllocator.getSymbolConstructor(); var Type7 = objectAllocator.getTypeConstructor(); var Signature5 = objectAllocator.getSignatureConstructor(); @@ -50111,7 +50142,8 @@ function createTypeChecker(host) { deepCloneOrReuseNode, /*context*/ void 0, - deepCloneOrReuseNodes + deepCloneOrReuseNodes, + deepCloneOrReuseNode )), node); } function deepCloneOrReuseNodes(nodes, visitor, test, start, count) { @@ -71090,6 +71122,53 @@ function createTypeChecker(host) { return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList); } } + function checkGrammarRegularExpressionLiteral(node) { + const sourceFile = getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + let lastError; + scanner ?? (scanner = createScanner( + 99 /* ESNext */, + /*skipTrivia*/ + true + )); + scanner.setScriptTarget(sourceFile.languageVersion); + scanner.setLanguageVariant(sourceFile.languageVariant); + scanner.setOnError((message, length2, arg0) => { + const start = scanner.getTokenEnd(); + if (message.category === 3 /* Message */ && lastError && start === lastError.start && length2 === lastError.length) { + const error2 = createDetachedDiagnostic(sourceFile.fileName, sourceFile.text, start, length2, message, arg0); + addRelatedInfo(lastError, error2); + } else if (!lastError || start !== lastError.start) { + lastError = createFileDiagnostic(sourceFile, start, length2, message, arg0); + diagnostics.add(lastError); + } + }); + scanner.setText(sourceFile.text, node.pos, node.end - node.pos); + try { + scanner.scan(); + Debug.assert(scanner.reScanSlashToken( + /*reportErrors*/ + true + ) === 14 /* RegularExpressionLiteral */, "Expected scanner to rescan RegularExpressionLiteral"); + return !!lastError; + } finally { + scanner.setText(""); + scanner.setOnError( + /*onError*/ + void 0 + ); + } + } + return false; + } + function checkRegularExpressionLiteral(node) { + const nodeLinks2 = getNodeLinks(node); + if (!(nodeLinks2.flags & 1 /* TypeChecked */)) { + nodeLinks2.flags |= 1 /* TypeChecked */; + addLazyDiagnostic(() => checkGrammarRegularExpressionLiteral(node)); + } + return globalRegExpType; + } function checkSpreadExpression(node, checkMode) { if (languageVersion < 2 /* SpreadElements */) { checkExternalEmitHelpers(node, compilerOptions.downlevelIteration ? 1536 /* SpreadIncludes */ : 1024 /* SpreadArray */); @@ -76993,7 +77072,7 @@ function createTypeChecker(host) { parent = parent.parent; } if (operator === 56 /* AmpersandAmpersandToken */ || isIfStatement(parent)) { - checkTestingKnownTruthyCallableOrAwaitableType(node.left, leftType, isIfStatement(parent) ? parent.thenStatement : void 0); + checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.left, leftType, isIfStatement(parent) ? parent.thenStatement : void 0); } checkTruthinessOfType(leftType, node.left); } @@ -77551,7 +77630,7 @@ function createTypeChecker(host) { } function checkConditionalExpression(node, checkMode) { const type = checkTruthinessExpression(node.condition, checkMode); - checkTestingKnownTruthyCallableOrAwaitableType(node.condition, type, node.whenTrue); + checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.condition, type, node.whenTrue); const type1 = checkExpression(node.whenTrue, checkMode); const type2 = checkExpression(node.whenFalse, checkMode); return getUnionType([type1, type2], 2 /* Subtype */); @@ -78024,7 +78103,7 @@ function createTypeChecker(host) { case 228 /* TemplateExpression */: return checkTemplateExpression(node); case 14 /* RegularExpressionLiteral */: - return globalRegExpType; + return checkRegularExpressionLiteral(node); case 209 /* ArrayLiteralExpression */: return checkArrayLiteral(node, checkMode, forceTuple); case 210 /* ObjectLiteralExpression */: @@ -80599,14 +80678,14 @@ function createTypeChecker(host) { function checkIfStatement(node) { checkGrammarStatementInAmbientContext(node); const type = checkTruthinessExpression(node.expression); - checkTestingKnownTruthyCallableOrAwaitableType(node.expression, type, node.thenStatement); + checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.expression, type, node.thenStatement); checkSourceElement(node.thenStatement); if (node.thenStatement.kind === 242 /* EmptyStatement */) { error(node.thenStatement, Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement); } checkSourceElement(node.elseStatement); } - function checkTestingKnownTruthyCallableOrAwaitableType(condExpr, condType, body) { + function checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(condExpr, condType, body) { if (!strictNullChecks) return; bothHelper(condExpr, body); @@ -80628,6 +80707,10 @@ function createTypeChecker(host) { return; } const type = location === condExpr2 ? condType : checkTruthinessExpression(location); + if (type.flags & 1024 /* EnumLiteral */ && isPropertyAccessExpression(location) && (getNodeLinks(location.expression).resolvedSymbol ?? unknownSymbol).flags & 384 /* Enum */) { + error(location, Diagnostics.This_condition_will_always_return_0, !!type.value ? "true" : "false"); + return; + } const isPropertyExpressionCast = isPropertyAccessExpression(location) && isTypeAssertion(location.expression); if (!hasTypeFacts(type, 4194304 /* Truthy */) || isPropertyExpressionCast) return; @@ -83073,7 +83156,7 @@ function createTypeChecker(host) { if (checkGrammarModuleElementContext(node, isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { return; } - if (!checkGrammarModifiers(node) && hasEffectiveModifiers(node)) { + if (!checkGrammarModifiers(node) && node.modifiers) { grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers); } if (checkExternalImportOrExportDeclaration(node)) { @@ -121313,7 +121396,7 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config } return Version.zero; } - function checkDeprecations(deprecatedIn, removedIn, createDiagnostic, fn) { + function checkDeprecations(deprecatedIn, stopsWorkingIn, removedIn, createDiagnostic, fn) { const deprecatedInVersion = new Version(deprecatedIn); const removedInVersion = new Version(removedIn); const typescriptVersion = new Version(typeScriptVersion2 || versionMajorMinor); @@ -121330,9 +121413,9 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config } } else { if (value === void 0) { - createDiagnostic(name, value, useInstead, Diagnostics.Option_0_is_deprecated_and_will_stop_functioning_in_TypeScript_1_Specify_compilerOption_ignoreDeprecations_Colon_2_to_silence_this_error, name, removedIn, deprecatedIn); + createDiagnostic(name, value, useInstead, Diagnostics.Option_0_is_deprecated_and_will_stop_functioning_in_TypeScript_1_Specify_compilerOption_ignoreDeprecations_Colon_2_to_silence_this_error, name, stopsWorkingIn, deprecatedIn); } else { - createDiagnostic(name, value, useInstead, Diagnostics.Option_0_1_is_deprecated_and_will_stop_functioning_in_TypeScript_2_Specify_compilerOption_ignoreDeprecations_Colon_3_to_silence_this_error, name, value, removedIn, deprecatedIn); + createDiagnostic(name, value, useInstead, Diagnostics.Option_0_1_is_deprecated_and_will_stop_functioning_in_TypeScript_2_Specify_compilerOption_ignoreDeprecations_Colon_3_to_silence_this_error, name, value, stopsWorkingIn, deprecatedIn); } } }); @@ -121368,7 +121451,7 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config ); } } - checkDeprecations("5.0", "5.5", createDiagnostic, (createDeprecatedDiagnostic) => { + checkDeprecations("5.0", "5.5", "6.0", createDiagnostic, (createDeprecatedDiagnostic) => { if (options.target === 0 /* ES3 */) { createDeprecatedDiagnostic("target", "ES3"); } @@ -121420,7 +121503,7 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config function createDiagnostic(_name, _value, _useInstead, message, ...args) { createDiagnosticForReference(parentFile, index, message, ...args); } - checkDeprecations("5.0", "5.5", createDiagnostic, (createDeprecatedDiagnostic) => { + checkDeprecations("5.0", "5.5", "6.0", createDiagnostic, (createDeprecatedDiagnostic) => { if (ref.prepend) { createDeprecatedDiagnostic("prepend"); } diff --git a/lib/typescript.js b/lib/typescript.js index bdb872bfc1f9c..6b52dd6758621 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -9813,8 +9813,8 @@ var Diagnostics = { Unicode_property_value_expressions_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v_flag_is_set: diag(1530, 1 /* Error */, "Unicode_property_value_expressions_are_only_available_when_the_Unicode_u_flag_or_the_Unicode_Sets_v__1530", "Unicode property value expressions are only available when the Unicode (u) flag or the Unicode Sets (v) flag is set."), _0_must_be_followed_by_a_Unicode_property_value_expression_enclosed_in_braces: diag(1531, 1 /* Error */, "_0_must_be_followed_by_a_Unicode_property_value_expression_enclosed_in_braces_1531", "'\\{0}' must be followed by a Unicode property value expression enclosed in braces."), There_is_no_capturing_group_named_0_in_this_regular_expression: diag(1532, 1 /* Error */, "There_is_no_capturing_group_named_0_in_this_regular_expression_1532", "There is no capturing group named '{0}' in this regular expression."), - A_decimal_escape_must_refer_to_an_existent_capturing_group_There_are_only_0_capturing_groups_in_this_regular_expression: diag(1533, 1 /* Error */, "A_decimal_escape_must_refer_to_an_existent_capturing_group_There_are_only_0_capturing_groups_in_this_1533", "A decimal escape must refer to an existent capturing group. There are only {0} capturing groups in this regular expression."), - Decimal_escapes_are_invalid_when_there_are_no_capturing_groups_in_a_regular_expression: diag(1534, 1 /* Error */, "Decimal_escapes_are_invalid_when_there_are_no_capturing_groups_in_a_regular_expression_1534", "Decimal escapes are invalid when there are no capturing groups in a regular expression."), + This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_regular_expression: diag(1533, 1 /* Error */, "This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_r_1533", "This backreference refers to a group that does not exist. There are only {0} capturing groups in this regular expression."), + This_backreference_is_invalid_because_the_containing_regular_expression_contains_no_capturing_groups: diag(1534, 1 /* Error */, "This_backreference_is_invalid_because_the_containing_regular_expression_contains_no_capturing_groups_1534", "This backreference is invalid because the containing regular expression contains no capturing groups."), This_character_cannot_be_escaped_in_a_regular_expression: diag(1535, 1 /* Error */, "This_character_cannot_be_escaped_in_a_regular_expression_1535", "This character cannot be escaped in a regular expression."), The_types_of_0_are_incompatible_between_these_types: diag(2200, 1 /* Error */, "The_types_of_0_are_incompatible_between_these_types_2200", "The types of '{0}' are incompatible between these types."), The_types_returned_by_0_are_incompatible_between_these_types: diag(2201, 1 /* Error */, "The_types_returned_by_0_are_incompatible_between_these_types_2201", "The types returned by '{0}' are incompatible between these types."), @@ -12643,7 +12643,9 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan tokenFlags |= 2048 /* ContainsInvalidEscape */; if (isRegularExpression || shouldEmitInvalidEscapeError) { const code = parseInt(text.substring(start2 + 1, pos), 8); - error2(Diagnostics.Octal_escape_sequences_are_not_allowed_Use_the_syntax_0, start2, pos - start2, "\\x" + code.toString(16).padStart(2, "0")); + if (isRegularExpression !== "annex-b") { + error2(Diagnostics.Octal_escape_sequences_are_not_allowed_Use_the_syntax_0, start2, pos - start2, "\\x" + code.toString(16).padStart(2, "0")); + } return String.fromCharCode(code); } return text.substring(start2, pos); @@ -12674,7 +12676,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan case 117 /* u */: if ((!isRegularExpression || shouldEmitInvalidEscapeError) && pos < end && text.charCodeAt(pos) === 123 /* openBrace */) { pos -= 2; - return scanExtendedUnicodeEscape(isRegularExpression || shouldEmitInvalidEscapeError); + return scanExtendedUnicodeEscape(!!isRegularExpression || shouldEmitInvalidEscapeError); } for (; pos < start2 + 6; pos++) { if (!(pos < end && isHexDigit(text.charCodeAt(pos)))) { @@ -12724,7 +12726,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan case 8233 /* paragraphSeparator */: return ""; default: - if (isRegularExpression && (shouldEmitInvalidEscapeError || isIdentifierPart(ch, languageVersion))) { + if (isRegularExpression === true && (shouldEmitInvalidEscapeError || isIdentifierPart(ch, languageVersion))) { error2(Diagnostics.This_character_cannot_be_escaped_in_a_regular_expression, pos - 2, 2); } return String.fromCharCode(ch); @@ -13419,7 +13421,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan pos = tokenStart + 1; return token = 64 /* EqualsToken */; } - function reScanSlashToken() { + function reScanSlashToken(reportErrors2) { if (token === 44 /* SlashToken */ || token === 69 /* SlashEqualsToken */) { let p = tokenStart + 1; let inEscape = false; @@ -13458,38 +13460,54 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan if (!isIdentifierPart(ch, languageVersion)) { break; } - const flag = characterToRegularExpressionFlag(String.fromCharCode(ch)); - if (flag === void 0) { - error2(Diagnostics.Unknown_regular_expression_flag, p, 1); - } else if (regExpFlags & flag) { - error2(Diagnostics.Duplicate_regular_expression_flag, p, 1); - } else if (((regExpFlags | flag) & 96 /* UnicodeMode */) === 96 /* UnicodeMode */) { - error2(Diagnostics.The_Unicode_u_flag_and_the_Unicode_Sets_v_flag_cannot_be_set_simultaneously, p, 1); - } else { - regExpFlags |= flag; - const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag); - if (languageVersion < availableFrom) { - error2(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, p, 1, getNameOfScriptTarget(availableFrom)); + if (reportErrors2) { + const flag = characterToRegularExpressionFlag(String.fromCharCode(ch)); + if (flag === void 0) { + error2(Diagnostics.Unknown_regular_expression_flag, p, 1); + } else if (regExpFlags & flag) { + error2(Diagnostics.Duplicate_regular_expression_flag, p, 1); + } else if (((regExpFlags | flag) & 96 /* UnicodeMode */) === 96 /* UnicodeMode */) { + error2(Diagnostics.The_Unicode_u_flag_and_the_Unicode_Sets_v_flag_cannot_be_set_simultaneously, p, 1); + } else { + regExpFlags |= flag; + const availableFrom = regExpFlagToFirstAvailableLanguageVersion.get(flag); + if (languageVersion < availableFrom) { + error2(Diagnostics.This_regular_expression_flag_is_only_available_when_targeting_0_or_later, p, 1, getNameOfScriptTarget(availableFrom)); + } } } p++; } - pos = tokenStart + 1; - const saveTokenPos = tokenStart; - const saveTokenFlags = tokenFlags; - scanRegularExpressionWorker(text, endOfBody, regExpFlags, isUnterminated); - if (!isUnterminated) { + if (reportErrors2) { + pos = tokenStart + 1; + const saveTokenPos = tokenStart; + const saveTokenFlags = tokenFlags; + scanRegularExpressionWorker( + text, + endOfBody, + regExpFlags, + isUnterminated, + /*annexB*/ + true + ); + if (!isUnterminated) { + pos = p; + } + tokenStart = saveTokenPos; + tokenFlags = saveTokenFlags; + } else { pos = p; } - tokenStart = saveTokenPos; - tokenFlags = saveTokenFlags; tokenValue = text.substring(tokenStart, pos); token = 14 /* RegularExpressionLiteral */; } return token; - function scanRegularExpressionWorker(text2, end2, regExpFlags, isUnterminated) { - const unicodeMode = !!(regExpFlags & 96 /* UnicodeMode */); + function scanRegularExpressionWorker(text2, end2, regExpFlags, isUnterminated, annexB) { const unicodeSetsMode = !!(regExpFlags & 64 /* UnicodeSets */); + const unicodeMode = !!(regExpFlags & 96 /* UnicodeMode */); + if (unicodeMode) { + annexB = false; + } let mayContainStrings = false; let numberOfCapturingGroups = 0; const groupSpecifiers = /* @__PURE__ */ new Set(); @@ -13540,7 +13558,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan case 61 /* equals */: case 33 /* exclamation */: pos++; - isPreviousTermQuantifiable = false; + isPreviousTermQuantifiable = annexB; break; case 60 /* lessThan */: const groupNameStart = pos; @@ -13719,7 +13737,10 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan break; } default: - Debug.assert(scanCharacterClassEscape() || scanDecimalEscape() || scanCharacterEscape()); + Debug.assert(scanCharacterClassEscape() || scanDecimalEscape() || scanCharacterEscape( + /*atomEscape*/ + true + )); break; } } @@ -13734,7 +13755,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan } return false; } - function scanCharacterEscape() { + function scanCharacterEscape(atomEscape) { Debug.assertEqual(text2.charCodeAt(pos - 1), 92 /* backslash */); let ch = text2.charCodeAt(pos); switch (ch) { @@ -13747,6 +13768,9 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan } if (unicodeMode) { error2(Diagnostics.c_must_be_followed_by_an_ASCII_letter, pos - 2, 2); + } else if (atomEscape && annexB) { + pos--; + return "\\"; } return String.fromCharCode(ch); case 94 /* caret */: @@ -13776,7 +13800,7 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan /*shouldEmitInvalidEscapeError*/ unicodeMode, /*isRegularExpression*/ - true + annexB ? "annex-b" : true ); } } @@ -13816,12 +13840,12 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan if (isClassContentExit(ch2)) { return; } - if (!minCharacter) { + if (!minCharacter && !annexB) { error2(Diagnostics.A_character_class_range_must_not_be_bounded_by_another_character_class, minStart, pos - 1 - minStart); } const maxStart = pos; const maxCharacter = scanClassAtom(); - if (!maxCharacter) { + if (!maxCharacter && !annexB) { error2(Diagnostics.A_character_class_range_must_not_be_bounded_by_another_character_class, maxStart, pos - maxStart); continue; } @@ -14104,7 +14128,10 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan pos++; return String.fromCharCode(ch2); default: - return scanCharacterEscape(); + return scanCharacterEscape( + /*atomEscape*/ + false + ); } } else if (ch === text2.charCodeAt(pos + 1)) { switch (ch) { @@ -14161,7 +14188,10 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan if (scanCharacterClassEscape()) { return ""; } - return scanCharacterEscape(); + return scanCharacterEscape( + /*atomEscape*/ + false + ); } } else { return scanSourceCharacter(); @@ -14276,11 +14306,11 @@ function createScanner(languageVersion, skipTrivia2, languageVariant = 0 /* Stan } }); forEach(decimalEscapes, (escape) => { - if (escape.value > numberOfCapturingGroups) { + if (!annexB && escape.value > numberOfCapturingGroups) { if (numberOfCapturingGroups) { - error2(Diagnostics.A_decimal_escape_must_refer_to_an_existent_capturing_group_There_are_only_0_capturing_groups_in_this_regular_expression, escape.pos, escape.end - escape.pos, numberOfCapturingGroups); + error2(Diagnostics.This_backreference_refers_to_a_group_that_does_not_exist_There_are_only_0_capturing_groups_in_this_regular_expression, escape.pos, escape.end - escape.pos, numberOfCapturingGroups); } else { - error2(Diagnostics.Decimal_escapes_are_invalid_when_there_are_no_capturing_groups_in_a_regular_expression, escape.pos, escape.end - escape.pos); + error2(Diagnostics.This_backreference_is_invalid_because_the_containing_regular_expression_contains_no_capturing_groups, escape.pos, escape.end - escape.pos); } } }); @@ -50077,6 +50107,7 @@ function createTypeChecker(host) { var requestedExternalEmitHelperNames = /* @__PURE__ */ new Set(); var requestedExternalEmitHelpers; var externalHelpersModule; + var scanner2; var Symbol47 = objectAllocator.getSymbolConstructor(); var Type28 = objectAllocator.getTypeConstructor(); var Signature14 = objectAllocator.getSignatureConstructor(); @@ -54936,7 +54967,8 @@ function createTypeChecker(host) { deepCloneOrReuseNode, /*context*/ void 0, - deepCloneOrReuseNodes + deepCloneOrReuseNodes, + deepCloneOrReuseNode )), node); } function deepCloneOrReuseNodes(nodes, visitor, test, start, count) { @@ -75915,6 +75947,53 @@ function createTypeChecker(host) { return signatureList.length === 1 ? signatureList[0] : createUnionSignature(signatureList[0], signatureList); } } + function checkGrammarRegularExpressionLiteral(node) { + const sourceFile = getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + let lastError; + scanner2 ?? (scanner2 = createScanner( + 99 /* ESNext */, + /*skipTrivia*/ + true + )); + scanner2.setScriptTarget(sourceFile.languageVersion); + scanner2.setLanguageVariant(sourceFile.languageVariant); + scanner2.setOnError((message, length2, arg0) => { + const start = scanner2.getTokenEnd(); + if (message.category === 3 /* Message */ && lastError && start === lastError.start && length2 === lastError.length) { + const error3 = createDetachedDiagnostic(sourceFile.fileName, sourceFile.text, start, length2, message, arg0); + addRelatedInfo(lastError, error3); + } else if (!lastError || start !== lastError.start) { + lastError = createFileDiagnostic(sourceFile, start, length2, message, arg0); + diagnostics.add(lastError); + } + }); + scanner2.setText(sourceFile.text, node.pos, node.end - node.pos); + try { + scanner2.scan(); + Debug.assert(scanner2.reScanSlashToken( + /*reportErrors*/ + true + ) === 14 /* RegularExpressionLiteral */, "Expected scanner to rescan RegularExpressionLiteral"); + return !!lastError; + } finally { + scanner2.setText(""); + scanner2.setOnError( + /*onError*/ + void 0 + ); + } + } + return false; + } + function checkRegularExpressionLiteral(node) { + const nodeLinks2 = getNodeLinks(node); + if (!(nodeLinks2.flags & 1 /* TypeChecked */)) { + nodeLinks2.flags |= 1 /* TypeChecked */; + addLazyDiagnostic(() => checkGrammarRegularExpressionLiteral(node)); + } + return globalRegExpType; + } function checkSpreadExpression(node, checkMode) { if (languageVersion < 2 /* SpreadElements */) { checkExternalEmitHelpers(node, compilerOptions.downlevelIteration ? 1536 /* SpreadIncludes */ : 1024 /* SpreadArray */); @@ -81818,7 +81897,7 @@ function createTypeChecker(host) { parent2 = parent2.parent; } if (operator === 56 /* AmpersandAmpersandToken */ || isIfStatement(parent2)) { - checkTestingKnownTruthyCallableOrAwaitableType(node.left, leftType, isIfStatement(parent2) ? parent2.thenStatement : void 0); + checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.left, leftType, isIfStatement(parent2) ? parent2.thenStatement : void 0); } checkTruthinessOfType(leftType, node.left); } @@ -82376,7 +82455,7 @@ function createTypeChecker(host) { } function checkConditionalExpression(node, checkMode) { const type = checkTruthinessExpression(node.condition, checkMode); - checkTestingKnownTruthyCallableOrAwaitableType(node.condition, type, node.whenTrue); + checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.condition, type, node.whenTrue); const type1 = checkExpression(node.whenTrue, checkMode); const type2 = checkExpression(node.whenFalse, checkMode); return getUnionType([type1, type2], 2 /* Subtype */); @@ -82849,7 +82928,7 @@ function createTypeChecker(host) { case 228 /* TemplateExpression */: return checkTemplateExpression(node); case 14 /* RegularExpressionLiteral */: - return globalRegExpType; + return checkRegularExpressionLiteral(node); case 209 /* ArrayLiteralExpression */: return checkArrayLiteral(node, checkMode, forceTuple); case 210 /* ObjectLiteralExpression */: @@ -85424,14 +85503,14 @@ function createTypeChecker(host) { function checkIfStatement(node) { checkGrammarStatementInAmbientContext(node); const type = checkTruthinessExpression(node.expression); - checkTestingKnownTruthyCallableOrAwaitableType(node.expression, type, node.thenStatement); + checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(node.expression, type, node.thenStatement); checkSourceElement(node.thenStatement); if (node.thenStatement.kind === 242 /* EmptyStatement */) { error2(node.thenStatement, Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement); } checkSourceElement(node.elseStatement); } - function checkTestingKnownTruthyCallableOrAwaitableType(condExpr, condType, body) { + function checkTestingKnownTruthyCallableOrAwaitableOrEnumMemberType(condExpr, condType, body) { if (!strictNullChecks) return; bothHelper(condExpr, body); @@ -85453,6 +85532,10 @@ function createTypeChecker(host) { return; } const type = location === condExpr2 ? condType : checkTruthinessExpression(location); + if (type.flags & 1024 /* EnumLiteral */ && isPropertyAccessExpression(location) && (getNodeLinks(location.expression).resolvedSymbol ?? unknownSymbol).flags & 384 /* Enum */) { + error2(location, Diagnostics.This_condition_will_always_return_0, !!type.value ? "true" : "false"); + return; + } const isPropertyExpressionCast = isPropertyAccessExpression(location) && isTypeAssertion(location.expression); if (!hasTypeFacts(type, 4194304 /* Truthy */) || isPropertyExpressionCast) return; @@ -87898,7 +87981,7 @@ function createTypeChecker(host) { if (checkGrammarModuleElementContext(node, isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) { return; } - if (!checkGrammarModifiers(node) && hasEffectiveModifiers(node)) { + if (!checkGrammarModifiers(node) && node.modifiers) { grammarErrorOnFirstToken(node, Diagnostics.An_import_declaration_cannot_have_modifiers); } if (checkExternalImportOrExportDeclaration(node)) { @@ -126382,7 +126465,7 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config } return Version.zero; } - function checkDeprecations(deprecatedIn, removedIn, createDiagnostic, fn) { + function checkDeprecations(deprecatedIn, stopsWorkingIn, removedIn, createDiagnostic, fn) { const deprecatedInVersion = new Version(deprecatedIn); const removedInVersion = new Version(removedIn); const typescriptVersion = new Version(typeScriptVersion3 || versionMajorMinor); @@ -126399,9 +126482,9 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config } } else { if (value === void 0) { - createDiagnostic(name, value, useInstead, Diagnostics.Option_0_is_deprecated_and_will_stop_functioning_in_TypeScript_1_Specify_compilerOption_ignoreDeprecations_Colon_2_to_silence_this_error, name, removedIn, deprecatedIn); + createDiagnostic(name, value, useInstead, Diagnostics.Option_0_is_deprecated_and_will_stop_functioning_in_TypeScript_1_Specify_compilerOption_ignoreDeprecations_Colon_2_to_silence_this_error, name, stopsWorkingIn, deprecatedIn); } else { - createDiagnostic(name, value, useInstead, Diagnostics.Option_0_1_is_deprecated_and_will_stop_functioning_in_TypeScript_2_Specify_compilerOption_ignoreDeprecations_Colon_3_to_silence_this_error, name, value, removedIn, deprecatedIn); + createDiagnostic(name, value, useInstead, Diagnostics.Option_0_1_is_deprecated_and_will_stop_functioning_in_TypeScript_2_Specify_compilerOption_ignoreDeprecations_Colon_3_to_silence_this_error, name, value, stopsWorkingIn, deprecatedIn); } } }); @@ -126437,7 +126520,7 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config ); } } - checkDeprecations("5.0", "5.5", createDiagnostic, (createDeprecatedDiagnostic) => { + checkDeprecations("5.0", "5.5", "6.0", createDiagnostic, (createDeprecatedDiagnostic) => { if (options.target === 0 /* ES3 */) { createDeprecatedDiagnostic("target", "ES3"); } @@ -126489,7 +126572,7 @@ function createProgram(rootNamesOrOptions, _options, _host, _oldProgram, _config function createDiagnostic(_name, _value, _useInstead, message, ...args) { createDiagnosticForReference(parentFile, index, message, ...args); } - checkDeprecations("5.0", "5.5", createDiagnostic, (createDeprecatedDiagnostic) => { + checkDeprecations("5.0", "5.5", "6.0", createDiagnostic, (createDeprecatedDiagnostic) => { if (ref.prepend) { createDeprecatedDiagnostic("prepend"); }