ember-codemod-remove-ember-css-modules 0.1.0

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.
Files changed (48) hide show
  1. package/LICENSE.md +9 -0
  2. package/README.md +222 -0
  3. package/bin/ember-codemod-remove-ember-css-modules.js +34 -0
  4. package/package.json +58 -0
  5. package/src/blueprints/ember-cli/component/javascript.js +7 -0
  6. package/src/blueprints/ember-cli/component/typescript.ts +26 -0
  7. package/src/blueprints/ember-cli/controller/javascript.js +7 -0
  8. package/src/blueprints/ember-cli/controller/typescript.ts +7 -0
  9. package/src/index.js +5 -0
  10. package/src/migration/ember-app/index.js +37 -0
  11. package/src/migration/ember-app/steps/analyze-app.js +155 -0
  12. package/src/migration/ember-app/steps/create-options.js +44 -0
  13. package/src/migration/ember-app/steps/import-styles-in-components.js +43 -0
  14. package/src/migration/ember-app/steps/import-styles-in-routes.js +39 -0
  15. package/src/migration/ember-app/steps/index.js +9 -0
  16. package/src/migration/ember-app/steps/move-stylesheets.js +51 -0
  17. package/src/migration/ember-app/steps/shared/import-styles.js +211 -0
  18. package/src/migration/ember-app/steps/shared/index.js +2 -0
  19. package/src/migration/ember-app/steps/shared/update-templates.js +437 -0
  20. package/src/migration/ember-app/steps/update-app-app-js.js +64 -0
  21. package/src/migration/ember-app/steps/update-component-templates.js +28 -0
  22. package/src/migration/ember-app/steps/update-package-json.js +58 -0
  23. package/src/migration/ember-app/steps/update-route-templates.js +22 -0
  24. package/src/migration/index.js +1 -0
  25. package/src/utils/abstract-syntax-tree/handlebars.js +20 -0
  26. package/src/utils/abstract-syntax-tree/javascript.js +109 -0
  27. package/src/utils/abstract-syntax-tree.js +2 -0
  28. package/src/utils/blueprints/blueprint-root.js +13 -0
  29. package/src/utils/blueprints/decide-version.js +25 -0
  30. package/src/utils/blueprints/process-template.js +12 -0
  31. package/src/utils/blueprints.js +3 -0
  32. package/src/utils/files/copy-files.js +19 -0
  33. package/src/utils/files/create-files.js +17 -0
  34. package/src/utils/files/find-files.js +28 -0
  35. package/src/utils/files/map-file-paths.js +25 -0
  36. package/src/utils/files/move-files.js +22 -0
  37. package/src/utils/files/remove-directory-if-empty.js +19 -0
  38. package/src/utils/files/remove-files.js +15 -0
  39. package/src/utils/files.js +7 -0
  40. package/src/utils/json/convert-to-map.js +3 -0
  41. package/src/utils/json/convert-to-object.js +5 -0
  42. package/src/utils/json.js +2 -0
  43. package/src/utils/string/ember-cli-string-utils/camelize.js +12 -0
  44. package/src/utils/string/ember-cli-string-utils/classify.js +14 -0
  45. package/src/utils/string/entity-name/classify.js +6 -0
  46. package/src/utils/string/entity-name/double-colonize.js +10 -0
  47. package/src/utils/string/entity-name/parse.js +19 -0
  48. package/src/utils/string.js +5 -0
