@craft-ng/dev-tools 0.1.9 → 0.4.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,240 +0,0 @@
1
- const fs = require('node:fs');
2
- const path = require('node:path');
3
-
4
- process.env.TS_NODE_PROJECT ??= path.resolve(
5
- __dirname,
6
- '../tsconfig.codemod.json',
7
- );
8
- require('ts-node/register/transpile-only');
9
-
10
- const { Project, Node, SyntaxKind } = require('ts-morph');
11
- const { getAngularKind } = require('../scripts/angular-brand-codemod.ts');
12
-
13
- const projectCache = new Map();
14
-
15
- module.exports = {
16
- meta: {
17
- type: 'problem',
18
- docs: {
19
- description:
20
- 'Disallow direct exports of Angular symbols managed through brandAngularSymbol.',
21
- },
22
- schema: [],
23
- },
24
- create(context) {
25
- return {
26
- 'Program:exit'() {
27
- const sourceCode = context.sourceCode ?? context.getSourceCode();
28
- const filePath = getFilePath(context);
29
- if (!filePath || !filePath.endsWith('.ts')) {
30
- return;
31
- }
32
-
33
- const text = sourceCode.getText();
34
- if (!text.includes('export')) {
35
- return;
36
- }
37
-
38
- const sourceFile = getProjectSourceFile(
39
- getProject(getCwd(context)),
40
- filePath,
41
- text,
42
- );
43
- const angularClasses = sourceFile
44
- .getClasses()
45
- .map((classDeclaration) => ({
46
- classDeclaration,
47
- angularKind: getAngularKind(classDeclaration),
48
- }))
49
- .filter((entry) => Boolean(entry.angularKind));
50
-
51
- for (const { classDeclaration, angularKind } of angularClasses) {
52
- reportDirectClassExport(
53
- context,
54
- sourceCode,
55
- classDeclaration,
56
- angularKind,
57
- );
58
- reportIdentifierExportAssignment(
59
- context,
60
- sourceCode,
61
- sourceFile,
62
- classDeclaration,
63
- angularKind,
64
- );
65
- reportNamedExports(
66
- context,
67
- sourceCode,
68
- sourceFile,
69
- classDeclaration,
70
- angularKind,
71
- );
72
- }
73
- },
74
- };
75
- },
76
- };
77
-
78
- function getProject(cwd) {
79
- let project = projectCache.get(cwd);
80
- if (project) {
81
- return project;
82
- }
83
-
84
- const tsConfigFilePath = path.join(cwd, 'tsconfig.json');
85
- project = fs.existsSync(tsConfigFilePath)
86
- ? new Project({ tsConfigFilePath })
87
- : new Project({
88
- compilerOptions: {
89
- experimentalDecorators: true,
90
- target: 9,
91
- },
92
- });
93
-
94
- projectCache.set(cwd, project);
95
- return project;
96
- }
97
-
98
- function getProjectSourceFile(project, filePath, text) {
99
- const normalizedPath = path.resolve(filePath);
100
- const existingSourceFile = project.getSourceFile(normalizedPath);
101
- if (existingSourceFile) {
102
- existingSourceFile.replaceWithText(text);
103
- return existingSourceFile;
104
- }
105
-
106
- const sourceFile = project.addSourceFileAtPathIfExists(normalizedPath);
107
- if (sourceFile) {
108
- sourceFile.replaceWithText(text);
109
- return sourceFile;
110
- }
111
-
112
- return project.createSourceFile(normalizedPath, text, { overwrite: true });
113
- }
114
-
115
- function getFilePath(context) {
116
- const filePath = context.filename ?? context.getFilename();
117
- if (!filePath || filePath === '<input>') {
118
- return undefined;
119
- }
120
-
121
- return filePath;
122
- }
123
-
124
- function getCwd(context) {
125
- return context.cwd ?? process.cwd();
126
- }
127
-
128
- function reportDirectClassExport(
129
- context,
130
- sourceCode,
131
- classDeclaration,
132
- angularKind,
133
- ) {
134
- const hasDirectExportModifier = classDeclaration
135
- .getModifiers()
136
- .some(
137
- (modifier) =>
138
- modifier.getKind() === SyntaxKind.ExportKeyword ||
139
- modifier.getKind() === SyntaxKind.DefaultKeyword,
140
- );
141
-
142
- if (!hasDirectExportModifier) {
143
- return;
144
- }
145
-
146
- reportNode(
147
- context,
148
- sourceCode,
149
- classDeclaration,
150
- `Do not export ${formatAngularKind(angularKind)} classes directly. Keep the class local and export default brandAngularSymbol(...).`,
151
- );
152
- }
153
-
154
- function reportIdentifierExportAssignment(
155
- context,
156
- sourceCode,
157
- sourceFile,
158
- classDeclaration,
159
- angularKind,
160
- ) {
161
- const className = classDeclaration.getName();
162
- if (!className) {
163
- return;
164
- }
165
-
166
- for (const exportAssignment of sourceFile.getExportAssignments()) {
167
- if (exportAssignment.isExportEquals()) {
168
- continue;
169
- }
170
-
171
- const expression = exportAssignment.getExpression();
172
- if (!Node.isIdentifier(expression) || expression.getText() !== className) {
173
- continue;
174
- }
175
-
176
- reportNode(
177
- context,
178
- sourceCode,
179
- exportAssignment,
180
- `Do not export ${formatAngularKind(angularKind)} classes directly. Use export default brandAngularSymbol(${className}, deps(...)).`,
181
- );
182
- }
183
- }
184
-
185
- function reportNamedExports(
186
- context,
187
- sourceCode,
188
- sourceFile,
189
- classDeclaration,
190
- angularKind,
191
- ) {
192
- const className = classDeclaration.getName();
193
- if (!className) {
194
- return;
195
- }
196
-
197
- for (const exportDeclaration of sourceFile.getExportDeclarations()) {
198
- if (exportDeclaration.getModuleSpecifier()) {
199
- continue;
200
- }
201
-
202
- for (const namedExport of exportDeclaration.getNamedExports()) {
203
- if (namedExport.getName() !== className) {
204
- continue;
205
- }
206
-
207
- reportNode(
208
- context,
209
- sourceCode,
210
- namedExport,
211
- `Do not export ${formatAngularKind(angularKind)} classes directly. Keep ${className} local and export the branded symbol instead.`,
212
- );
213
- }
214
- }
215
- }
216
-
217
- function reportNode(context, sourceCode, node, message) {
218
- context.report({
219
- loc: {
220
- start: sourceCode.getLocFromIndex(node.getStart()),
221
- end: sourceCode.getLocFromIndex(node.getEnd()),
222
- },
223
- message,
224
- });
225
- }
226
-
227
- function formatAngularKind(angularKind) {
228
- switch (angularKind) {
229
- case 'component':
230
- return '@Component';
231
- case 'directive':
232
- return '@Directive';
233
- case 'pipe':
234
- return '@Pipe';
235
- case 'injectable':
236
- return '@Injectable';
237
- default:
238
- return 'Angular';
239
- }
240
- }