|
1 |
| -import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary'; |
| 1 | +import ts, { CodeFixAction, InferencePriority, ScriptElementKind } from 'typescript/lib/tsserverlibrary'; |
2 | 2 | import * as path from 'path';
|
3 | 3 |
|
4 | 4 | type PluginConfig = {
|
@@ -34,8 +34,9 @@ export function getCompletionEntryDetails(
|
34 | 34 | name: string,
|
35 | 35 | selfPath: string,
|
36 | 36 | modulePath: string,
|
| 37 | + project: ts.server.Project, |
37 | 38 | ): ts.CompletionEntryDetails {
|
38 |
| - const importPath = path.relative(path.dirname(selfPath), modulePath); |
| 39 | + const importPath = transformModulePath(selfPath, modulePath, project); |
39 | 40 | const text = `import * as ${name} from "${getFilePathWithoutExt(importPath)}";\n`;
|
40 | 41 | const action: CodeFixAction = {
|
41 | 42 | fixName: 'namespace-import',
|
@@ -74,3 +75,12 @@ function getFilePathWithoutExt(filePath: string): string {
|
74 | 75 | const ext = path.extname(filePath);
|
75 | 76 | return filePath.slice(0, filePath.length - ext.length);
|
76 | 77 | }
|
| 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