@@ -7,6 +7,7 @@ import type {
77 ArrayPattern ,
88 AssignmentPattern ,
99 CallExpression ,
10+ Declaration ,
1011 FunctionExpression ,
1112 Identifier ,
1213 ImportDeclaration ,
@@ -27,7 +28,9 @@ import type {
2728 ASTPath as _ASTPath ,
2829 Collection as _Collection ,
2930} from 'jscodeshift' ;
30- import { LAYOUT_DECORATOR_NAME } from './util/index' ;
31+ import { default as j } from 'jscodeshift' ;
32+ import type { ClassDecoratorName } from './util/index' ;
33+ import { ACTIONS_NAME , CLASS_DECORATOR_NAMES } from './util/index' ;
3134import { isRecord } from './util/types' ;
3235
3336export type { ClassBodyBuilder } from 'ast-types/gen/builders' ;
@@ -50,8 +53,8 @@ export type {
5053 VariableDeclaration ,
5154} from 'jscodeshift' ;
5255
53- export interface ASTPath < N extends ASTNode = ASTNode > extends _ASTPath < N > {
54- parentPath : ASTPath | null | undefined ;
56+ export interface Path < N extends ASTNode = ASTNode > extends _ASTPath < N > {
57+ parentPath : Path | null | undefined ;
5558}
5659
5760export type Collection < N extends Node | ASTNode = ASTNode > = _Collection < N > ;
@@ -71,12 +74,12 @@ function isIdent(u: unknown, ...names: string[]): u is Identifier {
7174 return isNode ( u , 'Identifier' ) && names . includes ( u . name ) ;
7275}
7376
74- export interface RawEOExtendExpression extends CallExpression {
77+ export interface EOExtendExpression extends CallExpression {
7578 callee : EOExtendExpressionCallee ;
7679 arguments : EOExtendArg [ ] ;
7780}
7881
79- export function isEOExtendExpression ( u : unknown ) : u is RawEOExtendExpression {
82+ export function isEOExtendExpression ( u : unknown ) : u is EOExtendExpression {
8083 return (
8184 isNode ( u , 'CallExpression' ) &&
8285 isEOExtendExpressionCallee ( u . callee ) &&
@@ -131,10 +134,10 @@ export function isEOExpression(u: unknown): u is EOExpression {
131134 ) ;
132135}
133136
134- export type EOExpressionProp = EOProperty | EOMethod ;
137+ export type EOExpressionProp = EOProp | EOMethod ;
135138
136139function isEOExpressionProp ( u : unknown ) : u is EOExpressionProp {
137- return isEOProperty ( u ) || isEOMethod ( u ) ;
140+ return isEOProp ( u ) || isEOMethod ( u ) ;
138141}
139142
140143export type EOMixin = Identifier ;
@@ -144,11 +147,11 @@ function isEOMixin(u: unknown): u is EOMixin {
144147}
145148
146149/** A top-level instance property in an Ember Object's ObjectExpression */
147- export interface EOProperty extends ObjectProperty {
150+ export interface EOProp extends ObjectProperty {
148151 key : Identifier ;
149152}
150153
151- function isEOProperty ( u : unknown ) : u is EOProperty {
154+ function isEOProp ( u : unknown ) : u is EOProp {
152155 return isNode ( u , 'ObjectProperty' ) && isNode ( u . key , 'Identifier' ) ;
153156}
154157
@@ -161,24 +164,22 @@ export function isEOMethod(u: unknown): u is EOMethod {
161164 return isNode ( u , 'ObjectMethod' ) && isNode ( u . key , 'Identifier' ) ;
162165}
163166
164- export interface EOPropertyWithFunctionExpression extends EOProperty {
167+ export interface EOFunctionExpressionProp extends EOProp {
165168 value : FunctionExpression ;
166169}
167170
168- export function isEOPropertyWithFunctionExpression (
171+ export function isEOFunctionExpressionProp (
169172 u : unknown
170- ) : u is EOPropertyWithFunctionExpression {
171- return isEOProperty ( u ) && isNode ( u . value , 'FunctionExpression' ) ;
173+ ) : u is EOFunctionExpressionProp {
174+ return isEOProp ( u ) && isNode ( u . value , 'FunctionExpression' ) ;
172175}
173176
174- export interface EOPropertyWithCallExpression extends EOProperty {
177+ export interface EOCallExpressionProp extends EOProp {
175178 value : EOCallExpression ;
176179}
177180
178- export function isEOPropertyWithCallExpression (
179- u : unknown
180- ) : u is EOPropertyWithCallExpression {
181- return isEOProperty ( u ) && isEOCallExpression ( u . value ) ;
181+ export function isEOCallExpressionProp ( u : unknown ) : u is EOCallExpressionProp {
182+ return isEOProp ( u ) && isEOCallExpression ( u . value ) ;
182183}
183184
184185/** A CallExpression value for an EOProperty */
@@ -217,16 +218,14 @@ export function isEOCallExpressionInnerCallee(
217218}
218219
219220/** An EOProperty that should be transformed into a class decorator */
220- export interface EOPropertyForClassDecorator extends EOProperty {
221+ export interface EOClassDecoratorProp extends EOProp {
221222 value : EOClassDecoratorValue ;
222223 key : EOClassDecoratorKey ;
223224}
224225
225- export function isEOPropertyForClassDecorator (
226- u : unknown
227- ) : u is EOPropertyForClassDecorator {
226+ export function isEOClassDecoratorProp ( u : unknown ) : u is EOClassDecoratorProp {
228227 return (
229- isEOProperty ( u ) &&
228+ isEOProp ( u ) &&
230229 isEOClassDecoratorValue ( u . value ) &&
231230 isEOClassDecoratorKey ( u . key )
232231 ) ;
@@ -239,36 +238,21 @@ function isEOClassDecoratorValue(u: unknown): u is EOClassDecoratorValue {
239238}
240239
241240interface EOClassDecoratorKey extends Identifier {
242- name :
243- | LAYOUT_DECORATOR_NAME
244- | 'tagName'
245- | 'classNames'
246- | 'classNameBindings'
247- | 'attributeBindings' ;
248- }
249-
250- const ClassDecoratorPropNames = new Set ( [
251- LAYOUT_DECORATOR_NAME ,
252- 'tagName' ,
253- 'classNames' ,
254- 'classNameBindings' ,
255- 'attributeBindings' ,
256- ] ) ;
241+ name : ClassDecoratorName ;
242+ }
257243
258244function isEOClassDecoratorKey ( u : unknown ) : u is EOClassDecoratorKey {
259- return isIdent ( u , ...ClassDecoratorPropNames ) ;
245+ return isIdent ( u , ...CLASS_DECORATOR_NAMES ) ;
260246}
261247
262- export interface EOPropertyWithActionsObject extends EOProperty {
248+ export interface EOActionsProp extends EOProp {
263249 value : EOActionsObjectExpression ;
264250 key : EOActionsObjectKey ;
265251}
266252
267- export function isEOPropertyForActionsObject (
268- u : unknown
269- ) : u is EOPropertyWithActionsObject {
253+ export function isEOActionsProp ( u : unknown ) : u is EOActionsProp {
270254 return (
271- isEOProperty ( u ) &&
255+ isEOProp ( u ) &&
272256 isEOActionsObjectExpression ( u . value ) &&
273257 isEOActionsObjectKey ( u . key )
274258 ) ;
@@ -284,10 +268,10 @@ function isEOActionsObjectExpression(
284268 return isNode ( u , 'ObjectExpression' ) && u . properties . every ( isEOAction ) ;
285269}
286270
287- type EOAction = EOActionMethod | EOActionProperty ;
271+ type EOAction = EOActionMethod | EOActionProp ;
288272
289273function isEOAction ( u : unknown ) : u is EOAction {
290- return isEOActionMethod ( u ) || isEOActionProperty ( u ) ;
274+ return isEOActionMethod ( u ) || isEOActionProp ( u ) ;
291275}
292276
293277type EOActionMethod = EOMethod ;
@@ -296,25 +280,25 @@ export function isEOActionMethod(u: unknown): u is EOActionMethod {
296280 return isEOMethod ( u ) ;
297281}
298282
299- export interface EOActionProperty extends EOProperty {
283+ export interface EOActionProp extends EOProp {
300284 value : Identifier ;
301285}
302286
303- function isEOActionProperty ( u : unknown ) : u is EOActionProperty {
304- return isEOProperty ( u ) && isNode ( u . value , 'Identifier' ) ;
287+ function isEOActionProp ( u : unknown ) : u is EOActionProp {
288+ return isEOProp ( u ) && isNode ( u . value , 'Identifier' ) ;
305289}
306290
307291interface EOActionsObjectKey extends Identifier {
308- name : 'actions' ;
292+ name : ACTIONS_NAME ;
309293}
310294
311295function isEOActionsObjectKey ( u : unknown ) : u is EOActionsObjectKey {
312- return isIdent ( u , 'actions' ) ;
296+ return isIdent ( u , ACTIONS_NAME ) ;
313297}
314298
315- export interface EOPropertySimple extends EOProperty {
299+ export interface EOSimpleProp extends EOProp {
316300 value : Exclude <
317- EOProperty [ 'value' ] ,
301+ EOProp [ 'value' ] ,
318302 | ArrayPattern
319303 | AssignmentPattern
320304 | ObjectPattern
@@ -326,9 +310,9 @@ export interface EOPropertySimple extends EOProperty {
326310 > ;
327311}
328312
329- export function isEOPropertySimple ( u : unknown ) : u is EOPropertySimple {
313+ export function isEOSimpleProp ( u : unknown ) : u is EOSimpleProp {
330314 return (
331- isEOProperty ( u ) &&
315+ isEOProp ( u ) &&
332316 ! u . value . type . includes ( 'Pattern' ) &&
333317 u . value . type !== 'RestElement' &&
334318 u . value . type !== 'TSParameterProperty'
@@ -459,6 +443,11 @@ export function findPaths<T extends ASTNode, M extends T>(
459443/** Get the first path in a collection */
460444export function getFirstPath < T extends ASTNode > (
461445 collection : Collection < T >
462- ) : ASTPath < T > | undefined {
463- return collection . length > 0 ? ( collection . get ( ) as ASTPath < T > ) : undefined ;
446+ ) : Path < T > | undefined {
447+ return collection . length > 0 ? ( collection . get ( ) as Path < T > ) : undefined ;
448+ }
449+
450+ /** Get the first declaration in the program */
451+ export function getFirstDeclaration ( root : Collection ) : Collection < Declaration > {
452+ return root . find ( j . Declaration ) . at ( 0 ) as Collection < Declaration > ;
464453}
0 commit comments