Skip to content

Commit f690999

Browse files
ssutarpzuraq
authored andcommitted
Handle shorthand actions (#37)
#38
1 parent 6f0b1af commit f690999

File tree

11 files changed

+193
-54
lines changed

11 files changed

+193
-54
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const Foo = Test.extend(MyMixin, {
1010
numProp: 123,
1111
[MY_VAL]: "val",
1212
queryParams: {},
13+
someVal,
1314

1415
/**
1516
* Method comments

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Foo extends Test.extend(MyMixin) {
1111
numProp = 123;
1212
[MY_VAL] = "val";
1313
queryParams = {};
14+
someVal = someVal;
1415

1516
/**
1617
* Method comments

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { inject as controller } from "@ember/controller";
1010
import { inject as service } from "@ember/service";
1111
import { on } from "@ember/object/evented";
1212
import layout from "components/templates/foo";
13+
import { someActionUtil } from "some/action/util";
1314

1415
const Foo = EmberObject.extend({
1516
tagName: "div",
@@ -25,6 +26,7 @@ const Foo = EmberObject.extend({
2526
}),
2627

2728
actions: {
29+
someActionUtil,
2830
/**
2931
Comments
3032
*/

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { controller } from "@ember-decorators/controller";
66
import { service } from "@ember-decorators/service";
77
import { on } from "@ember-decorators/object/evented";
88
import templateLayout from "components/templates/foo";
9+
import { someActionUtil } from "some/action/util";
910

1011
@tagName("div")
1112
@classNames("test-class", "custom-class")
@@ -28,6 +29,11 @@ class Foo extends EmberObject {
2829
return "abc";
2930
}
3031

32+
@action
33+
someActionUtil() {
34+
return someActionUtil.call(this, ...arguments);
35+
}
36+
3137
/**
3238
Comments
3339
*/

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import RuntimeInput from "common/runtime/input";
2+
13
/**
24
* Program comments
35
*/
4-
const Foo = Test.extend(MyMixin, {
6+
export default RuntimeInput.extend(MyMixin, {
57
/**
68
* Property comments
79
*/

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { action, off, unobserves } from "@ember-decorators/object";
2+
import RuntimeInput from "common/runtime/input";
23

34
/**
45
* Program comments
56
*/
6-
class Foo extends Test.extend(MyMixin) {
7+
export default class RuntimeInputEmberObject extends RuntimeInput.extend(MyMixin) {
78
/**
89
* Property comments
910
*/

transforms/ember-object/index.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
const { getOptions } = require("codemod-cli");
22
const { replaceEmberObjectExpressions } = require("../helpers/parse-helper");
3-
const { getRuntimeData } = require("../helpers/util");
43

54
module.exports = function transformer(file, api, opts) {
65
const j = api.jscodeshift;
76
const options = Object.assign({}, opts, getOptions());
87
let { source, path } = file;
9-
const runtimeConfigPath = options["runtime-config-path"];
10-
11-
if (runtimeConfigPath) {
12-
options.runtimeData = getRuntimeData(runtimeConfigPath, path);
13-
if (!options.runtimeData) {
14-
return;
15-
}
16-
}
178

189
const root = j(source);
1910

transforms/helpers/decorator-helper.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ function createCallExpressionDecorators(j, decoratorName, instanceProp) {
6565
);
6666
}
6767

68+
/**
69+
* Create decorators which need arguments
70+
*
71+
* @param {Object} j - jscodeshift lib reference
72+
* @param {String} identifier
73+
* @param {String[]} args
74+
* @returns {Decorator[]}
75+
*/
6876
function createDecoratorsWithArgs(j, identifier, args) {
6977
return [
7078
j.decorator(
@@ -80,6 +88,7 @@ function createDecoratorsWithArgs(j, identifier, args) {
8088
* Create `@action` decorator
8189
*
8290
* @param {Object} j - jscodeshift lib reference
91+
* @param {String} identifier
8392
* @returns {Decorator[]}
8493
*/
8594
function createIdentifierDecorators(j, identifier = "action") {
@@ -132,12 +141,7 @@ function createInstancePropDecorators(j, instanceProp) {
132141
);
133142
}
134143
return decorators.concat(
135-
createCallExpressionDecorators(
136-
j,
137-
decorator,
138-
instanceProp,
139-
instanceProp.decoratorArgs[decorator]
140-
)
144+
createCallExpressionDecorators(j, decorator, instanceProp)
141145
);
142146
}, []);
143147
}

transforms/helpers/parse-helper.js

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
const path = require("path");
22
const camelCase = require("camelcase");
33
const {
4-
get,
5-
getOptions,
64
capitalizeFirstLetter,
7-
startsWithUpperCaseLetter,
85
DECORATOR_PATHS,
6+
EMBER_DECORATOR_SPECIFIERS,
7+
get,
8+
getOptions,
9+
getRuntimeData,
910
LAYOUT_IMPORT_SPECIFIER,
10-
METHOD_DECORATORS,
1111
META_DECORATORS,
12-
EMBER_DECORATOR_SPECIFIERS
12+
METHOD_DECORATORS,
13+
startsWithUpperCaseLetter
1314
} = require("./util");
14-
const { hasValidProps, isFileOfType } = require("./validation-helper");
1515
const {
16-
withComments,
16+
hasValidProps,
17+
isFileOfType,
18+
isTestFile
19+
} = require("./validation-helper");
20+
const {
1721
createClass,
22+
createEmberDecoratorSpecifiers,
1823
createImportDeclaration,
19-
createEmberDecoratorSpecifiers
24+
withComments
2025
} = require("./transform-helper");
2126
const EOProp = require("./EOProp");
2227
const logger = require("./log-helper");
@@ -131,11 +136,11 @@ function getDecoratorInfo(specifier, importPropDecoratorMap) {
131136
const isMethodDecorator = METHOD_DECORATORS.includes(importedName);
132137
return {
133138
decoratorName,
134-
localName,
135139
importedName,
136140
isImportedAs,
137141
isMetaDecorator,
138-
isMethodDecorator
142+
isMethodDecorator,
143+
localName
139144
};
140145
}
141146

@@ -223,15 +228,15 @@ function getDecoratorImports(j, root) {
223228
function getDecoratorsToImport(instanceProps, decoratorsMap = {}) {
224229
return instanceProps.reduce((specs, prop) => {
225230
return {
226-
attribute: specs.attribute || prop.hasAttributeDecorator,
227-
readOnly: specs.readOnly || prop.hasReadOnly,
228231
action: specs.action || prop.isAction,
229-
layout: specs.layout || prop.isLayout,
230-
tagName: specs.tagName || prop.isTagName,
232+
attribute: specs.attribute || prop.hasAttributeDecorator,
231233
className: specs.className || prop.hasClassNameDecorator,
232234
classNames: specs.classNames || prop.isClassNames,
233-
unobserves: specs.unobserves || prop.hasUnobservesDecorator,
235+
layout: specs.layout || prop.isLayout,
234236
off: specs.off || prop.hasOffDecorator,
237+
readOnly: specs.readOnly || prop.hasReadOnly,
238+
tagName: specs.tagName || prop.isTagName,
239+
unobserves: specs.unobserves || prop.hasUnobservesDecorator,
235240
volatile: specs.volatile || prop.hasVolatile
236241
};
237242
}, decoratorsMap);
@@ -460,11 +465,21 @@ function getExpressionToReplace(j, eoCallExpression) {
460465
* @param {String} filePath
461466
* @return {String}
462467
*/
463-
function getClassName(j, eoCallExpression, filePath) {
468+
function getClassName(
469+
j,
470+
eoCallExpression,
471+
filePath,
472+
superClassName,
473+
type = "EmberObject"
474+
) {
464475
const varDeclaration = getClosetVariableDeclaration(j, eoCallExpression);
465476
const className =
466477
getVariableName(varDeclaration) || camelCase(path.basename(filePath, "js"));
467-
return capitalizeFirstLetter(className);
478+
let capitalizedClassName = capitalizeFirstLetter(className);
479+
if (capitalizedClassName === superClassName) {
480+
capitalizedClassName += capitalizeFirstLetter(type);
481+
}
482+
return capitalizedClassName;
468483
}
469484

470485
/**
@@ -499,6 +514,24 @@ function parseEmberObjectCallExpression(eoCallExpression) {
499514
*/
500515
function replaceEmberObjectExpressions(j, root, filePath, options = {}) {
501516
logger.info(`[${filePath}]: BEGIN`);
517+
518+
const runtimeConfigPath = options["runtime-config-path"];
519+
520+
if (runtimeConfigPath) {
521+
options.runtimeData = getRuntimeData(runtimeConfigPath, filePath);
522+
if (!options.runtimeData) {
523+
logger.warn(
524+
`${filePath} SKIPPED Cound not find runtime data NO_RUNTIME_DATA`
525+
);
526+
return;
527+
}
528+
}
529+
530+
if (isTestFile(filePath)) {
531+
logger.warn(`[${filePath}]: Skipping test file`);
532+
return;
533+
}
534+
502535
if (options.type && !isFileOfType(filePath, options.type)) {
503536
logger.warn(
504537
`[${filePath}]: FAILURE Type mismatch, expected type '${
@@ -536,7 +569,13 @@ function replaceEmberObjectExpressions(j, root, filePath, options = {}) {
536569
const superClassName = get(eoCallExpression, "value.callee.object.name");
537570
const es6ClassDeclaration = createClass(
538571
j,
539-
getClassName(j, eoCallExpression, filePath),
572+
getClassName(
573+
j,
574+
eoCallExpression,
575+
filePath,
576+
superClassName,
577+
get(options, "runtimeData.type")
578+
),
540579
eoProps,
541580
superClassName,
542581
mixins
@@ -568,11 +607,11 @@ function replaceEmberObjectExpressions(j, root, filePath, options = {}) {
568607
}
569608

570609
module.exports = {
571-
getVariableName,
572-
getEmberObjectProps,
573-
getEmberObjectCallExpressions,
610+
getClassName,
574611
getClosetVariableDeclaration,
612+
getEmberObjectCallExpressions,
613+
getEmberObjectProps,
575614
getExpressionToReplace,
576-
replaceEmberObjectExpressions,
577-
getClassName
615+
getVariableName,
616+
replaceEmberObjectExpressions
578617
};

0 commit comments

Comments
 (0)