Skip to content

fix(40320): Better errors when using properties/methods from newer versions of ECMAScript #40650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,15 @@ namespace ts {
}
}
if (!suggestion) {
error(errorLocation, nameNotFoundMessage, diagnosticName(nameArg!));
if (nameArg) {
const lib = getSuggestedLibForNonExistentName(nameArg);
if (lib) {
error(errorLocation, nameNotFoundMessage, diagnosticName(nameArg), lib);
}
else {
error(errorLocation, nameNotFoundMessage, diagnosticName(nameArg));
}
}
}
suggestionCount++;
}
Expand Down Expand Up @@ -20595,7 +20603,17 @@ namespace ts {
case "WeakSet":
case "Iterator":
case "AsyncIterator":
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later;
case "SharedArrayBuffer":
case "Atomics":
case "AsyncIterable":
case "AsyncIterableIterator":
case "AsyncGenerator":
case "AsyncGeneratorFunction":
case "BigInt":
case "Reflect":
case "BigInt64Array":
case "BigUint64Array":
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_1_or_later;
default:
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
return Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;
Expand Down Expand Up @@ -25852,7 +25870,15 @@ namespace ts {
relatedInfo = suggestion.valueDeclaration && createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestedName);
}
else {
errorInfo = chainDiagnosticMessages(elaborateNeverIntersection(errorInfo, containingType), Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
const missingProperty = declarationNameToString(propNode);
const container = typeToString(containingType);
const lib = getSuggestedLibForNonExistentProperty(missingProperty, containingType);
if (lib) {
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_2_or_later, missingProperty, container, lib);
}
else {
errorInfo = chainDiagnosticMessages(elaborateNeverIntersection(errorInfo, containingType), Diagnostics.Property_0_does_not_exist_on_type_1, missingProperty, container);
}
}
}
}
Expand All @@ -25868,6 +25894,39 @@ namespace ts {
return prop !== undefined && prop.valueDeclaration && hasSyntacticModifier(prop.valueDeclaration, ModifierFlags.Static);
}

function getSuggestedLibForNonExistentName(name: __String | Identifier) {
const missingName = diagnosticName(name);
const allFeatures = getScriptTargetFeatures();
const libTargets = getOwnKeys(allFeatures);
for (const libTarget of libTargets) {
const containingTypes = getOwnKeys(allFeatures[libTarget]);
for (const containingType of containingTypes) {
if (containingType === missingName) {
return libTarget;
}
}
}
}

function getSuggestedLibForNonExistentProperty(missingProperty: string, containingType: Type) {
const container = getApparentType(containingType).symbol;
if (container) {
const allFeatures = getScriptTargetFeatures();
const libTargets = getOwnKeys(allFeatures);
for (const libTarget of libTargets) {
const featuresOfLib = allFeatures[libTarget];
const featuresOfContainingType = featuresOfLib[symbolName(container)];
if (featuresOfContainingType) {
for (const feature of featuresOfContainingType) {
if (feature === missingProperty) {
return libTarget;
}
}
}
}
}
}

