Skip to content

Commit 5eb1011

Browse files
authored
Merge pull request #6 from Monchi/ignore-named-import
Option to remove named export from auto-completion
2 parents b4e7133 + 64a7e27 commit 5eb1011

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,24 @@ Then add this plugin in `tsconfig.json`.
7676
### paths (required)
7777

7878
Specify directory in relative path to the project's root (`tsconfig.json`'s dir). All `.ts` or `.js` files in the directories can be Namespace Imported with auto-completion.
79+
80+
example:
81+
82+
```json
83+
"options": {
84+
"paths": ["src/logics"]
85+
}
86+
```
87+
88+
### ignoreNamedExport
89+
90+
If true, named export from files in `paths` won't be shown in auto-completion.
91+
92+
example:
93+
94+
```json
95+
"options": {
96+
"paths": ["src/logics"],
97+
"ignoreNamedExport": true
98+
}
99+
```

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ function init() {
2525
return original;
2626
}
2727

28-
original.entries = [...original.entries, ...namespaceImportPlugin.getCompletionEntries(info)];
28+
const originalEntries = namespaceImportPlugin.filterNamedImportEntries(original.entries, info);
29+
const namespaceImportEntries = namespaceImportPlugin.getCompletionEntries(info);
30+
original.entries = [...originalEntries, ...namespaceImportEntries];
2931
return original;
3032
};
3133

src/lib/import.ts

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

4-
type PluginOptions = {
4+
export type PluginOptions = {
55
paths: readonly string[];
6+
ignoreNamedExport?: boolean;
67
};
78

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

11-
return filePaths.map((filePath) => {
12-
const name = getFileNameWithoutExt(filePath);
12+
return modulePaths.map((modulePath) => {
13+
const name = getFileNameWithoutExt(modulePath);
1314
return {
1415
name: name,
1516
kind: ts.ScriptElementKind.alias,
16-
source: filePath,
17+
source: modulePath,
1718
sortText: name,
1819
hasAction: true,
1920
isImportStatementCompletion: true,
2021
data: {
2122
exportName: name,
22-
modulePath: filePath,
23+
modulePath: modulePath,
2324
},
2425
};
2526
});
2627
}
2728

29+
export function filterNamedImportEntries(
30+
entries: ts.CompletionEntry[],
31+
info: ts.server.PluginCreateInfo,
32+
): ts.CompletionEntry[] {
33+
const options: PluginOptions = info.config.options;
34+
if (!options.ignoreNamedExport) {
35+
return entries;
36+
}
37+
38+
const currentDir = info.project.getCurrentDirectory();
39+
const dirPaths = options.paths.map((dirPath) => path.resolve(currentDir, dirPath));
40+
return entries.filter((entry) => {
41+
return !dirPaths.some((dirPath) => entry.data?.exportName && entry.data.fileName?.startsWith(dirPath));
42+
});
43+
}
44+
2845
export function getCompletionEntryDetails(
2946
name: string,
3047
selfPath: string,
@@ -52,16 +69,16 @@ export function getCodeFixActionByName(
5269
return null;
5370
}
5471

55-
const filePaths = getPathsToImport(info.config.options, info.project);
56-
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);
5774
if (modulePath) {
5875
return getCodeFixActionFromPath(name, selfPath, modulePath, info.project);
5976
} else {
6077
return null;
6178
}
6279
}
6380

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

6784
return options.paths.flatMap((dirPath) => {
@@ -79,23 +96,27 @@ function getFilePathWithoutExt(filePath: string): string {
7996
return filePath.slice(0, filePath.length - ext.length);
8097
}
8198

82-
function transformModulePath(selfPath: string, filePath: string, project: ts.server.Project) {
99+
function getModuleSpceifier(selfPath: string, modulePath: string, project: ts.server.Project) {
83100
const compilerOptions = project.getCompilerOptions();
101+
102+
let specifier: string;
84103
if (compilerOptions.baseUrl) {
85-
return path.relative(compilerOptions.baseUrl, filePath);
104+
specifier = path.relative(compilerOptions.baseUrl, modulePath);
86105
} else {
87-
return './' + path.relative(path.dirname(selfPath), filePath);
106+
specifier = './' + path.relative(path.dirname(selfPath), modulePath);
88107
}
108+
109+
return getFilePathWithoutExt(specifier);
89110
}
90111

91-
export function getCodeFixActionFromPath(
112+
function getCodeFixActionFromPath(
92113
name: string,
93114
selfPath: string,
94115
modulePath: string,
95116
project: ts.server.Project,
96117
): CodeFixAction {
97-
const importPath = transformModulePath(selfPath, modulePath, project);
98-
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`;
99120
return {
100121
fixName: 'namespace-import',
101122
description: text,

0 commit comments

Comments
 (0)