Skip to content

Commit 81be170

Browse files
authored
Merge pull request #8 from Monchi/import-case
2 parents fcfe171 + 6f5ccc7 commit 81be170

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ Then add this plugin in `tsconfig.json`.
7575

7676
### paths (required)
7777

78+
Value: `string[]`
79+
7880
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.
7981

80-
example:
82+
Example:
8183

8284
```json
8385
"options": {
@@ -87,13 +89,32 @@ example:
8789

8890
### ignoreNamedExport
8991

92+
Value: `boolean`
93+
9094
If true, named export from files in `paths` won't be shown in auto-completion.
9195

92-
example:
96+
Example:
9397

9498
```json
9599
"options": {
96100
"paths": ["src/logics"],
97101
"ignoreNamedExport": true
98102
}
99103
```
104+
105+
### nameTransform
106+
107+
Value: `"upperCamelCase" | "lowerCamelCase"`
108+
109+
Transform import name. If not set, the filename will be used as an import name.
110+
111+
Example:
112+
113+
```json
114+
"options": {
115+
"paths": ["src/logics"],
116+
"nameTransform": "lowerCamelCase"
117+
}
118+
```
119+
120+
Then `SomeLogic.ts` will be imported like `import * as someLogic from "./SomeLogic"`.

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"homepage": "https://github.com/Monchi/typescript-plugin-namespace-import#readme",
1919
"dependencies": {
20+
"camelcase": "^6.2.0",
2021
"typescript": "^4.3.5"
2122
},
2223
"devDependencies": {

src/index.ts

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

41-
return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath, info.project);
41+
return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath, info);
4242
};
4343

4444
const getCodeFixesAtPosition = info.languageService.getCodeFixesAtPosition;

src/lib/import.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
22
import * as path from 'path';
3+
import camelCase from 'camelcase';
34

45
export type PluginOptions = {
56
paths: readonly string[];
67
ignoreNamedExport?: boolean;
8+
nameTransform?: 'upperCamelCase' | 'lowerCamelCase';
79
};
810

911
export function getCompletionEntries(info: ts.server.PluginCreateInfo): ts.CompletionEntry[] {
1012
const modulePaths = getModulePathsToImport(info.config.options, info.project);
1113

1214
return modulePaths.map((modulePath) => {
13-
const name = getFileNameWithoutExt(modulePath);
15+
const name = transformImportName(getFileNameWithoutExt(modulePath), info.config.options);
1416
return {
1517
name: name,
1618
kind: ts.ScriptElementKind.alias,
@@ -46,9 +48,9 @@ export function getCompletionEntryDetails(
4648
name: string,
4749
selfPath: string,
4850
modulePath: string,
49-
project: ts.server.Project,
51+
info: ts.server.PluginCreateInfo,
5052
): ts.CompletionEntryDetails {
51-
const action: CodeFixAction = getCodeFixActionFromPath(name, selfPath, modulePath, project);
53+
const action: CodeFixAction = getCodeFixActionFromPath(name, selfPath, modulePath, info.project);
5254
return {
5355
name: name,
5456
kind: ScriptElementKind.alias,
@@ -139,3 +141,11 @@ function getCodeFixActionFromPath(
139141
commands: [],
140142
};
141143
}
144+
145+
function transformImportName(name: string, options: PluginOptions) {
146+
if (options.nameTransform) {
147+
return camelCase(name, { pascalCase: options.nameTransform === 'upperCamelCase' });
148+
} else {
149+
return name;
150+
}
151+
}

0 commit comments

Comments
 (0)