Skip to content

Commit

Permalink
Merge branch 'main' into fix44572
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Aug 29, 2021
2 parents a1f82c1 + fd067e9 commit 813b04b
Show file tree
Hide file tree
Showing 53 changed files with 792 additions and 463 deletions.
544 changes: 178 additions & 366 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@types/chai": "latest",
"@types/convert-source-map": "latest",
"@types/glob": "latest",
"@types/gulp": "^4.0.5",
"@types/gulp": "^4.0.9",
"@types/gulp-concat": "latest",
"@types/gulp-newer": "latest",
"@types/gulp-rename": "0.0.33",
Expand All @@ -57,7 +57,7 @@
"@typescript-eslint/experimental-utils": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"async": "latest",
"azure-devops-node-api": "^10.1.0",
"azure-devops-node-api": "^11.0.1",
"browser-resolve": "^1.11.2",
"browserify": "latest",
"chai": "latest",
Expand Down Expand Up @@ -86,12 +86,11 @@
"mocha": "latest",
"mocha-fivemat-progress-reporter": "latest",
"ms": "latest",
"node-fetch": "^2.6.0",
"node-fetch": "^2.6.1",
"plugin-error": "latest",
"pretty-hrtime": "^1.0.3",
"prex": "^0.4.3",
"q": "latest",
"remove-internal": "^2.9.2",
"source-map-support": "latest",
"through2": "latest",
"typescript": "^4.2.3",
Expand Down
47 changes: 32 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8402,12 +8402,12 @@ namespace ts {
}

function isNullOrUndefined(node: Expression) {
const expr = skipParentheses(node);
const expr = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
return expr.kind === SyntaxKind.NullKeyword || expr.kind === SyntaxKind.Identifier && getResolvedSymbol(expr as Identifier) === undefinedSymbol;
}

function isEmptyArrayLiteral(node: Expression) {
const expr = skipParentheses(node);
const expr = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
return expr.kind === SyntaxKind.ArrayLiteralExpression && (expr as ArrayLiteralExpression).elements.length === 0;
}

