-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·190 lines (174 loc) · 6.29 KB
/
index.js
File metadata and controls
executable file
·190 lines (174 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const { getParser } = require('codemod-cli').jscodeshift;
const {
addTrackedImport,
getDependentKeys,
buildTrackedDecorator,
reformatTrackedDecorators,
filterGlimmerArgs,
} = require('./utils/helper');
const { getOptions } = require('codemod-cli');
const DEFAULT_OPTIONS = {
alwaysPrefix: 'true',
};
/**
* Return true if the computed property is readOnly.
* @param {*} nodeItem
*/
function _isReadOnlyComputedProperty(nodeItem) {
return (
_isComputedProperty(nodeItem) &&
nodeItem.expression.callee.property &&
nodeItem.expression.callee.property.name === 'readOnly'
);
}
/**
* Return true if the nodeItem is a computed property. It could either
* be a regular or readOnly computed property.
* @param {*} nodeItem
*/
function _isComputedProperty(nodeItem) {
return (
nodeItem.expression.callee &&
(nodeItem.expression.callee.name === 'computed' ||
(nodeItem.expression.callee.object &&
nodeItem.expression.callee.object.callee.name === 'computed'))
);
}
/**
* Returns true if the file is a Glimmer component.
* @param {*} j
* @param {*} root
*/
function _isGlimmerComponent(j, root) {
return root
.find(j.ImportDeclaration)
.filter((path) => path.node.source.value === '@glimmer/component')
.length === 1;
}
/**
* If the nodeItem is a computed property, then return an array of argument values.
* @param {*} nodeItem
*/
function _getArgValues(nodeItem) {
if (_isComputedProperty(nodeItem)) {
const nodeArguments = _isReadOnlyComputedProperty(nodeItem)
? nodeItem.expression.callee.object.arguments
: nodeItem.expression.arguments;
return nodeArguments.map(item => item.value);
}
}
module.exports = function transformer(file, api) {
const configOptions = Object.assign({}, DEFAULT_OPTIONS, getOptions());
const classProps = [];
let computedProps = [];
let computedPropsMap = {};
let shouldImportBeAdded = false;
const j = getParser(api);
const isGlimmerComponent = _isGlimmerComponent(j, j(file.source));
j(file.source)
.find(j.ClassBody)
.forEach(path => {
path.node.body.forEach(classItem => {
// Collect all the class properties in the file and add it to the
// classProps array. If there is a decorator associated with a class
// property, then only add it to the array if it is a @tracked property.
if (
classItem.type === 'ClassProperty' &&
(!classItem.decorators ||
classItem.decorators.every(
item => item.expression.name === 'tracked'
))
) {
classProps.push(classItem.key.name);
}
// Collect all the dependent keys of the computed properties present in the file
// and add it to the computedProps array.
if (
classItem.type === 'ClassMethod' &&
classItem.kind === 'get' &&
classItem.decorators
) {
classItem.decorators.forEach(decoratorItem => {
const argValues = _getArgValues(decoratorItem);
if (argValues) {
computedPropsMap[classItem.key.name] = argValues;
computedProps = computedProps.concat(argValues);
}
});
}
});
});
// Iterate through all the class properties in the file and determine if
// the property needs to be prefixed with @tracked.
// If the class property exists in the `computedProps` array, then prefix
// with @tracked. Also, set the `shouldImportBeAdded` to true which would help
// determine if the import statement `@glimmer/tracking` needs to be added to
// the file.
let trackedConvertedSource = j(file.source)
.find(j.ClassProperty)
.forEach(path => {
if (!path.node.decorators && computedProps.includes(path.node.key.name)) {
shouldImportBeAdded = true;
const trackedDecorator = buildTrackedDecorator(path.node.key.name, j);
// @TODO: Determine if @tracked can be prefixed alongside other decorators in a property,
// if yes, then change this code to push the trackedDecorator along with the
// others.
path.node.decorators = trackedDecorator;
}
return path;
})
.toSource();
if (configOptions.alwaysPrefix === 'true') {
trackedConvertedSource = reformatTrackedDecorators(trackedConvertedSource);
}
/**
* Iterate on all the class items, if the class item has decorators and it is not a dependent
* key of some other property, then go ahead and check if the `computed` decorator can be safely removed.
*/
const convertedResult = j(trackedConvertedSource)
.find(j.ClassBody)
.forEach(path => {
path.node.body.forEach(classItem => {
const propName = classItem.key.name;
// Check if the class item is not a dependent key of any other computed properties in the class
// and if the item has any decorators.
if (
!Object.keys(computedPropsMap).some(item =>
computedPropsMap[item].includes(propName)
) &&
classItem.decorators
) {
classItem.decorators.forEach((decoratorItem, i) => {
if (
decoratorItem.expression.type === 'CallExpression' &&
_isComputedProperty(decoratorItem)
) {
const isReadOnlyProperty = _isReadOnlyComputedProperty(
decoratorItem
);
const computedPropArguments = isReadOnlyProperty
? decoratorItem.expression.callee.object.arguments
: decoratorItem.expression.arguments;
let dependentKeys = getDependentKeys(
computedPropArguments,
computedPropsMap,
classProps
);
// When working in a Glimmer component, filter our args which are tracked by default
if (isGlimmerComponent) {
dependentKeys = filterGlimmerArgs(dependentKeys);
}
// If all the arguments of the decorator are class properties, then remove the decorator completely
// from the item.
if (!dependentKeys.length) {
classItem.decorators.splice(i, 1);
}
}
});
}
});
});
return shouldImportBeAdded
? addTrackedImport(convertedResult.toSource(), j)
: convertedResult.toSource();
};