Skip to content

Commit 5256ff7

Browse files
committed
recommend set if el is on RHS of assignment else get
1 parent a481be7 commit 5256ff7

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

src/compiler/checker.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10117,9 +10117,9 @@ namespace ts {
1011710117
}
1011810118
}
1011910119
else {
10120-
const suggestions = getSuggestionsForNonexistentIndexSignature(objectType);
10121-
if (suggestions) {
10122-
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1, typeToString(objectType), suggestions);
10120+
const suggestion = getSuggestionForNonexistentIndexSignature(objectType, accessExpression);
10121+
if (suggestion !== undefined) {
10122+
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1, typeToString(objectType), suggestion);
1012310123
}
1012410124
else {
1012510125
error(accessExpression, Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature, typeToString(objectType));
@@ -20192,23 +20192,33 @@ namespace ts {
2019220192
return suggestion && symbolName(suggestion);
2019320193
}
2019420194

20195-
function getSuggestionsForNonexistentIndexSignature(objectType: Type): string | undefined {
20196-
let suggestions: string | undefined;
20197-
const props = [
20198-
getPropertyOfObjectType(objectType, <__String>"get"),
20199-
getPropertyOfObjectType(objectType, <__String>"set")
20200-
];
20201-
20202-
for (const prop of props) {
20195+
function getSuggestionForNonexistentIndexSignature(objectType: Type, expr: ElementAccessExpression): string | undefined {
20196+
// check if object type has setter or getter
20197+
const hasProp = (name: "set" | "get", argCount = 1) => {
20198+
const prop = getPropertyOfObjectType(objectType, <__String>name);
2020320199
if (prop) {
2020420200
const s = getSingleCallSignature(getTypeOfSymbol(prop));
20205-
if (s && getMinArgumentCount(s) === 1 && typeToString(getTypeAtPosition(s, 0)) === "string") {
20206-
const suggestion = symbolToString(objectType.symbol) + "." + symbolToString(prop);
20207-
suggestions = (!suggestions) ? suggestion : suggestions.concat(" or " + suggestion);
20201+
if (s && getMinArgumentCount(s) === argCount && typeToString(getTypeAtPosition(s, 0)) === "string") {
20202+
return true;
2020820203
}
2020920204
}
20205+
return false;
20206+
};
20207+
20208+
const suggestedMethod = isAssignmentTarget(expr) ? "set" : "get";
20209+
if (!hasProp(suggestedMethod)) {
20210+
return undefined;
20211+
}
20212+
20213+
let suggestion = tryGetPropertyAccessOrIdentifierToString(expr);
20214+
if (suggestion === undefined) {
20215+
suggestion = suggestedMethod;
2021020216
}
20211-
return suggestions;
20217+
else {
20218+
suggestion += "." + suggestedMethod;
20219+
}
20220+
20221+
return suggestion;
2021220222
}
2021320223

2021420224
/**

src/compiler/utilities.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3960,6 +3960,16 @@ namespace ts {
39603960
return isPropertyAccessExpression(node) && isEntityNameExpression(node.expression);
39613961
}
39623962

3963+
export function tryGetPropertyAccessOrIdentifierToString(expr: Expression): string | undefined {
3964+
if (isPropertyAccessExpression(expr)) {
3965+
return tryGetPropertyAccessOrIdentifierToString(expr.expression) + "." + expr.name;
3966+
}
3967+
if (isIdentifier(expr)) {
3968+
return unescapeLeadingUnderscores(expr.escapedText);
3969+
}
3970+
return undefined;
3971+
}
3972+
39633973
export function isPrototypeAccess(node: Node): node is PropertyAccessExpression {
39643974
return isPropertyAccessExpression(node) && node.name.escapedText === "prototype";
39653975
}

0 commit comments

Comments
 (0)