Skip to content

Commit

Permalink
fix: double import if using import type (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmihaylov authored May 24, 2022
1 parent f761e93 commit c7ace0c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/typegen/src/write/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import {WriteFile} from '.'

const queryNamespace = 'queries' // todo: at some point we might want to make this configurable

const importVariations = [
'import * as',
'import', // synthetic default import
'import type * as', // type-only import
'import type', // type-only synthetic default import
]

export const defaultGetQueriesModule = (filepath: string) => filepath

export interface WriteTSFileOptions {
Expand Down Expand Up @@ -52,19 +59,20 @@ export function getFileWriter({getQueriesModulePath = defaultGetQueriesModule, w
await writeFile(destPath, content)

const importPath = relativeUnixPath(destPath, path.dirname(file))
const importStatement = `import * as ${queryNamespace} from './${importPath.replace(/\.(js|ts|tsx)$/, '')}'`
const importPathNoExtension = importPath.replace(/\.(js|ts|tsx)$/, '')

const importStatementVariations = [
...importVariations.map(imps => `${imps} ${queryNamespace} from './${importPathNoExtension}'`), // single quotes
...importVariations.map(imps => `${imps} ${queryNamespace} from "./${importPathNoExtension}"`), // double quotes
]

const importExists =
originalSource.includes(importStatement) ||
originalSource.includes(importStatement.replace(/'/g, `"`)) || // double quotes
originalSource.includes(importStatement.replace('import * as', 'import')) || // synthetic default import
originalSource.includes(importStatement.replace(/'/g, `"`).replace('import * as', 'import')) // synthetic default import with double quotes
const importExists = importStatementVariations.some(imp => originalSource.includes(imp))

if (!importExists) {
edits.push({
start: 0,
end: 0,
replacement: importStatement + '\n',
replacement: `import * as ${queryNamespace} from './${importPathNoExtension}'\n`,
})
}
}
Expand Down
26 changes: 26 additions & 0 deletions packages/typegen/test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ test('can write queries to separate file', async () => {
export default sql\`select 1 as a\`
module queries {
// this should be removed!
}
`,
// this file has already imported its queries with a type-only import
'c.ts': `
import {sql} from 'slonik'
import type * as queries from "./__sql__/c";
export default sql\`select 1 as a\`
module queries {
// this should be removed!
}
Expand Down Expand Up @@ -348,6 +359,12 @@ test('can write queries to separate file', async () => {
export default sql<queries.A>\`select 1 as a\`
c.ts: |-
import {sql} from 'slonik'
import type * as queries from './__sql__/c'
export default sql<queries.A>\`select 1 as a\`
__sql__:
a.ts: |-
// Generated by @slonik/typegen
Expand All @@ -366,6 +383,15 @@ test('can write queries to separate file', async () => {
/** regtype: \`integer\` */
a: number | null
}
c.ts: |-
// Generated by @slonik/typegen
/** - query: \`select 1 as a\` */
export interface A {
/** regtype: \`integer\` */
a: number | null
}
"
`)
})
Expand Down

0 comments on commit c7ace0c

Please sign in to comment.