Expand Down Expand Up @@ -11800,7 +11800,7 @@ namespace ts {
// Flags we want to propagate to the result if they exist in all source symbols
let optionalFlag = isUnion ? SymbolFlags.None : SymbolFlags.Optional;
let syntheticFlag = CheckFlags.SyntheticMethod;
let checkFlags = 0;
let checkFlags = isUnion ? 0 : CheckFlags.Readonly;
let mergedInstantiations = false;
for (const current of containingType.types) {
const type = getApparentType(current);
Expand Down Expand Up @@ -11839,8 +11839,13 @@ namespace ts {
}
}
}
checkFlags |= (isReadonlySymbol(prop) ? CheckFlags.Readonly : 0) |
(!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
if (isUnion && isReadonlySymbol(prop)) {
checkFlags |= CheckFlags.Readonly;
}
else if (!isUnion && !isReadonlySymbol(prop)) {
checkFlags &= ~CheckFlags.Readonly;
}
checkFlags |= (!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
Expand Down Expand Up @@ -22966,7 +22971,7 @@ namespace ts {
}

function isFalseExpression(expr: Expression): boolean {
const node = skipParentheses(expr);
const node = skipParentheses(expr, /*excludeJSDocTypeAssertions*/ true);
return node.kind === SyntaxKind.FalseKeyword || node.kind === SyntaxKind.BinaryExpression && (
(node as BinaryExpression).operatorToken.kind === SyntaxKind.AmpersandAmpersandToken && (isFalseExpression((node as BinaryExpression).left) || isFalseExpression((node as BinaryExpression).right)) ||
(node as BinaryExpression).operatorToken.kind === SyntaxKind.BarBarToken && isFalseExpression((node as BinaryExpression).left) && isFalseExpression((node as BinaryExpression).right));
Expand Down Expand Up @@ -23288,7 +23293,7 @@ namespace ts {
}

function narrowTypeByAssertion(type: Type, expr: Expression): Type {
const node = skipParentheses(expr);
const node = skipParentheses(expr, /*excludeJSDocTypeAssertions*/ true);
if (node.kind === SyntaxKind.FalseKeyword) {
return unreachableNeverType;
}
Expand Down Expand Up @@ -25872,7 +25877,9 @@ namespace ts {
case SyntaxKind.ParenthesizedExpression: {
// Like in `checkParenthesizedExpression`, an `/** @type {xyz} */` comment before a parenthesized expression acts as a type cast.
const tag = isInJSFile(parent) ? getJSDocTypeTag(parent) : undefined;
return tag ? getTypeFromTypeNode(tag.typeExpression.type) : getContextualType(parent as ParenthesizedExpression, contextFlags);
return !tag ? getContextualType(parent as ParenthesizedExpression, contextFlags) :
isJSDocTypeTag(tag) && isConstTypeReference(tag.typeExpression.type) ? tryFindWhenConstTypeReference(parent as ParenthesizedExpression) :
getTypeFromTypeNode(tag.typeExpression.type);
}
case SyntaxKind.NonNullExpression:
return getContextualType(parent as NonNullExpression, contextFlags);
Expand Down Expand Up @@ -32855,8 +32862,10 @@ namespace ts {
}

function isTypeAssertion(node: Expression) {
node = skipParentheses(node);
return node.kind === SyntaxKind.TypeAssertionExpression || node.kind === SyntaxKind.AsExpression;
node = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
return node.kind === SyntaxKind.TypeAssertionExpression ||
node.kind === SyntaxKind.AsExpression ||
isJSDocTypeAssertion(node);
}

function checkDeclarationInitializer(declaration: HasExpressionInitializer, contextualType?: Type | undefined) {
Expand Down Expand Up @@ -32931,6 +32940,7 @@ namespace ts {
function isConstContext(node: Expression): boolean {
const parent = node.parent;
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||
isJSDocTypeAssertion(parent) && isConstTypeReference(getJSDocTypeAssertionType(parent)) ||
(isParenthesizedExpression(parent) || isArrayLiteralExpression(parent) || isSpreadElement(parent)) && isConstContext(parent) ||
(isPropertyAssignment(parent) || isShorthandPropertyAssignment(parent) || isTemplateSpan(parent)) && isConstContext(parent.parent);
}
Expand Down Expand Up @@ -33143,7 +33153,14 @@ namespace ts {
}

function getQuickTypeOfExpression(node: Expression) {
const expr = skipParentheses(node);
let expr = skipParentheses(node, /*excludeJSDocTypeAssertions*/ true);
if (isJSDocTypeAssertion(expr)) {
const type = getJSDocTypeAssertionType(expr);
if (!isConstTypeReference(type)) {
return getTypeFromTypeNode(type);
}
}
expr = skipParentheses(node);
// Optimize for the common case of a call to a function with a single non-generic call
// signature where we can just fetch the return type without checking the arguments.
if (isCallExpression(expr) && expr.expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(expr, /*checkArgumentIsStringLiteralLike*/ true) && !isSymbolOrSymbolForCall(expr)) {
Expand Down Expand Up @@ -33230,9 +33247,9 @@ namespace ts {
}

function checkParenthesizedExpression(node: ParenthesizedExpression, checkMode?: CheckMode): Type {
const tag = isInJSFile(node) ? getJSDocTypeTag(node) : undefined;
if (tag) {
return checkAssertionWorker(tag.typeExpression.type, tag.typeExpression.type, node.expression, checkMode);
if (isJSDocTypeAssertion(node)) {
const type = getJSDocTypeAssertionType(node);
return checkAssertionWorker(type, type, node.expression, checkMode);
}
return checkExpression(node.expression, checkMode);
}
Expand Down Expand Up @@ -36182,7 +36199,7 @@ namespace ts {
if (getFalsyFlags(type)) return;

const location = isBinaryExpression(condExpr) ? condExpr.right : condExpr;
if (isPropertyAccessExpression(location) && isAssertionExpression(skipParentheses(location.expression))) {
if (isPropertyAccessExpression(location) && isTypeAssertion(location.expression)) {
return;
}

Expand Down
15 changes: 15 additions & 0 deletions src/compiler/factory/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,24 @@ namespace ts {
node.kind === SyntaxKind.CommaListExpression;
}

export function isJSDocTypeAssertion(node: Node): node is JSDocTypeAssertion {
return isParenthesizedExpression(node)
&& isInJSFile(node)
&& !!getJSDocTypeTag(node);
}

export function getJSDocTypeAssertionType(node: JSDocTypeAssertion) {
const type = getJSDocType(node);
Debug.assertIsDefined(type);
return type;
}

export function isOuterExpression(node: Node, kinds = OuterExpressionKinds.All): node is OuterExpression {
switch (node.kind) {
case SyntaxKind.ParenthesizedExpression:
if (kinds & OuterExpressionKinds.ExcludeJSDocTypeAssertion && isJSDocTypeAssertion(node)) {
return false;
}
return (kinds & OuterExpressionKinds.Parentheses) !== 0;
case SyntaxKind.TypeAssertionExpression:
case SyntaxKind.AsExpression:
Expand Down
14 changes: 9 additions & 5 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ namespace ts {
}
const nodeModulesAtTypes = combinePaths("node_modules", "@types");

function arePathsEqual(path1: string, path2: string, host: ModuleResolutionHost): boolean {
const useCaseSensitiveFileNames = typeof host.useCaseSensitiveFileNames === "function" ? host.useCaseSensitiveFileNames() : host.useCaseSensitiveFileNames;
return comparePaths(path1, path2, !useCaseSensitiveFileNames) === Comparison.EqualTo;
}

/**
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
Expand Down Expand Up @@ -343,7 +348,7 @@ namespace ts {
resolvedTypeReferenceDirective = {
primary,
resolvedFileName,
originalPath: fileName === resolvedFileName ? undefined : fileName,
originalPath: arePathsEqual(fileName, resolvedFileName, host) ? undefined : fileName,
packageId,
isExternalLibraryImport: pathContainsNodeModules(fileName),
};
Expand Down Expand Up @@ -1078,9 +1083,8 @@ namespace ts {
}

/* @internal */
export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost): string | undefined {
const { resolvedModule } = tryResolveJSModuleWorker(moduleName, initialDir, host);
return resolvedModule && resolvedModule.resolvedFileName;
export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost) {
return tryResolveJSModuleWorker(moduleName, initialDir, host).resolvedModule;
}

const jsOnlyExtensions = [Extensions.JavaScript];
Expand Down Expand Up @@ -1123,7 +1127,7 @@ namespace ts {
let resolvedValue = resolved.value;
if (!compilerOptions.preserveSymlinks && resolvedValue && !resolvedValue.originalPath) {
const path = realPath(resolvedValue.path, host, traceEnabled);
const originalPath = path === resolvedValue.path ? undefined : resolvedValue.path;
const originalPath = arePathsEqual(path, resolvedValue.path, host) ? undefined : resolvedValue.path;
resolvedValue = { ...resolvedValue, path, originalPath };
}
// For node_modules lookups, get the real path so that multiple accesses to an `npm link`-ed module do not create duplicate files.
Expand Down
Loading

0 comments on commit 813b04b

Please sign in to comment.