Skip to content

Commit f928d92

Browse files
ssutarpzuraq
authored andcommitted
Handle this._super in computed properties (#35)
1 parent ceca65c commit f928d92

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ const Foo = EmberObject.extend({
5757
*/
5858
fullName: computed("firstName", "lastName", {
5959
get(key) {
60-
return `${this.get("firstName")} ${this.get("lastName")}`;
60+
return (
61+
this._super(...arguments) &&
62+
`${this.get("firstName")} ${this.get("lastName")}`
63+
);
6164
},
6265
set(key, value) {
6366
let [firstName, lastName] = value.split(/\s+/);
@@ -70,6 +73,10 @@ const Foo = EmberObject.extend({
7073
Computed description
7174
*/
7275
description: computed("fullName", "age", "country", function() {
76+
const desc = this._super(...arguments);
77+
if (desc) {
78+
return desc;
79+
}
7380
return `${this.get(
7481
"fullName"
7582
)}; Age: ${this.get("age")}; Country: ${this.get("country")}`;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class Foo extends EmberObject {
7676
@computed("firstName", "lastName")
7777
@readOnly
7878
get fullName() {
79-
return `${this.get("firstName")} ${this.get("lastName")}`;
79+
return super.fullName &&
80+
`${this.get("firstName")} ${this.get("lastName")}`;
8081
}
8182

8283
set fullName(value) {
@@ -90,6 +91,10 @@ class Foo extends EmberObject {
9091
Computed description
9192
*/
9293
get description() {
94+
const desc = super.description;
95+
if (desc) {
96+
return desc;
97+
}
9398
return `${this.get(
9499
"fullName"
95100
)}; Age: ${this.get("age")}; Country: ${this.get("country")}`;

transforms/helpers/transform-helper.js

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ function createSuperExpressionStatement(j) {
6868
* @param {MethodDefinition} methodDefinition - MethodDefinition to replce instances from
6969
* @returns {MethodDefinition}
7070
*/
71-
function replaceSuperExpressions(
72-
j,
73-
methodDefinition,
74-
replaceWithUndefined,
75-
isAction = false
76-
) {
71+
function replaceSuperExpressions(j, methodDefinition, functionProp) {
72+
const replaceWithUndefined = functionProp.hasRuntimeData
73+
? !functionProp.isOverridden
74+
: false;
7775
const superExprs = j(methodDefinition).find(j.CallExpression, {
7876
callee: {
7977
type: "MemberExpression",
@@ -93,7 +91,9 @@ function replaceSuperExpressions(
9391
} else {
9492
let superMethodCall;
9593
const superMethodArgs = get(superExpr, "value.arguments") || [];
96-
if (isAction) {
94+
if (functionProp.isComputed) {
95+
superMethodCall = j.memberExpression(j.super(), functionProp.key);
96+
} else if (functionProp.isAction) {
9797
superMethodCall = j.callExpression(
9898
j.memberExpression(
9999
j.memberExpression(
@@ -131,24 +131,15 @@ function replaceSuperExpressions(
131131
* @param {Decorator[]} decorators
132132
* @returns {MethodDefinition[]}
133133
*/
134-
function createMethodProp(
135-
j,
136-
functionProp,
137-
decorators = [],
138-
replaceWithUndefined = false,
139-
isAction = false
140-
) {
134+
function createMethodProp(j, functionProp, decorators = []) {
141135
const propKind = functionProp.kind === "init" ? "method" : functionProp.kind;
142-
if (functionProp.hasRuntimeData) {
143-
replaceWithUndefined = !functionProp.isOverridden;
144-
}
136+
145137
return withDecorators(
146138
withComments(
147139
replaceSuperExpressions(
148140
j,
149141
j.methodDefinition(propKind, functionProp.key, functionProp.value),
150-
replaceWithUndefined,
151-
isAction
142+
functionProp
152143
),
153144
functionProp
154145
),
@@ -232,16 +223,12 @@ function createActionDecoratedProps(j, actionsProp) {
232223
const actionProps = get(actionsProp, "value.properties");
233224
const overriddenActions = get(actionsProp, "overriddenActions") || [];
234225
const actionDecorators = createIdentifierDecorators(j);
235-
return actionProps.map(actionProp =>
236-
createMethodProp(
237-
j,
238-
actionProp,
239-
actionDecorators,
240-
actionsProp.hasRuntimeData &&
241-
!overriddenActions.includes(actionProp.key.name),
242-
true
243-
)
244-
);
226+
return actionProps.map(actionProp => {
227+
actionProp.isAction = true;
228+
actionProp.hasRuntimeData = actionsProp.hasRuntimeData;
229+
actionProp.isOverridden = overriddenActions.includes(actionProp.key.name);
230+
return createMethodProp(j, actionProp, actionDecorators);
231+
});
245232
}
246233

247234
/**
@@ -258,6 +245,7 @@ function createCallExpressionProp(j, callExprProp) {
258245

259246
if (lastArgType === "FunctionExpression") {
260247
const functionExpr = {
248+
isComputed: true,
261249
kind: callExprProp.kind,
262250
key: callExprProp.key,
263251
value: callExprLastArg,
@@ -272,6 +260,7 @@ function createCallExpressionProp(j, callExprProp) {
272260
];
273261
} else if (lastArgType === "ObjectExpression") {
274262
const callExprMethods = callExprLastArg.properties.map(callExprFunction => {
263+
callExprFunction.isComputed = true;
275264
callExprFunction.kind = getPropName(callExprFunction);
276265
callExprFunction.key = callExprProp.key;
277266
callExprFunction.value.params.shift();

0 commit comments

Comments
 (0)