From 06fdcbf6e8348980df70631eeba16c3729a21abe Mon Sep 17 00:00:00 2001 From: Igor Katsuba Date: Wed, 12 Jun 2024 01:01:23 +0300 Subject: [PATCH] feat(mutates): refactoring, part 2 --- packages/angular/project.json | 3 +- packages/angular/src/index.ts | 3 + ...ovider-to-bootstrap-application-fn.spec.ts | 93 ------- ...dd-provider-to-bootstrap-application-fn.ts | 30 --- .../bootstrap/get-bootstrap-application-fn.ts | 23 +- .../src/lib/bootstrap/get-bootstrap-fn.ts | 22 -- ...pec.ts => get-bootstrap-module-fn.spec.ts} | 6 +- .../lib/bootstrap/get-bootstrap-module-fn.ts | 21 ++ packages/angular/src/lib/bootstrap/index.ts | 3 +- .../component/add-import-to-component.spec.ts | 121 --------- .../lib/component/add-import-to-component.ts | 14 -- .../add-provider-to-component.spec.ts | 121 --------- .../component/add-provider-to-component.ts | 14 -- .../add-style-url-to-component.spec.ts | 121 --------- .../component/add-style-url-to-component.ts | 13 - .../src/lib/component/get-components.ts | 14 ++ packages/angular/src/lib/component/index.ts | 4 +- .../add-provider-to-directive.spec.ts | 121 --------- .../directive/add-provider-to-directive.ts | 14 -- .../src/lib/directive/get-directives.ts | 14 ++ packages/angular/src/lib/directive/index.ts | 2 +- ...-module.spec.ts => get-app-module.spec.ts} | 4 +- .../{get-main-module.ts => get-app-module.ts} | 6 +- packages/angular/src/lib/helpers/index.ts | 6 +- .../lib/helpers/is-standalone-component.ts | 24 -- .../angular/src/lib/helpers/is-standalone.ts | 29 +++ packages/angular/src/lib/helpers/ng-module.ts | 31 --- .../src/lib/injectable/get-injectables.ts | 14 ++ packages/angular/src/lib/injectable/index.ts | 1 + .../src/lib/metadata/get-metadata.spec.ts | 41 ++++ .../angular/src/lib/metadata/get-metadata.ts | 2 +- packages/angular/src/lib/metadata/index.ts | 1 + .../module/add-bootstrap-to-ng-module.spec.ts | 105 -------- .../lib/module/add-bootstrap-to-ng-module.ts | 18 -- .../add-declaration-to-ng-module.spec.ts | 115 --------- .../module/add-declaration-to-ng-module.ts | 14 -- .../add-entry-component-to-ng-module.spec.ts | 115 --------- .../add-entry-component-to-ng-module.ts | 14 -- .../module/add-export-to-ng-module.spec.ts | 106 -------- .../src/lib/module/add-export-to-ng-module.ts | 14 -- .../module/add-import-to-ng-module.spec.ts | 232 ------------------ .../src/lib/module/add-import-to-ng-module.ts | 14 -- .../module/add-provider-to-ng-module.spec.ts | 210 ---------------- .../lib/module/add-provider-to-ng-module.ts | 14 -- .../module/add-schema-to-ng-module.spec.ts | 115 --------- .../src/lib/module/add-schema-to-ng-module.ts | 14 -- .../angular/src/lib/module/get-ng-modules.ts | 14 ++ packages/angular/src/lib/module/index.ts | 8 +- .../ng-component.ts => pipe/get-pipes.ts} | 10 +- packages/angular/src/lib/pipe/index.ts | 1 + packages/core/project.json | 3 +- packages/core/src/lib/imports/index.ts | 2 + .../properties/edit-class-properties.spec.ts | 2 + .../utils/helpers/get-declaration-editor.ts | 17 +- .../src/lib/utils/types/structure-editor.ts | 2 +- 55 files changed, 205 insertions(+), 1890 deletions(-) delete mode 100644 packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.spec.ts delete mode 100644 packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.ts delete mode 100644 packages/angular/src/lib/bootstrap/get-bootstrap-fn.ts rename packages/angular/src/lib/bootstrap/{get-bootstrap-fn.spec.ts => get-bootstrap-module-fn.spec.ts} (86%) create mode 100644 packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.ts delete mode 100644 packages/angular/src/lib/component/add-import-to-component.spec.ts delete mode 100644 packages/angular/src/lib/component/add-import-to-component.ts delete mode 100644 packages/angular/src/lib/component/add-provider-to-component.spec.ts delete mode 100644 packages/angular/src/lib/component/add-provider-to-component.ts delete mode 100644 packages/angular/src/lib/component/add-style-url-to-component.spec.ts delete mode 100644 packages/angular/src/lib/component/add-style-url-to-component.ts create mode 100644 packages/angular/src/lib/component/get-components.ts delete mode 100644 packages/angular/src/lib/directive/add-provider-to-directive.spec.ts delete mode 100644 packages/angular/src/lib/directive/add-provider-to-directive.ts create mode 100644 packages/angular/src/lib/directive/get-directives.ts rename packages/angular/src/lib/helpers/{get-main-module.spec.ts => get-app-module.spec.ts} (91%) rename packages/angular/src/lib/helpers/{get-main-module.ts => get-app-module.ts} (59%) delete mode 100644 packages/angular/src/lib/helpers/is-standalone-component.ts create mode 100644 packages/angular/src/lib/helpers/is-standalone.ts delete mode 100644 packages/angular/src/lib/helpers/ng-module.ts create mode 100644 packages/angular/src/lib/injectable/get-injectables.ts create mode 100644 packages/angular/src/lib/injectable/index.ts create mode 100644 packages/angular/src/lib/metadata/get-metadata.spec.ts create mode 100644 packages/angular/src/lib/metadata/index.ts delete mode 100644 packages/angular/src/lib/module/add-bootstrap-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-bootstrap-to-ng-module.ts delete mode 100644 packages/angular/src/lib/module/add-declaration-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-declaration-to-ng-module.ts delete mode 100644 packages/angular/src/lib/module/add-entry-component-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-entry-component-to-ng-module.ts delete mode 100644 packages/angular/src/lib/module/add-export-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-export-to-ng-module.ts delete mode 100644 packages/angular/src/lib/module/add-import-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-import-to-ng-module.ts delete mode 100644 packages/angular/src/lib/module/add-provider-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-provider-to-ng-module.ts delete mode 100644 packages/angular/src/lib/module/add-schema-to-ng-module.spec.ts delete mode 100644 packages/angular/src/lib/module/add-schema-to-ng-module.ts create mode 100644 packages/angular/src/lib/module/get-ng-modules.ts rename packages/angular/src/lib/{helpers/ng-component.ts => pipe/get-pipes.ts} (55%) create mode 100644 packages/angular/src/lib/pipe/index.ts diff --git a/packages/angular/project.json b/packages/angular/project.json index 4f4dd0d..cfefd9a 100644 --- a/packages/angular/project.json +++ b/packages/angular/project.json @@ -25,7 +25,8 @@ }, "nx-release-publish": { "options": { - "packageRoot": "dist/{projectRoot}" + "packageRoot": "dist/{projectRoot}", + "tag": "next" } }, "test": { diff --git a/packages/angular/src/index.ts b/packages/angular/src/index.ts index de9bd14..fea3213 100644 --- a/packages/angular/src/index.ts +++ b/packages/angular/src/index.ts @@ -2,7 +2,10 @@ export * from './lib/bootstrap'; export * from './lib/component'; export * from './lib/directive'; export * from './lib/helpers'; +export * from './lib/injectable'; +export * from './lib/metadata'; export * from './lib/module'; +export * from './lib/pipe'; export * from './lib/testing'; export * from './lib/create-angular-project'; export * from './lib/ng-tree-file-system'; diff --git a/packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.spec.ts b/packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.spec.ts deleted file mode 100644 index 1293f24..0000000 --- a/packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.spec.ts +++ /dev/null @@ -1,93 +0,0 @@ -import type { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getVariables } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addProviderToBootstrapApplicationFn } from './add-provider-to-bootstrap-application-fn'; -import { getBootstrapApplicationFn } from './get-bootstrap-application-fn'; - -describe('addProviderToBootstrapApplicationFn', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - it('should add provider to bootstrapApplication', () => { - createSourceFile( - 'src/main.ts', - `import {bootstrapApplication} from '@angular/platform-browser'; -import {AppComponent} from './app/app.component'; - -bootstrapApplication(AppComponent) -`, - ); - const bootstrapFn = getBootstrapApplicationFn('src/main.ts')!; - - addProviderToBootstrapApplicationFn(bootstrapFn, 'provideApp()'); - - expect(bootstrapFn.getText()).toBe( - 'bootstrapApplication(AppComponent, {providers: [provideApp()]})', - ); - }); - - it('should add provider to bootstrapApplication with existing providers', () => { - createSourceFile( - 'src/main.ts', - `import {bootstrapApplication} from '@angular/platform-browser'; -import {AppComponent} from './app/app.component'; - -bootstrapApplication(AppComponent, {providers: [provideApp()]}) -`, - ); - const bootstrapFn = getBootstrapApplicationFn('src/main.ts')!; - - addProviderToBootstrapApplicationFn(bootstrapFn, 'provideApp2()'); - - expect(bootstrapFn.getText()).toBe( - 'bootstrapApplication(AppComponent, {providers: [provideApp(), provideApp2()]})', - ); - }); - - it('should add provider to bootstrapApplication with existing providers and unique option', () => { - createSourceFile( - 'src/main.ts', - `import {bootstrapApplication} from '@angular/platform-browser'; -import {AppComponent} from './app/app.component'; - -bootstrapApplication(AppComponent, {providers: [provideApp()]}) -`, - ); - const bootstrapFn = getBootstrapApplicationFn('src/main.ts')!; - - addProviderToBootstrapApplicationFn(bootstrapFn, 'provideApp()', { - unique: true, - }); - - expect(bootstrapFn.getText()).toBe( - 'bootstrapApplication(AppComponent, {providers: [provideApp()]})', - ); - }); - - it('should add provider to variable that used for bootstrapApplication', () => { - createSourceFile( - 'src/main.ts', - `import {bootstrapApplication} from '@angular/platform-browser'; -import {AppComponent} from './app/app.component'; - -const options = {providers: [provideApp()]}; - -bootstrapApplication(AppComponent, options) -`, - ); - const bootstrapFn = getBootstrapApplicationFn('src/main.ts')!; - const [options] = getVariables('src/main.ts')[0].getDeclarations(); - - addProviderToBootstrapApplicationFn(bootstrapFn, 'provideApp2()'); - - expect(options.getText()).toBe('options = {providers: [provideApp(), provideApp2()]}'); - }); -}); diff --git a/packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.ts b/packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.ts deleted file mode 100644 index c656550..0000000 --- a/packages/angular/src/lib/bootstrap/add-provider-to-bootstrap-application-fn.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { CallExpression, Identifier, ObjectLiteralExpression } from 'ts-morph'; -import { Node, SyntaxKind } from 'ts-morph'; - -import { pushToObjectLiteralArrayProperty } from '../helpers/push-to-object-literal-array-property'; - -export function addProviderToBootstrapApplicationFn( - callExpression: CallExpression, - provider: string, - { unique = false }: { unique?: boolean } = {}, -): void { - const [, options = callExpression.addArgument('{providers: []}')] = callExpression.getArguments(); - - if (!Node.isIdentifier(options) && !Node.isObjectLiteralExpression(options)) { - return; - } - - pushToObjectLiteralArrayProperty(getOptionsObject(options), 'providers', provider, { - unique, - }); -} - -function getOptionsObject(options: Identifier | ObjectLiteralExpression): ObjectLiteralExpression { - if (Node.isObjectLiteralExpression(options)) { - return options; - } - - const definition = options.getDefinitionNodes()[0]; - - return definition.getChildrenOfKind(SyntaxKind.ObjectLiteralExpression)[0]; -} diff --git a/packages/angular/src/lib/bootstrap/get-bootstrap-application-fn.ts b/packages/angular/src/lib/bootstrap/get-bootstrap-application-fn.ts index 28b47c4..2dfcd02 100644 --- a/packages/angular/src/lib/bootstrap/get-bootstrap-application-fn.ts +++ b/packages/angular/src/lib/bootstrap/get-bootstrap-application-fn.ts @@ -1,20 +1,23 @@ import type { CallExpression } from 'ts-morph'; import { Node, SyntaxKind } from 'ts-morph'; -import { getImports } from '@mutates/core'; +import { getImportRefs, getImports, getNamedImports } from '@mutates/core'; export function getBootstrapApplicationFn(mainFilePath: string): CallExpression | undefined { - const [bootstrapApplicationImport] = getImports(mainFilePath, { - moduleSpecifier: '@angular/platform-browser', - }); + const [namedImport] = getNamedImports( + getImports(mainFilePath, { + moduleSpecifier: '@angular/platform-browser', + }), + { + name: 'bootstrapApplication', + }, + ); - const namedImport = bootstrapApplicationImport - ?.getNamedImports() - .find((imp) => imp.getName() === 'bootstrapApplication'); + if (!namedImport) { + return; + } - return namedImport - ?.getNameNode() - .findReferencesAsNodes() + return getImportRefs(namedImport) .find((ref) => Node.isCallExpression(ref.getParent())) ?.getParentIfKind(SyntaxKind.CallExpression); } diff --git a/packages/angular/src/lib/bootstrap/get-bootstrap-fn.ts b/packages/angular/src/lib/bootstrap/get-bootstrap-fn.ts deleted file mode 100644 index 3019447..0000000 --- a/packages/angular/src/lib/bootstrap/get-bootstrap-fn.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { CallExpression } from 'ts-morph'; -import { Node, SyntaxKind } from 'ts-morph'; - -import { getImports } from '@mutates/core'; - -export function getBootstrapFn(mainFilePath: string): CallExpression | undefined { - const [platformBrowserImport] = getImports(mainFilePath, { - moduleSpecifier: '@angular/platform-browser-dynamic', - }); - - const namedImport = platformBrowserImport - ?.getNamedImports() - .find((imp) => imp.getName() === 'platformBrowserDynamic'); - - return namedImport - ?.getNameNode() - .findReferencesAsNodes() - .find((ref) => Node.isCallExpression(ref.getParent())) - ?.getParentIfKind(SyntaxKind.CallExpression) - ?.getParentIfKind(SyntaxKind.PropertyAccessExpression) - ?.getParentIfKind(SyntaxKind.CallExpression); -} diff --git a/packages/angular/src/lib/bootstrap/get-bootstrap-fn.spec.ts b/packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.spec.ts similarity index 86% rename from packages/angular/src/lib/bootstrap/get-bootstrap-fn.spec.ts rename to packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.spec.ts index 06d1452..8a12888 100644 --- a/packages/angular/src/lib/bootstrap/get-bootstrap-fn.spec.ts +++ b/packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.spec.ts @@ -5,7 +5,7 @@ import { createSourceFile } from '@mutates/core'; import { createAngularProject } from '../create-angular-project'; import { createTestingTree } from '../testing'; -import { getBootstrapFn } from './get-bootstrap-fn'; +import { getBootstrapModuleFn } from './get-bootstrap-module-fn'; describe('getBootstrapFn', () => { let host: UnitTestTree; @@ -32,7 +32,7 @@ platformBrowserDynamic() .catch(err => console.log(err)); `, ); - const bootstrapFn = getBootstrapFn('src/main.ts')!; + const bootstrapFn = getBootstrapModuleFn('src/main.ts')!; expect(bootstrapFn.getText()).toBe(`platformBrowserDynamic() .bootstrapModule(AppModule)`); @@ -41,7 +41,7 @@ platformBrowserDynamic() it('should return undefined if bootstrap function is not found', () => { createSourceFile('src/main.ts', ''); - const bootstrapFn = getBootstrapFn('src/main.ts'); + const bootstrapFn = getBootstrapModuleFn('src/main.ts'); expect(bootstrapFn).toBeUndefined(); }); diff --git a/packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.ts b/packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.ts new file mode 100644 index 0000000..e6721fe --- /dev/null +++ b/packages/angular/src/lib/bootstrap/get-bootstrap-module-fn.ts @@ -0,0 +1,21 @@ +import type { CallExpression } from 'ts-morph'; +import { Node, SyntaxKind } from 'ts-morph'; + +import { getImportRefs, getImports, getNamedImports } from '@mutates/core'; + +export function getBootstrapModuleFn(mainFilePath: string): CallExpression | undefined { + const namedImport = getNamedImports( + getImports(mainFilePath, { + moduleSpecifier: '@angular/platform-browser-dynamic', + }), + { + name: 'platformBrowserDynamic', + }, + ); + + return getImportRefs(namedImport) + .find((ref) => Node.isCallExpression(ref.getParent())) + ?.getParentIfKind(SyntaxKind.CallExpression) + ?.getParentIfKind(SyntaxKind.PropertyAccessExpression) + ?.getParentIfKind(SyntaxKind.CallExpression); +} diff --git a/packages/angular/src/lib/bootstrap/index.ts b/packages/angular/src/lib/bootstrap/index.ts index f0795d5..08de59d 100644 --- a/packages/angular/src/lib/bootstrap/index.ts +++ b/packages/angular/src/lib/bootstrap/index.ts @@ -1,3 +1,2 @@ -export * from './add-provider-to-bootstrap-application-fn'; export * from './get-bootstrap-application-fn'; -export * from './get-bootstrap-fn'; +export * from './get-bootstrap-module-fn'; diff --git a/packages/angular/src/lib/component/add-import-to-component.spec.ts b/packages/angular/src/lib/component/add-import-to-component.spec.ts deleted file mode 100644 index 493ad13..0000000 --- a/packages/angular/src/lib/component/add-import-to-component.spec.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addImportToComponent } from './add-import-to-component'; - -describe('addProviderToComponent', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No providers property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Component({}) -export class SomeComponent { - -}`, - ); - }); - - it('should create the providers property', () => { - addImportToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - 'TestImport', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Component({ - imports: [TestImport] -}) -export class SomeComponent { - -}`); - }); - }); - - describe('No decorator arguments', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Component() -export class SomeComponent { - -}`, - ); - }); - - it('should create the providers property', () => { - addImportToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - 'TestImport', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Component({imports: [TestImport]}) -export class SomeComponent { - -}`); - }); - }); - - describe('The providers property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; -import { TestImport } from '@angular/common'; - -@Component({ - imports: [TestImport] -}) -export class SomeComponent { - -}`, - ); - }); - - it('should add module to providers', () => { - addImportToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - 'NewTestImport', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; -import { TestImport } from '@angular/common'; - -@Component({ - imports: [TestImport, NewTestImport] -}) -export class SomeComponent { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/component/add-import-to-component.ts b/packages/angular/src/lib/component/add-import-to-component.ts deleted file mode 100644 index 4d8c84b..0000000 --- a/packages/angular/src/lib/component/add-import-to-component.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addImportToComponent( - classDeclaration: ClassDeclaration, - importName: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'Component', 'imports', importName, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/component/add-provider-to-component.spec.ts b/packages/angular/src/lib/component/add-provider-to-component.spec.ts deleted file mode 100644 index fb166e5..0000000 --- a/packages/angular/src/lib/component/add-provider-to-component.spec.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addProviderToComponent } from './add-provider-to-component'; - -describe('addProviderToComponent', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No providers property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Component({}) -export class SomeComponent { - -}`, - ); - }); - - it('should create the providers property', () => { - addProviderToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - 'TestProvider', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Component({ - providers: [TestProvider] -}) -export class SomeComponent { - -}`); - }); - }); - - describe('No decorator arguments', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Component() -export class SomeComponent { - -}`, - ); - }); - - it('should create the providers property', () => { - addProviderToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - 'TestProvider', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Component({providers: [TestProvider]}) -export class SomeComponent { - -}`); - }); - }); - - describe('The providers property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; -import { TestProvider } from '@angular/common'; - -@Component({ - providers: [TestProvider] -}) -export class SomeComponent { - -}`, - ); - }); - - it('should add module to providers', () => { - addProviderToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - 'NewTestProvider', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; -import { TestProvider } from '@angular/common'; - -@Component({ - providers: [TestProvider, NewTestProvider] -}) -export class SomeComponent { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/component/add-provider-to-component.ts b/packages/angular/src/lib/component/add-provider-to-component.ts deleted file mode 100644 index 597563e..0000000 --- a/packages/angular/src/lib/component/add-provider-to-component.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addProviderToComponent( - classDeclaration: ClassDeclaration, - provider: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'Component', 'providers', provider, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/component/add-style-url-to-component.spec.ts b/packages/angular/src/lib/component/add-style-url-to-component.spec.ts deleted file mode 100644 index b33a8f3..0000000 --- a/packages/angular/src/lib/component/add-style-url-to-component.spec.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addStyleUrlToComponent } from './add-style-url-to-component'; - -describe('addStyleUrlToComponent', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No styleUrl property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Component({}) -export class SomeComponent { - -}`, - ); - }); - - it('should create the providers property', () => { - addStyleUrlToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - '"./style.less"', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Component({ - styleUrls: ["./style.less"] -}) -export class SomeComponent { - -}`); - }); - }); - - describe('No decorator arguments', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Component() -export class SomeComponent { - -}`, - ); - }); - - it('should create the providers property', () => { - addStyleUrlToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - '"./style.less"', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Component({styleUrls: ["./style.less"]}) -export class SomeComponent { - -}`); - }); - }); - - describe('The providers property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@Component({ - styleUrls: ["./style.less"] -}) -export class SomeComponent { - -}`, - ); - }); - - it('should add module to providers', () => { - addStyleUrlToComponent( - getClasses('src/main.ts', { - name: 'SomeComponent', - })[0], - '"./new-style.less"', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@Component({ - styleUrls: ["./style.less", "./new-style.less"] -}) -export class SomeComponent { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/component/add-style-url-to-component.ts b/packages/angular/src/lib/component/add-style-url-to-component.ts deleted file mode 100644 index e30bf63..0000000 --- a/packages/angular/src/lib/component/add-style-url-to-component.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addStyleUrlToComponent( - classDeclaration: ClassDeclaration, - styleUrl: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'Component', 'styleUrls', styleUrl, { - unique, - }); -} diff --git a/packages/angular/src/lib/component/get-components.ts b/packages/angular/src/lib/component/get-components.ts new file mode 100644 index 0000000..a02f79e --- /dev/null +++ b/packages/angular/src/lib/component/get-components.ts @@ -0,0 +1,14 @@ +import type { ClassDeclaration } from 'ts-morph'; + +import { getClasses, Pattern, Query, StructureType } from '@mutates/core'; + +export function getComponents( + pattern: Pattern, + query?: Query, 'kind'>>, +): ClassDeclaration[] { + return getClasses(pattern, query).filter(isComponent); +} + +export function isComponent(declaration: ClassDeclaration): boolean { + return !!declaration.getDecorator('Component'); +} diff --git a/packages/angular/src/lib/component/index.ts b/packages/angular/src/lib/component/index.ts index 848264b..641c2e7 100644 --- a/packages/angular/src/lib/component/index.ts +++ b/packages/angular/src/lib/component/index.ts @@ -1,3 +1 @@ -export * from './add-import-to-component'; -export * from './add-provider-to-component'; -export * from './add-style-url-to-component'; +export * from './get-components'; diff --git a/packages/angular/src/lib/directive/add-provider-to-directive.spec.ts b/packages/angular/src/lib/directive/add-provider-to-directive.spec.ts deleted file mode 100644 index 83c4cb6..0000000 --- a/packages/angular/src/lib/directive/add-provider-to-directive.spec.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addProviderToDirective } from './add-provider-to-directive'; - -describe('addProviderToDirective', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No providers property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Directive({}) -export class SomeDirective { - -}`, - ); - }); - - it('should create the providers property', () => { - addProviderToDirective( - getClasses('src/main.ts', { - name: 'SomeDirective', - })[0], - 'TestProvider', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Directive({ - providers: [TestProvider] -}) -export class SomeDirective { - -}`); - }); - }); - - describe('No decorator arguments', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; - -@Directive() -export class SomeDirective { - -}`, - ); - }); - - it('should create the providers property', () => { - addProviderToDirective( - getClasses('src/main.ts', { - name: 'SomeDirective', - })[0], - 'TestProvider', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; - -@Directive({providers: [TestProvider]}) -export class SomeDirective { - -}`); - }); - }); - - describe('The providers property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { Component } from '@angular/core'; -import { TestService } from 'test-package'; - -@Directive({ - providers: [TestService] -}) -export class SomeDirective { - -}`, - ); - }); - - it('should add module to providers', () => { - addProviderToDirective( - getClasses('src/main.ts', { - name: 'SomeDirective', - })[0], - 'NewTestService', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { Component } from '@angular/core'; -import { TestService } from 'test-package'; - -@Directive({ - providers: [TestService, NewTestService] -}) -export class SomeDirective { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/directive/add-provider-to-directive.ts b/packages/angular/src/lib/directive/add-provider-to-directive.ts deleted file mode 100644 index 0a63e16..0000000 --- a/packages/angular/src/lib/directive/add-provider-to-directive.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addProviderToDirective( - classDeclaration: ClassDeclaration, - provider: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'Directive', 'providers', provider, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/directive/get-directives.ts b/packages/angular/src/lib/directive/get-directives.ts new file mode 100644 index 0000000..af390a3 --- /dev/null +++ b/packages/angular/src/lib/directive/get-directives.ts @@ -0,0 +1,14 @@ +import type { ClassDeclaration } from 'ts-morph'; + +import { getClasses, Pattern, Query, StructureType } from '@mutates/core'; + +export function getDirectives( + pattern: Pattern, + query?: Query, 'kind'>>, +): ClassDeclaration[] { + return getClasses(pattern, query).filter(isDirective); +} + +export function isDirective(declaration: ClassDeclaration): boolean { + return !!declaration.getDecorator('Directive'); +} diff --git a/packages/angular/src/lib/directive/index.ts b/packages/angular/src/lib/directive/index.ts index 7c11887..452c527 100644 --- a/packages/angular/src/lib/directive/index.ts +++ b/packages/angular/src/lib/directive/index.ts @@ -1 +1 @@ -export * from './add-provider-to-directive'; +export * from './get-directives'; diff --git a/packages/angular/src/lib/helpers/get-main-module.spec.ts b/packages/angular/src/lib/helpers/get-app-module.spec.ts similarity index 91% rename from packages/angular/src/lib/helpers/get-main-module.spec.ts rename to packages/angular/src/lib/helpers/get-app-module.spec.ts index afb417d..b0d0126 100644 --- a/packages/angular/src/lib/helpers/get-main-module.spec.ts +++ b/packages/angular/src/lib/helpers/get-app-module.spec.ts @@ -5,7 +5,7 @@ import { createSourceFile } from '@mutates/core'; import { createAngularProject } from '../create-angular-project'; import { createTestingTree } from '../testing'; -import { getMainModule } from './get-main-module'; +import { getAppModule } from './get-app-module'; describe('getMainModule', () => { let host: UnitTestTree; @@ -45,7 +45,7 @@ export class AppModule { }); it('should find the AppModule class', () => { - const classDeclaration = getMainModule('src/main.ts'); + const classDeclaration = getAppModule('src/main.ts'); expect(classDeclaration.getText()).toBe(`@NgModule() export class AppModule { diff --git a/packages/angular/src/lib/helpers/get-main-module.ts b/packages/angular/src/lib/helpers/get-app-module.ts similarity index 59% rename from packages/angular/src/lib/helpers/get-main-module.ts rename to packages/angular/src/lib/helpers/get-app-module.ts index 10648fb..033a4a6 100644 --- a/packages/angular/src/lib/helpers/get-main-module.ts +++ b/packages/angular/src/lib/helpers/get-app-module.ts @@ -1,9 +1,9 @@ import type { ClassDeclaration, Identifier } from 'ts-morph'; -import { getBootstrapFn } from '../bootstrap'; +import { getBootstrapModuleFn } from '../bootstrap'; -export function getMainModule(mainFilePath: string): ClassDeclaration { - const bootstrapFn = getBootstrapFn(mainFilePath); +export function getAppModule(mainFilePath: string): ClassDeclaration { + const bootstrapFn = getBootstrapModuleFn(mainFilePath); const [mainModuleIdentifier] = bootstrapFn?.getArguments() as [Identifier]; diff --git a/packages/angular/src/lib/helpers/index.ts b/packages/angular/src/lib/helpers/index.ts index 099b9e7..46d5ab6 100644 --- a/packages/angular/src/lib/helpers/index.ts +++ b/packages/angular/src/lib/helpers/index.ts @@ -1,6 +1,4 @@ -export * from './get-main-module'; -export * from './is-standalone-component'; -export * from './ng-component'; -export * from './ng-module'; +export * from './get-app-module'; +export * from './is-standalone'; export * from './push-to-decorator-array-property'; export * from './push-to-object-literal-array-property'; diff --git a/packages/angular/src/lib/helpers/is-standalone-component.ts b/packages/angular/src/lib/helpers/is-standalone-component.ts deleted file mode 100644 index 2b03d2e..0000000 --- a/packages/angular/src/lib/helpers/is-standalone-component.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; -import { Node } from 'ts-morph'; - -export function isStandaloneComponent(component: ClassDeclaration): boolean { - const decorator = component.getDecorator('Component'); - - if (!decorator) { - return false; - } - - const [metadata] = decorator.getArguments(); - - if (!Node.isObjectLiteralExpression(metadata)) { - return false; - } - - const property = metadata.getProperty('standalone'); - - if (!Node.isPropertyAssignment(property)) { - return false; - } - - return property.getInitializer()?.getText() === 'true'; -} diff --git a/packages/angular/src/lib/helpers/is-standalone.ts b/packages/angular/src/lib/helpers/is-standalone.ts new file mode 100644 index 0000000..4c1b947 --- /dev/null +++ b/packages/angular/src/lib/helpers/is-standalone.ts @@ -0,0 +1,29 @@ +import { ClassDeclaration } from 'ts-morph'; + +import { getObjectProperties } from '@mutates/core'; + +import { getMetadata, MetadataType } from '../metadata/get-metadata'; + +export function isStandalone(ngEntity: ClassDeclaration): boolean { + const [metadata] = getMetadata(ngEntity, [ + MetadataType.NgModule, + MetadataType.Component, + MetadataType.Directive, + MetadataType.Pipe, + MetadataType.Injectable, + ]); + + if (!metadata) { + return false; + } + + const [property] = getObjectProperties(metadata, { + name: 'standalone', + }); + + if (!property) { + return false; + } + + return property.getInitializer()?.getText() === 'true'; +} diff --git a/packages/angular/src/lib/helpers/ng-module.ts b/packages/angular/src/lib/helpers/ng-module.ts deleted file mode 100644 index 3250468..0000000 --- a/packages/angular/src/lib/helpers/ng-module.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { getClasses, getImports, Pattern, Query, StructureType } from '@mutates/core'; - -export function getNgModules( - pattern: Pattern, - query?: Query, 'kind'>>, -): ClassDeclaration[] { - return getClasses(pattern, query).filter((declaration) => !!declaration.getDecorator('NgModule')); -} - -/** - * Find NgModule where the component was declared. - */ -export function findNgModule( - ngComponent: ClassDeclaration, - pattern: Pattern, -): ClassDeclaration | null { - const allNgModules = getNgModules(pattern); - - return ( - allNgModules.find((module) => { - const moduleFile = module.getSourceFile(); - const imports = getImports(moduleFile.getFilePath(), { - namedImports: ngComponent.getName(), - }); - - return imports.some((i) => i.getModuleSpecifierSourceFile() === ngComponent.getSourceFile()); - }) || null - ); -} diff --git a/packages/angular/src/lib/injectable/get-injectables.ts b/packages/angular/src/lib/injectable/get-injectables.ts new file mode 100644 index 0000000..491e367 --- /dev/null +++ b/packages/angular/src/lib/injectable/get-injectables.ts @@ -0,0 +1,14 @@ +import { ClassDeclaration } from 'ts-morph'; + +import { getClasses, Pattern, Query, StructureType } from '@mutates/core'; + +export function getInjectables( + pattern: Pattern, + query?: Query, 'kind'>>, +): ClassDeclaration[] { + return getClasses(pattern, query).filter(isInjectable); +} + +export function isInjectable(declaration: ClassDeclaration): boolean { + return !!declaration.getDecorator('Injectable'); +} diff --git a/packages/angular/src/lib/injectable/index.ts b/packages/angular/src/lib/injectable/index.ts new file mode 100644 index 0000000..9fb27b4 --- /dev/null +++ b/packages/angular/src/lib/injectable/index.ts @@ -0,0 +1 @@ +export * from './get-injectables'; diff --git a/packages/angular/src/lib/metadata/get-metadata.spec.ts b/packages/angular/src/lib/metadata/get-metadata.spec.ts new file mode 100644 index 0000000..5c64ffe --- /dev/null +++ b/packages/angular/src/lib/metadata/get-metadata.spec.ts @@ -0,0 +1,41 @@ +import { Tree } from '@angular-devkit/schematics'; + +import { createSourceFile, getClasses } from '@mutates/core'; + +import { createAngularProject } from '../create-angular-project'; +import { createTestingTree } from '../testing/index'; +import { getComponentMetadata } from './get-metadata'; + +describe('getMetadata', () => { + let host: Tree; + + beforeEach(() => { + host = createTestingTree(); + + createAngularProject(host); + + createSourceFile( + `src/app/app.component.ts`, + ` +import { Component } from '@angular/core'; + +@Component({ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'], +}) +export class AppComponent {} + `, + ); + }); + + it('should return metadata', () => { + const metadata = getComponentMetadata(getClasses('src/app/**/*.ts')); + + expect(metadata.at(0)?.getText()).toEqual(`{ + selector: 'app-root', + templateUrl: './app.component.html', + styleUrls: ['./app.component.css'], +}`); + }); +}); diff --git a/packages/angular/src/lib/metadata/get-metadata.ts b/packages/angular/src/lib/metadata/get-metadata.ts index 35e34dc..9775c97 100644 --- a/packages/angular/src/lib/metadata/get-metadata.ts +++ b/packages/angular/src/lib/metadata/get-metadata.ts @@ -12,7 +12,7 @@ export enum MetadataType { export function getMetadata( klass: ClassDeclaration | ClassDeclaration[], - metadataType: MetadataType, + metadataType: MetadataType | MetadataType[], ): ObjectLiteralExpression[] { const decorators = getDecorators(klass, { name: metadataType, diff --git a/packages/angular/src/lib/metadata/index.ts b/packages/angular/src/lib/metadata/index.ts new file mode 100644 index 0000000..26cb943 --- /dev/null +++ b/packages/angular/src/lib/metadata/index.ts @@ -0,0 +1 @@ +export * from './get-metadata'; diff --git a/packages/angular/src/lib/module/add-bootstrap-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-bootstrap-to-ng-module.spec.ts deleted file mode 100644 index 5dfcdce..0000000 --- a/packages/angular/src/lib/module/add-bootstrap-to-ng-module.spec.ts +++ /dev/null @@ -1,105 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addBootstrapToNgModule } from './add-bootstrap-to-ng-module'; - -describe('addBootstrapToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(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', () => { - addBootstrapToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestComponent'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`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', () => { - addBootstrapToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestComponent'); - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`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 component to bootstrap', () => { - addBootstrapToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestComponent'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - bootstrap: [CommonComponent, TestComponent] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-bootstrap-to-ng-module.ts b/packages/angular/src/lib/module/add-bootstrap-to-ng-module.ts deleted file mode 100644 index 1dcc1db..0000000 --- a/packages/angular/src/lib/module/add-bootstrap-to-ng-module.ts +++ /dev/null @@ -1,18 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { getDecorators } from '@mutates/core'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addBootstrapToNgModule( - classDeclaration: ClassDeclaration, - component: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'bootstrap', component, { - unique, - forceToArray: true, - }); - - getDecorators(classDeclaration, { name: 'NgModule' }); -} diff --git a/packages/angular/src/lib/module/add-declaration-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-declaration-to-ng-module.spec.ts deleted file mode 100644 index f4dda1d..0000000 --- a/packages/angular/src/lib/module/add-declaration-to-ng-module.spec.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addDeclarationToNgModule } from './add-declaration-to-ng-module'; - -describe('addDeclarationToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(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', () => { - addDeclarationToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestComponent', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`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', () => { - addDeclarationToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestComponent', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`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 component to declarations', () => { - addDeclarationToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestComponent', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - declarations: [CommonComponent, TestComponent] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-declaration-to-ng-module.ts b/packages/angular/src/lib/module/add-declaration-to-ng-module.ts deleted file mode 100644 index 748e1c4..0000000 --- a/packages/angular/src/lib/module/add-declaration-to-ng-module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addDeclarationToNgModule( - classDeclaration: ClassDeclaration, - declaration: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'declarations', declaration, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/module/add-entry-component-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-entry-component-to-ng-module.spec.ts deleted file mode 100644 index 60cd22a..0000000 --- a/packages/angular/src/lib/module/add-entry-component-to-ng-module.spec.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addEntryComponentToNgModule } from './add-entry-component-to-ng-module'; - -describe('addEntryComponentToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No entryComponents property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; - -@NgModule({}) -export class SomeModule { - -}`, - ); - }); - - it('should create the entryComponents property', () => { - addEntryComponentToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestComponent', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({ - entryComponents: [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 entryComponents property', () => { - addEntryComponentToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestComponent', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({entryComponents: [TestComponent]}) -export class SomeModule { - -}`); - }); - }); - - describe('The entryComponents property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - entryComponents: [CommonComponent] -}) -export class SomeModule { - -}`, - ); - }); - - it('should add module to entryComponents', () => { - addEntryComponentToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestComponent', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - entryComponents: [CommonComponent, TestComponent] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-entry-component-to-ng-module.ts b/packages/angular/src/lib/module/add-entry-component-to-ng-module.ts deleted file mode 100644 index 1f7fc7c..0000000 --- a/packages/angular/src/lib/module/add-entry-component-to-ng-module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addEntryComponentToNgModule( - classDeclaration: ClassDeclaration, - component: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'entryComponents', component, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/module/add-export-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-export-to-ng-module.spec.ts deleted file mode 100644 index 3a25b51..0000000 --- a/packages/angular/src/lib/module/add-export-to-ng-module.spec.ts +++ /dev/null @@ -1,106 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addExportToNgModule } from './add-export-to-ng-module'; - -describe('addExportToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No exports property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; - -@NgModule({}) -export class SomeModule { - -}`, - ); - }); - - it('should create the imports property', () => { - addExportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({ - exports: [TestModule] -}) -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 exports property', () => { - addExportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({exports: [TestModule]}) -export class SomeModule { - -}`); - }); - }); - - describe('The exports property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - exports: [CommonModule] -}) -export class SomeModule { - -}`, - ); - }); - - it('should add module to exports', () => { - addExportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - exports: [CommonModule, TestModule] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-export-to-ng-module.ts b/packages/angular/src/lib/module/add-export-to-ng-module.ts deleted file mode 100644 index e094134..0000000 --- a/packages/angular/src/lib/module/add-export-to-ng-module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addExportToNgModule( - classDeclaration: ClassDeclaration, - exportName: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'exports', exportName, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/module/add-import-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-import-to-ng-module.spec.ts deleted file mode 100644 index 17841db..0000000 --- a/packages/angular/src/lib/module/add-import-to-ng-module.spec.ts +++ /dev/null @@ -1,232 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addImportToNgModule } from './add-import-to-ng-module'; - -describe('addImportToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No imports property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; - -@NgModule({}) -export class SomeModule { - -}`, - ); - }); - - it('should create the imports property', () => { - addImportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({ - imports: [TestModule] -}) -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 imports property', () => { - addImportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({imports: [TestModule]}) -export class SomeModule { - -}`); - }); - }); - - describe('The imports property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - imports: [CommonModule] -}) -export class SomeModule { - -}`, - ); - }); - - it('should add module to imports', () => { - addImportToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'TestModule.forRoot()', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - imports: [CommonModule, TestModule.forRoot()] -}) -export class SomeModule { - -}`); - }); - }); - - describe('With unique flag', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - imports: [CommonModule] -}) -export class SomeModule { - -}`, - ); - }); - - it('should not add duplicate module to imports', () => { - addImportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'CommonModule', { - unique: true, - }); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - imports: [CommonModule] -}) -export class SomeModule { - -}`); - }); - }); - - describe('Imports is a const', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { APP_IMPORTS } from './imports.ts'; - -@NgModule({ - imports: APP_IMPORTS -}) -export class SomeModule { - -}`, - ); - }); - - it('should wrap const with array and push new provider', () => { - addImportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'CommonModule', { - unique: true, - }); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { APP_IMPORTS } from './imports.ts'; - -@NgModule({ - imports: [APP_IMPORTS, CommonModule] -}) -export class SomeModule { - -}`); - }); - }); - - describe('Package name is specified', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; -import { ExistingModule } from 'test-package'; - -@NgModule({ - imports: [CommonModule, ExistingModule] -}) -export class SomeModule { - -}`, - ); - }); - - it('should add module to imports', () => { - addImportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; -import { ExistingModule } from 'test-package'; - -@NgModule({ - imports: [CommonModule, ExistingModule, TestModule] -}) -export class SomeModule { - -}`); - }); - - it('should add module to imports when unique is true', () => { - addImportToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestModule', { - unique: true, - }); - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; -import { ExistingModule } from 'test-package'; - -@NgModule({ - imports: [CommonModule, ExistingModule, TestModule] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-import-to-ng-module.ts b/packages/angular/src/lib/module/add-import-to-ng-module.ts deleted file mode 100644 index 23a491d..0000000 --- a/packages/angular/src/lib/module/add-import-to-ng-module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addImportToNgModule( - classDeclaration: ClassDeclaration, - moduleName: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'imports', moduleName, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/module/add-provider-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-provider-to-ng-module.spec.ts deleted file mode 100644 index ccc0e9a..0000000 --- a/packages/angular/src/lib/module/add-provider-to-ng-module.spec.ts +++ /dev/null @@ -1,210 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addProviderToNgModule } from './add-provider-to-ng-module'; - -describe('addProviderToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No providers property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; - -@NgModule({}) -export class SomeModule { - -}`, - ); - }); - - it('should create the providers property', () => { - addProviderToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestService'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({ - providers: [TestService] -}) -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 providers property', () => { - addProviderToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestService'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({providers: [TestService]}) -export class SomeModule { - -}`); - }); - }); - - describe('The providers property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - providers: [CommonService] -}) -export class SomeModule { - -}`, - ); - }); - - it('should add module to providers', () => { - addProviderToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'TestService'); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - providers: [CommonService, TestService] -}) -export class SomeModule { - -}`); - }); - }); - - describe('With unique flag', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonService } from '@angular/common'; - -@NgModule({ - providers: [CommonService] -}) -export class SomeModule { - -}`, - ); - }); - - it('should not add duplicate module to providers', () => { - addProviderToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'CommonService', { - unique: true, - }); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonService } from '@angular/common'; - -@NgModule({ - providers: [CommonService] -}) -export class SomeModule { - -}`); - }); - }); - - describe('Providers is a const', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { APP_PROVIDERS } from './providers.ts'; - -@NgModule({ - providers: APP_PROVIDERS -}) -export class SomeModule { - -}`, - ); - }); - - it('should wrap const with array and push new provider', () => { - addProviderToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'CommonService', { - unique: true, - }); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { APP_PROVIDERS } from './providers.ts'; - -@NgModule({ - providers: [APP_PROVIDERS, CommonService] -}) -export class SomeModule { - -}`); - }); - }); - - describe('With specified packageName', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonService } from '@angular/common'; - -@NgModule({ - providers: [CommonService] -}) -export class SomeModule { - -}`, - ); - }); - - it('should not add service to providers', () => { - addProviderToNgModule(getClasses('src/main.ts', { name: 'SomeModule' })[0], 'NewService', { - unique: true, - }); - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonService } from '@angular/common'; - -@NgModule({ - providers: [CommonService, NewService] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-provider-to-ng-module.ts b/packages/angular/src/lib/module/add-provider-to-ng-module.ts deleted file mode 100644 index a4476a3..0000000 --- a/packages/angular/src/lib/module/add-provider-to-ng-module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addProviderToNgModule( - classDeclaration: ClassDeclaration, - provider: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'providers', provider, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/module/add-schema-to-ng-module.spec.ts b/packages/angular/src/lib/module/add-schema-to-ng-module.spec.ts deleted file mode 100644 index 53c527e..0000000 --- a/packages/angular/src/lib/module/add-schema-to-ng-module.spec.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { UnitTestTree } from '@angular-devkit/schematics/testing'; - -import { createSourceFile, getClasses, saveProject } from '@mutates/core'; - -import { createAngularProject } from '../create-angular-project'; -import { createTestingTree } from '../testing'; -import { addSchemaToNgModule } from './add-schema-to-ng-module'; - -describe('addSchemaToModule', () => { - let host: UnitTestTree; - - beforeEach(() => { - host = createTestingTree(); - - createAngularProject(host); - }); - - describe('No schemas property', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; - -@NgModule({}) -export class SomeModule { - -}`, - ); - }); - - it('should create the schemas property', () => { - addSchemaToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'CUSTOM_ELEMENTS_SCHEMA', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({ - schemas: [CUSTOM_ELEMENTS_SCHEMA] -}) -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 schemas property', () => { - addSchemaToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'CUSTOM_ELEMENTS_SCHEMA', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; - -@NgModule({schemas: [CUSTOM_ELEMENTS_SCHEMA]}) -export class SomeModule { - -}`); - }); - }); - - describe('The schemas property is exists', () => { - beforeEach(() => { - createSourceFile( - 'src/main.ts', - `import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - schemas: [NO_ERRORS_SCHEMA] -}) -export class SomeModule { - -}`, - ); - }); - - it('should add module to schemas', () => { - addSchemaToNgModule( - getClasses('src/main.ts', { name: 'SomeModule' })[0], - 'CUSTOM_ELEMENTS_SCHEMA', - ); - - saveProject(); - - expect(host.readContent('src/main.ts')).toBe(`import { NgModule } from '@angular/core'; -import { CommonModule } from '@angular/common'; - -@NgModule({ - schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA] -}) -export class SomeModule { - -}`); - }); - }); -}); diff --git a/packages/angular/src/lib/module/add-schema-to-ng-module.ts b/packages/angular/src/lib/module/add-schema-to-ng-module.ts deleted file mode 100644 index 3719f17..0000000 --- a/packages/angular/src/lib/module/add-schema-to-ng-module.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { ClassDeclaration } from 'ts-morph'; - -import { pushToDecoratorArrayProperty } from '../helpers/push-to-decorator-array-property'; - -export function addSchemaToNgModule( - classDeclaration: ClassDeclaration, - schema: string, - { unique = false }: { unique?: boolean } = {}, -): void { - pushToDecoratorArrayProperty(classDeclaration, 'NgModule', 'schemas', schema, { - unique, - forceToArray: true, - }); -} diff --git a/packages/angular/src/lib/module/get-ng-modules.ts b/packages/angular/src/lib/module/get-ng-modules.ts new file mode 100644 index 0000000..2b1cc6e --- /dev/null +++ b/packages/angular/src/lib/module/get-ng-modules.ts @@ -0,0 +1,14 @@ +import type { ClassDeclaration } from 'ts-morph'; + +import { getClasses, Pattern, Query, StructureType } from '@mutates/core'; + +export function getNgModule( + pattern: Pattern, + query?: Query, 'kind'>>, +): ClassDeclaration[] { + return getClasses(pattern, query).filter(isNgModule); +} + +export function isNgModule(declaration: ClassDeclaration): boolean { + return !!declaration.getDecorator('NgModule'); +} diff --git a/packages/angular/src/lib/module/index.ts b/packages/angular/src/lib/module/index.ts index 62f20b9..1bbb462 100644 --- a/packages/angular/src/lib/module/index.ts +++ b/packages/angular/src/lib/module/index.ts @@ -1,7 +1 @@ -export * from './add-bootstrap-to-ng-module'; -export * from './add-declaration-to-ng-module'; -export * from './add-entry-component-to-ng-module'; -export * from './add-export-to-ng-module'; -export * from './add-import-to-ng-module'; -export * from './add-provider-to-ng-module'; -export * from './add-schema-to-ng-module'; +export * from './get-ng-modules'; diff --git a/packages/angular/src/lib/helpers/ng-component.ts b/packages/angular/src/lib/pipe/get-pipes.ts similarity index 55% rename from packages/angular/src/lib/helpers/ng-component.ts rename to packages/angular/src/lib/pipe/get-pipes.ts index b6da9a2..c6eb0a2 100644 --- a/packages/angular/src/lib/helpers/ng-component.ts +++ b/packages/angular/src/lib/pipe/get-pipes.ts @@ -2,11 +2,13 @@ import type { ClassDeclaration } from 'ts-morph'; import { getClasses, Pattern, Query, StructureType } from '@mutates/core'; -export function getNgComponents( +export function getPipes( pattern: Pattern, query?: Query, 'kind'>>, ): ClassDeclaration[] { - return getClasses(pattern, query).filter( - (declaration) => !!declaration.getDecorator('Component'), - ); + return getClasses(pattern, query).filter(isPipe); +} + +export function isPipe(declaration: ClassDeclaration): boolean { + return !!declaration.getDecorator('Pipe'); } diff --git a/packages/angular/src/lib/pipe/index.ts b/packages/angular/src/lib/pipe/index.ts new file mode 100644 index 0000000..1caf58d --- /dev/null +++ b/packages/angular/src/lib/pipe/index.ts @@ -0,0 +1 @@ +export * from './get-pipes'; diff --git a/packages/core/project.json b/packages/core/project.json index b700568..1217c6f 100644 --- a/packages/core/project.json +++ b/packages/core/project.json @@ -27,7 +27,8 @@ }, "nx-release-publish": { "options": { - "packageRoot": "dist/{projectRoot}" + "packageRoot": "dist/{projectRoot}", + "tag": "next" } }, "test": { diff --git a/packages/core/src/lib/imports/index.ts b/packages/core/src/lib/imports/index.ts index 6db98ed..50f23ff 100644 --- a/packages/core/src/lib/imports/index.ts +++ b/packages/core/src/lib/imports/index.ts @@ -1,4 +1,6 @@ export * from './add-imports'; export * from './edit-imports'; +export * from './get-import-refs'; export * from './get-imports'; +export * from './get-named-imports'; export * from './remove-imports'; diff --git a/packages/core/src/lib/properties/edit-class-properties.spec.ts b/packages/core/src/lib/properties/edit-class-properties.spec.ts index 53f8667..3b330f8 100644 --- a/packages/core/src/lib/properties/edit-class-properties.spec.ts +++ b/packages/core/src/lib/properties/edit-class-properties.spec.ts @@ -1,3 +1,5 @@ +import { Node } from 'ts-morph'; + import { getClasses } from '../classes'; import { readFileSync } from '../fs/file-system'; import { resetActiveProject, saveProject } from '../project'; diff --git a/packages/core/src/lib/utils/helpers/get-declaration-editor.ts b/packages/core/src/lib/utils/helpers/get-declaration-editor.ts index 128d987..62d7207 100644 --- a/packages/core/src/lib/utils/helpers/get-declaration-editor.ts +++ b/packages/core/src/lib/utils/helpers/get-declaration-editor.ts @@ -15,14 +15,15 @@ export function getDeclarationEditor< editor: StructureEditor>, ) { coerceArray(declarations).forEach((declaration) => { - const newStructure = Object.assign( - {}, - declaration.getStructure(), - // TODO: refactor it to support new typings - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - editor(declaration.getStructure(), declaration), - ) as Structures; + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + const result = editor(declaration.getStructure(), declaration); + + if (!result) { + return; + } + + const newStructure = Object.assign({}, declaration.getStructure(), result) as Structures; // todo: see https://github.com/dsherret/ts-morph/issues/882 // if the issue is resolved code will be remove diff --git a/packages/core/src/lib/utils/types/structure-editor.ts b/packages/core/src/lib/utils/types/structure-editor.ts index 9f891d7..dfe7209 100644 --- a/packages/core/src/lib/utils/types/structure-editor.ts +++ b/packages/core/src/lib/utils/types/structure-editor.ts @@ -9,4 +9,4 @@ export type StructureEditor< StructureType >, UpdatableData extends FilterPrimitive = FilterPrimitive, -> = (structure: Structure, declaration: Declaration) => Partial; +> = (structure: Structure, declaration: Declaration) => Partial | void;