function getSuggestedSymbolForNonexistentProperty(name: Identifier | PrivateIdentifier | string, containingType: Type): Symbol | undefined {
return getSpellingSuggestionForName(isString(name) ? name : idText(name), getPropertiesOfType(containingType), SymbolFlags.Value);
}
Expand Down Expand Up @@ -25957,6 +26016,7 @@ namespace ts {
*/
function getSpellingSuggestionForName(name: string, symbols: Symbol[], meaning: SymbolFlags): Symbol | undefined {
return getSpellingSuggestion(name, symbols, getCandidateName);

function getCandidateName(candidate: Symbol) {
const candidateName = symbolName(candidate);
if (startsWith(candidateName, "\"")) {
Expand Down
6 changes: 5 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,10 @@
"category": "Error",
"code": 2549
},
"Property '{0}' does not exist on type '{1}'. Do you need to change your target library? Try changing the `lib` compiler option to '{2}' or later.": {
"category": "Error",
"code": 2550
},
"Property '{0}' does not exist on type '{1}'. Did you mean '{2}'?": {
"category": "Error",
"code": 2551
Expand Down Expand Up @@ -2313,7 +2317,7 @@
"category": "Error",
"code": 2582
},
"Cannot find name '{0}'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.": {
"Cannot find name '{0}'. Do you need to change your target library? Try changing the `lib` compiler option to '{1}' or later.": {
"category": "Error",
"code": 2583
},
Expand Down
71 changes: 70 additions & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,76 @@ namespace ts {
return emitNode && emitNode.flags || 0;
}

interface ScriptTargetFeatures {
[key: string]: { [key: string]: string[] | undefined };
};

export function getScriptTargetFeatures(): ScriptTargetFeatures {
return {
es2015: {
Array: ["find", "findIndex", "fill", "copyWithin", "entries", "keys", "values"],
RegExp: ["flags", "sticky", "unicode"],
Reflect: ["apply", "construct", "defineProperty", "deleteProperty", "get"," getOwnPropertyDescriptor", "getPrototypeOf", "has", "isExtensible", "ownKeys", "preventExtensions", "set", "setPrototypeOf"],
ArrayConstructor: ["from", "of"],
ObjectConstructor: ["assign", "getOwnPropertySymbols", "keys", "is", "setPrototypeOf"],
NumberConstructor: ["isFinite", "isInteger", "isNaN", "isSafeInteger", "parseFloat", "parseInt"],
Math: ["clz32", "imul", "sign", "log10", "log2", "log1p", "expm1", "cosh", "sinh", "tanh", "acosh", "asinh", "atanh", "hypot", "trunc", "fround", "cbrt"],
Map: ["entries", "keys", "values"],
Set: ["entries", "keys", "values"],
Promise: ["all", "race", "reject", "resolve"],
Symbol: ["for", "keyFor"],
WeakMap: ["entries", "keys", "values"],
WeakSet: ["entries", "keys", "values"],
Iterator: emptyArray,
AsyncIterator: emptyArray,
String: ["codePointAt", "includes", "endsWith", "normalize", "repeat", "startsWith", "anchor", "big", "blink", "bold", "fixed", "fontcolor", "fontsize", "italics", "link", "small", "strike", "sub", "sup"],
StringConstructor: ["fromCodePoint", "raw"]
},
es2016: {
Array: ["includes"]
},
es2017: {
Atomics: emptyArray,
SharedArrayBuffer: emptyArray,
String: ["padStart", "padEnd"],
ObjectConstructor: ["values", "entries", "getOwnPropertyDescriptors"],
DateTimeFormat: ["formatToParts"]
},
es2018: {
Promise: ["finally"],
RegExpMatchArray: ["groups"],
RegExpExecArray: ["groups"],
RegExp: ["dotAll"],
Intl: ["PluralRules"],
AsyncIterable: emptyArray,
AsyncIterableIterator: emptyArray,
AsyncGenerator: emptyArray,
AsyncGeneratorFunction: emptyArray,
},
es2019: {
Array: ["flat", "flatMap"],
ObjectConstructor: ["fromEntries"],
String: ["trimStart", "trimEnd", "trimLeft", "trimRight"],
Symbol: ["description"]
},
es2020: {
BigInt: emptyArray,
BigInt64Array: emptyArray,
BigUint64Array: emptyArray,
Promise: ["allSettled"],
SymbolConstructor: ["matchAll"],
String: ["matchAll"],
DataView: ["setBigInt64", "setBigUint64", "getBigInt64", "getBigUint64"],
RelativeTimeFormat: ["format", "formatToParts", "resolvedOptions"]
},
esnext: {
Promise: ["any"],
String: ["replaceAll"],
NumberFormat: ["formatToParts"]
}
};
}

export const enum GetLiteralTextFlags {
None = 0,
NeverAsciiEscape = 1 << 0,
Expand Down Expand Up @@ -5797,7 +5867,6 @@ namespace ts {
if (arguments.length > 2) {
text = formatStringFromArgs(text, arguments, 2);
}

return {
messageText: text,
category: message.category,
Expand Down
Loading