1
- import camelCase from 'camelcase' ;
2
1
import { default as j } from 'jscodeshift' ;
3
2
import path from 'path' ;
4
3
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' ;
7
5
import { assert , defined , isRecord } from './util/types' ;
8
6
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
+
9
22
/**
10
23
* Get the map of decorators to import other than the computed props, services etc
11
24
* which already have imports in the code
@@ -23,6 +36,7 @@ export function mergeDecoratorImportSpecs(
23
36
templateLayout : existing . templateLayout || newSpecs . templateLayout ,
24
37
off : existing . off || newSpecs . off ,
25
38
tagName : existing . tagName || newSpecs . tagName ,
39
+ observes : existing . observes || newSpecs . observes ,
26
40
unobserves : existing . unobserves || newSpecs . unobserves ,
27
41
} ;
28
42
}
@@ -74,7 +88,8 @@ export function getExpressionToReplace(
74
88
export function getClassName (
75
89
eoExtendExpressionPath : AST . Path < AST . EOExtendExpression > ,
76
90
filePath : string ,
77
- type = ''
91
+ superClassName : string ,
92
+ type : string | undefined
78
93
) : string {
79
94
const varDeclaration = getClosestVariableDeclaration ( eoExtendExpressionPath ) ;
80
95
if ( varDeclaration ) {
@@ -93,22 +108,24 @@ export function getClassName(
93
108
return identifier . name ;
94
109
}
95
110
96
- let className = capitalizeFirstLetter (
97
- camelCase ( path . basename ( filePath , 'js' ) )
98
- ) ;
99
- const capitalizedType = capitalizeFirstLetter ( type ) ;
111
+ let className = classify ( path . basename ( filePath , 'js' ) ) ;
100
112
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 ) ) ) ;
105
125
}
106
126
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 } ` ;
112
129
}
113
130
114
131
return className ;
0 commit comments