-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from TinkoffCreditSystems/add-import-to-module
feat: add more fn
- Loading branch information
Showing
18 changed files
with
846 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getDecorators } from 'ng-morph/decorators'; | ||
import { ClassDeclaration, Node } from 'ts-morph'; | ||
|
||
export function pushToArrayProperty( | ||
classDeclaration: ClassDeclaration, | ||
decoratorName: string, | ||
propertyName: string, | ||
initializer: string | ||
) { | ||
const [decorator] = getDecorators(classDeclaration, { | ||
name: decoratorName, | ||
}); | ||
|
||
const [ | ||
metadata = decorator.addArgument(`{${propertyName}: []}`), | ||
] = decorator.getArguments(); | ||
|
||
if (!Node.isObjectLiteralExpression(metadata)) { | ||
return; | ||
} | ||
|
||
const property = | ||
metadata.getProperty(propertyName) ?? | ||
metadata.addProperty(`${propertyName}: []`); | ||
|
||
if (!Node.isPropertyAssignment(property)) { | ||
return; | ||
} | ||
|
||
const importsInitializer = property.getInitializer(); | ||
|
||
if (!Node.isArrayLiteralExpression(importsInitializer)) { | ||
return; | ||
} | ||
|
||
importsInitializer.addElement(initializer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './helpers/add-import-to-module'; | ||
export * from './helpers/get-bootstrap-fn'; | ||
export * from './helpers/get-main-mofule'; | ||
|
||
export * from './module'; |
121 changes: 121 additions & 0 deletions
121
libs/ng-morph/ng/module/add-bootstrap-to-module.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { HostTree } from '@angular-devkit/schematics'; | ||
import { | ||
createProject, | ||
saveActiveProject, | ||
setActiveProject, | ||
} from 'ng-morph/project'; | ||
import { createSourceFile } from 'ng-morph/source-file'; | ||
import { getClasses } from 'ng-morph/classes'; | ||
import { addBootstrapToModule } from './add-bootstrap-to-module'; | ||
|
||
describe('addBootstrapToModule', () => { | ||
let host: UnitTestTree; | ||
|
||
beforeEach(() => { | ||
host = new UnitTestTree(new HostTree()); | ||
|
||
setActiveProject(createProject(host)); | ||
}); | ||
|
||
describe('No bootstrap property', () => { | ||
beforeEach(() => { | ||
createSourceFile( | ||
'src/main.ts', | ||
`import { NgModule } from '@angular/core'; | ||
@NgModule({}) | ||
export class SomeModule { | ||
}` | ||
); | ||
}); | ||
|
||
it('should create the declarations property', () => { | ||
addBootstrapToModule( | ||
getClasses('src/main.ts', { name: 'SomeModule' })[0], | ||
'TestComponent' | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
expect(host.readContent('src/main.ts')) | ||
.toStrictEqual(`import { NgModule } from '@angular/core'; | ||
@NgModule({ | ||
bootstrap: [TestComponent] | ||
}) | ||
export class SomeModule { | ||
}`); | ||
}); | ||
}); | ||
|
||
describe('No decorator arguments', () => { | ||
beforeEach(() => { | ||
createSourceFile( | ||
'src/main.ts', | ||
`import { NgModule } from '@angular/core'; | ||
@NgModule() | ||
export class SomeModule { | ||
}` | ||
); | ||
}); | ||
|
||
it('should create the bootstrap property', () => { | ||
addBootstrapToModule( | ||
getClasses('src/main.ts', { name: 'SomeModule' })[0], | ||
'TestComponent' | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
expect(host.readContent('src/main.ts')) | ||
.toStrictEqual(`import { NgModule } from '@angular/core'; | ||
@NgModule({bootstrap: [TestComponent]}) | ||
export class SomeModule { | ||
}`); | ||
}); | ||
}); | ||
|
||
describe('The bootstrap property is exists', () => { | ||
beforeEach(() => { | ||
createSourceFile( | ||
'src/main.ts', | ||
`import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
@NgModule({ | ||
bootstrap: [CommonComponent] | ||
}) | ||
export class SomeModule { | ||
}` | ||
); | ||
}); | ||
|
||
it('should add module to entryComponents', () => { | ||
addBootstrapToModule( | ||
getClasses('src/main.ts', { name: 'SomeModule' })[0], | ||
'TestComponent' | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
expect(host.readContent('src/main.ts')) | ||
.toStrictEqual(`import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
@NgModule({ | ||
bootstrap: [CommonComponent, TestComponent] | ||
}) | ||
export class SomeModule { | ||
}`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ClassDeclaration } from 'ts-morph'; | ||
import { pushToArrayProperty } from '../helpers/push-to-array-property'; | ||
|
||
export function addBootstrapToModule( | ||
classDeclaration: ClassDeclaration, | ||
component: string | ||
) { | ||
pushToArrayProperty(classDeclaration, 'NgModule', 'bootstrap', component); | ||
} |
121 changes: 121 additions & 0 deletions
121
libs/ng-morph/ng/module/add-declaration-to-module.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import { HostTree } from '@angular-devkit/schematics'; | ||
import { | ||
createProject, | ||
saveActiveProject, | ||
setActiveProject, | ||
} from 'ng-morph/project'; | ||
import { createSourceFile } from 'ng-morph/source-file'; | ||
import { getClasses } from 'ng-morph/classes'; | ||
import { addDeclarationToModule } from './add-declaration-to-module'; | ||
|
||
describe('addDeclarationToModule', () => { | ||
let host: UnitTestTree; | ||
|
||
beforeEach(() => { | ||
host = new UnitTestTree(new HostTree()); | ||
|
||
setActiveProject(createProject(host)); | ||
}); | ||
|
||
describe('No declaration property', () => { | ||
beforeEach(() => { | ||
createSourceFile( | ||
'src/main.ts', | ||
`import { NgModule } from '@angular/core'; | ||
@NgModule({}) | ||
export class SomeModule { | ||
}` | ||
); | ||
}); | ||
|
||
it('should create the declarations property', () => { | ||
addDeclarationToModule( | ||
getClasses('src/main.ts', { name: 'SomeModule' })[0], | ||
'TestComponent' | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
expect(host.readContent('src/main.ts')) | ||
.toStrictEqual(`import { NgModule } from '@angular/core'; | ||
@NgModule({ | ||
declarations: [TestComponent] | ||
}) | ||
export class SomeModule { | ||
}`); | ||
}); | ||
}); | ||
|
||
describe('No decorator arguments', () => { | ||
beforeEach(() => { | ||
createSourceFile( | ||
'src/main.ts', | ||
`import { NgModule } from '@angular/core'; | ||
@NgModule() | ||
export class SomeModule { | ||
}` | ||
); | ||
}); | ||
|
||
it('should create the declarations property', () => { | ||
addDeclarationToModule( | ||
getClasses('src/main.ts', { name: 'SomeModule' })[0], | ||
'TestComponent' | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
expect(host.readContent('src/main.ts')) | ||
.toStrictEqual(`import { NgModule } from '@angular/core'; | ||
@NgModule({declarations: [TestComponent]}) | ||
export class SomeModule { | ||
}`); | ||
}); | ||
}); | ||
|
||
describe('The declarations property is exists', () => { | ||
beforeEach(() => { | ||
createSourceFile( | ||
'src/main.ts', | ||
`import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
@NgModule({ | ||
declarations: [CommonComponent] | ||
}) | ||
export class SomeModule { | ||
}` | ||
); | ||
}); | ||
|
||
it('should add module to entryComponents', () => { | ||
addDeclarationToModule( | ||
getClasses('src/main.ts', { name: 'SomeModule' })[0], | ||
'TestComponent' | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
expect(host.readContent('src/main.ts')) | ||
.toStrictEqual(`import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
@NgModule({ | ||
declarations: [CommonComponent, TestComponent] | ||
}) | ||
export class SomeModule { | ||
}`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ClassDeclaration } from 'ts-morph'; | ||
import { pushToArrayProperty } from '../helpers/push-to-array-property'; | ||
|
||
export function addDeclarationToModule( | ||
classDeclaration: ClassDeclaration, | ||
declaration: string | ||
) { | ||
pushToArrayProperty( | ||
classDeclaration, | ||
'NgModule', | ||
'declarations', | ||
declaration | ||
); | ||
} |
Oops, something went wrong.