Skip to content

Commit 1b44d72

Browse files
committed
Improve readability of isExcludedJSError
1 parent 38d806d commit 1b44d72

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@ namespace ts {
20632063
if (isGlobalScopeAugmentationDeclaration) {
20642064
suggestion = undefined;
20652065
}
2066-
if (originalLocation && isBadUncheckedJSSuggestion(lastLocation, /*parent*/ undefined, suggestion?.valueDeclaration ? [suggestion.valueDeclaration] : [])) {
2066+
if (originalLocation && isExcludedJSError(originalLocation, suggestion, /*forbidClasses*/ false)) {
20672067
suggestion = undefined;
20682068
}
20692069
if (suggestion) {
@@ -27455,8 +27455,8 @@ namespace ts {
2745527455
if (!prop) {
2745627456
const indexInfo = !isPrivateIdentifier(right) && (assignmentKind === AssignmentKind.None || !isGenericObjectType(leftType) || isThisTypeParameter(leftType)) ? getIndexInfoOfType(apparentType, IndexKind.String) : undefined;
2745727457
if (!(indexInfo && indexInfo.type)) {
27458-
const isBadJSSuggestion = isBadUncheckedJSSuggestion(node, leftType.symbol, leftType.symbol?.declarations);
27459-
if (isBadJSSuggestion === undefined ? isJSLiteralType(leftType) : isBadJSSuggestion) {
27458+
const isExcludedError = isExcludedJSError(node, leftType.symbol, /*forbidClasses*/ true);
27459+
if (isExcludedError === undefined ? isJSLiteralType(leftType) : isExcludedError) {
2746027460
return anyType;
2746127461
}
2746227462
if (leftType.symbol === globalThisSymbol) {
@@ -27504,17 +27504,20 @@ namespace ts {
2750427504

2750527505
/**
2750627506
* Only applies to unchecked JS files without checkJS, // @ts-check or // @ts-nocheck
27507-
* @returns undefined when not applicable, true for bad suggestions, false for good ones
27508-
* TODO: Should probably only pass one declaration
27507+
* @returns undefined when not applicable, true for excluded errors, false for included ones
27508+
*
27509+
* An error is excluded when it:
27510+
* - Is from a global file that is different from the reference file, or
27511+
* - (optionally) Is a class, or is a this.x property access expression
2750927512
*/
27510-
function isBadUncheckedJSSuggestion(node: Node | undefined, parent: Symbol | undefined, declarations: Node[] | undefined): boolean | undefined {
27513+
function isExcludedJSError(node: Node | undefined, suggestion: Symbol | undefined, excludeClasses: boolean): boolean | undefined {
2751127514
const file = getSourceFileOfNode(node);
2751227515
if (file) {
2751327516
if (compilerOptions.checkJs === undefined && !file.checkJsDirective && (file.scriptKind === ScriptKind.JS || file.scriptKind === ScriptKind.JSX)) {
27514-
const parentFile = forEach(declarations, getSourceFileOfNode);
27515-
return file !== parentFile && !!parentFile && isGlobalSourceFile(parentFile)
27516-
|| !!(parent && parent.flags & SymbolFlags.Class)
27517-
|| !!node && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword;
27517+
const declarationFile = forEach(suggestion?.declarations, getSourceFileOfNode);
27518+
return file !== declarationFile && !!declarationFile && isGlobalSourceFile(declarationFile)
27519+
|| !!(excludeClasses && suggestion && suggestion.flags & SymbolFlags.Class)
27520+
|| !!node && excludeClasses && isPropertyAccessExpression(node) && node.expression.kind === SyntaxKind.ThisKeyword;
2751827521
}
2751927522
}
2752027523
}

0 commit comments

Comments
 (0)