Skip to content

Commit d100e7c

Browse files
committed
Clean up AST types
1 parent f9b4b79 commit d100e7c

19 files changed

Lines changed: 650 additions & 395 deletions

transforms/helpers/ast.ts

Lines changed: 439 additions & 0 deletions
Large diffs are not rendered by default.

transforms/helpers/decorator-helper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import type { Decorator, JSCodeshift } from 'jscodeshift';
1+
import type { JSCodeshift } from 'jscodeshift';
2+
import type { Decorator } from './ast';
3+
import type { EOClassDecoratorProp, EOSimpleProp } from './eo-prop/index';
24
import { EOCallExpressionProp } from './eo-prop/index';
3-
import type { EOBaseProp, EOClassDecoratorProp } from './eo-prop/index';
45
import { assert, defined } from './util/types';
56

67
/** Copy decorators `from` => `to` */
@@ -110,7 +111,7 @@ function createBindingDecorators(
110111
/** Handles decorators for instance properties */
111112
export function createInstancePropDecorators(
112113
j: JSCodeshift,
113-
instanceProp: EOCallExpressionProp | EOBaseProp
114+
instanceProp: EOCallExpressionProp | EOSimpleProp
114115
): Decorator[] {
115116
let decorators: Decorator[] = [];
116117
for (const decorator of instanceProp.decoratorNames) {

transforms/helpers/decorator-info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { ImportSpecifier } from 'jscodeshift';
21
import { assert } from './util/types';
32
import { METHOD_DECORATORS } from './util/index';
3+
import type { ImportSpecifier } from './ast';
44

55
export interface DecoratorImportInfo {
66
name: 'unobserves' | 'off' | 'className' | 'attribute' | string;
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
import type { Property } from 'jscodeshift';
1+
import type { EOProperty } from '../ast';
2+
import {
3+
isEOPropertyForActionsObject,
4+
isEOPropertyForClassDecorator,
5+
isEOPropertySimple,
6+
isEOPropertyWithCallExpression,
7+
isEOPropertyWithFunctionExpression,
8+
} from '../ast';
29
import type { DecoratorImportInfoMap } from '../decorator-info';
310
import type { RuntimeData } from '../runtime-data';
411
import { assert } from '../util/types';
5-
import EOActionsObjectProp, {
6-
isEOActionsPropProperty,
7-
} from './private/actions-object';
8-
import EOBaseProp, { isEOProperty } from './private/base';
9-
import EOCallExpressionProp, {
10-
isCallExpressionProperty,
11-
} from './private/call-expression';
12-
import EOClassDecoratorProp, {
13-
isClassDecoratorProperty,
14-
} from './private/class-decorator';
15-
import EOFunctionExpressionProp, {
16-
isFunctionExpressionProperty,
17-
} from './private/function-expression';
12+
import EOActionsObjectProp from './private/actions-object';
13+
import EOCallExpressionProp from './private/call-expression';
14+
import EOClassDecoratorProp from './private/class-decorator';
15+
import EOFunctionExpressionProp from './private/function-expression';
16+
import EOSimpleProp from './private/simple';
1817

1918
export { default as EOActionsObjectProp } from './private/actions-object';
20-
export type { Action } from './private/actions-object';
21-
export { default as EOBaseProp } from './private/base';
2219
export { default as EOCallExpressionProp } from './private/call-expression';
2320
export { default as EOClassDecoratorProp } from './private/class-decorator';
2421
export { default as EOFunctionExpressionProp } from './private/function-expression';
22+
export { default as EOSimpleProp } from './private/simple';
2523

2624
export type EOProp =
2725
| EOActionsObjectProp
28-
| EOBaseProp
26+
| EOSimpleProp
2927
| EOCallExpressionProp
3028
| EOClassDecoratorProp
3129
| EOFunctionExpressionProp;
@@ -39,24 +37,24 @@ export interface EOProps {
3937
* Property, RuntimeData, and ImportPropDecoratorMap.
4038
*/
4139
export default function makeEOProp(
42-
eoProp: Property,
40+
eoProp: EOProperty,
4341
runtimeData: RuntimeData | undefined,
4442
existingDecoratorImportInfos: DecoratorImportInfoMap
4543
): EOProp {
46-
if (isCallExpressionProperty(eoProp)) {
44+
if (isEOPropertyWithCallExpression(eoProp)) {
4745
return new EOCallExpressionProp(
4846
eoProp,
4947
runtimeData,
5048
existingDecoratorImportInfos
5149
);
52-
} else if (isFunctionExpressionProperty(eoProp)) {
50+
} else if (isEOPropertyWithFunctionExpression(eoProp)) {
5351
return new EOFunctionExpressionProp(eoProp, runtimeData);
54-
} else if (isClassDecoratorProperty(eoProp)) {
52+
} else if (isEOPropertyForClassDecorator(eoProp)) {
5553
return new EOClassDecoratorProp(eoProp, runtimeData);
56-
} else if (isEOActionsPropProperty(eoProp)) {
54+
} else if (isEOPropertyForActionsObject(eoProp)) {
5755
return new EOActionsObjectProp(eoProp, runtimeData);
5856
} else {
59-
assert(isEOProperty(eoProp));
60-
return new EOBaseProp(eoProp, runtimeData);
57+
assert(isEOPropertySimple(eoProp));
58+
return new EOSimpleProp(eoProp, runtimeData);
6159
}
6260
}

transforms/helpers/eo-prop/private/abstract.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { Property } from 'jscodeshift';
1+
import type { EOProperty } from '../../ast';
22
import type { DecoratorImportInfo } from '../../decorator-info';
33
import type { RuntimeData } from '../../runtime-data';
4-
import { getPropName } from '../../util/index';
54

65
interface EODecoratorArgs {
76
unobserves?: Array<string | boolean | number | null> | undefined;
@@ -14,8 +13,7 @@ interface EODecoratorArgs {
1413
* A wrapper object for ember object properties
1514
*/
1615
export default abstract class AbstractEOProp<
17-
V extends Property['value'] = Property['value'],
18-
P extends Property & { value: V } = Property & { value: V }
16+
P extends EOProperty = EOProperty
1917
> {
2018
readonly _prop: P;
2119

@@ -56,7 +54,7 @@ export default abstract class AbstractEOProp<
5654
}
5755
}
5856

59-
get value(): V {
57+
get value(): P['value'] {
6058
return this._prop.value;
6159
}
6260

@@ -65,10 +63,10 @@ export default abstract class AbstractEOProp<
6563
}
6664

6765
get name(): string {
68-
return getPropName(this._prop);
66+
return this._prop.key.name;
6967
}
7068

71-
get type(): V['type'] {
69+
get type(): P['value']['type'] {
7270
return this._prop.value.type;
7371
}
7472

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,12 @@
1-
import type { Identifier, ObjectExpression, Property } from 'jscodeshift';
1+
import type { EOAction, EOPropertyWithActionsObject } from '../../ast';
22
import type { RuntimeData } from '../../runtime-data';
33
import AbstractEOProp from './abstract';
44

5-
export type Action = Property & {
6-
key: Identifier;
7-
};
8-
9-
type ActionsObjectExpression = ObjectExpression & {
10-
properties: Action[];
11-
};
12-
13-
export type ActionsObjectProperty = Property & {
14-
value: ActionsObjectExpression;
15-
key: Identifier & { name: 'actions' };
16-
};
17-
18-
/** Type predicate */
19-
export function isEOActionsPropProperty(
20-
property: Property
21-
): property is ActionsObjectProperty {
22-
return (
23-
property.value.type === 'ObjectExpression' &&
24-
'name' in property.key &&
25-
property.key.name === 'actions' &&
26-
property.value.properties.every(
27-
(p) => p.type === 'Property' && p.key.type === 'Identifier'
28-
)
29-
);
30-
}
31-
32-
export default class EOActionsObjectProp extends AbstractEOProp<
33-
ActionsObjectExpression,
34-
ActionsObjectProperty
35-
> {
5+
export default class EOActionsObjectProp extends AbstractEOProp<EOPropertyWithActionsObject> {
366
readonly overriddenActions: string[] = [];
377

388
constructor(
39-
eoProp: ActionsObjectProperty,
9+
eoProp: EOPropertyWithActionsObject,
4010
runtimeData: RuntimeData | undefined
4111
) {
4212
super(eoProp, runtimeData);
@@ -45,7 +15,7 @@ export default class EOActionsObjectProp extends AbstractEOProp<
4515
}
4616
}
4717

48-
get properties(): Action[] {
18+
get properties(): EOAction[] {
4919
return this._prop.value.properties;
5020
}
5121
}

transforms/helpers/eo-prop/private/base.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

transforms/helpers/eo-prop/private/call-expression.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
1-
import type { CallExpression, Identifier, Property } from 'jscodeshift';
1+
import type {
2+
CallExpression,
3+
EOCallExpression,
4+
EOPropertyWithCallExpression,
5+
} from '../../ast';
26
import type { DecoratorImportInfoMap } from '../../decorator-info';
37
import type { RuntimeData } from '../../runtime-data';
48
import { assert, isString, verified } from '../../util/types';
59
import AbstractEOProp from './abstract';
610

7-
type CallExpressionProperty = Property & {
8-
value: CallExpression;
9-
key: Identifier;
10-
};
11-
12-
/** Type predicate */
13-
export function isCallExpressionProperty(
14-
property: Property
15-
): property is CallExpressionProperty {
16-
return property.value.type === 'CallExpression';
17-
}
18-
1911
interface CallExpressionModifier {
2012
prop:
21-
| Extract<CallExpression['callee'], { property: unknown }>['property']
13+
| Extract<EOCallExpression['callee'], { property: unknown }>['property']
2214
| undefined;
23-
args: CallExpression['arguments'];
15+
args: EOCallExpression['arguments'];
2416
}
2517

2618
/**
2719
* Get property modifier from the property callee object
2820
*/
29-
function getModifier(calleeObject: CallExpression): CallExpressionModifier {
21+
function getModifier(calleeObject: EOCallExpression): CallExpressionModifier {
3022
return {
3123
prop:
3224
'property' in calleeObject.callee
@@ -36,15 +28,12 @@ function getModifier(calleeObject: CallExpression): CallExpressionModifier {
3628
};
3729
}
3830

39-
export default class EOCallExpressionProp extends AbstractEOProp<
40-
CallExpression,
41-
CallExpressionProperty
42-
> {
31+
export default class EOCallExpressionProp extends AbstractEOProp<EOPropertyWithCallExpression> {
4332
private calleeObject: CallExpression;
4433
readonly modifiers: CallExpressionModifier[];
4534

4635
constructor(
47-
eoProp: CallExpressionProperty,
36+
eoProp: EOPropertyWithCallExpression,
4837
runtimeData: RuntimeData | undefined,
4938
existingDecoratorImportInfos: DecoratorImportInfoMap
5039
) {

transforms/helpers/eo-prop/private/class-decorator.ts

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,11 @@
1-
import type {
2-
ArrayExpression,
3-
Identifier,
4-
Literal,
5-
Property,
6-
} from 'jscodeshift';
1+
import type { EOPropertyForClassDecorator } from '../../ast';
72
import {
83
LAYOUT_DECORATOR_LOCAL_NAME,
94
LAYOUT_DECORATOR_NAME,
105
} from '../../util/index';
116
import AbstractEOProp from './abstract';
127

13-
type ClassDecoratorPropertyValue = (Literal | ArrayExpression | Identifier) & {
14-
name: string;
15-
};
16-
17-
export type ClassDecoratorProperty = Property & {
18-
value: ClassDecoratorPropertyValue;
19-
};
20-
21-
const ClassDecoratorPropNames = new Set([
22-
'layout',
23-
'tagName',
24-
'classNames',
25-
'classNameBindings',
26-
'attributeBindings',
27-
]);
28-
29-
/** Type predicate */
30-
export function isClassDecoratorProperty(
31-
property: Property
32-
): property is ClassDecoratorProperty {
33-
return (
34-
(property.value.type === 'Literal' ||
35-
property.value.type === 'ArrayExpression' ||
36-
property.value.type === 'Identifier') &&
37-
'name' in property.key &&
38-
typeof property.key.name === 'string' &&
39-
ClassDecoratorPropNames.has(property.key.name)
40-
);
41-
}
42-
43-
export default class EOClassDecoratorProp extends AbstractEOProp<ClassDecoratorPropertyValue> {
8+
export default class EOClassDecoratorProp extends AbstractEOProp<EOPropertyForClassDecorator> {
449
get classDecoratorName(): string {
4510
if (
4611
this.name === LAYOUT_DECORATOR_NAME &&
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
1-
import type { FunctionExpression, Identifier, Property } from 'jscodeshift';
1+
import type { EOPropertyWithFunctionExpression } from '../../ast';
22
import AbstractEOProp from './abstract';
33

4-
export type FunctionExpressionProperty = Property & {
5-
value: FunctionExpression;
6-
key: Identifier;
7-
};
8-
9-
/** Type predicate */
10-
export function isFunctionExpressionProperty(
11-
property: Property
12-
): property is FunctionExpressionProperty {
13-
return property.value.type === 'FunctionExpression';
14-
}
15-
16-
export default class EOFunctionExpressionProp extends AbstractEOProp<
17-
FunctionExpression,
18-
FunctionExpressionProperty
19-
> {}
4+
export default class EOFunctionExpressionProp extends AbstractEOProp<EOPropertyWithFunctionExpression> {}

0 commit comments

Comments
 (0)