Skip to content

Commit

Permalink
Merge branch 'main' into fix54167
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed May 19, 2023
2 parents fda0a8f + 215fe6e commit e67193d
Show file tree
Hide file tree
Showing 1,234 changed files with 20,730 additions and 19,227 deletions.
8 changes: 3 additions & 5 deletions .github/ISSUE_TEMPLATE/Bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,16 @@ Please keep and fill in the line that best applies:
### ⏯ Playground Link

<!--
A link to a TypeScript Playground "Share" link which shows this behavior
The TypeScript Workbench can be used for more complex setups, try
https://www.typescriptlang.org/dev/bug-workbench/
A link to a TypeScript Playground "Share" link which shows this behavior.
This should have the same code as the code snippet below, and use whichever settings are relevant to your report.
As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
[Playground link with relevant code](https://www.typescriptlang.org/play?#code/PTAEFkE9QYwewCYFNQHM5IM6gBZIE5JA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
<!-- Please post the relevant code sample here as well. This code and the Playground code should be the same, do not use separate examples -->
```ts
// We can quickly address your report if:
// - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
Expand Down
722 changes: 362 additions & 360 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "typescript",
"author": "Microsoft Corp.",
"homepage": "https://www.typescriptlang.org/",
"version": "5.1.0",
"version": "5.2.0",
"license": "Apache-2.0",
"description": "TypeScript is a language for application scale JavaScript development",
"keywords": [
Expand Down Expand Up @@ -83,9 +83,7 @@
"which": "^2.0.2"
},
"overrides": {
"typescript@*": "$typescript",
"@octokit/types": "9.0.0",
"@octokit/openapi-types": "16.0.0"
"typescript@*": "$typescript"
},
"scripts": {
"test": "hereby runtests-parallel --light=false",
Expand Down
92 changes: 63 additions & 29 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14262,7 +14262,36 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* maps primitive types and type parameters are to their apparent types.
*/
function getSignaturesOfType(type: Type, kind: SignatureKind): readonly Signature[] {
return getSignaturesOfStructuredType(getReducedApparentType(type), kind);
const result = getSignaturesOfStructuredType(getReducedApparentType(type), kind);
if (kind === SignatureKind.Call && !length(result) && type.flags & TypeFlags.Union) {
if ((type as UnionType).arrayFallbackSignatures) {
return (type as UnionType).arrayFallbackSignatures!;
}
// If the union is all different instantiations of a member of the global array type...
let memberName: __String;
if (everyType(type, t => !!t.symbol?.parent && isArrayOrTupleSymbol(t.symbol.parent) && (!memberName ? (memberName = t.symbol.escapedName, true) : memberName === t.symbol.escapedName))) {
// Transform the type from `(A[] | B[])["member"]` to `(A | B)[]["member"]` (since we pretend array is covariant anyway)
const arrayArg = mapType(type, t => getMappedType((isReadonlyArraySymbol(t.symbol.parent) ? globalReadonlyArrayType : globalArrayType).typeParameters![0], (t as AnonymousType).mapper!));
const arrayType = createArrayType(arrayArg, someType(type, t => isReadonlyArraySymbol(t.symbol.parent)));
return (type as UnionType).arrayFallbackSignatures = getSignaturesOfType(getTypeOfPropertyOfType(arrayType, memberName!)!, kind);
}
(type as UnionType).arrayFallbackSignatures = result;
}
return result;
}

function isArrayOrTupleSymbol(symbol: Symbol | undefined) {
if (!symbol || !globalArrayType.symbol || !globalReadonlyArrayType.symbol) {
return false;
}
return !!getSymbolIfSameReference(symbol, globalArrayType.symbol) || !!getSymbolIfSameReference(symbol, globalReadonlyArrayType.symbol);
}

function isReadonlyArraySymbol(symbol: Symbol | undefined) {
if (!symbol || !globalReadonlyArrayType.symbol) {
return false;
}
return !!getSymbolIfSameReference(symbol, globalReadonlyArrayType.symbol);
}

function findIndexInfo(indexInfos: readonly IndexInfo[], keyType: Type) {
Expand Down Expand Up @@ -16486,36 +16515,31 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getUnionOrIntersectionTypePredicate(signatures: readonly Signature[], kind: TypeFlags | undefined): TypePredicate | undefined {
let first: TypePredicate | undefined;
let last: TypePredicate | undefined;
const types: Type[] = [];
for (const sig of signatures) {
const pred = getTypePredicateOfSignature(sig);
if (!pred || pred.kind === TypePredicateKind.AssertsThis || pred.kind === TypePredicateKind.AssertsIdentifier) {
if (kind !== TypeFlags.Intersection) {
continue;
}
else {
return; // intersections demand all members be type predicates for the result to have a predicate
}
}

if (first) {
if (!typePredicateKindsMatch(first, pred)) {
// No common type predicate.
if (pred) {
// Constituent type predicates must all have matching kinds. We don't create composite type predicates for assertions.
if (pred.kind !== TypePredicateKind.This && pred.kind !== TypePredicateKind.Identifier || last && !typePredicateKindsMatch(last, pred)) {
return undefined;
}
last = pred;
types.push(pred.type);
}
else {
first = pred;
// In composite union signatures we permit and ignore signatures with a return type `false`.
const returnType = kind !== TypeFlags.Intersection ? getReturnTypeOfSignature(sig) : undefined;
if (returnType !== falseType && returnType !== regularFalseType) {
return undefined;
}
}
types.push(pred.type);
}
if (!first) {
// No signatures had a type predicate.
if (!last) {
return undefined;
}
const compositeType = getUnionOrIntersectionType(types, kind);
return createTypePredicate(first.kind, first.parameterName, first.parameterIndex, compositeType);
return createTypePredicate(last.kind, last.parameterName, last.parameterIndex, compositeType);
}

function typePredicateKindsMatch(a: TypePredicate, b: TypePredicate): boolean {
Expand Down Expand Up @@ -29294,6 +29318,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
case SyntaxKind.NumericLiteral:
case SyntaxKind.BigIntLiteral:
case SyntaxKind.NoSubstitutionTemplateLiteral:
case SyntaxKind.TemplateExpression:
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
case SyntaxKind.NullKeyword:
Expand Down Expand Up @@ -32590,8 +32615,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// can be specified by users through attributes property.
const paramType = getEffectiveFirstArgumentForJsxSignature(signature, node);
const attributesType = checkExpressionWithContextualType(node.attributes, paramType, /*inferenceContext*/ undefined, checkMode);
const checkAttributesType = checkMode & CheckMode.SkipContextSensitive ? getRegularTypeOfObjectLiteral(attributesType) : attributesType;
return checkTagNameDoesNotExpectTooManyArguments() && checkTypeRelatedToAndOptionallyElaborate(
attributesType,
checkAttributesType,
paramType,
relation,
reportErrors ? node.tagName : undefined,
Expand Down Expand Up @@ -34514,6 +34540,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
return getRegularTypeOfLiteralType(exprType);
}
const links = getNodeLinks(node);
links.assertionExpressionType = exprType;
checkSourceElement(type);
checkNodeDeferred(node);
return getTypeFromTypeNode(type);
Expand All @@ -34538,9 +34566,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function checkAssertionDeferred(node: JSDocTypeAssertion | AssertionExpression) {
const { type, expression } = getAssertionTypeAndExpression(node);
const { type } = getAssertionTypeAndExpression(node);
const errNode = isParenthesizedExpression(node) ? type : node;
const exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(expression)));
const links = getNodeLinks(node);
Debug.assertIsDefined(links.assertionExpressionType);
const exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(links.assertionExpressionType));
const targetType = getTypeFromTypeNode(type);
if (!isErrorType(targetType)) {
addLazyDiagnostic(() => {
Expand Down Expand Up @@ -37342,7 +37372,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
texts.push(span.literal.text);
types.push(isTypeAssignableTo(type, templateConstraintType) ? type : stringType);
}
return isConstContext(node) || isTemplateLiteralContext(node) || someType(getContextualType(node, /*contextFlags*/ undefined) || unknownType, isTemplateLiteralContextualType) ? getTemplateLiteralType(texts, types) : stringType;
if (isConstContext(node) || isTemplateLiteralContext(node) || someType(getContextualType(node, /*contextFlags*/ undefined) || unknownType, isTemplateLiteralContextualType)) {
return getTemplateLiteralType(texts, types);
}
const evaluated = node.parent.kind !== SyntaxKind.TaggedTemplateExpression && evaluateTemplateExpression(node);
return evaluated ? getFreshTypeOfLiteralType(getStringLiteralType(evaluated)) : stringType;
}

function isTemplateLiteralContextualType(type: Type): boolean {
Expand Down Expand Up @@ -43619,7 +43653,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return value;
}

function evaluate(expr: Expression, location: Declaration): string | number | undefined {
function evaluate(expr: Expression, location?: Declaration): string | number | undefined {
switch (expr.kind) {
case SyntaxKind.PrefixUnaryExpression:
const value = evaluate((expr as PrefixUnaryExpression).operand, location);
Expand Down Expand Up @@ -43676,11 +43710,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const symbol = resolveEntityName(expr, SymbolFlags.Value, /*ignoreErrors*/ true);
if (symbol) {
if (symbol.flags & SymbolFlags.EnumMember) {
return evaluateEnumMember(expr, symbol, location);
return location ? evaluateEnumMember(expr, symbol, location) : getEnumMemberValue(symbol.valueDeclaration as EnumMember);
}
if (isConstVariable(symbol)) {
const declaration = symbol.valueDeclaration as VariableDeclaration | undefined;
if (declaration && !declaration.type && declaration.initializer && declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location)) {
if (declaration && !declaration.type && declaration.initializer && (!location || declaration !== location && isBlockScopedNameDeclaredBeforeUse(declaration, location))) {
return evaluate(declaration.initializer, declaration);
}
}
Expand All @@ -43695,7 +43729,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const name = escapeLeadingUnderscores(((expr as ElementAccessExpression).argumentExpression as StringLiteralLike).text);
const member = rootSymbol.exports!.get(name);
if (member) {
return evaluateEnumMember(expr, member, location);
return location ? evaluateEnumMember(expr, member, location) : getEnumMemberValue(member.valueDeclaration as EnumMember);
}
}
}
Expand All @@ -43717,7 +43751,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return getEnumMemberValue(declaration as EnumMember);
}

function evaluateTemplateExpression(expr: TemplateExpression, location: Declaration) {
function evaluateTemplateExpression(expr: TemplateExpression, location?: Declaration) {
let result = expr.head.text;
for (const span of expr.templateSpans) {
const value = evaluate(span.expression, location);
Expand Down Expand Up @@ -46985,7 +47019,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (requestedExternalEmitHelperNames.has(name)) continue;
requestedExternalEmitHelperNames.add(name);

const symbol = getSymbol(helpersModule.exports!, escapeLeadingUnderscores(name), SymbolFlags.Value);
const symbol = getSymbol(getExportsOfModule(helpersModule), escapeLeadingUnderscores(name), SymbolFlags.Value);
if (!symbol) {
error(location, Diagnostics.This_syntax_requires_an_imported_helper_named_1_which_does_not_exist_in_0_Consider_upgrading_your_version_of_0, externalHelpersModuleNameText, name);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/corePublic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// WARNING: The script `configurePrerelease.ts` uses a regexp to parse out these values.
// If changing the text in this section, be sure to test `configurePrerelease` too.
export const versionMajorMinor = "5.1";
export const versionMajorMinor = "5.2";
// The following is baselined as a literal template type without intervention
/** The version of the TypeScript compiler release */
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4917,6 +4917,10 @@
"category": "Message",
"code": 6163
},
"Skipping module '{0}' that looks like an absolute URI, target file types: {1}.": {
"category": "Message",
"code": 6164
},
"Do not truncate error messages.": {
"category": "Message",
"code": 6165
Expand Down Expand Up @@ -7608,6 +7612,10 @@
"category": "Message",
"code": 95178
},
"Cannot move to file, selected file is invalid": {
"category": "Message",
"code": 95179
},

"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
"category": "Error",
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/factory/emitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -761,10 +761,10 @@ export const esDecorateHelper: UnscopedEmitHelper = {
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
if (_ = accept(result.get)) descriptor.get = _;
if (_ = accept(result.set)) descriptor.set = _;
if (_ = accept(result.init)) initializers.push(_);
if (_ = accept(result.init)) initializers.unshift(_);
}
else if (_ = accept(result)) {
if (kind === "field") initializers.push(_);
if (kind === "field") initializers.unshift(_);
else descriptor[key] = _;
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ import {
getSyntheticLeadingComments,
getSyntheticTrailingComments,
getTextOfIdentifierOrLiteral,
HasDecorators,
hasInvalidEscape,
HasModifiers,
hasProperty,
Expand Down Expand Up @@ -1023,6 +1024,7 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
liftToBlock,
mergeLexicalEnvironment,
updateModifiers,
updateModifierLike,
};

forEach(nodeFactoryPatchers, fn => fn(factory));
Expand Down Expand Up @@ -6989,6 +6991,18 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
Debug.assertNever(node);
}

function updateModifierLike<T extends HasModifiers & HasDecorators>(node: T, modifiers: readonly ModifierLike[]): T;
function updateModifierLike(node: HasModifiers & HasDecorators, modifierArray: readonly ModifierLike[]) {
return isParameter(node) ? updateParameterDeclaration(node, modifierArray, node.dotDotDotToken, node.name, node.questionToken, node.type, node.initializer) :
isPropertyDeclaration(node) ? updatePropertyDeclaration(node, modifierArray, node.name, node.questionToken ?? node.exclamationToken, node.type, node.initializer) :
isMethodDeclaration(node) ? updateMethodDeclaration(node, modifierArray, node.asteriskToken, node.name, node.questionToken, node.typeParameters, node.parameters, node.type, node.body) :
isGetAccessorDeclaration(node) ? updateGetAccessorDeclaration(node, modifierArray, node.name, node.parameters, node.type, node.body) :
isSetAccessorDeclaration(node) ? updateSetAccessorDeclaration(node, modifierArray, node.name, node.parameters, node.body) :
isClassExpression(node) ? updateClassExpression(node, modifierArray, node.name, node.typeParameters, node.heritageClauses, node.members) :
isClassDeclaration(node) ? updateClassDeclaration(node, modifierArray, node.name, node.typeParameters, node.heritageClauses, node.members) :
Debug.assertNever(node);
}

function asNodeArray<T extends Node>(array: readonly T[]): NodeArray<T>;
function asNodeArray<T extends Node>(array: readonly T[] | undefined): NodeArray<T> | undefined;
function asNodeArray<T extends Node>(array: readonly T[] | undefined): NodeArray<T> | undefined {
Expand Down
9 changes: 7 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,12 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
resolved = loadModuleFromSelfNameReference(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
}
if (!resolved) {
if (moduleName.indexOf(":") > -1) {
if (traceEnabled) {
trace(host, Diagnostics.Skipping_module_0_that_looks_like_an_absolute_URI_target_file_types_Colon_1, moduleName, formatExtensions(extensions));
}
return undefined;
}
if (traceEnabled) {
trace(host, Diagnostics.Loading_module_0_from_node_modules_folder_target_file_types_Colon_1, moduleName, formatExtensions(extensions));
}
Expand Down Expand Up @@ -2894,7 +2900,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu

const loader: ResolutionKindSpecificLoader = (extensions, candidate, onlyRecordFailures, state) => {
let pathAndExtension =
loadModuleFromFile(extensions, candidate, onlyRecordFailures, state) ||
(rest || !(state.features & NodeResolutionFeatures.EsmMode)) && loadModuleFromFile(extensions, candidate, onlyRecordFailures, state) ||
loadNodeModuleFromDirectoryWorker(
extensions,
candidate,
Expand Down Expand Up @@ -2935,7 +2941,6 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
return fromPaths.value;
}
}

return loader(extensions, candidate, !nodeModulesDirectoryExists, state);
}

Expand Down
Loading

0 comments on commit e67193d

Please sign in to comment.