Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
106 changes: 69 additions & 37 deletions src/services/codefixes/importFixes.ts

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ namespace ts.Completions {
}
case "symbol": {
const { symbol, location, symbolToOriginInfoMap, previousToken } = symbolCompletion;
const { codeActions, sourceDisplay } = getCompletionEntryCodeActionsAndSourceDisplay(symbolToOriginInfoMap, symbol, program, typeChecker, host, compilerOptions, sourceFile, previousToken, formatContext, program.getSourceFiles(), preferences);
const { codeActions, sourceDisplay } = getCompletionEntryCodeActionsAndSourceDisplay(symbolToOriginInfoMap, symbol, program, typeChecker, host, compilerOptions, sourceFile, position, previousToken, formatContext, preferences);
return createCompletionDetailsForSymbol(symbol, typeChecker, sourceFile, location!, cancellationToken, codeActions, sourceDisplay); // TODO: GH#18217
}
case "literal": {
Expand Down Expand Up @@ -652,9 +652,9 @@ namespace ts.Completions {
host: LanguageServiceHost,
compilerOptions: CompilerOptions,
sourceFile: SourceFile,
position: number,
previousToken: Node | undefined,
formatContext: formatting.FormatContext,
allSourceFiles: ReadonlyArray<SourceFile>,
preferences: UserPreferences,
): CodeActionsAndSourceDisplay {
const symbolOriginInfo = symbolToOriginInfoMap[getSymbolId(symbol)];
Expand All @@ -671,10 +671,8 @@ namespace ts.Completions {
getSymbolName(symbol, symbolOriginInfo, compilerOptions.target!),
host,
program,
checker,
allSourceFiles,
formatContext,
previousToken,
previousToken && isIdentifier(previousToken) ? previousToken.getStart(sourceFile) : position,
preferences);
return { sourceDisplay: [textPart(moduleSpecifier)], codeActions: [codeAction] };
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ namespace ts.textChanges {
this.changes.push({ kind: ChangeKind.Text, sourceFile, range, text });
}

private insertText(sourceFile: SourceFile, pos: number, text: string): void {
public insertText(sourceFile: SourceFile, pos: number, text: string): void {
this.replaceRangeWithText(sourceFile, createTextRange(pos), text);
}

Expand Down
8 changes: 8 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,14 @@ namespace ts {
}
}

export function getQuoteFromPreference(qp: QuotePreference): string {
switch (qp) {
case QuotePreference.Single: return "'";
case QuotePreference.Double: return '"';
default: return Debug.assertNever(qp);
}
}

export function symbolNameNoDefault(symbol: Symbol): string | undefined {
const escaped = symbolEscapedNameNoDefault(symbol);
return escaped === undefined ? undefined : unescapeLeadingUnderscores(escaped);
Expand Down
60 changes: 60 additions & 0 deletions tests/cases/fourslash/completionsImport_importType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// <reference path="fourslash.ts" />

// @allowJs: true

// @Filename: /a.js
////export const x = 0;
////export class C {}
/////** @typedef {number} T */

// @Filename: /b.js
/////** @type {/*0*/} */
/////** @type {/*1*/} */

verify.completions({
marker: ["0", "1"],
includes: [
{
name: "C",
source: "/a",
sourceDisplay: "./a",
text: "class C",
hasAction: true,
},
{
name: "T",
source: "/a",
sourceDisplay: "./a",
text: "type T = number",
hasAction: true,
},
],
excludes: "x",
preferences: {
includeCompletionsForModuleExports: true,
},
});

// Something with a value-side will get a normal import.
verify.applyCodeActionFromCompletion("0", {
name: "C",
source: "/a",
description: `Import 'C' from module "./a"`,
newFileContent:
`import { C } from "./a";

/** @type {} */
/** @type {} */`,
});

// A pure type will get `import().T`
verify.applyCodeActionFromCompletion("1", {
name: "T",
source: "/a",
description: `Change 'T' to 'import("./a").T'`,
newFileContent:
`import { C } from "./a";

/** @type {} */
/** @type {import("./a").} */`,
});
25 changes: 25 additions & 0 deletions tests/cases/fourslash/importNameCodeFix_all_js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference path='fourslash.ts' />

// @allowJs: true
// @checkJs: true

// @Filename: /a.js
////export class C {}
/////** @typedef {number} T */

// @Filename: /b.js
////C;
/////** @type {T} */
////const x = 0;

goTo.file("/b.js");
verify.codeFixAll({
fixId: "fixMissingImport",
fixAllDescription: "Add all missing imports",
newFileContent:
`import { C } from "./a";

C;
/** @type {import("./a").T} */
const x = 0;`,
});
17 changes: 17 additions & 0 deletions tests/cases/fourslash/importNameCodeFix_importType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// <reference path='fourslash.ts' />

// @allowJs: true
// @checkJs: true

// @Filename: /a.js
////export {};
/////** @typedef {number} T */

// @Filename: /b.js
/////** @type {T} */
////const x = 0;

goTo.file("/b.js");
verify.importFixAtPosition([
`/** @type {import("./a").T} */
const x = 0;`]);