-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
567 additions
and
48 deletions.
There are no files selected for viewing
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,13 +1,10 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { ProjectOptions } from 'ts-morph'; | ||
|
||
import { createProject } from '@mutates/core'; | ||
|
||
import { NgTreeFileSystem } from './ng-tree-file-system'; | ||
|
||
export function createAngularProject(tree: Tree) { | ||
const project = createProject(new NgTreeFileSystem(tree)); | ||
|
||
project.addSourceFilesAtPaths('**/*.ts'); | ||
|
||
return project; | ||
export function createAngularProject(tree: Tree, options?: Omit<ProjectOptions, 'fileSystem'>) { | ||
return createProject(new NgTreeFileSystem(tree), options); | ||
} |
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,56 @@ | ||
import { ClassDeclaration, Node, ObjectLiteralExpression } from 'ts-morph'; | ||
|
||
import { getDecorators } from '@mutates/core'; | ||
|
||
export enum MetadataType { | ||
NgModule = 'NgModule', | ||
Component = 'Component', | ||
Directive = 'Directive', | ||
Pipe = 'Pipe', | ||
Injectable = 'Injectable', | ||
} | ||
|
||
export function getMetadata( | ||
klass: ClassDeclaration | ClassDeclaration[], | ||
metadataType: MetadataType, | ||
): ObjectLiteralExpression[] { | ||
const decorators = getDecorators(klass, { | ||
name: metadataType, | ||
}); | ||
|
||
const metadatas = decorators.map((decorator) => decorator.getArguments()).flat(); | ||
|
||
return metadatas.filter((metadata): metadata is ObjectLiteralExpression => | ||
Node.isObjectLiteralExpression(metadata), | ||
); | ||
} | ||
|
||
export function getNgModuleMetadata( | ||
klass: ClassDeclaration | ClassDeclaration[], | ||
): ObjectLiteralExpression[] { | ||
return getMetadata(klass, MetadataType.NgModule); | ||
} | ||
|
||
export function getComponentMetadata( | ||
klass: ClassDeclaration | ClassDeclaration[], | ||
): ObjectLiteralExpression[] { | ||
return getMetadata(klass, MetadataType.Component); | ||
} | ||
|
||
export function getDirectiveMetadata( | ||
klass: ClassDeclaration | ClassDeclaration[], | ||
): ObjectLiteralExpression[] { | ||
return getMetadata(klass, MetadataType.Directive); | ||
} | ||
|
||
export function getPipeMetadata( | ||
klass: ClassDeclaration | ClassDeclaration[], | ||
): ObjectLiteralExpression[] { | ||
return getMetadata(klass, MetadataType.Pipe); | ||
} | ||
|
||
export function getInjectableMetadata( | ||
klass: ClassDeclaration | ClassDeclaration[], | ||
): ObjectLiteralExpression[] { | ||
return getMetadata(klass, MetadataType.Injectable); | ||
} |
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
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
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
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
Empty file.
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
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
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,30 @@ | ||
import { createSourceFile } from '../source-file'; | ||
import { createTestingProject } from '../testing'; | ||
import { getImportRefs } from './get-import-refs'; | ||
import { getImports } from './get-imports'; | ||
import { getNamedImports } from './get-named-imports'; | ||
|
||
describe('getImportRefs', () => { | ||
beforeEach(() => { | ||
createTestingProject(); | ||
}); | ||
|
||
it('should find one named import', () => { | ||
createSourceFile( | ||
'some/path/one-more-file.ts', | ||
` | ||
import { a } from 'd'; | ||
a(); | ||
`, | ||
); | ||
|
||
const imports = getNamedImports(getImports('some/path/**.ts'), { | ||
name: 'a', | ||
}); | ||
|
||
const refs = getImportRefs(imports); | ||
console.log(refs); | ||
expect(refs.length).toBe(1); | ||
}); | ||
}); |
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,19 @@ | ||
import { ImportSpecifier, Node } from 'ts-morph'; | ||
|
||
import { coerceArray } from '../utils'; | ||
|
||
export function getImportRefs(imports: ImportSpecifier | ImportSpecifier[]): Node[] { | ||
const importNames = coerceArray(imports).map((imp) => imp.getName()); | ||
|
||
return coerceArray(imports) | ||
.flatMap((imp) => imp.getNameNode().findReferencesAsNodes()) | ||
.filter((node) => { | ||
const parent = node.getParent(); | ||
|
||
if (parent && Node.isImportSpecifier(parent)) { | ||
return !importNames.includes(parent.getName()); | ||
} | ||
|
||
return true; | ||
}); | ||
} |
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,25 @@ | ||
import { createSourceFile } from '../source-file'; | ||
import { createTestingProject } from '../testing'; | ||
import { getImports } from './get-imports'; | ||
import { getNamedImports } from './get-named-imports'; | ||
|
||
describe('getNamedImports', () => { | ||
beforeEach(() => { | ||
createTestingProject(); | ||
}); | ||
|
||
it('should find one named import', () => { | ||
createSourceFile( | ||
'some/path/one-more-file.ts', | ||
` | ||
import { a } from 'd'; | ||
`, | ||
); | ||
|
||
const imports = getNamedImports(getImports('some/path/**.ts'), { | ||
name: 'a', | ||
}); | ||
|
||
expect(imports.length).toBe(1); | ||
}); | ||
}); |
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,12 @@ | ||
import { ImportDeclaration, ImportSpecifier, ImportSpecifierStructure } from 'ts-morph'; | ||
|
||
import { coerceArray, matchQuery, type Query } from '../utils'; | ||
|
||
export function getNamedImports( | ||
imports: ImportDeclaration | ImportDeclaration[], | ||
query?: Query<Omit<ImportSpecifierStructure, 'kind'>>, | ||
): ImportSpecifier[] { | ||
return coerceArray(imports) | ||
.flatMap((imp) => imp.getNamedImports()) | ||
.filter((named) => matchQuery(named.getStructure(), query)); | ||
} |
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
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
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 { getSourceFiles } from '../source-file'; | ||
import { addSourceFiles, createProject } from './project'; | ||
|
||
describe('Project', () => { | ||
beforeEach(() => { | ||
createProject(); | ||
|
||
addSourceFiles('./package.json'); | ||
}); | ||
|
||
it('should not add files from node_modules', () => { | ||
expect(getSourceFiles('./**/*.json').at(0)?.getFilePath()).contain('package.json'); | ||
}); | ||
}); |
Oops, something went wrong.