Skip to content

Commit

Permalink
fix: organize import to consider { type Name } sytax
Browse files Browse the repository at this point in the history
  • Loading branch information
rintoj committed Jul 11, 2024
1 parent fc28071 commit 03c5573
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/ts/create-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export function createImport(from: string, ...imports: string[]) {
undefined,
ts.factory.createNamedImports(
imports.map(item => {
const [name, property] = item.split(':')
const [name, property, isTypeOnly] = item.split(':')
return ts.factory.createImportSpecifier(
false,
isTypeOnly === 'true',
property ? ts.factory.createIdentifier(property) : undefined,
ts.factory.createIdentifier(name),
)
Expand All @@ -33,9 +33,9 @@ export function createTypeOnlyImport(from: string, ...imports: string[]) {
undefined,
ts.factory.createNamedImports(
imports.map(item => {
const [name, property] = item.split(':')
const [name, property, isTypeOnly] = item.split(':')
return ts.factory.createImportSpecifier(
false,
isTypeOnly === 'true',
property ? ts.factory.createIdentifier(property) : undefined,
ts.factory.createIdentifier(name),
)
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 @@ -119,4 +119,25 @@ describe('organizeImport', () => {
`),
)
})

test('to organize imports with inline type', async () => {
const sourceFile = parseTS(`
import { Context, Parent, ResolveField, Resolver } from '@nestjs/graphql'
import { type GQLContext } from '../../context'
import { type LocationService as LS } from '../location/location.service'
import { StorageLocation } from './storage-location.model'
import { StorageLocationType } from './storage-location.type'
import { FieldResolver as FR } 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 as FR } from 'src/common/field-resolver-type'
import { type GQLContext } from '../../context'
import { type LocationService as LS } from '../location/location.service'
import { StorageLocation } from './storage-location.model'
import { StorageLocationType } from './storage-location.type'
`),
)
})
})
4 changes: 2 additions & 2 deletions src/ts/organize-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function organizeImports(sourceFile: ts.SourceFile): ts.SourceFile {
statement.importClause.namedBindings.elements.map(el => {
importStatements[importFrom].isTypeOnly = true
return importStatements[importFrom].names.add(
[el.name.text, el.propertyName?.text].filter(Boolean).join(':'),
[el.name.text, el.propertyName?.text ?? '', el.isTypeOnly].join(':'),
)
})
} else if (
Expand All @@ -49,7 +49,7 @@ export function organizeImports(sourceFile: ts.SourceFile): ts.SourceFile {
) {
statement.importClause.namedBindings.elements.map(el =>
importStatements[importFrom].names.add(
[el.name.text, el.propertyName?.text].filter(Boolean).join(':'),
[el.name.text, el.propertyName?.text ?? '', el.isTypeOnly].join(':'),
),
)
}
Expand Down

0 comments on commit 03c5573

Please sign in to comment.