-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
module-info.ts
347 lines (297 loc) · 9.04 KB
/
module-info.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import type { Tree } from '@nrwl/devkit';
import {
logger,
normalizePath,
stripIndents,
visitNotIgnoredFiles,
} from '@nrwl/devkit';
import { findNodes } from 'nx/src/utils/typescript';
import { tsquery } from '@phenomnomnominal/tsquery';
import { extname } from 'path';
import type {
ClassDeclaration,
Node,
SourceFile,
VariableDeclaration,
} from 'typescript';
import { SyntaxKind } from 'typescript';
import { getDecoratorMetadata } from '../../../utils/nx-devkit/ast-utils';
import type { EntryPoint } from './entry-point';
export function getModuleDeclaredComponents(
file: SourceFile,
moduleFilePath: string,
projectName: string
): string[] {
const ngModuleDecorator = getNgModuleDecorator(file, moduleFilePath);
const declarationsPropertyAssignment =
getNgModuleDeclarationsPropertyAssignment(
ngModuleDecorator,
moduleFilePath,
projectName
);
if (!declarationsPropertyAssignment) {
return [];
}
const declarationsArray = getDeclarationsArray(
file,
declarationsPropertyAssignment,
moduleFilePath,
projectName
);
if (!declarationsArray) {
return [];
}
return getDeclaredComponentsInDeclarations(declarationsArray);
}
export function getModuleFilePaths(
tree: Tree,
entryPoint: EntryPoint
): string[] {
let moduleFilePaths = [] as string[];
visitNotIgnoredFiles(tree, entryPoint.path, (filePath: string) => {
const normalizedFilePath = normalizePath(filePath);
if (
entryPoint.excludeDirs?.some((excludeDir) =>
normalizedFilePath.startsWith(excludeDir)
)
) {
return;
}
if (
extname(normalizedFilePath) === '.ts' &&
!normalizedFilePath.includes('.storybook') &&
hasNgModule(tree, normalizedFilePath)
) {
moduleFilePaths.push(normalizedFilePath);
}
});
return moduleFilePaths;
}
function hasNgModule(tree: Tree, filePath: string): boolean {
const fileContent = tree.read(filePath, 'utf-8');
const ast = tsquery.ast(fileContent);
const ngModule = tsquery(
ast,
'ClassDeclaration > Decorator > CallExpression > Identifier[name=NgModule]',
{ visitAllChildren: true }
);
return ngModule.length > 0;
}
function getDeclaredComponentsInDeclarations(
declarationsArray: Node
): string[] {
return getDeclaredComponentNodes(declarationsArray)
.map((node) => node.getText())
.filter((name) => name.endsWith('Component'));
}
function getDeclaredComponentNodes(declarationsArray: Node): Node[] {
const cmps = declarationsArray
.getChildren()
.find((node) => node.kind === SyntaxKind.SyntaxList)
.getChildren()
.map((node) => {
if (node.kind === SyntaxKind.Identifier) {
return node;
}
// if the node is a destructuring, follow the variable
if (node.kind === SyntaxKind.SpreadElement) {
const declarationVariableNode = node
.getChildren()
.find((node) => node.kind === SyntaxKind.Identifier);
// try to find the variable declaration in the same component
const declarationVariable = getVariableDeclaration(
declarationVariableNode.getText(),
declarationVariableNode.getSourceFile()
);
if (
declarationVariable &&
declarationVariable.initializer.kind ===
SyntaxKind.ArrayLiteralExpression
) {
const nodes = getDeclaredComponentNodes(
declarationVariable.initializer
);
return nodes;
}
}
return null;
})
.filter((node) => !!node);
return flatten(cmps);
}
function flatten(arr) {
let flattened = [];
for (const entry of arr) {
if (Array.isArray(entry)) {
flattened.push(...flatten(entry));
} else {
flattened.push(entry);
}
}
return flattened;
}
function getDeclarationsArray(
file: SourceFile,
declarationsPropertyAssignment: Node,
moduleFilePath: string,
projectName: string
): Node | undefined {
let declarationArray = declarationsPropertyAssignment
.getChildren()
.find((node) => node.kind === SyntaxKind.ArrayLiteralExpression);
if (declarationArray) {
return declarationArray;
}
// Attempt to follow a variable instead of the literal
declarationArray = getModuleDeclaredComponentsFromVariable(
file,
declarationsPropertyAssignment
);
if (declarationArray) {
return declarationArray;
}
// Attempt to follow a class declaration instead of the literal
declarationArray = getModuleDeclaredComponentsFromClass(
file,
declarationsPropertyAssignment
);
if (!declarationArray) {
logger.warn(
stripIndents`No stories generated because the declarations in ${moduleFilePath} is not an array literal or the variable could not be found. Hint: you can always generate stories later with the 'nx generate @nrwl/angular:stories --name=${projectName}' command.`
);
}
return declarationArray;
}
/**
* Try to get declared components like `declarations: someComponentsArrayConst`
*/
function getModuleDeclaredComponentsFromVariable(
file: SourceFile,
declarationsPropertyAssignment: Node
): Node | undefined {
let declarationsVariable = declarationsPropertyAssignment
.getChildren()
.filter((node) => node.kind === SyntaxKind.Identifier)[1];
if (!declarationsVariable) {
return undefined;
}
// Attempt to find variable declaration in the file
let variableDeclaration = getVariableDeclaration(
declarationsVariable.getText(),
file
);
if (!variableDeclaration) {
return undefined;
}
const declarationArray = variableDeclaration
.getChildren()
.find((node) => node.kind === SyntaxKind.ArrayLiteralExpression);
return declarationArray;
}
/**
* Try to get declared components like `declarations: SomeClass.components` as in
* https://github.com/nrwl/nx/issues/7276.
*/
function getModuleDeclaredComponentsFromClass(
file: SourceFile,
declarationsPropertyAssignment: Node
): Node | undefined {
const propertyAccessExpression = declarationsPropertyAssignment
.getChildren()
.filter((node) => node.kind === SyntaxKind.PropertyAccessExpression)[0];
if (!propertyAccessExpression) {
return undefined;
}
// Should contain 2 identifiers [SomeClass, components]
const [clazz, componentsProperty] = propertyAccessExpression
.getChildren()
.filter((node) => node.kind === SyntaxKind.Identifier);
if (!clazz || !componentsProperty) {
return undefined;
}
// Attempt to find class declaration in the file
let classDeclaration = getClassDeclaration(clazz.getText(), file);
if (!classDeclaration) {
return undefined;
}
const declarationArray = classDeclaration.members
.filter((node) => node.kind === SyntaxKind.PropertyDeclaration)
.find((propertyDeclaration) =>
propertyDeclaration
.getChildren()
.find(
(node) =>
node.kind === SyntaxKind.Identifier &&
node.getText() === componentsProperty.getText()
)
)
.getChildren()
.find((node) => node.kind === SyntaxKind.ArrayLiteralExpression);
return declarationArray;
}
function getClassDeclaration(
className: string,
file: SourceFile
): ClassDeclaration | undefined {
const classDeclaration = findNodes(file, SyntaxKind.ClassDeclaration).find(
(classDeclaration) =>
classDeclaration
.getChildren()
.find(
(node) =>
node.kind === SyntaxKind.Identifier && node.getText() === className
)
) as ClassDeclaration;
return classDeclaration;
}
function getVariableDeclaration(
variableName: string,
file: SourceFile
): VariableDeclaration | undefined {
const variableDeclaration = findNodes(
file,
SyntaxKind.VariableDeclaration
).find((variableDeclaration) =>
variableDeclaration
.getChildren()
.find(
(node) =>
node.kind === SyntaxKind.Identifier && node.getText() === variableName
)
) as VariableDeclaration;
return variableDeclaration;
}
function getNgModuleDeclarationsPropertyAssignment(
ngModuleDecorator: Node,
moduleFilePath: string,
projectName: string
): Node | undefined {
const syntaxList = ngModuleDecorator.getChildren().find((node) => {
return node.kind === SyntaxKind.SyntaxList;
});
const declarationsPropertyAssignment = syntaxList
.getChildren()
.find((node) => {
return (
node.kind === SyntaxKind.PropertyAssignment &&
node.getChildren()[0].getText() === 'declarations'
);
});
if (!declarationsPropertyAssignment) {
logger.warn(
stripIndents`No stories generated because there were no components declared in ${moduleFilePath}. Hint: you can always generate stories later with the 'nx generate @nrwl/angular:stories --name=${projectName}' command.`
);
}
return declarationsPropertyAssignment;
}
function getNgModuleDecorator(file: SourceFile, moduleFilePath: string): Node {
const ngModuleDecorators = getDecoratorMetadata(
file,
'NgModule',
'@angular/core'
);
if (ngModuleDecorators.length === 0) {
throw new Error(`No @NgModule decorator in ${moduleFilePath}.`);
}
return ngModuleDecorators[0];
}