@@ -0,0 +1,437 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+
4
+ import { ASTHandlebars as AST } from '../../../../utils/abstract-syntax-tree.js';
5
+ import { createFiles } from '../../../../utils/files.js';
6
+
7
+ function sanitizeClassAndLocalClassAttributes(file) {
8
+ function removeAttributeWithoutValue(attributeName, attributes) {
9
+ const attributeIndex = attributes.findIndex(
10
+ (attribute) => attribute.name === attributeName
11
+ );
12
+
13
+ if (attributeIndex === -1) {
14
+ return;
15
+ }
16
+
17
+ const attribute = attributes[attributeIndex];
18
+
19
+ if (attribute.isValueless) {
20
+ attributes.splice(attributeIndex, 1);
21
+ return;
22
+ }
23
+
24
+ if (attribute.value.type !== 'TextNode') {
25
+ return;
26
+ }
27
+
28
+ const attributeValue = attribute.value.chars.trim();
29
+
30
+ if (attributeValue === '') {
31
+ attributes.splice(attributeIndex, 1);
32
+ return;
33
+ }
34
+ }
35
+
36
+ const ast = AST.traverse(file, {
37
+ ElementNode(node) {
38
+ const { attributes } = node;
39
+
40
+ removeAttributeWithoutValue('class', attributes);
41
+ removeAttributeWithoutValue('local-class', attributes);
42
+ },
43
+ });
44
+
45
+ return AST.print(ast);
46
+ }
47
+
48
+ function mergeClassAndLocalClassAttributes(file, data) {
49
+ const ast = AST.traverse(file, {
50
+ ElementNode(node) {
51
+ // Check that both class and local-class attributes exist
52
+ const { attributes } = node;
53
+
54
+ const localClassAttributeIndex = attributes.findIndex(
55
+ (attribute) => attribute.name === 'local-class'
56
+ );
57
+ const classAttributeIndex = attributes.findIndex(
58
+ (attribute) => attribute.name === 'class'
59
+ );
60
+
61
+ if (localClassAttributeIndex === -1 || classAttributeIndex === -1) {
62
+ return;
63
+ }
64
+
65
+ // Merge attributes only when both have TextNode values
66
+ const localClassAttribute = attributes[localClassAttributeIndex];
67
+ const classAttribute = attributes[classAttributeIndex];
68
+
69
+ if (
70
+ localClassAttribute.value.type !== 'TextNode' ||
71
+ classAttribute.value.type !== 'TextNode'
72
+ ) {
73
+ return;
74
+ }
75
+
76
+ const localClassAttributeValue = localClassAttribute.value.chars.trim();
77
+ const classAttributeValue = classAttribute.value.chars.trim();
78
+
79
+ // Update the class attribute
80
+ const localClassNames = localClassAttributeValue.split(/\s+/);
81
+ let params;
82
+
83
+ if (localClassNames.length === 1) {
84
+ params = [
85
+ AST.builders.path(`this.${data.__styles__}.${localClassNames[0]}`),
86
+ AST.builders.string(` ${classAttributeValue}`),
87
+ ];
88
+ } else {
89
+ params = [
90
+ AST.builders.sexpr('local-class', [
91
+ AST.builders.path(`this.${data.__styles__}`),
92
+ ...localClassNames.map(AST.builders.string),
93
+ ]),
94
+ AST.builders.string(` ${classAttributeValue}`),
95
+ ];
96
+ }
97
+
98
+ attributes[classAttributeIndex].value = AST.builders.mustache(
99
+ AST.builders.path('concat'),
100
+ params
101
+ );
102
+
103
+ // Remove the local-class attribute
104
+ attributes.splice(localClassAttributeIndex, 1);
105
+ },
106
+ });
107
+
108
+ return AST.print(ast);
109
+ }
110
+
111
+ function removeLocalClassHelpers(file, data) {
112
+ /*
113
+ The {{local-class}} helper from ember-css-modules allows
114
+ 1 positional argument. The argument's value is presumed
115
+ to be a concatenated string or `undefined`.
116
+ */
117
+ function canRemoveLocalClassHelper(path) {
118
+ const hasFromArgument = path.hash.pairs.some((pair) => pair.key === 'from');
119
+
120
+ if (hasFromArgument) {
121
+ throw new RangeError(
122
+ `Unable to handle the {{local-class}} helper's \`from\` key. See lines ${path.loc.start.line}-${path.loc.end.line}.`
123
+ );
124
+ }
125
+
126
+ const param = path.params[0];
127
+
128
+ if (param === undefined) {
129
+ return true;
130
+ }
131
+
132
+ if (param.type !== 'StringLiteral') {
133
+ return false;
134
+ }
135
+
136
+ const value = param.value.trim();
137
+
138
+ return value === '';
139
+ }
140
+
141
+ const ast = AST.traverse(file, {
142
+ AttrNode(node) {
143
+ if (node.name !== 'class') {
144
+ return;
145
+ }
146
+
147
+ const hasLocalClassHelper =
148
+ node.value.type === 'MustacheStatement' &&
149
+ node.value.path.original === 'local-class';
150
+
151
+ if (!hasLocalClassHelper) {
152
+ return;
153
+ }
154
+
155
+ if (canRemoveLocalClassHelper(node.value)) {
156
+ return null;
157
+ }
158
+
159
+ const param = node.value.params[0];
160
+
161
+ if (param.type !== 'StringLiteral') {
162
+ return;
163
+ }
164
+
165
+ const localClassNames = param.value.trim().split(/\s+/);
166
+
167
+ if (localClassNames.length === 1) {
168
+ node.value = AST.builders.mustache(
169
+ AST.builders.path(`this.${data.__styles__}.${localClassNames[0]}`)
170
+ );
171
+
172
+ return;
173
+ }
174
+
175
+ node.value = AST.builders.mustache(AST.builders.path('local-class'), [
176
+ AST.builders.path(`this.${data.__styles__}`),
177
+ ...localClassNames.map(AST.builders.string),
178
+ ]);
179
+ },
180
+
181
+ SubExpression(node) {
182
+ const hasLocalClassHelper = node.path.original === 'local-class';
183
+
184
+ if (!hasLocalClassHelper) {
185
+ return;
186
+ }
187
+
188
+ if (canRemoveLocalClassHelper(node)) {
189
+ return AST.builders.string('');
190
+ }
191
+
192
+ const param = node.params[0];
193
+
194
+ if (param.type !== 'StringLiteral') {
195
+ return node;
196
+ }
197
+
198
+ const localClassNames = param.value.trim().split(/\s+/);
199
+
200
+ if (localClassNames.length === 1) {
201
+ return AST.builders.path(
202
+ `this.${data.__styles__}.${localClassNames[0]}`
203
+ );
204
+ }
205
+
206
+ return AST.builders.sexpr(AST.builders.path('local-class'), [
207
+ AST.builders.path(`this.${data.__styles__}`),
208
+ ...localClassNames.map(AST.builders.string),
209
+ ]);
210
+ },
211
+ });
212
+
213
+ return AST.print(ast);
214
+ }
215
+
216
+ function removeLocalClassAttributes(file, data) {
217
+ function transformParam(param) {
218
+ switch (param.type) {
219
+ case 'StringLiteral': {
220
+ const localClassNames = param.value.trim().split(/\s+/);
221
+
222
+ if (localClassNames.length === 1) {
223
+ param.value = localClassNames[0];
224
+ } else {
225
+ param = AST.builders.sexpr(
226
+ 'array',
227
+ localClassNames.map(AST.builders.string)
228
+ );
229
+ }
230
+
231
+ break;
232
+ }
233
+
234
+ case 'SubExpression': {
235
+ switch (param.path.original) {
236
+ case 'if':
237
+ case 'unless': {
238
+ const subparams = param.params.map(transformParam);
239
+
240
+ param = AST.builders.sexpr(param.path.original, subparams);
241
+
242
+ break;
243
+ }
244
+ }
245
+
246
+ break;
247
+ }
248
+ }
249
+
250
+ return param;
251
+ }
252
+
253
+ function transformPart(part) {
254
+ switch (part.type) {
255
+ case 'MustacheStatement': {
256
+ switch (part.path.original) {
257
+ case 'concat': {
258
+ // eslint-disable-next-line no-inner-declarations
259
+ function hasPathExpression(params) {
260
+ return params.some((param) => param.type === 'PathExpression');
261
+ }
262
+
263
+ if (hasPathExpression(part.params)) {
264
+ return AST.builders.mustache(AST.builders.path('get'), [
265
+ AST.builders.path(`this.${data.__styles__}`),
266
+ AST.builders.sexpr(part.path.original, part.params),
267
+ ]);
268
+ }
269
+
270
+ const params = part.params.map(transformParam);
271
+
272
+ return AST.builders.mustache('local-class', [
273
+ AST.builders.path(`this.${data.__styles__}`),
274
+ ...params,
275
+ ]);
276
+ }
277
+
278
+ case 'if':
279
+ case 'unless': {
280
+ const params = part.params.map(transformParam);
281
+
282
+ return AST.builders.mustache('local-class', [
283
+ AST.builders.path(`this.${data.__styles__}`),
284
+ AST.builders.sexpr(part.path.original, params),
285
+ ]);
286
+ }
287
+
288
+ default: {
289
+ return AST.builders.mustache(AST.builders.path('get'), [
290
+ AST.builders.path(`this.${data.__styles__}`),
291
+ part.path,
292
+ ]);
293
+ }
294
+ }
295
+ }
296
+
297
+ case 'TextNode': {
298
+ const value = part.chars.trim();
299
+
300
+ if (value === '') {
301
+ return part;
302
+ }
303
+
304
+ const localClassNames = value.split(/\s+/);
305
+
306
+ if (localClassNames.length === 1) {
307
+ return AST.builders.mustache(
308
+ AST.builders.path(`this.${data.__styles__}.${localClassNames[0]}`)
309
+ );
310
+ }
311
+
312
+ return AST.builders.mustache(AST.builders.path('local-class'), [
313
+ AST.builders.path(`this.${data.__styles__}`),
314
+ ...localClassNames.map(AST.builders.string),
315
+ ]);
316
+ }
317
+ }
318
+ }
319
+
320
+ function transformParts(parts) {
321
+ const numParts = parts.length;
322
+
323
+ return parts.reduce((accumulator, part, index) => {
324
+ accumulator.push(transformPart(part));
325
+
326
+ if (index < numParts - 1) {
327
+ accumulator.push(AST.builders.text(' '));
328
+ }
329
+
330
+ return accumulator;
331
+ }, []);
332
+ }
333
+
334
+ const ast = AST.traverse(file, {
335
+ ElementNode(node) {
336
+ // Check if the local-class attribute (still) exists
337
+ const { attributes } = node;
338
+
339
+ const localClassAttributeIndex = attributes.findIndex(
340
+ (attribute) => attribute.name === 'local-class'
341
+ );
342
+
343
+ if (localClassAttributeIndex === -1) {
344
+ return;
345
+ }
346
+
347
+ // Change the local-class attribute to class
348
+ const localClassAttribute = attributes[localClassAttributeIndex];
349
+
350
+ switch (localClassAttribute.value.type) {
351
+ case 'ConcatStatement': {
352
+ localClassAttribute.name = 'class';
353
+ localClassAttribute.value.parts = transformParts(
354
+ localClassAttribute.value.parts
355
+ );
356
+
357
+ break;
358
+ }
359
+
360
+ case 'MustacheStatement': {
361
+ localClassAttribute.name = 'class';
362
+ localClassAttribute.value = transformPart(localClassAttribute.value);
363
+
364
+ break;
365
+ }
366
+
367
+ case 'TextNode': {
368
+ localClassAttribute.name = 'class';
369
+ localClassAttribute.value = transformPart(localClassAttribute.value);
370
+
371
+ break;
372
+ }
373
+ }
374
+ },
375
+
376
+ HashPair(node) {
377
+ if (node.key !== 'local-class') {
378
+ return;
379
+ }
380
+
381
+ node.key = 'class';
382
+ node.value = AST.builders.sexpr('local-class', [
383
+ AST.builders.path(`this.${data.__styles__}`),
384
+ transformParam(node.value),
385
+ ]);
386
+ },
387
+ });
388
+
389
+ return AST.print(ast);
390
+ }
391
+
392
+ function updateTemplate(customizations, options) {
393
+ const { filePath } = customizations;
394
+ const { __styles__, projectRoot } = options;
395
+
396
+ let file = readFileSync(join(projectRoot, filePath), 'utf8');
397
+
398
+ try {
399
+ file = sanitizeClassAndLocalClassAttributes(file);
400
+
401
+ file = mergeClassAndLocalClassAttributes(file, {
402
+ __styles__,
403
+ });
404
+
405
+ file = removeLocalClassHelpers(file, {
406
+ __styles__,
407
+ });
408
+
409
+ file = removeLocalClassAttributes(file, {
410
+ __styles__,
411
+ });
412
+
413
+ const fileMapping = new Map([[filePath, file]]);
414
+
415
+ createFiles(fileMapping, options);
416
+ } catch (e) {
417
+ console.warn(
418
+ `WARNING: updateTemplate could not update \`${filePath}\`. Please update the file manually. (${e.message})\n`
419
+ );
420
+ }
421
+ }
422
+
423
+ export function updateTemplates(customizations, options) {
424
+ const { entities, getFilePath } = customizations;
425
+
426
+ for (const [entityName, extensions] of entities) {
427
+ const hasTemplate = extensions.has('.hbs');
428
+
429
+ if (!hasTemplate) {
430
+ continue;
431
+ }
432
+
433
+ const filePath = getFilePath(entityName);
434
+
435
+ updateTemplate({ filePath }, options);
436
+ }
437
+ }
@@ -0,0 +1,64 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { extname, join } from 'node:path';
3
+
4
+ import { ASTJavaScript as AST } from '../../../utils/abstract-syntax-tree.js';
5
+ import { createFiles, findFiles } from '../../../utils/files.js';
6
+
7
+ function addCssEntryPoint(file, data) {
8
+ const traverse = AST.traverse(data.hasTypeScript);
9
+
10
+ const ast = traverse(file, {
11
+ visitProgram(path) {
12
+ const nodesToAdd = [
13
+ AST.builders.importDeclaration(
14
+ [],
15
+ AST.builders.literal('./assets/app.css')
16
+ ),
17
+ AST.builders.noop(),
18
+ ];
19
+
20
+ path.value.body.unshift(...nodesToAdd);
21
+
22
+ return false;
23
+ },
24
+ });
25
+
26
+ return AST.print(ast);
27
+ }
28
+
29
+ export function updateAppAppJs(options) {
30
+ const { projectRoot } = options;
31
+
32
+ const filePaths = findFiles('app/app.{js,ts}', {
33
+ cwd: projectRoot,
34
+ });
35
+
36
+ if (filePaths.length !== 1) {
37
+ console.warn(
38
+ 'WARNING: updateAppAppJs could not find app.{js,ts} in the app folder. Will skip this step.\n'
39
+ );
40
+
41
+ return;
42
+ }
43
+
44
+ const filePath = filePaths[0];
45
+
46
+ const fileExtension = extname(filePath);
47
+ const hasTypeScript = fileExtension === '.ts';
48
+
49
+ let file = readFileSync(join(projectRoot, filePath), 'utf8');
50
+
51
+ try {
52
+ file = addCssEntryPoint(file, {
53
+ hasTypeScript,
54
+ });
55
+
56
+ const fileMapping = new Map([[filePath, file]]);
57
+
58
+ createFiles(fileMapping, options);
59
+ } catch (e) {
60
+ console.warn(
61
+ `WARNING: updateAppAppJs could not update \`${filePath}\`. Please update the file manually. (${e.message})\n`
62
+ );
63
+ }
64
+ }
@@ -0,0 +1,28 @@
1
+ import { join } from 'node:path';
2
+
3
+ import { updateTemplates } from './shared/index.js';
4
+
5
+ function getFilePath(options) {
6
+ const { componentStructure } = options;
7
+
8
+ return function (entityName) {
9
+ let filePath = join('app/components', entityName);
10
+
11
+ if (componentStructure === 'nested') {
12
+ filePath += '/index';
13
+ }
14
+
15
+ filePath += '.hbs';
16
+
17
+ return filePath;
18
+ };
19
+ }
20
+
21
+ export function updateComponentTemplates(context, options) {
22
+ const customizations = {
23
+ entities: context.components,
24
+ getFilePath: getFilePath(options),
25
+ };
26
+
27
+ updateTemplates(customizations, options);
28
+ }
@@ -0,0 +1,58 @@
1
+ import { readFileSync, writeFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+
4
+ import { decideVersion } from '../../../utils/blueprints.js';
5
+ import { convertToMap, convertToObject } from '../../../utils/json.js';
6
+
7
+ function updateDevDependencies(packageJson, options) {
8
+ const { project } = options;
9
+
10
+ const devDependencies = convertToMap(packageJson['devDependencies']);
11
+
12
+ const packagesToDelete = ['ember-css-modules'];
13
+
14
+ packagesToDelete.forEach((packageName) => {
15
+ devDependencies.delete(packageName);
16
+ });
17
+
18
+ const packagesToInstall = new Set(['embroider-css-modules']);
19
+
20
+ if (project.hasTypeScript) {
21
+ packagesToInstall.add('type-css-modules');
22
+ }
23
+
24
+ [...packagesToInstall].sort().forEach((packageName) => {
25
+ const version = decideVersion(packageName, options);
26
+
27
+ devDependencies.set(packageName, version);
28
+ });
29
+
30
+ packageJson['devDependencies'] = convertToObject(devDependencies);
31
+ }
32
+
33
+ function updateScripts(packageJson, options) {
34
+ const { project } = options;
35
+
36
+ const scripts = convertToMap(packageJson.scripts);
37
+
38
+ if (project.hasTypeScript) {
39
+ scripts.set('prelint:types', 'type-css-modules --src app');
40
+ }
41
+
42
+ packageJson['scripts'] = convertToObject(scripts);
43
+ }
44
+
45
+ export function updatePackageJson(options) {
46
+ const { projectRoot } = options;
47
+
48
+ const oldPath = join(projectRoot, 'package.json');
49
+ const oldFile = readFileSync(oldPath, 'utf8');
50
+ const packageJson = JSON.parse(oldFile);
51
+
52
+ updateDevDependencies(packageJson, options);
53
+ updateScripts(packageJson, options);
54
+
55
+ const newFile = JSON.stringify(packageJson, null, 2) + '\n';
56
+
57
+ writeFileSync(oldPath, newFile, 'utf8');
58
+ }
@@ -0,0 +1,22 @@
1
+ import { join } from 'node:path';
2
+
3
+ import { updateTemplates } from './shared/index.js';
4
+
5
+ function getFilePath() {
6
+ return function (entityName) {
7
+ let filePath = join('app/templates', entityName);
8
+
9
+ filePath += '.hbs';
10
+
11
+ return filePath;
12
+ };
13
+ }
14
+
15
+ export function updateRouteTemplates(context, options) {
16
+ const customizations = {
17
+ entities: context.routes,
18
+ getFilePath: getFilePath(),
19
+ };
20
+
21
+ updateTemplates(customizations, options);
22
+ }
@@ -0,0 +1 @@
1
+ export * from './ember-app/index.js';
@@ -0,0 +1,20 @@
1
+ import { builders, print, transform } from 'ember-template-recast';
2
+
3
+ function traverseHandlebars(file, visitMethods = {}) {
4
+ const { ast } = transform({
5
+ plugin() {
6
+ return visitMethods;
7
+ },
8
+ template: file,
9
+ });
10
+
11
+ return ast;
12
+ }
13
+
14
+ const tools = {
15
+ builders,
16
+ print,
17
+ traverse: traverseHandlebars,
18
+ };
19
+
20
+ export default tools;