Skip to content

Commit

Permalink
Merge pull request #13 from rintoj/feat/definition-provider-enhancements
Browse files Browse the repository at this point in the history
fix: issue with organize imports not supporting alias
  • Loading branch information
rintoj authored Jul 9, 2024
2 parents 0c39881 + 9f2ed9f commit 544f12e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/ts/create-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ export function createImport(from: string, ...imports: string[]) {
false,
undefined,
ts.factory.createNamedImports(
imports.map(item =>
ts.factory.createImportSpecifier(false, undefined, ts.factory.createIdentifier(item)),
),
imports.map(item => {
const [name, property] = item.split(':')
return ts.factory.createImportSpecifier(
false,
property ? ts.factory.createIdentifier(property) : undefined,
ts.factory.createIdentifier(name),
)
}),
),
)
: undefined,
Expand Down
21 changes: 21 additions & 0 deletions src/ts/organize-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,25 @@ describe('organizeImport', () => {
`),
)
})

test('to organize imports with alias', async () => {
const sourceFile = parseTS(`
import { Context, Parent, ResolveField, Resolver } from '@nestjs/graphql'
import { GQLContext as Context } from '../../context'
import { LocationService } from '../location/location.service'
import { StorageLocation } from './storage-location.model'
import { StorageLocationType } from './storage-location.type'
import { FieldResolver } from 'src/common/field-resolver-type'`)
const code = await prettify(printTS(organizeImports(sourceFile)))
expect(toParsedOutput(code)).toEqual(
toParsedOutput(`
import { Context, Parent, ResolveField, Resolver } from '@nestjs/graphql'
import { FieldResolver } from 'src/common/field-resolver-type'
import { GQLContext as Context } from '../../context'
import { LocationService } from '../location/location.service'
import { StorageLocation } from './storage-location.model'
import { StorageLocationType } from './storage-location.type'
`),
)
})
})
4 changes: 3 additions & 1 deletion src/ts/organize-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export function organizeImports(sourceFile: ts.SourceFile): ts.SourceFile {
ts.isNamedImports(statement.importClause.namedBindings)
) {
statement.importClause.namedBindings.elements.map(el =>
importStatements[importFrom].names.add(el.name.text),
importStatements[importFrom].names.add(
[el.name.text, el.propertyName?.text].filter(Boolean).join(':'),
),
)
}
}
Expand Down

0 comments on commit 544f12e

Please sign in to comment.