mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
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:
@@ -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 {
|
||||
|
||||
@@ -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)!;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -330,6 +330,7 @@ declare namespace FourSlashInterface {
|
||||
fileAfterApplyingRefactorAtMarker(markerName: string, expectedContent: string, refactorNameToApply: string, formattingOptions?: FormatCodeOptions): void;
|
||||
getAndApplyCodeFix(errorCode?: number, index?: number): void;
|
||||
importFixAtPosition(expectedTextArray: string[], errorCode?: number, options?: UserPreferences): void;
|
||||
importFixModuleSpecifiers(marker: string, moduleSpecifiers: string[]): void;
|
||||
|
||||
navigationBar(json: any, options?: { checkSpans?: boolean }): void;
|
||||
navigationTree(json: any, options?: { checkSpans?: boolean }): void;
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @target: es2015
|
||||
// @strict: true
|
||||
// @esModuleInterop: true
|
||||
|
||||
// @Filename: /array.ts
|
||||
////declare const arr: number[];
|
||||
////export = arr;
|
||||
|
||||
// @Filename: /class-instance-member.ts
|
||||
////class C { filter() {} }
|
||||
////export = new C();
|
||||
|
||||
// @Filename: /object-literal.ts
|
||||
////declare function filter(): void;
|
||||
////export = { filter };
|
||||
|
||||
// @Filename: /jquery.d.ts
|
||||
////interface JQueryStatic {
|
||||
//// filter(): void;
|
||||
////}
|
||||
////declare const $: JQueryStatic;
|
||||
////export = $;
|
||||
|
||||
// @Filename: /jquery.js
|
||||
////module.exports = {};
|
||||
|
||||
// @Filename: /index.ts
|
||||
////filter/**/
|
||||
|
||||
verify.importFixModuleSpecifiers('', ['./object-literal', './jquery']);
|
||||
Reference in New Issue
Block a user