Skip to content

Commit b5fe247

Browse files
committed
refactor
1 parent 29234d2 commit b5fe247

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/lib/import.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ export type PluginOptions = {
77
};
88

99
export function getCompletionEntries(info: ts.server.PluginCreateInfo): ts.CompletionEntry[] {
10-
const filePaths = getPathsToImport(info.config.options, info.project);
10+
const modulePaths = getModulePathsToImport(info.config.options, info.project);
1111

12-
return filePaths.map((filePath) => {
13-
const name = getFileNameWithoutExt(filePath);
12+
return modulePaths.map((modulePath) => {
13+
const name = getFileNameWithoutExt(modulePath);
1414
return {
1515
name: name,
1616
kind: ts.ScriptElementKind.alias,
17-
source: filePath,
17+
source: modulePath,
1818
sortText: name,
1919
hasAction: true,
2020
isImportStatementCompletion: true,
2121
data: {
2222
exportName: name,
23-
modulePath: filePath,
23+
modulePath: modulePath,
2424
},
2525
};
2626
});
@@ -69,16 +69,16 @@ export function getCodeFixActionByName(
6969
return null;
7070
}
7171

72-
const filePaths = getPathsToImport(info.config.options, info.project);
73-
const modulePath = filePaths.find((filePath) => getFileNameWithoutExt(filePath) === name);
72+
const modulePaths = getModulePathsToImport(info.config.options, info.project);
73+
const modulePath = modulePaths.find((filePath) => getFileNameWithoutExt(filePath) === name);
7474
if (modulePath) {
7575
return getCodeFixActionFromPath(name, selfPath, modulePath, info.project);
7676
} else {
7777
return null;
7878
}
7979
}
8080

81-
function getPathsToImport(options: PluginOptions, project: ts.server.Project): string[] {
81+
function getModulePathsToImport(options: PluginOptions, project: ts.server.Project): string[] {
8282
const currentDir = project.getCurrentDirectory();
8383

8484
return options.paths.flatMap((dirPath) => {
@@ -96,13 +96,17 @@ function getFilePathWithoutExt(filePath: string): string {
9696
return filePath.slice(0, filePath.length - ext.length);
9797
}
9898

99-
function transformModulePath(selfPath: string, filePath: string, project: ts.server.Project) {
99+
function getModuleSpceifier(selfPath: string, modulePath: string, project: ts.server.Project) {
100100
const compilerOptions = project.getCompilerOptions();
101+
102+
let specifier: string;
101103
if (compilerOptions.baseUrl) {
102-
return path.relative(compilerOptions.baseUrl, filePath);
104+
specifier = path.relative(compilerOptions.baseUrl, modulePath);
103105
} else {
104-
return './' + path.relative(path.dirname(selfPath), filePath);
106+
specifier = './' + path.relative(path.dirname(selfPath), modulePath);
105107
}
108+
109+
return getFilePathWithoutExt(specifier);
106110
}
107111

108112
function getCodeFixActionFromPath(
@@ -111,8 +115,8 @@ function getCodeFixActionFromPath(
111115
modulePath: string,
112116
project: ts.server.Project,
113117
): CodeFixAction {
114-
const importPath = transformModulePath(selfPath, modulePath, project);
115-
const text = `import * as ${name} from "${getFilePathWithoutExt(importPath)}";\n`;
118+
const moduleSpecifier = getModuleSpceifier(selfPath, modulePath, project);
119+
const text = `import * as ${name} from "${moduleSpecifier}";\n`;
116120
return {
117121
fixName: 'namespace-import',
118122
description: text,

0 commit comments

Comments
 (0)