@@ -7,11 +7,7 @@ import {
7
7
createEmberDecoratorSpecifiers ,
8
8
createImportDeclaration ,
9
9
} from './transform-helper' ;
10
- import {
11
- DECORATOR_PATHS ,
12
- DECORATOR_PATH_OVERRIDES ,
13
- EMBER_DECORATOR_SPECIFIERS ,
14
- } from './util/index' ;
10
+ import { EMBER_DECORATOR_SPECIFIERS , PROPS_TO_DECORATORS } from './util/index' ;
15
11
import { assert , defined , isString , verified } from './util/types' ;
16
12
17
13
/** Returns true of the specifier is a decorator */
@@ -66,7 +62,7 @@ function getExistingDecoratorImports(
66
62
) : Array < AST . Path < AST . DecoratorImportDeclaration > > {
67
63
const imports : Array < AST . Path < AST . DecoratorImportDeclaration > > = [ ] ;
68
64
69
- for ( const path in Object . fromEntries ( DECORATOR_PATHS ) ) {
65
+ for ( const path in Object . fromEntries ( PROPS_TO_DECORATORS ) ) {
70
66
const decoratorImport = getExistingImportForPath ( root , path ) ;
71
67
if ( decoratorImport ) {
72
68
imports . push ( decoratorImport ) ;
@@ -143,71 +139,60 @@ function getDecoratorPathSpecifiers(
143
139
// Extract and process the specifiers
144
140
// Construct the map with path as key and value as list of specifiers to import from the path
145
141
for ( const decoratorImport of existingDecoratorImports ) {
146
- const { importPropDecoratorMap, decoratorPath } = defined (
147
- DECORATOR_PATHS . get (
148
- verified ( decoratorImport . value . source . value , isString )
149
- )
150
- ) ;
151
- // Decorators to be imported for the path
152
- // These are typically additional decorators which need to be imported for a path
153
- // For example - `@action` decorator
154
- const decoratorsForPath = edPathNameMap . get ( decoratorPath ) ?? [ ] ;
155
- // delete the visited path to avoid duplicate imports
156
- edPathNameMap . delete ( decoratorPath ) ;
157
-
158
- // Create decorator specifiers for which no existing specifiers present in the current path
159
- // e.g. `actions` need not to be imported but `@action` need to be imported from `@ember-decorators/object`
160
- const decoratedSpecifiers = createEmberDecoratorSpecifiers (
161
- decoratorsForPath ,
162
- decoratorsToImport
163
- ) ;
164
- const existingSpecifiers = decoratorImport . value . specifiers ?? [ ] ;
165
-
166
- // Iterate over existing specifiers for the current path. This is needed
167
- // to pick the only required specifiers from the existing imports
168
- // For example - To pick `observer` from `import { get, set, observer } from "@ember/object"`
169
- for ( let i = existingSpecifiers . length - 1 ; i >= 0 ; i -= 1 ) {
170
- const existingSpecifier = defined ( existingSpecifiers [ i ] ) ;
171
-
172
- if ( isSpecifierDecorator ( existingSpecifier , importPropDecoratorMap ) ) {
173
- // Update decorator local and imported names,
174
- // Needed in case of `observer` which need to be renamed to `@observes`
175
- setSpecifierNames ( existingSpecifier , importPropDecoratorMap ) ;
176
- // Check if the decorator import path is overridden
177
- // Needed in case of `observes` which need to be imported from `@ember-decorators/object`
178
- const overriddenPath = DECORATOR_PATH_OVERRIDES . get (
179
- existingSpecifier . imported . name
180
- ) ;
181
- if ( overriddenPath ) {
182
- decoratorPathSpecifierMap [ overriddenPath ] = [
183
- ...( decoratorPathSpecifierMap [ overriddenPath ] ?? [ ] ) ,
184
- existingSpecifier ,
185
- ] ;
186
- } else {
187
- const isSpecifierPresent = decoratedSpecifiers . some ( ( specifier ) => {
142
+ const path = verified ( decoratorImport . value . source . value , isString ) ;
143
+ const infos = defined ( PROPS_TO_DECORATORS . get ( path ) ) ;
144
+ for ( const { decoratorPath, importPropDecoratorMap } of infos ) {
145
+ // Decorators to be imported for the path
146
+ // These are typically additional decorators which need to be imported for a path
147
+ // For example - `@action` decorator
148
+ const decoratorsForPath = edPathNameMap . get ( decoratorPath ) ?? [ ] ;
149
+ // delete the visited path to avoid duplicate imports
150
+ edPathNameMap . delete ( decoratorPath ) ;
151
+
152
+ // Create decorator specifiers for which no existing specifiers present in the current path
153
+ // e.g. `actions` need not to be imported but `@action` need to be imported from `@ember-decorators/object`
154
+ const decoratorSpecifiers = createEmberDecoratorSpecifiers (
155
+ decoratorsForPath ,
156
+ decoratorsToImport
157
+ ) ;
158
+
159
+ const existingSpecifiers = decoratorImport . value . specifiers ?? [ ] ;
160
+
161
+ // Iterate over existing specifiers for the current path. This is needed
162
+ // to pick the only required specifiers from the existing imports
163
+ // For example - To pick `observer` from `import { get, set, observer } from "@ember/object"`
164
+ for ( let i = existingSpecifiers . length - 1 ; i >= 0 ; i -= 1 ) {
165
+ const existingSpecifier = defined ( existingSpecifiers [ i ] ) ;
166
+
167
+ if ( isSpecifierDecorator ( existingSpecifier , importPropDecoratorMap ) ) {
168
+ // Update decorator local and imported names,
169
+ // Needed in case of `observer` which need to be renamed to `@observes`
170
+ setSpecifierNames ( existingSpecifier , importPropDecoratorMap ) ;
171
+
172
+ const isSpecifierPresent = decoratorSpecifiers . some ( ( specifier ) => {
188
173
return (
189
174
! specifier . local ?. name &&
190
175
specifier . imported . name === existingSpecifier . imported . name
191
176
) ;
192
177
} ) ;
193
178
if ( ! isSpecifierPresent ) {
194
- decoratedSpecifiers . push ( existingSpecifier ) ;
179
+ decoratorSpecifiers . push ( existingSpecifier ) ;
195
180
}
196
- }
197
181
198
- // Remove the specifier from the existing import
199
- existingSpecifiers . splice ( i , 1 ) ;
182
+ // Remove the specifier from the existing import
183
+ existingSpecifiers . splice ( i , 1 ) ;
184
+ }
200
185
}
201
- }
202
186
203
- if ( decoratedSpecifiers . length > 0 ) {
204
- decoratorPathSpecifierMap [ decoratorPath ] = [
205
- ...( decoratorPathSpecifierMap [ decoratorPath ] ?? [ ] ) ,
206
- ...decoratedSpecifiers ,
207
- ] ;
187
+ if ( decoratorSpecifiers . length > 0 ) {
188
+ decoratorPathSpecifierMap [ decoratorPath ] = [
189
+ ...( decoratorPathSpecifierMap [ decoratorPath ] ?? [ ] ) ,
190
+ ...decoratorSpecifiers ,
191
+ ] ;
208
192
209
- if ( existingSpecifiers . length <= 0 ) {
210
- j ( decoratorImport ) . remove ( ) ;
193
+ if ( existingSpecifiers . length <= 0 ) {
194
+ j ( decoratorImport ) . remove ( ) ;
195
+ }
211
196
}
212
197
}
213
198
}
@@ -311,22 +296,19 @@ export function getDecoratorImportInfos(
311
296
const decoratorImportInfo : DecoratorImportInfoMap = new Map ( ) ;
312
297
313
298
for ( const decoratorImport of existingDecoratorImports ) {
314
- const { importPropDecoratorMap } = defined (
315
- DECORATOR_PATHS . get (
316
- verified ( decoratorImport . value . source . value , isString )
317
- )
318
- ) ;
319
-
299
+ const path = verified ( decoratorImport . value . source . value , isString ) ;
300
+ const infos = defined ( PROPS_TO_DECORATORS . get ( path ) ) ;
320
301
const specifiers = decoratorImport . value . specifiers ?? [ ] ;
321
-
322
- for ( const specifier of specifiers ) {
323
- if ( isSpecifierDecorator ( specifier , importPropDecoratorMap ) ) {
324
- const localName = specifier . local ?. name ;
325
- assert ( localName , 'expected localName' ) ;
326
- decoratorImportInfo . set (
327
- localName ,
328
- getDecoratorImportInfo ( specifier , importPropDecoratorMap )
329
- ) ;
302
+ for ( const { importPropDecoratorMap } of infos ) {
303
+ for ( const specifier of specifiers ) {
304
+ if ( isSpecifierDecorator ( specifier , importPropDecoratorMap ) ) {
305
+ const localName = specifier . local ?. name ;
306
+ assert ( localName , 'expected localName' ) ;
307
+ decoratorImportInfo . set (
308
+ localName ,
309
+ getDecoratorImportInfo ( specifier , importPropDecoratorMap )
310
+ ) ;
311
+ }
330
312
}
331
313
}
332
314
}
0 commit comments