Skip to content

Commit f393901

Browse files
ssutarpzuraq
authored andcommitted
Support for filter and map computed property (#40)
Fix for: #39
1 parent f690999 commit f393901

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
readOnly,
44
reads,
55
oneWay as enoWay,
6-
sum as add
6+
sum as add,
7+
map as computedMap,
8+
filter
79
} from "@ember/object/computed";
810
import { get, set, observer as watcher, computed } from "@ember/object";
911
import { inject as controller } from "@ember/controller";
@@ -24,6 +26,12 @@ const Foo = EmberObject.extend({
2426
event: on("click", function() {
2527
return "abc";
2628
}),
29+
excitingChores: computedMap("chores", function(chore, index) {
30+
return chore.toUpperCase() + "!";
31+
}),
32+
remainingChores: filter("chores", function(chore, index, array) {
33+
return !chore.done;
34+
}),
2735

2836
actions: {
2937
someActionUtil,

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { attribute, className, classNames, layout, tagName } from "@ember-decorators/component";
2-
import { sum as add, overridableReads as enoWay, overridableReads, reads, alias } from "@ember-decorators/object/computed";
2+
3+
import {
4+
filter,
5+
map as computedMap,
6+
sum as add,
7+
overridableReads as enoWay,
8+
overridableReads,
9+
reads,
10+
alias,
11+
} from "@ember-decorators/object/computed";
12+
313
import { get, set } from "@ember/object";
414
import { action, readOnly, volatile, computed, observes as watcher } from "@ember-decorators/object";
515
import { controller } from "@ember-decorators/controller";
@@ -29,6 +39,16 @@ class Foo extends EmberObject {
2939
return "abc";
3040
}
3141

42+
@computedMap("chores", function(chore, index) {
43+
return chore.toUpperCase() + "!";
44+
})
45+
excitingChores;
46+
47+
@filter("chores", function(chore, index, array) {
48+
return !chore.done;
49+
})
50+
remainingChores;
51+
3252
@action
3353
someActionUtil() {
3454
return someActionUtil.call(this, ...arguments);

transforms/helpers/EOProp.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,15 @@ class EOProp {
146146
setDecorators(importedDecoratedProps) {
147147
if (this.isCallExpression) {
148148
this.setCallExpressionProps();
149-
const { decoratorName, isMethodDecorator, isMetaDecorator } =
150-
importedDecoratedProps[this.calleeName] || {};
149+
const {
150+
decoratorName,
151+
isMethodDecorator,
152+
isMetaDecorator,
153+
importedName
154+
} = importedDecoratedProps[this.calleeName] || {};
151155
if (decoratorName) {
156+
this.hasMapDecorator = importedName === "map";
157+
this.hasFilterDecorator = importedName === "filter";
152158
this.hasMethodDecorator = isMethodDecorator;
153159
this.hasMetaDecorator = isMetaDecorator;
154160
this.decoratorNames.push(decoratorName);

transforms/helpers/decorator-helper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ function createClassDecorator(j, classDecoratorProp) {
4242
* @returns {Decorator[]}
4343
*/
4444
function createCallExpressionDecorators(j, decoratorName, instanceProp) {
45-
const decoratorArgs = instanceProp.hasNonLiteralArg
46-
? instanceProp.callExprArgs.slice(0, -1)
47-
: instanceProp.callExprArgs.slice(0);
45+
const decoratorArgs =
46+
!instanceProp.hasMapDecorator &&
47+
!instanceProp.hasFilterDecorator &&
48+
instanceProp.hasNonLiteralArg
49+
? instanceProp.callExprArgs.slice(0, -1)
50+
: instanceProp.callExprArgs.slice(0);
4851

4952
if (instanceProp.isVolatileReadOnly) {
5053
return [];

transforms/helpers/transform-helper.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ function createActionDecoratedProps(j, actionsProp) {
306306
function createCallExpressionProp(j, callExprProp) {
307307
const callExprArgs = callExprProp.callExprArgs.slice(0);
308308
const callExprLastArg = callExprArgs.pop();
309-
const lastArgType = get(callExprLastArg, "type");
309+
const lastArgType =
310+
!callExprProp.hasMapDecorator && !callExprProp.hasFilterDecorator
311+
? get(callExprLastArg, "type")
312+
: "";
310313

311314
if (lastArgType === "FunctionExpression") {
312315
const functionExpr = {

0 commit comments

Comments
 (0)