Skip to content

Commit e120812

Browse files
committed
Clean up class naming logic
1 parent 87d1095 commit e120812

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

transforms/helpers/eo-extend-expression.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ export default class EOExtendExpression {
3131
) {
3232
const raw = path.value;
3333

34-
this.className = getClassName(path, filePath, options.runtimeData.type);
3534
this.superClassName = raw.callee.object.name;
35+
this.className = getClassName(
36+
path,
37+
filePath,
38+
this.superClassName,
39+
options.runtimeData?.type
40+
);
3641

3742
const mixins: AST.EOMixin[] = [];
3843
for (const arg of raw.arguments) {

transforms/helpers/parse-helper.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import camelCase from 'camelcase';
21
import { default as j } from 'jscodeshift';
32
import path from 'path';
43
import * as AST from './ast';
5-
import type { DecoratorImportSpecs } from './util/index';
6-
import { capitalizeFirstLetter } from './util/index';
4+
import { classify, type DecoratorImportSpecs } from './util/index';
75
import { assert, defined, isRecord } from './util/types';
86

7+
const DO_NOT_SUFFIX = new Set(['Component', 'Helper', 'EmberObject']);
8+
9+
// List copied from ember-codemods-telemetry-helpers
10+
const TELEMETRY_TYPES = new Set([
11+
'Application',
12+
'Controller',
13+
'Route',
14+
'Component',
15+
'Service',
16+
'Helper',
17+
'Router',
18+
'Engine',
19+
'EmberObject',
20+
]);
21+
922
/**
1023
* Get the map of decorators to import other than the computed props, services etc
1124
* which already have imports in the code
@@ -23,6 +36,7 @@ export function mergeDecoratorImportSpecs(
2336
templateLayout: existing.templateLayout || newSpecs.templateLayout,
2437
off: existing.off || newSpecs.off,
2538
tagName: existing.tagName || newSpecs.tagName,
39+
observes: existing.observes || newSpecs.observes,
2640
unobserves: existing.unobserves || newSpecs.unobserves,
2741
};
2842
}
@@ -74,7 +88,8 @@ export function getExpressionToReplace(
7488
export function getClassName(
7589
eoExtendExpressionPath: AST.Path<AST.EOExtendExpression>,
7690
filePath: string,
77-
type = ''
91+
superClassName: string,
92+
type: string | undefined
7893
): string {
7994
const varDeclaration = getClosestVariableDeclaration(eoExtendExpressionPath);
8095
if (varDeclaration) {
@@ -93,22 +108,24 @@ export function getClassName(
93108
return identifier.name;
94109
}
95110

96-
let className = capitalizeFirstLetter(
97-
camelCase(path.basename(filePath, 'js'))
98-
);
99-
const capitalizedType = capitalizeFirstLetter(type);
111+
let className = classify(path.basename(filePath, 'js'));
100112

101-
if (capitalizedType === className) {
102-
className = capitalizeFirstLetter(
103-
camelCase(path.basename(path.dirname(filePath)))
104-
);
113+
// If type is undefined, this means we couldn't find the telemetry or the user
114+
// is running in NO_TELEMETRY mode. In this case, try to infer the type from
115+
// the super class name.
116+
if (!type) {
117+
superClassName = classify(superClassName);
118+
if (TELEMETRY_TYPES.has(superClassName)) {
119+
type = superClassName;
120+
}
121+
}
122+
123+
if (type === className) {
124+
className = classify(path.basename(path.dirname(filePath)));
105125
}
106126

107-
if (
108-
!['Component', 'Helper', 'EmberObject'].includes(type) &&
109-
!className.endsWith(type)
110-
) {
111-
className = `${className}${capitalizedType}`;
127+
if (type && !DO_NOT_SUFFIX.has(type) && !className.endsWith(type)) {
128+
className = `${className}${type}`;
112129
}
113130

114131
return className;

transforms/helpers/util/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import camelcase from 'camelcase';
2+
13
export const ACTIONS_NAME = 'actions' as const;
24
export type ACTIONS_NAME = typeof ACTIONS_NAME;
35

@@ -285,7 +287,9 @@ export function allowObjectLiteralDecorator(
285287
);
286288
}
287289

288-
/** Convert the first letter to uppercase */
289-
export function capitalizeFirstLetter(name: string): string {
290-
return name ? name.charAt(0).toUpperCase() + name.slice(1) : '';
290+
/**
291+
* Returns a PascalCase version of the given string.
292+
*/
293+
export function classify(name: string): string {
294+
return camelcase(name, { pascalCase: true });
291295
}

0 commit comments

Comments
 (0)