1- import camelCase from 'camelcase' ;
21import { default as j } from 'jscodeshift' ;
32import path from 'path' ;
43import * 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' ;
75import { 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(
7488export 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 ;
0 commit comments