Skip to content

Commit b420924

Browse files
committed
respect baseUrl
1 parent c358f38 commit b420924

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function init() {
3636
return getCompletionEntryDetails(fileName, position, name, options, source, preferences, data);
3737
}
3838

39-
return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath);
39+
return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath, info.project);
4040
};
4141
}
4242

src/lib/import.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
1+
import ts, { CodeFixAction, InferencePriority, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
22
import * as path from 'path';
33

44
type PluginConfig = {
@@ -34,8 +34,9 @@ export function getCompletionEntryDetails(
3434
name: string,
3535
selfPath: string,
3636
modulePath: string,
37+
project: ts.server.Project,
3738
): ts.CompletionEntryDetails {
38-
const importPath = path.relative(path.dirname(selfPath), modulePath);
39+
const importPath = transformModulePath(selfPath, modulePath, project);
3940
const text = `import * as ${name} from "${getFilePathWithoutExt(importPath)}";\n`;
4041
const action: CodeFixAction = {
4142
fixName: 'namespace-import',
@@ -74,3 +75,12 @@ function getFilePathWithoutExt(filePath: string): string {
7475
const ext = path.extname(filePath);
7576
return filePath.slice(0, filePath.length - ext.length);
7677
}
78+
79+
function transformModulePath(selfPath: string, filePath: string, project: ts.server.Project) {
80+
const compilerOptions = project.getCompilerOptions();
81+
if (compilerOptions.baseUrl) {
82+
return path.relative(compilerOptions.baseUrl, filePath);
83+
} else {
84+
return './' + path.relative(path.dirname(selfPath), filePath);
85+
}
86+
}

0 commit comments

Comments
 (0)