Last active
July 26, 2019 18:00
-
-
Save tw3/98fd99897d05cfc3e8ebc0a0a9c84e14 to your computer and use it in GitHub Desktop.
Lists references of all properties in all classes in a file
This file contains hidden or 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
/* tslint:disable:no-console */ | |
/** | |
* Example usage: ts-node --project ./tsconfig-tsnode.json ./test_encapsulation.ts <some-file.ts> | |
* | |
* Example tsconfig-tsnode.json is at the bottom of this file | |
*/ | |
import { ClassDeclaration, Project, PropertyDeclaration, ReferencedSymbol, ReferenceEntry, SourceFile } from 'ts-morph'; | |
function main(fileNameOrPath: string): void { | |
const project: Project = new Project(); | |
project.addSourceFilesFromTsConfig('./tsconfig.json'); | |
project.addExistingSourceFiles(['src/**/*{.d.ts,.ts}', '!src/**/*.spec.ts']); | |
// optionally call this when complete to resolve and | |
// add the dependent source files to the project | |
project.resolveSourceFileDependencies(); | |
const sourceFile: SourceFile = project.getSourceFileOrThrow(fileNameOrPath); | |
const classDeclarations: ClassDeclaration[] = sourceFile.getClasses(); | |
for (const classDeclaration of classDeclarations) { | |
console.log('================='); | |
console.log(`Class ${classDeclaration.getName()}`); | |
console.log('================='); | |
const props: PropertyDeclaration[] = classDeclaration.getProperties(); | |
for (const prop of props) { | |
findReferences(prop); | |
} | |
} | |
} | |
function findReferences(prop: PropertyDeclaration): void { | |
const referencedSymbols: ReferencedSymbol[] = prop.findReferences(); | |
for (const referencedSymbol of referencedSymbols) { | |
const referenceEntries: ReferenceEntry[] = referencedSymbol.getReferences(); | |
const numReferences = referencedSymbols.length + referenceEntries.length; | |
console.log('-------------------'); | |
console.log(`${numReferences} REFERENCES for ${prop.getName()}`); | |
console.log('-------------------'); | |
for (const referenceEntry of referenceEntries) { | |
console.log('File path: ' + referenceEntry.getSourceFile().getFilePath()); | |
console.log('Line number: ' + referenceEntry.getNode().getStartLineNumber()); | |
// console.log('Start: ' + referenceEntry.getTextSpan().getStart()); | |
// console.log('Length: ' + referenceEntry.getTextSpan().getLength()); | |
console.log('Parent kind: ' + referenceEntry.getNode().getParentOrThrow().getKindName()); | |
console.log('\n'); | |
} | |
} | |
} | |
main(process.argv[2]); | |
/* Example tsconfig-tsnode.json | |
{ | |
"compileOnSave": false, | |
"compilerOptions": { | |
"importHelpers": true, | |
"outDir": "./dist/out-tsc", | |
"baseUrl": "src", | |
"sourceMap": true, | |
"declaration": false, | |
"moduleResolution": "node", | |
"emitDecoratorMetadata": true, | |
"experimentalDecorators": true, | |
"allowSyntheticDefaultImports": true, | |
"target": "es5", | |
"typeRoots": [ | |
"node_modules/@types" | |
], | |
"lib": [ | |
"es2015", | |
"es2016", | |
"es2017", | |
"dom" | |
], | |
"module": "commonjs" | |
}, | |
"files": [ | |
"src/main.ts", | |
"src/polyfills.ts" | |
], | |
"include": [ | |
"src/**\/*.ts" | |
] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment