1- import type { JSCodeshift } from 'jscodeshift' ;
1+ import { default as j } from 'jscodeshift' ;
22import * as AST from '../helpers/ast' ;
33import type { DecoratorImportInfoMap } from './decorator-info' ;
44import { getDecoratorImportInfo } from './decorator-info' ;
@@ -62,13 +62,12 @@ function setSpecifierNames(
6262
6363/** Get decorated props from `import` statements */
6464function getExistingDecoratorImports (
65- j : JSCodeshift ,
6665 root : AST . Collection
6766) : Array < AST . Path < AST . DecoratorImportDeclaration > > {
6867 const imports : Array < AST . Path < AST . DecoratorImportDeclaration > > = [ ] ;
6968
7069 for ( const path in Object . fromEntries ( DECORATOR_PATHS ) ) {
71- const decoratorImport = getExistingImportForPath ( j , root , path ) ;
70+ const decoratorImport = getExistingImportForPath ( root , path ) ;
7271 if ( decoratorImport ) {
7372 imports . push ( decoratorImport ) ;
7473 }
@@ -84,7 +83,6 @@ function getExistingDecoratorImports(
8483 * `@ember-decorators/component`
8584 */
8685function createNewImportDeclarations (
87- j : JSCodeshift ,
8886 root : AST . Collection ,
8987 decoratorsToImport : string [ ] ,
9088 /** Already imported paths */
@@ -93,11 +91,9 @@ function createNewImportDeclarations(
9391) : void {
9492 const firstDeclaration = AST . getFirstDeclaration ( root ) ;
9593
96- // FIXME: Why is this necessary?
9794 if ( options . classicDecorator ) {
9895 firstDeclaration . insertBefore (
9996 createImportDeclaration (
100- j ,
10197 [ j . importDefaultSpecifier ( j . identifier ( 'classic' ) ) ] ,
10298 'ember-classic-decorator'
10399 )
@@ -113,15 +109,12 @@ function createNewImportDeclarations(
113109
114110 for ( const path of paths ) {
115111 const specifiers = createEmberDecoratorSpecifiers (
116- j ,
117112 edPathNameMap . get ( path ) ,
118113 decoratorsToImport
119114 ) . sort ( ( a , b ) => a . imported . name . localeCompare ( b . imported . name ) ) ;
120115
121116 if ( specifiers . length > 0 ) {
122- firstDeclaration . insertBefore (
123- createImportDeclaration ( j , specifiers , path )
124- ) ;
117+ firstDeclaration . insertBefore ( createImportDeclaration ( specifiers , path ) ) ;
125118 }
126119 }
127120}
@@ -137,15 +130,14 @@ function createNewImportDeclarations(
137130 * ```
138131 */
139132function getDecoratorPathSpecifiers (
140- j : JSCodeshift ,
141133 root : AST . Collection ,
142134 decoratorsToImport : string [ ] = [ ]
143135) : Record < string , AST . ImportSpecifier [ ] > {
144136 const edPathNameMap = new Map ( EMBER_DECORATOR_SPECIFIERS ) ;
145137
146138 const decoratorPathSpecifierMap : Record < string , AST . ImportSpecifier [ ] > = { } ;
147139
148- const existingDecoratorImports = getExistingDecoratorImports ( j , root ) ;
140+ const existingDecoratorImports = getExistingDecoratorImports ( root ) ;
149141
150142 // Iterate over the existing imports
151143 // Extract and process the specifiers
@@ -166,7 +158,6 @@ function getDecoratorPathSpecifiers(
166158 // Create decorator specifiers for which no existing specifiers present in the current path
167159 // e.g. `actions` need not to be imported but `@action` need to be imported from `@ember-decorators/object`
168160 const decoratedSpecifiers = createEmberDecoratorSpecifiers (
169- j ,
170161 decoratorsForPath ,
171162 decoratorsToImport
172163 ) ;
@@ -226,7 +217,6 @@ function getDecoratorPathSpecifiers(
226217
227218/** Get existing import statement matching the import path */
228219function getExistingImportForPath (
229- j : JSCodeshift ,
230220 root : AST . Collection ,
231221 importPath : string
232222) : AST . Path < AST . DecoratorImportDeclaration > | undefined {
@@ -244,14 +234,12 @@ function getExistingImportForPath(
244234 * 3. Insert the new imports for which no existing imports found
245235 */
246236export function createDecoratorImportDeclarations (
247- j : JSCodeshift ,
248237 root : AST . Collection ,
249238 decoratorsToImport : string [ ] ,
250239 options : Options
251240) : void {
252241 // Iterate through existing imports, extract the already imported specifiers
253242 const decoratorPathSpecifierMap = getDecoratorPathSpecifiers (
254- j ,
255243 root ,
256244 decoratorsToImport
257245 ) ;
@@ -261,7 +249,7 @@ export function createDecoratorImportDeclarations(
261249 // Create import statement replacing the existing ones with specifiers importing from ember-decorators namespace
262250 for ( const decoratorPath of decoratorPathsImported ) {
263251 const specifiers = defined ( decoratorPathSpecifierMap [ decoratorPath ] ) ;
264- const existingImport = getExistingImportForPath ( j , root , decoratorPath ) ;
252+ const existingImport = getExistingImportForPath ( root , decoratorPath ) ;
265253 if ( existingImport ) {
266254 const existingSpecifiers = existingImport . value . specifiers ;
267255 if ( existingSpecifiers ) {
@@ -272,14 +260,13 @@ export function createDecoratorImportDeclarations(
272260 }
273261 } else {
274262 firstDeclaration . insertBefore (
275- createImportDeclaration ( j , specifiers , decoratorPath )
263+ createImportDeclaration ( specifiers , decoratorPath )
276264 ) ;
277265 }
278266 }
279267
280268 // Create new import declarations
281269 createNewImportDeclarations (
282- j ,
283270 root ,
284271 decoratorsToImport ,
285272 decoratorPathsImported ,
@@ -316,10 +303,9 @@ export function createDecoratorImportDeclarations(
316303 * ```
317304 */
318305export function getDecoratorImportInfos (
319- j : JSCodeshift ,
320306 root : AST . Collection
321307) : DecoratorImportInfoMap {
322- const existingDecoratorImports = getExistingDecoratorImports ( j , root ) ;
308+ const existingDecoratorImports = getExistingDecoratorImports ( root ) ;
323309 const decoratorImportInfo : DecoratorImportInfoMap = new Map ( ) ;
324310
325311 for ( const decoratorImport of existingDecoratorImports ) {
0 commit comments