Skip to content

Commit 34123b3

Browse files
committed
Clean up computed properties classes
1 parent 7915ddb commit 34123b3

5 files changed

Lines changed: 106 additions & 48 deletions

File tree

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
"lint:prettier:fix": "prettier --write .",
4444
"lint:ts": "tsc --noEmit",
4545
"test": "yarn build && codemod-cli test && node ./test/run-test.js && yarn clean",
46-
"fixme": "rm codemods.log; yarn build && codemod-cli test; yarn clean",
4746
"update-docs": "codemod-cli update-docs"
4847
},
4948
"dependencies": {

transforms/helpers/eo-prop/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import type { DecoratorImportInfoMap } from '../decorator-info';
33
import type { Options } from '../options';
44
import { assert } from '../util/types';
55
import EOActionsProp from './private/actions';
6-
import type EOComputedFunctionExpressionProp from './private/computed/function-expression';
6+
import EOClassDecorator from './private/class-decorator';
7+
import type EOComputedFunctionExpressionGetter from './private/computed/function-expression-getter';
8+
import type EOComputedFunctionExpressionMethod from './private/computed/function-expression-method';
79
import { makeEOComputedProp } from './private/computed/index';
810
import type EOComputedObjectExpressionProp from './private/computed/object-expression';
911
import type EOComputedProp from './private/computed/property';
10-
import EOClassDecorator from './private/class-decorator';
1112
import EOFunctionExpressionProp from './private/function-expression';
1213
import EOMethod from './private/method';
1314
import EOSimpleProp from './private/simple';
@@ -18,7 +19,8 @@ export type { default as EOClassDecorator } from './private/class-decorator';
1819
export type EOProp =
1920
| EOActionsProp
2021
| EOSimpleProp
21-
| EOComputedFunctionExpressionProp
22+
| EOComputedFunctionExpressionGetter
23+
| EOComputedFunctionExpressionMethod
2224
| EOComputedObjectExpressionProp
2325
| EOComputedProp
2426
| EOFunctionExpressionProp

transforms/helpers/eo-prop/private/computed/function-expression.ts renamed to transforms/helpers/eo-prop/private/computed/function-expression-getter.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { default as j } from 'jscodeshift';
22
import type * as AST from '../../../ast';
3-
import {
4-
replaceGetterSetterSuperExpressions,
5-
replaceMethodSuperExpressions,
6-
} from '../../../transform-helper';
7-
import { assert, defined } from '../../../util/types';
3+
import { replaceGetterSetterSuperExpressions } from '../../../transform-helper';
4+
import { assert } from '../../../util/types';
85
import AbstractEOComputedProp from './abstract';
96

107
/**
@@ -64,17 +61,17 @@ import AbstractEOComputedProp from './abstract';
6461
* Notably, if the modifiers `volatile` and `readOnly` are used in conjunction,
6562
* a non-computed getter will be returned.
6663
*/
67-
export default class EOComputedFunctionExpressionProp extends AbstractEOComputedProp<AST.ClassMethod> {
64+
export default class EOComputedFunctionExpressionGetter extends AbstractEOComputedProp<AST.ClassMethod> {
6865
build(): AST.ClassMethod {
6966
const args = this.arguments;
7067
const lastArg = args[args.length - 1];
7168
assert(
7269
lastArg && lastArg.type === 'FunctionExpression',
7370
'expected lastArg to be a FunctionExpression'
7471
);
75-
return this.replaceSuperExpression(
72+
return replaceGetterSetterSuperExpressions(
7673
j.classMethod.from({
77-
kind: defined(this.kind),
74+
kind: 'get',
7875
key: this.key,
7976
params: lastArg.params,
8077
body: lastArg.body,
@@ -85,14 +82,4 @@ export default class EOComputedFunctionExpressionProp extends AbstractEOComputed
8582
this.key
8683
);
8784
}
88-
89-
private get replaceSuperExpression(): (
90-
classMethod: AST.ClassMethod,
91-
replaceWithUndefined: boolean,
92-
identifier: AST.Identifier
93-
) => AST.ClassMethod {
94-
return this.kind === 'method'
95-
? replaceMethodSuperExpressions
96-
: replaceGetterSetterSuperExpressions;
97-
}
9885
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { default as j } from 'jscodeshift';
2+
import type * as AST from '../../../ast';
3+
import { replaceMethodSuperExpressions } from '../../../transform-helper';
4+
import { assert } from '../../../util/types';
5+
import AbstractEOComputedProp from './abstract';
6+
7+
/**
8+
* Ember Object Computed Function Expression Property
9+
*
10+
* A wrapper object for Ember Object properties where the value is a
11+
* `CallExpression` with a `FunctionExpression` as its last argument.
12+
*
13+
* These represent computed properties (including computed macros) to be
14+
* transformed into `ClassMethod`s with their appropriate decorators (and any
15+
* modifiers).
16+
*
17+
* @example
18+
*
19+
* ```
20+
* import { observer as watcher } from '@ember/object';
21+
*
22+
* const MyObject = EmberObject.extend({
23+
* observedProp: watcher('xyz', function() {
24+
* return 'observed';
25+
* }),
26+
* });
27+
* ```
28+
*
29+
* transforms into:
30+
*
31+
* ```
32+
* import { observes as watcher } from '@ember-decorators/object';
33+
*
34+
* class MyObject extends EmberObject {
35+
* @watcher('xyz')
36+
* observedProp() {
37+
* return 'observed';
38+
* }
39+
* }
40+
* ```
41+
*
42+
* Notably, if the modifiers `volatile` and `readOnly` are used in conjunction,
43+
* a non-computed getter will be returned.
44+
*/
45+
export default class EOComputedFunctionExpressionMethod extends AbstractEOComputedProp<AST.ClassMethod> {
46+
build(): AST.ClassMethod {
47+
const args = this.arguments;
48+
const lastArg = args[args.length - 1];
49+
assert(
50+
lastArg && lastArg.type === 'FunctionExpression',
51+
'expected lastArg to be a FunctionExpression'
52+
);
53+
return replaceMethodSuperExpressions(
54+
j.classMethod.from({
55+
kind: 'method',
56+
key: this.key,
57+
params: lastArg.params,
58+
body: lastArg.body,
59+
comments: this.comments,
60+
decorators: this.buildDecorators(),
61+
}),
62+
this.replaceSuperWithUndefined
63+
);
64+
}
65+
}

transforms/helpers/eo-prop/private/computed/index.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import type {
55
} from '../../../decorator-info';
66
import type { Options } from '../../../options';
77
import { assert } from '../../../util/types';
8-
import EOComputedFunctionExpressionProp from './function-expression';
8+
import EOComputedFunctionExpressionGetter from './function-expression-getter';
9+
import EOComputedFunctionExpressionMethod from './function-expression-method';
910
import { getModifier } from './modifier-helper';
1011
import EOComputedObjectExpressionProp from './object-expression';
1112
import EOComputedProp from './property';
@@ -18,7 +19,8 @@ export function makeEOComputedProp(
1819
existingDecoratorImportInfos: DecoratorImportInfoMap,
1920
options: Options
2021
):
21-
| EOComputedFunctionExpressionProp
22+
| EOComputedFunctionExpressionGetter
23+
| EOComputedFunctionExpressionMethod
2224
| EOComputedObjectExpressionProp
2325
| EOComputedProp {
2426
let calleeObject = raw.value;
@@ -48,30 +50,33 @@ export function makeEOComputedProp(
4850

4951
const args = calleeObject.arguments;
5052
const lastArg = args[args.length - 1];
51-
if ((kind === 'method' || kind === 'get') && lastArg) {
52-
if (lastArg.type === 'FunctionExpression') {
53-
return new EOComputedFunctionExpressionProp(
54-
raw,
55-
calleeObject,
56-
modifiers,
57-
kind,
58-
decorators,
59-
options
60-
);
61-
} else if (lastArg.type === 'ObjectExpression') {
62-
return new EOComputedObjectExpressionProp(
63-
raw,
64-
calleeObject,
65-
modifiers,
66-
kind,
67-
decorators,
68-
options
69-
);
70-
} else {
71-
throw new Error(
72-
'Expected last argument in call expression to be a FunctionExpression or ObjectExpression'
73-
);
74-
}
53+
if (kind === 'method' && lastArg?.type === 'FunctionExpression') {
54+
return new EOComputedFunctionExpressionMethod(
55+
raw,
56+
calleeObject,
57+
modifiers,
58+
kind,
59+
decorators,
60+
options
61+
);
62+
} else if (kind === 'get' && lastArg?.type === 'FunctionExpression') {
63+
return new EOComputedFunctionExpressionGetter(
64+
raw,
65+
calleeObject,
66+
modifiers,
67+
kind,
68+
decorators,
69+
options
70+
);
71+
} else if (kind === 'get' && lastArg?.type === 'ObjectExpression') {
72+
return new EOComputedObjectExpressionProp(
73+
raw,
74+
calleeObject,
75+
modifiers,
76+
kind,
77+
decorators,
78+
options
79+
);
7580
}
7681

7782
return new EOComputedProp(

0 commit comments

Comments
 (0)