Skip to content

Commit f61fdd2

Browse files
ssutarpzuraq
authored andcommitted
[Bugfix] Handle the service and controller imports (#89)
* [Bugfix] Handle the service and controller imports * Handle the Evented import, Fix for #92
1 parent 18d722d commit f61fdd2

File tree

9 files changed

+83
-15
lines changed

9 files changed

+83
-15
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
alias,
3+
gt,
4+
equal,
35
readOnly,
46
reads,
57
oneWay as enoWay,
@@ -13,6 +15,7 @@ import { inject as service } from "@ember/service";
1315
import { on } from "@ember/object/evented";
1416
import layout from "components/templates/foo";
1517
import { someActionUtil } from "some/action/util";
18+
import NUMERIC_CONSTANT from "constants/numbers";
1619

1720
const Foo = EmberObject.extend({
1821
tagName: "div",
@@ -146,7 +149,11 @@ const Foo = EmberObject.extend({
146149
/**
147150
* Lname5
148151
*/
149-
lName5: add("description", "lastName").readOnly()
152+
lName5: add("description", "lastName").readOnly(),
153+
154+
isEqualToLimit: equal("limit", NUMERIC_CONSTANT.LIMIT).readOnly(),
155+
156+
isGreaterThanLimit: gt("limit", NUMERIC_CONSTANT).readOnly()
150157
});
151158

152159
const Foo = EmberObject.extend({

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ import {
1010
oneWay as enoWay,
1111
reads,
1212
readOnly,
13+
equal,
14+
gt,
1315
alias,
1416
} from "@ember-decorators/object/computed";
1517

1618
import { get, set } from "@ember/object";
1719
import layout from "components/templates/foo";
1820
import { someActionUtil } from "some/action/util";
21+
import NUMERIC_CONSTANT from "constants/numbers";
1922

2023
@tagName("div")
2124
@classNames("test-class", "custom-class")
@@ -187,6 +190,12 @@ class Foo extends EmberObject {
187190
*/
188191
@(add("description", "lastName").readOnly())
189192
lName5;
193+
194+
@(equal("limit", NUMERIC_CONSTANT.LIMIT).readOnly())
195+
isEqualToLimit;
196+
197+
@(gt("limit", NUMERIC_CONSTANT).readOnly())
198+
isGreaterThanLimit;
190199
}
191200

192201
@templateLayout(layout)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Service from "@ember/service";
2+
import Controller from "@ember/controller";
3+
import Evented, { on } from "@ember/object/evented";
4+
5+
const ser = Service.extend({});
6+
const ctrl = Controller.extend({});
7+
const evt = Service.extend(Evented, {
8+
e: on("click", function() {
9+
return "e";
10+
})
11+
});
12+
13+
export { ser, ctrl, evt };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"decorators": true
3+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { on } from "@ember-decorators/object";
2+
import Service from "@ember/service";
3+
import Controller from "@ember/controller";
4+
import Evented from "@ember/object/evented";
5+
6+
class Ser extends Service {}
7+
class Ctrl extends Controller {}
8+
9+
class Evt extends Service.extend(Evented) {
10+
@on("click")
11+
e() {
12+
return "e";
13+
}
14+
}
15+
16+
export { Ser, Ctrl, Evt };

transforms/helpers/EOProp.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,12 @@ class EOProp {
9191
return get(this.calleeObject, "arguments") || [];
9292
}
9393

94-
get hasNonLiteralArg() {
95-
return this.callExprArgs.some(arg => arg.type !== "Literal");
94+
get shouldRemoveLastArg() {
95+
const lastArg = this.callExprArgs.slice(-1) || [];
96+
return (
97+
lastArg[0].type === "FunctionExpression" ||
98+
lastArg[0].type === "ObjectExpression"
99+
);
96100
}
97101

98102
get hasModifierWithArgs() {

transforms/helpers/decorator-helper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function createCallExpressionDecorators(j, decoratorName, instanceProp) {
5757
const decoratorArgs =
5858
!instanceProp.hasMapDecorator &&
5959
!instanceProp.hasFilterDecorator &&
60-
instanceProp.hasNonLiteralArg
60+
instanceProp.shouldRemoveLastArg
6161
? instanceProp.callExprArgs.slice(0, -1)
6262
: instanceProp.callExprArgs.slice(0);
6363

transforms/helpers/parse-helper.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,20 +190,19 @@ function getExpressionToReplace(j, eoCallExpression) {
190190
* Returns name of class to be created
191191
*
192192
* @param {Object} j - jscodeshift lib reference
193-
* @param {CallExpression} eoCallExpression
193+
* @param {String} classVariableName
194194
* @param {String} filePath
195195
* @return {String}
196196
*/
197197
function getClassName(
198198
j,
199-
eoCallExpression,
199+
classVariableName,
200200
filePath,
201201
superClassName,
202202
type = ""
203203
) {
204-
const varDeclaration = getClosetVariableDeclaration(j, eoCallExpression);
205204
const className =
206-
getVariableName(varDeclaration) || camelCase(path.basename(filePath, "js"));
205+
classVariableName || camelCase(path.basename(filePath, "js"));
207206
let capitalizedClassName = `${capitalizeFirstLetter(
208207
className
209208
)}${capitalizeFirstLetter(type)}`;
@@ -298,15 +297,23 @@ function replaceEmberObjectExpressions(j, root, filePath, options = {}) {
298297
}
299298

300299
const superClassName = get(eoCallExpression, "value.callee.object.name");
300+
const varDeclaration = getClosetVariableDeclaration(j, eoCallExpression);
301+
const classVariableName = getVariableName(varDeclaration);
302+
const className = getClassName(
303+
j,
304+
classVariableName,
305+
filePath,
306+
superClassName,
307+
get(options, "runtimeData.type")
308+
);
309+
310+
if (classVariableName) {
311+
root.findVariableDeclarators(classVariableName).renameTo(className);
312+
}
313+
301314
const es6ClassDeclaration = createClass(
302315
j,
303-
getClassName(
304-
j,
305-
eoCallExpression,
306-
filePath,
307-
superClassName,
308-
get(options, "runtimeData.type")
309-
),
316+
className,
310317
eoProps,
311318
superClassName,
312319
mixins

transforms/helpers/util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,21 @@ const DECORATOR_PATHS = {
1313
decoratorPath: "@ember-decorators/object"
1414
},
1515
"@ember/object/evented": {
16+
importPropDecoratorMap: {
17+
on: "on"
18+
},
1619
decoratorPath: "@ember-decorators/object"
1720
},
1821
"@ember/controller": {
22+
importPropDecoratorMap: {
23+
inject: "inject"
24+
},
1925
decoratorPath: "@ember-decorators/controller"
2026
},
2127
"@ember/service": {
28+
importPropDecoratorMap: {
29+
inject: "inject"
30+
},
2231
decoratorPath: "@ember-decorators/service"
2332
},
2433
"@ember/object/computed": {

0 commit comments

Comments
 (0)