Skip to content

Commit 552806a

Browse files
committed
Improve documentation
1 parent 427391e commit 552806a

21 files changed

Lines changed: 454 additions & 195 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Foo1 extends EmberObject {
7878
// class's actions to be safe. This should be refactored to call a normal method
7979
// on the parent class. If the parent class has not been converted to native
8080
// classes, it may need to be refactored as well. See
81-
// https: //github.com/scalvert/ember-native-class-codemod/blob/master/README.md
81+
// https://github.com/scalvert/ember-native-class-codemod/blob/master/README.md
8282
// for more details.
8383
super.actions.baz.call(this, ...arguments);
8484
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default class _Runtime extends Runtime.extend(MyMixin) {
8585
// class's actions to be safe. This should be refactored to call a normal method
8686
// on the parent class. If the parent class has not been converted to native
8787
// classes, it may need to be refactored as well. See
88-
// https: //github.com/scalvert/ember-native-class-codemod/blob/master/README.md
88+
// https://github.com/scalvert/ember-native-class-codemod/blob/master/README.md
8989
// for more details.
9090
super.actions.overriddenActionMethod.call(this, ...arguments) && this.boolProp;
9191
}

transforms/helpers/ast.ts

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ function isIdent(u: unknown, ...names: string[]): u is Identifier {
7474
return isNode(u, 'Identifier') && names.includes(u.name);
7575
}
7676

77+
/**
78+
* A CallExpression following the `EmberObject.extend(Mixin, {})` pattern.
79+
*/
7780
export interface EOExtendExpression extends CallExpression {
7881
callee: EOExtendExpressionCallee;
7982
arguments: EOExtendArg[];
@@ -124,6 +127,10 @@ function isEOExtendExpressionCalleeProperty(
124127
return isIdent(u, 'extend');
125128
}
126129

130+
/**
131+
* The ObjectExpression argument from the `EmberObject.extend(Mixin, {})`
132+
* pattern.
133+
*/
127134
export interface EOExpression extends ObjectExpression {
128135
properties: EOExpressionProp[];
129136
}
@@ -146,7 +153,7 @@ function isEOMixin(u: unknown): u is EOMixin {
146153
return isNode(u, 'Identifier');
147154
}
148155

149-
/** A top-level instance property in an Ember Object's ObjectExpression */
156+
/** A top-level instance property in an EOExpression */
150157
export interface EOProp extends ObjectProperty {
151158
key: Identifier;
152159
}
@@ -155,7 +162,7 @@ function isEOProp(u: unknown): u is EOProp {
155162
return isNode(u, 'ObjectProperty') && isNode(u.key, 'Identifier');
156163
}
157164

158-
/** A top-level instance property in an Ember Object's ObjectExpression */
165+
/** A top-level instance method in an EOExpression */
159166
export interface EOMethod extends ObjectMethod {
160167
key: Identifier;
161168
}
@@ -164,6 +171,10 @@ export function isEOMethod(u: unknown): u is EOMethod {
164171
return isNode(u, 'ObjectMethod') && isNode(u.key, 'Identifier');
165172
}
166173

174+
/**
175+
* A top-level instance property in an EOExpression where the value is a
176+
* `FunctionExpression`.
177+
*/
167178
export interface EOFunctionExpressionProp extends EOProp {
168179
value: FunctionExpression;
169180
}
@@ -174,15 +185,22 @@ export function isEOFunctionExpressionProp(
174185
return isEOProp(u) && isNode(u.value, 'FunctionExpression');
175186
}
176187

177-
export interface EOCallExpressionProp extends EOProp {
188+
/**
189+
* A top-level instance property in an EOExpression where the value is a
190+
* `CallExpression`. These represent computed properties.
191+
*/
192+
export interface EOComputedProp extends EOProp {
178193
value: EOCallExpression;
179194
}
180195

181-
export function isEOCallExpressionProp(u: unknown): u is EOCallExpressionProp {
196+
export function isEOCallExpressionProp(u: unknown): u is EOComputedProp {
182197
return isEOProp(u) && isEOCallExpression(u.value);
183198
}
184199

185-
/** A CallExpression value for an EOProperty */
200+
/**
201+
* A top-level instance property in an EOExpression where the value is a
202+
* `CallExpression`.
203+
*/
186204
export interface EOCallExpression extends CallExpression {
187205
callee: EOCallExpressionCallee;
188206
}
@@ -217,7 +235,10 @@ export function isEOCallExpressionInnerCallee(
217235
return isNode(u, 'CallExpression') && isNode(u.callee, 'Identifier');
218236
}
219237

220-
/** An EOProperty that should be transformed into a class decorator */
238+
/**
239+
* A top-level instance property in an EOExpression that should be transformed
240+
* into a class decorator.
241+
*/
221242
export interface EOClassDecoratorProp extends EOProp {
222243
value: EOClassDecoratorValue;
223244
key: EOClassDecoratorKey;
@@ -245,6 +266,10 @@ function isEOClassDecoratorKey(u: unknown): u is EOClassDecoratorKey {
245266
return isIdent(u, ...CLASS_DECORATOR_NAMES);
246267
}
247268

269+
/**
270+
* A top-level instance property in an EOExpression representing the `actions`
271+
* object.
272+
*/
248273
export interface EOActionsProp extends EOProp {
249274
value: EOActionsObjectExpression;
250275
key: EOActionsObjectKey;
@@ -274,12 +299,19 @@ function isEOAction(u: unknown): u is EOAction {
274299
return isEOActionMethod(u) || isEOActionProp(u);
275300
}
276301

302+
/**
303+
* An instance property in an EOActionsProp representing a method-style action.
304+
*/
277305
type EOActionMethod = EOMethod;
278306

279307
export function isEOActionMethod(u: unknown): u is EOActionMethod {
280308
return isEOMethod(u);
281309
}
282310

311+
/**
312+
* An instance property in an EOActionsProp representing an identifier-style
313+
* action.
314+
*/
283315
export interface EOActionProp extends EOProp {
284316
value: Identifier;
285317
}
@@ -296,6 +328,10 @@ function isEOActionsObjectKey(u: unknown): u is EOActionsObjectKey {
296328
return isIdent(u, ACTIONS_NAME);
297329
}
298330

331+
/**
332+
* A top-level instance property in an EOExpression representing a simple
333+
* Property (typically a primitive).
334+
*/
299335
export interface EOSimpleProp extends EOProp {
300336
value: Exclude<
301337
EOProp['value'],
@@ -323,7 +359,11 @@ interface EOActionInfiniteCall extends CallExpression {
323359
callee: EOActionInfiniteCallCallee;
324360
}
325361

326-
export function makeEOActionInfiniteCallAssertion(
362+
/**
363+
* Makes a type predicate to check for an action that would call itself once
364+
* transformed, resulting in an infinite loop.
365+
*/
366+
export function makeEOActionInfiniteCallPredicate(
327367
name: string
328368
): (u: unknown) => u is EOActionInfiniteCall {
329369
return function isEOActionInfiniteCallForName(
@@ -363,7 +403,11 @@ interface EOActionInfiniteLiteral extends StringLiteral {
363403
value: string;
364404
}
365405

366-
export function makeEOActionInfiniteLiteralAssertion(
406+
/**
407+
* Makes a type predicate to check for an action that would call itself once
408+
* transformed, resulting in an infinite loop.
409+
*/
410+
export function makeEOActionInfiniteLiteralPredicate(
367411
name: string
368412
): (u: unknown) => u is EOActionInfiniteLiteral {
369413
return function isEOActionInfiniteLiteralForName(
@@ -400,7 +444,10 @@ export interface DecoratorImportDeclaration extends ImportDeclaration {
400444
source: StringLiteral;
401445
}
402446

403-
export function makeDecoratorImportDeclarationAssertion(
447+
/**
448+
* Makes a type predicate to find an import declaration with the given name.
449+
*/
450+
export function makeDecoratorImportDeclarationPredicate(
404451
path: string
405452
): (u: unknown) => u is DecoratorImportDeclaration {
406453
return function isDecoratorImportDeclarationForPath(

transforms/helpers/decorator-helper.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function createClassDecorator(
1515
);
1616
}
1717

18-
/** Create decorators which need arguments */
18+
/** Create decorators which need arguments. */
1919
export function createDecoratorWithArgs(
2020
decoratorName: string,
2121
args: Array<string | number | boolean | RegExp | null>
@@ -28,9 +28,7 @@ export function createDecoratorWithArgs(
2828
);
2929
}
3030

31-
/**
32-
* Create simple decorator with given name
33-
*/
31+
/** Create simple decorator with given name. */
3432
export function createIdentifierDecorator(
3533
decoratorName: string
3634
): AST.Decorator {

transforms/helpers/decorator-info.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { assert } from './util/types';
2-
import { METHOD_DECORATORS } from './util/index';
2+
import { COMPUTED_DECORATOR_NAME, METHOD_DECORATORS } from './util/index';
33
import type * as AST from '../helpers/ast';
44

55
export interface DecoratorImportInfo {
66
name: string;
77
importedName?: string;
88
isImportedAs?: boolean;
9+
isComputedDecorator?: boolean;
910
isMetaDecorator?: boolean;
1011
isMethodDecorator?: boolean;
1112
localName?: string;
@@ -40,10 +41,12 @@ export function getDecoratorImportInfo(
4041
}
4142

4243
const isMethodDecorator = METHOD_DECORATORS.has(importedName);
44+
const isComputedDecorator = COMPUTED_DECORATOR_NAME === importedName;
4345
return {
4446
name,
4547
importedName,
4648
isImportedAs,
49+
isComputedDecorator,
4750
isMetaDecorator,
4851
isMethodDecorator,
4952
localName,

transforms/helpers/eo-prop/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ 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/call-expression/function-expression';
7-
import { makeEOCallExpressionProp } from './private/call-expression/index';
8-
import type EOComputedObjectExpressionProp from './private/call-expression/object-expression';
9-
import type EODecoratedProp from './private/call-expression/property';
6+
import type EOComputedFunctionExpressionProp from './private/computed/function-expression';
7+
import { makeEOComputedProp } from './private/computed/index';
8+
import type EOComputedObjectExpressionProp from './private/computed/object-expression';
9+
import type EOComputedProp from './private/computed/property';
1010
import EOClassDecorator from './private/class-decorator';
1111
import EOFunctionExpressionProp from './private/function-expression';
1212
import EOMethod from './private/method';
@@ -20,25 +20,20 @@ export type EOProp =
2020
| EOSimpleProp
2121
| EOComputedFunctionExpressionProp
2222
| EOComputedObjectExpressionProp
23-
| EODecoratedProp
23+
| EOComputedProp
2424
| EOFunctionExpressionProp
2525
| EOMethod;
2626

2727
/**
28-
* Makes an object representing an Ember Object property for the given
29-
* Property, RuntimeData, and ImportPropDecoratorMap.
28+
* Makes an object representing an Ember Object property.
3029
*/
3130
export default function makeEOProp(
3231
eoProp: AST.EOExpressionProp,
3332
existingDecoratorImportInfos: DecoratorImportInfoMap,
3433
options: Options
3534
): EOProp | EOClassDecorator {
3635
if (AST.isEOCallExpressionProp(eoProp)) {
37-
return makeEOCallExpressionProp(
38-
eoProp,
39-
existingDecoratorImportInfos,
40-
options
41-
);
36+
return makeEOComputedProp(eoProp, existingDecoratorImportInfos, options);
4237
} else if (AST.isEOMethod(eoProp)) {
4338
return new EOMethod(eoProp, options);
4439
} else if (AST.isEOFunctionExpressionProp(eoProp)) {

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,44 @@ export interface Action {
99
hasInfiniteLoop: boolean;
1010
}
1111

12+
/**
13+
* Ember Object Actions Property
14+
*
15+
* A wrapper object for Ember Object `actions` object properties to be
16+
* transformed into a series of `ClassMethod`s with the `@action` decorator.
17+
*
18+
* Each action on the object is represented either by an `EOActionMethod` or
19+
* `EOActionProp`.
20+
*
21+
* @example
22+
*
23+
* ```
24+
* import someActionUtil from 'some/action/util';
25+
*
26+
* const MyObject = EmberObject.extend({
27+
* actions: {
28+
* someActionUtil,
29+
* bar() {},
30+
* }
31+
* });
32+
* ```
33+
*
34+
* transforms into:
35+
*
36+
* ```
37+
* import someActionUtil from 'some/action/util';
38+
*
39+
* class MyObject extends EmberObject {
40+
* @action
41+
* someActionUtil() {
42+
* return someActionUtil.call(this, ...arguments);
43+
* }
44+
*
45+
* @action
46+
* bar() {}
47+
* }
48+
* ```
49+
*/
1250
export default class EOActionsProp extends AbstractEOProp<
1351
AST.EOActionsProp,
1452
AST.ClassMethod[]
@@ -24,26 +62,6 @@ export default class EOActionsProp extends AbstractEOProp<
2462
};
2563
}
2664

27-
/**
28-
* FIXME: Verify docs
29-
*
30-
* Create action decorators
31-
* ```
32-
* Converts
33-
* {
34-
* actions: {
35-
* foo() {}
36-
* }
37-
* }
38-
* ```
39-
* to
40-
* ```
41-
* {
42-
* @action
43-
* foo(){ }
44-
* }
45-
* ```
46-
*/
4765
build(): AST.ClassMethod[] {
4866
return this.actions.map((action) => {
4967
return action.build();

0 commit comments

Comments
 (0)