Skip to content

Add nameTransform option to transform import name #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ Then add this plugin in `tsconfig.json`.

### paths (required)

Value: `string[]`

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.

example:
Example:

```json
"options": {
Expand All @@ -87,13 +89,32 @@ example:

### ignoreNamedExport

Value: `boolean`

If true, named export from files in `paths` won't be shown in auto-completion.

example:
Example:

```json
"options": {
"paths": ["src/logics"],
"ignoreNamedExport": true
}
```

### nameTransform

Value: `"upperCamelCase" | "lowerCamelCase"`

Transform import name. If not set, the filename will be used as an import name.

Example:

```json
"options": {
"paths": ["src/logics"],
"nameTransform": "lowerCamelCase"
}
```

Then `SomeLogic.ts` will be imported like `import * as someLogic from "./SomeLogic"`.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"homepage": "https://github.com/Monchi/typescript-plugin-namespace-import#readme",
"dependencies": {
"camelcase": "^6.2.0",
"typescript": "^4.3.5"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function init() {
return getCompletionEntryDetails(fileName, position, name, options, source, preferences, data);
}

return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath, info.project);
return namespaceImportPlugin.getCompletionEntryDetails(name, fileName, data.modulePath, info);
};

const getCodeFixesAtPosition = info.languageService.getCodeFixesAtPosition;
Expand Down
16 changes: 13 additions & 3 deletions src/lib/import.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import ts, { CodeFixAction, ScriptElementKind } from 'typescript/lib/tsserverlibrary';
import * as path from 'path';
import camelCase from 'camelcase';

export type PluginOptions = {
paths: readonly string[];
ignoreNamedExport?: boolean;
nameTransform?: 'upperCamelCase' | 'lowerCamelCase';
};

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

return modulePaths.map((modulePath) => {
const name = getFileNameWithoutExt(modulePath);
const name = transformImportName(getFileNameWithoutExt(modulePath), info.config.options);
return {
name: name,
kind: ts.ScriptElementKind.alias,
Expand Down Expand Up @@ -46,9 +48,9 @@ export function getCompletionEntryDetails(
name: string,
selfPath: string,
modulePath: string,
project: ts.server.Project,
info: ts.server.PluginCreateInfo,
): ts.CompletionEntryDetails {
const action: CodeFixAction = getCodeFixActionFromPath(name, selfPath, modulePath, project);
const action: CodeFixAction = getCodeFixActionFromPath(name, selfPath, modulePath, info.project);
return {
name: name,
kind: ScriptElementKind.alias,
Expand Down Expand Up @@ -139,3 +141,11 @@ function getCodeFixActionFromPath(
commands: [],
};
}

function transformImportName(name: string, options: PluginOptions) {
if (options.nameTransform) {
return camelCase(name, { pascalCase: options.nameTransform === 'upperCamelCase' });
} else {
return name;
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy