Don’t offer import fix for members of arrays or classes (#35635)

* Write failing test

* Don’t offer import fix for members of arrays or classes
This commit is contained in:
Andrew Branch
2019-12-13 14:20:54 -08:00
committed by GitHub
parent 543ec2362b
commit 8a88c1c84c
5 changed files with 66 additions and 3 deletions

View File

@@ -2878,7 +2878,11 @@ namespace ts {
}
const type = getTypeOfSymbol(exportEquals);
return type.flags & TypeFlags.Primitive ? undefined : getPropertyOfType(type, memberName);
return type.flags & TypeFlags.Primitive ||
getObjectFlags(type) & ObjectFlags.Class ||
isArrayOrTupleLikeType(type)
? undefined
: getPropertyOfType(type, memberName);
}
function getExportsOfSymbol(symbol: Symbol): SymbolTable {

View File

@@ -2553,7 +2553,7 @@ namespace FourSlash {
* Rerieves a codefix satisfying the parameters, or undefined if no such codefix is found.
* @param fileName Path to file where error should be retrieved from.
*/
private getCodeFixes(fileName: string, errorCode?: number, preferences: ts.UserPreferences = ts.emptyOptions): readonly ts.CodeFixAction[] {
private getCodeFixes(fileName: string, errorCode?: number, preferences: ts.UserPreferences = ts.emptyOptions, position?: number): readonly ts.CodeFixAction[] {
const diagnosticsForCodeFix = this.getDiagnostics(fileName, /*includeSuggestions*/ true).map(diagnostic => ({
start: diagnostic.start,
length: diagnostic.length,
@@ -2564,7 +2564,12 @@ namespace FourSlash {
if (errorCode !== undefined && errorCode !== diagnostic.code) {
return;
}
if (position !== undefined && diagnostic.start !== undefined && diagnostic.length !== undefined) {
const span = ts.createTextRangeFromSpan({ start: diagnostic.start, length: diagnostic.length });
if (!ts.textRangeContainsPositionInclusive(span, position)) {
return;
}
}
return this.languageService.getCodeFixesAtPosition(fileName, diagnostic.start!, diagnostic.start! + diagnostic.length!, [diagnostic.code], this.formatCodeSettings, preferences);
});
}
@@ -2614,6 +2619,23 @@ namespace FourSlash {
});
}
public verifyImportFixModuleSpecifiers(markerName: string, moduleSpecifiers: string[]) {
const marker = this.getMarkerByName(markerName);
const codeFixes = this.getCodeFixes(marker.fileName, ts.Diagnostics.Cannot_find_name_0.code, {
includeCompletionsForModuleExports: true,
includeCompletionsWithInsertText: true
}, marker.position).filter(f => f.fixId === ts.codefix.importFixId);
const actualModuleSpecifiers = ts.mapDefined(codeFixes, fix => {
return ts.forEach(ts.flatMap(fix.changes, c => c.textChanges), c => {
const match = /(?:from |require\()(['"])((?:(?!\1).)*)\1/.exec(c.newText);
return match?.[2];
});
});
assert.deepEqual(actualModuleSpecifiers, moduleSpecifiers);
}
public verifyDocCommentTemplate(expected: ts.TextInsertion | undefined) {
const name = "verifyDocCommentTemplate";
const actual = this.languageService.getDocCommentTemplateAtPosition(this.activeFile.fileName, this.currentCaretPosition)!;

View File

@@ -434,6 +434,10 @@ namespace FourSlashInterface {
this.state.verifyImportFixAtPosition(expectedTextArray, errorCode, preferences);
}
public importFixModuleSpecifiers(marker: string, moduleSpecifiers: string[]) {
this.state.verifyImportFixModuleSpecifiers(marker, moduleSpecifiers);
}
public navigationBar(json: any, options?: { checkSpans?: boolean }) {
this.state.verifyNavigationBar(json, options);
}