/
ast-utils.ts
224 lines (202 loc) · 6.93 KB
/
ast-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { joinPathFragments, readJsonFile } from '@nrwl/devkit';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript';
import { MappedProjectGraphNode } from '@nrwl/workspace/src/utils/runtime-lint-utils';
import { existsSync, readFileSync } from 'fs';
import { dirname } from 'path';
import ts = require('typescript');
import { logger } from '@nrwl/devkit';
import { workspaceRoot } from '@nrwl/devkit';
function tryReadBaseJson() {
try {
return readJsonFile(joinPathFragments(workspaceRoot, 'tsconfig.base.json'));
} catch (e) {
logger.warn(`Error reading "tsconfig.base.json": \n${JSON.stringify(e)}`);
return null;
}
}
/**
*
* @param importScope like `@myorg/somelib`
* @returns
*/
export function getBarrelEntryPointByImportScope(
importScope: string
): string[] | null {
const tsConfigBase = tryReadBaseJson();
return tsConfigBase?.compilerOptions?.paths[importScope] || null;
}
export function getBarrelEntryPointProjectNode(
importScope: MappedProjectGraphNode<any>
): { path: string; importScope: string }[] | null {
const tsConfigBase = tryReadBaseJson();
if (tsConfigBase?.compilerOptions?.paths) {
const potentialEntryPoints = Object.keys(tsConfigBase.compilerOptions.paths)
.filter((entry) => {
const sourceFolderPaths = tsConfigBase.compilerOptions.paths[entry];
return sourceFolderPaths.some((sourceFolderPath) => {
return sourceFolderPath.includes(importScope.data.root);
});
})
.map((entry) =>
tsConfigBase.compilerOptions.paths[entry].map((x) => ({
path: x,
importScope: entry,
}))
);
return potentialEntryPoints.flat();
}
return null;
}
function hasMemberExport(exportedMember, filePath) {
const fileContent = readFileSync(filePath, 'utf8');
// use the TypeScript AST to find the path to the file where exportedMember is defined
const sourceFile = ts.createSourceFile(
filePath,
fileContent,
ts.ScriptTarget.Latest,
true
);
// search whether there is already an export with our node
return (
findNodes(sourceFile, ts.SyntaxKind.Identifier).filter(
(identifier: any) => identifier.text === exportedMember
).length > 0
);
}
export function getRelativeImportPath(exportedMember, filePath, basePath) {
const fileContent = readFileSync(filePath, 'utf8');
// use the TypeScript AST to find the path to the file where exportedMember is defined
const sourceFile = ts.createSourceFile(
filePath,
fileContent,
ts.ScriptTarget.Latest,
true
);
// Search in the current file whether there's an export already!
const memberNodes = findNodes(sourceFile, ts.SyntaxKind.Identifier).filter(
(identifier: any) => identifier.text === exportedMember
);
let hasExport = false;
for (const memberNode of memberNodes || []) {
if (memberNode) {
// recursively navigate upwards to find the ExportKey modifier
let parent = memberNode;
do {
parent = parent.parent;
if (parent) {
// if we are inside a parameter list or decorator or param assignment
// then this is not what we're searching for, so break :)
if (
parent.kind === ts.SyntaxKind.Parameter ||
parent.kind === ts.SyntaxKind.PropertyAccessExpression ||
parent.kind === ts.SyntaxKind.TypeReference ||
parent.kind === ts.SyntaxKind.HeritageClause ||
parent.kind === ts.SyntaxKind.Decorator
) {
hasExport = false;
break;
}
// if our identifier is within an ExportDeclaration but is not just
// a re-export of some other module, we're good
if (
parent.kind === ts.SyntaxKind.ExportDeclaration &&
!(parent as any).moduleSpecifier
) {
hasExport = true;
break;
}
if (
parent.modifiers &&
parent.modifiers.find(
(modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword
)
) {
/**
* if we get to a function export declaration we need to verify whether the
* exported function is actually the member we are searching for. Otherwise
* we might end up finding a function that just uses our searched identifier
* internally.
*
* Example: assume we try to find a constant member: `export const SOME_CONSTANT = 'bla'`
*
* Then we might end up in a file that uses it like
*
* import { SOME_CONSTANT } from '@myorg/samelib'
*
* export function someFunction() {
* return `Hi, ${SOME_CONSTANT}`
* }
*
* We want to avoid accidentally picking the someFunction export since we're searching upwards
* starting from `SOME_CONSTANT` identifier usages.
*/
if (parent.kind === ts.SyntaxKind.FunctionDeclaration) {
const parentName = (parent as any).name?.text;
if (parentName === exportedMember) {
hasExport = true;
break;
}
} else {
hasExport = true;
break;
}
}
}
} while (!!parent);
}
if (hasExport) {
break;
}
}
if (hasExport) {
// we found the file, now grab the path
return filePath;
}
// if we didn't find an export, let's try to follow
// all export declarations and see whether any of those
// exports the node we're searching for
const exportDeclarations = findNodes(
sourceFile,
ts.SyntaxKind.ExportDeclaration
) as ts.ExportDeclaration[];
for (const exportDeclaration of exportDeclarations) {
if ((exportDeclaration as any).moduleSpecifier) {
// verify whether the export declaration we're looking at is a named export
// cause in that case we need to check whether our searched member is
// part of the exports
if (
exportDeclaration.exportClause &&
findNodes(exportDeclaration, ts.SyntaxKind.Identifier).filter(
(identifier: any) => identifier.text === exportedMember
).length === 0
) {
continue;
}
const modulePath = (exportDeclaration as any).moduleSpecifier.text;
let moduleFilePath = joinPathFragments(
'./',
dirname(filePath),
`${modulePath}.ts`
);
if (!existsSync(moduleFilePath)) {
// might be a index.ts
moduleFilePath = joinPathFragments(
'./',
dirname(filePath),
`${modulePath}/index.ts`
);
}
if (hasMemberExport(exportedMember, moduleFilePath)) {
const foundFilePath = getRelativeImportPath(
exportedMember,
moduleFilePath,
basePath
);
if (foundFilePath) {
return foundFilePath;
}
}
}
}
return null;
}