Skip to content

Commit 2a18c19

Browse files
committed
fix: Duplicate import
closes: #18
1 parent b934fe6 commit 2a18c19

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

src/generate.spec.ts

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,18 @@ describe('model with one id int', () => {
330330
});
331331
});
332332

333-
it('duplicated fields in exising file', async () => {
334-
await testGenerate({
335-
schema: `
336-
model User {
337-
id Int @id
338-
}
339-
`,
340-
createSouceFile: {
341-
type: 'input',
342-
name: 'UserCreateInput',
343-
text: `
333+
describe('duplicated', () => {
334+
it('duplicated fields in exising file', async () => {
335+
await testGenerate({
336+
schema: `
337+
model User {
338+
id Int @id
339+
}
340+
`,
341+
createSouceFile: {
342+
type: 'input',
343+
name: 'UserCreateInput',
344+
text: `
344345
import { Int } from '@nestjs/graphql';
345346
@InputType()
346347
export class UserCreateInput {
@@ -349,14 +350,41 @@ it('duplicated fields in exising file', async () => {
349350
})
350351
id?: string;
351352
`,
352-
},
353+
},
354+
});
355+
setSourceFile('/user-create.input.ts');
356+
const classFile = sourceFile.getClass(() => true)!;
357+
const names = classFile.getProperties().map(p => p.getName());
358+
expect(names).toStrictEqual(['id']);
359+
});
360+
361+
it('duplicated import decorators', async () => {
362+
await testGenerate({
363+
schema: `
364+
model User {
365+
id Int @id
366+
xl Int
367+
xs Int
368+
}
369+
`,
370+
createSouceFile: {
371+
type: 'model',
372+
name: 'User',
373+
text: `
374+
import { Field, ObjectType } from '@nestjs/graphql';
375+
import { Int, ID } from '@nestjs/graphql';
376+
@ObjectType()
377+
export class User {
378+
@Field(() => ID) id: number;
379+
@Field(() => Int) xl: number;
380+
@Field(() => Int) xs: number;
381+
`,
382+
},
383+
});
384+
setSourceFile('user.model.ts');
385+
expect(imports.filter(x => x.name === 'Field')).toHaveLength(1);
386+
expect(imports.filter(x => x.name === 'ObjectType')).toHaveLength(1);
353387
});
354-
sourceFile = project.getSourceFile(s =>
355-
s.getFilePath().endsWith('/user-create.input.ts'),
356-
)!;
357-
const classFile = sourceFile.getClass(() => true)!;
358-
const names = classFile.getProperties().map(p => p.getName());
359-
expect(names).toStrictEqual(['id']);
360388
});
361389

362390
describe('one model with scalar types', () => {

src/handlers/model-output-type.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import assert from 'assert';
22
import JSON5 from 'json5';
3+
import { remove } from 'lodash';
34
import {
45
ClassDeclarationStructure,
56
CommentStatement,
67
ExportSpecifierStructure,
8+
ImportDeclarationStructure,
9+
ImportSpecifierStructure,
10+
OptionalKind,
711
StatementStructures,
812
StructureKind,
913
} from 'ts-morph';
@@ -32,7 +36,21 @@ export function modelOutputType(outputType: OutputType, args: EventArguments) {
3236
type: 'model',
3337
});
3438
const sourceFileStructure = sourceFile.getStructure();
35-
const importDeclarations = new ImportDeclarationMap();
39+
const imports = remove(
40+
sourceFileStructure.statements as StatementStructures[],
41+
s => s.kind === StructureKind.ImportDeclaration,
42+
).flatMap(s => {
43+
return ((s as ImportDeclarationStructure)
44+
.namedImports as OptionalKind<ImportSpecifierStructure>[]).map(x => [
45+
x.name || x.alias,
46+
{
47+
moduleSpecifier: (s as ImportDeclarationStructure).moduleSpecifier,
48+
namedImports: [{ name: x.name, alias: x.alias }],
49+
},
50+
]);
51+
}) as Array<[string, OptionalKind<ImportDeclarationStructure>]>;
52+
const importDeclarations = new ImportDeclarationMap(imports);
53+
3654
let classStructure = (sourceFileStructure.statements as StatementStructures[]).find(
3755
(s: StatementStructures) => s.kind === StructureKind.Class,
3856
) as ClassDeclarationStructure | undefined;

src/helpers/import-declaration-map.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ export class ImportDeclarationMap extends Map<
44
string,
55
OptionalKind<ImportDeclarationStructure>
66
> {
7-
add(name: string, moduleSpecifier: string);
8-
add(name: string, value: OptionalKind<ImportDeclarationStructure>);
7+
add(name: string, moduleSpecifier: string): void;
8+
add(name: string, value: OptionalKind<ImportDeclarationStructure>): void;
99

10-
add(name: string, value: OptionalKind<ImportDeclarationStructure> | string) {
10+
add(name: string, value: OptionalKind<ImportDeclarationStructure> | string): void {
1111
if (!this.has(name)) {
1212
const structure: OptionalKind<ImportDeclarationStructure> =
1313
typeof value === 'string'

0 commit comments

Comments
 (0)