Skip to content

Commit

Permalink
Merge pull request #2 from TinkoffCreditSystems/add-import-to-module
Browse files Browse the repository at this point in the history
feat: change the addImportToModule declaration
  • Loading branch information
IKatsuba authored Apr 27, 2021
2 parents 8af2b8f + 174ed99 commit 35518aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 46 deletions.
29 changes: 13 additions & 16 deletions libs/ng-morph/ng/helpers/add-import-to-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@ export class SomeModule {
});

it('should create the imports property', () => {
addImportToModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], {
moduleName: 'TestModule',
moduleSpecifier: '@test/scope',
});
addImportToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestModule'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
import { TestModule } from "@test/scope";
@NgModule({
imports: [TestModule]
Expand All @@ -66,16 +65,15 @@ export class SomeModule {
});

it('should create the imports property', () => {
addImportToModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], {
moduleName: 'TestModule',
moduleSpecifier: '@test/scope',
});
addImportToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestModule'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
import { TestModule } from "@test/scope";
@NgModule({imports: [TestModule]})
export class SomeModule {
Expand All @@ -101,20 +99,19 @@ export class SomeModule {
});

it('should add module to imports', () => {
addImportToModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], {
moduleName: 'TestModule',
moduleSpecifier: '@test/scope',
});
addImportToModule(
getClasses('src/main.ts', { name: 'SomeModule' })[0],
'TestModule.forRoot()'
);

saveActiveProject();

expect(host.readContent('src/main.ts'))
.toStrictEqual(`import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TestModule } from "@test/scope";
@NgModule({
imports: [CommonModule, TestModule]
imports: [CommonModule, TestModule.forRoot()]
})
export class SomeModule {
Expand Down
32 changes: 2 additions & 30 deletions libs/ng-morph/ng/helpers/add-import-to-module.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { ClassDeclaration, Node } from 'ts-morph';
import { getDecorators } from 'ng-morph/decorators';
import { getImports } from 'ng-morph/imports';

export function addImportToModule(
classDeclaration: ClassDeclaration,
{
moduleName,
moduleSpecifier,
element = moduleName,
}: {
moduleName: string;
moduleSpecifier: string;
element?: string;
}
moduleName: string
) {
const file = classDeclaration.getSourceFile();

const [moduleDecorator] = getDecorators(classDeclaration, {
name: 'NgModule',
});
Expand All @@ -41,22 +30,5 @@ export function addImportToModule(
return;
}

importsInitializer.addElement(element);

const [
moduleImport = file.addImportDeclaration({
moduleSpecifier,
namedImports: [moduleName],
}),
] = getImports(file.getFilePath(), {
moduleSpecifier,
});

if (
!moduleImport
.getNamedImports()
.find((namedImport) => namedImport.getName() === moduleName)
) {
moduleImport.addNamedImport(moduleName);
}
importsInitializer.addElement(moduleName);
}

0 comments on commit 35518aa

Please sign in to comment.