Skip to content

Commit b0fcb3b

Browse files
committed
Clean up AST types
1 parent fcec15d commit b0fcb3b

26 files changed

Lines changed: 325 additions & 362 deletions

transforms/ember-object/__testfixtures__/decorators.output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import classic from 'ember-classic-decorator';
22

33
import {
4-
classNames,
54
attributeBindings,
65
classNameBindings,
7-
tagName,
6+
classNames,
87
layout as templateLayout,
8+
tagName,
99
} from '@ember-decorators/component';
1010

1111
import { observes as watcher, on } from '@ember-decorators/object';

transforms/ember-object/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Transform } from 'jscodeshift';
22
import path from 'path';
3-
import type { Collection } from '../helpers/ast';
3+
import type * as AST from '../helpers/ast';
44
import getConfig from '../helpers/config';
55
import maybeTransformEmberObjects from '../helpers/transform';
66

@@ -14,7 +14,7 @@ const transformer: Transform = function (
1414
return;
1515
}
1616

17-
const root = j(source) as Collection;
17+
const root = j(source) as AST.Collection;
1818
const userOptions = getConfig();
1919
const replaced = maybeTransformEmberObjects(j, root, filePath, userOptions);
2020

transforms/helpers/ast.ts

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
3134
import { isRecord } from './util/types';
3235

3336
export 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

5760
export 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

136139
function isEOExpressionProp(u: unknown): u is EOExpressionProp {
137-
return isEOProperty(u) || isEOMethod(u);
140+
return isEOProp(u) || isEOMethod(u);
138141
}
139142

140143
export 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

241240
interface 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

258244
function 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

289273
function isEOAction(u: unknown): u is EOAction {
290-
return isEOActionMethod(u) || isEOActionProperty(u);
274+
return isEOActionMethod(u) || isEOActionProp(u);
291275
}
292276

293277
type 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

307291
interface EOActionsObjectKey extends Identifier {
308-
name: 'actions';
292+
name: ACTIONS_NAME;
309293
}
310294

311295
function 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 */
460444
export 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
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { JSCodeshift } from 'jscodeshift';
22
import { default as j } from 'jscodeshift';
3-
import type { Decorator } from './ast';
3+
import type * as AST from '../helpers/ast';
44

55
type CallExpressionArg = Parameters<JSCodeshift['callExpression']>[1][number];
66

77
/** Creates a decorator for a class. */
88
export function createClassDecorator(
99
decoratorName: string,
1010
value: CallExpressionArg
11-
): Decorator {
11+
): AST.Decorator {
1212
const args = value.type === 'ArrayExpression' ? value.elements : [value];
1313
return j.decorator(
1414
j.callExpression(j.identifier(decoratorName), args as CallExpressionArg[])
@@ -19,7 +19,7 @@ export function createClassDecorator(
1919
export function createDecoratorWithArgs(
2020
decoratorName: string,
2121
args: Array<string | number | boolean | RegExp | null>
22-
): Decorator {
22+
): AST.Decorator {
2323
return j.decorator(
2424
j.callExpression(
2525
j.identifier(decoratorName),
@@ -28,14 +28,11 @@ export function createDecoratorWithArgs(
2828
);
2929
}
3030

31-
/** Create `@action` decorator */
32-
export function buildActionDecorator(): [Decorator] {
33-
return [createIdentifierDecorator('action')];
34-
}
35-
3631
/**
3732
* Create simple decorator with given name
3833
*/
39-
export function createIdentifierDecorator(decoratorName: string): Decorator {
34+
export function createIdentifierDecorator(
35+
decoratorName: string
36+
): AST.Decorator {
4037
return j.decorator(j.identifier(decoratorName));
4138
}

transforms/helpers/decorator-info.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { assert } from './util/types';
22
import { METHOD_DECORATORS } from './util/index';
3-
import type { ImportSpecifier } from './ast';
3+
import type * as AST from '../helpers/ast';
44

55
export interface DecoratorImportInfo {
66
name: string;
7-
importedName?: 'computed' | string;
7+
importedName?: string;
88
isImportedAs?: boolean;
99
isMetaDecorator?: boolean;
1010
isMethodDecorator?: boolean;
@@ -22,7 +22,7 @@ export type DecoratorImportInfoMap = Map<
2222
* `DECORATOR_PATHS` config (defined util.js)
2323
*/
2424
export function getDecoratorImportInfo(
25-
specifier: ImportSpecifier,
25+
specifier: AST.ImportSpecifier,
2626
importPropDecoratorMap: Record<string, string> | undefined
2727
): DecoratorImportInfo {
2828
const localName = specifier.local?.name;

0 commit comments

Comments
 (0)