Skip to content

Commit f2b7bde

Browse files
ssutarChris Garrett
authored andcommitted
Update the codemods as per the changes in ember-decorators v6 (#112)
* Update the codemods as per the changes in ember-decorators v6 * Add wrapComputed decorator with required changes
1 parent 652e71c commit f2b7bde

File tree

5 files changed

+49
-30
lines changed

5 files changed

+49
-30
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { attribute, className, classNames, tagName, layout as templateLayout } from "@ember-decorators/component";
2-
import { action, computed, observes as watcher, on } from "@ember-decorators/object";
3-
import { inject as controller } from "@ember-decorators/controller";
4-
import { inject as service } from "@ember-decorators/service";
2+
import { observes as watcher, on } from "@ember-decorators/object";
3+
import { action, computed } from "@ember/object";
4+
import { inject as controller } from "@ember/controller";
5+
import { inject as service } from "@ember/service";
56

67
import {
78
filter,
@@ -13,7 +14,7 @@ import {
1314
equal,
1415
gt,
1516
alias,
16-
} from "@ember-decorators/object/computed";
17+
} from "@ember/object/computed";
1718

1819
import { get, set } from "@ember/object";
1920
import layout from "components/templates/foo";

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { action, off, unobserves, wrapComputed, computed } from "@ember-decorators/object";
2-
import { alias } from "@ember-decorators/object/computed";
1+
import { off, unobserves } from "@ember-decorators/object";
2+
import { action, computed } from "@ember/object";
3+
import { alias } from "@ember/object/computed";
34
import RuntimeInput from "common/runtime/input";
45

56
/**
@@ -30,7 +31,7 @@ export default class RuntimeInputEmberObject extends RuntimeInput.extend(MyMixin
3031
@alias("numPlusOne")
3132
numPlusPlus;
3233

33-
@wrapComputed(customMacro())
34+
@customMacro
3435
computedMacro;
3536

3637
/**

transforms/helpers/decorator-helper.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ function createCallExpressionDecorators(j, decoratorName, instanceProp) {
4949
}
5050

5151
if (instanceProp.hasWrapComputedDecorator) {
52-
return j.decorator(
53-
j.callExpression(j.identifier(decoratorName), [instanceProp.value])
54-
);
52+
return j.decorator(j.identifier(instanceProp.calleeName));
5553
}
5654

5755
const decoratorArgs =

transforms/helpers/import-helper.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const {
22
DECORATOR_PATHS,
3+
DECORATOR_PATH_OVERRIDES,
34
EMBER_DECORATOR_SPECIFIERS,
45
get,
56
getFirstDeclaration,
@@ -170,11 +171,18 @@ function createNewImportDeclarations(
170171
function getDecoratorPathSpecifiers(j, root, decoratorsToImport = []) {
171172
// create a copy - we need to mutate the object later
172173
const edPathNameMap = Object.assign({}, EMBER_DECORATOR_SPECIFIERS);
174+
175+
// Iterate over the existing imports
176+
// Extract and process the specifiers
177+
// Construct the map with path as key and value as list of specifiers to import from the path
173178
return getExistingDecoratorImports(j, root).reduce(
174179
(decoratorPathSpecifierMap, existingDecoratorImport) => {
175180
const { importPropDecoratorMap, decoratorPath } = DECORATOR_PATHS[
176181
get(existingDecoratorImport, "value.source.value")
177182
];
183+
// Decorators to be imported for the path
184+
// These are typically additional decorators which need to be imported for a path
185+
// For example - `@action` decorator
178186
const decoratorsForPath = edPathNameMap[decoratorPath] || [];
179187
// delete the visited path to avoid duplicate imports
180188
delete edPathNameMap[decoratorPath];
@@ -186,7 +194,6 @@ function getDecoratorPathSpecifiers(j, root, decoratorsToImport = []) {
186194
decoratorsForPath,
187195
decoratorsToImport
188196
);
189-
190197
const existingSpecifiers =
191198
get(existingDecoratorImport, "value.specifiers") || [];
192199

@@ -200,15 +207,26 @@ function getDecoratorPathSpecifiers(j, root, decoratorsToImport = []) {
200207
// Update decorator local and imported names,
201208
// Needed in case of `observer` which need to be renamed to `@observes`
202209
setSpecifierNames(existingSpecifier, importPropDecoratorMap);
203-
const isSpecifierPresent = decoratedSpecifiers.some(specifier => {
204-
return (
205-
!get(specifier, "local.name") &&
206-
get(specifier, "imported.name") ===
207-
get(existingSpecifier, "imported.name")
210+
// Check if the decorator import path is overridden
211+
// Needed in case of `observes` which need to be imported from `@ember-decorators/object`
212+
const overriddenPath =
213+
DECORATOR_PATH_OVERRIDES[get(existingSpecifier, "imported.name")];
214+
if (overriddenPath) {
215+
decoratorPathSpecifierMap[overriddenPath] = [].concat(
216+
decoratorPathSpecifierMap[overriddenPath] || [],
217+
existingSpecifier
208218
);
209-
});
210-
if (!isSpecifierPresent) {
211-
decoratedSpecifiers.push(existingSpecifier);
219+
} else {
220+
const isSpecifierPresent = decoratedSpecifiers.some(specifier => {
221+
return (
222+
!get(specifier, "local.name") &&
223+
get(specifier, "imported.name") ===
224+
get(existingSpecifier, "imported.name")
225+
);
226+
});
227+
if (!isSpecifierPresent) {
228+
decoratedSpecifiers.push(existingSpecifier);
229+
}
212230
}
213231

214232
// Remove the specifier from the existing import
@@ -252,6 +270,7 @@ function createDecoratorImportDeclarations(j, root, decoratorsToImport = []) {
252270
root,
253271
decoratorsToImport
254272
);
273+
255274
const firstDeclaration = getFirstDeclaration(j, root);
256275
const decoratorPathsImported = Object.keys(decoratorPathSpecifierMap);
257276
// Create import statement replacing the existing ones with specifiers importing from ember-decorators namespace

transforms/helpers/util.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const DECORATOR_PATHS = {
1010
observer: "observes",
1111
computed: "computed"
1212
},
13-
decoratorPath: "@ember-decorators/object"
13+
decoratorPath: "@ember/object"
1414
},
1515
"@ember/object/evented": {
1616
importPropDecoratorMap: {
@@ -22,27 +22,26 @@ const DECORATOR_PATHS = {
2222
importPropDecoratorMap: {
2323
inject: "inject"
2424
},
25-
decoratorPath: "@ember-decorators/controller"
25+
decoratorPath: "@ember/controller"
2626
},
2727
"@ember/service": {
2828
importPropDecoratorMap: {
2929
inject: "inject"
3030
},
31-
decoratorPath: "@ember-decorators/service"
31+
decoratorPath: "@ember/service"
3232
},
3333
"@ember/object/computed": {
34-
decoratorPath: "@ember-decorators/object/computed"
34+
decoratorPath: "@ember/object/computed"
3535
}
3636
};
3737

38+
const DECORATOR_PATH_OVERRIDES = {
39+
observes: "@ember-decorators/object"
40+
};
41+
3842
const EMBER_DECORATOR_SPECIFIERS = {
39-
"@ember-decorators/object": [
40-
"action",
41-
"off",
42-
"on",
43-
"unobserves",
44-
"wrapComputed"
45-
],
43+
"@ember/object": ["action"],
44+
"@ember-decorators/object": ["off", "on", "unobserves"],
4645
"@ember-decorators/component": [
4746
"attribute",
4847
"className",
@@ -314,6 +313,7 @@ module.exports = {
314313
ACTION_SUPER_EXPRESSION_COMMENT,
315314
capitalizeFirstLetter,
316315
DECORATOR_PATHS,
316+
DECORATOR_PATH_OVERRIDES,
317317
EMBER_DECORATOR_SPECIFIERS,
318318
get,
319319
getFirstDeclaration,

0 commit comments

Comments
 (0)