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