-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtransform-helper.ts
More file actions
176 lines (166 loc) · 4.89 KB
/
transform-helper.ts
File metadata and controls
176 lines (166 loc) · 4.89 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
import { default as j } from 'jscodeshift';
import * as AST from '../helpers/ast';
import {
ACTIONS_NAME,
ACTION_SUPER_EXPRESSION_COMMENT,
LAYOUT_DECORATOR_LOCAL_NAME,
LAYOUT_DECORATOR_NAME,
} from './util/index';
/** Copy comments `from` => `to` */
export function withComments<T extends { comments?: unknown }>(
to: T,
from: { comments?: T['comments'] | undefined }
): T {
if (from.comments) {
to.comments = from.comments;
} else {
delete to.comments;
}
return to;
}
/** Creates line comments from passed lines */
function createLineComments(lines: readonly string[] = []): AST.CommentLine[] {
return lines.map((line) => j.commentLine(line));
}
/**
* Replace instances of `this._super(...arguments)` with the expression returned
* by the provided callback.
*/
function replaceSuperExpressions(
classMethod: AST.ClassMethod,
replaceWithUndefined: boolean,
buildSuperMethodCall: (
superMethodArgs: AST.EOSuperExpression['arguments']
) => AST.CallExpression | AST.MemberExpression
): AST.ClassMethod {
const superExpressionCollection = AST.findPaths(
j(classMethod) as AST.Collection,
j.CallExpression,
AST.isEOSuperExpression
);
if (superExpressionCollection.length === 0) {
return classMethod;
}
// eslint-disable-next-line unicorn/no-array-for-each
superExpressionCollection.forEach((superExpressionPath) => {
if (replaceWithUndefined) {
j(superExpressionPath).replaceWith(j.identifier('undefined'));
} else {
const superMethodArgs = superExpressionPath.value.arguments;
const superMethodCall = buildSuperMethodCall(superMethodArgs);
j(superExpressionPath).replaceWith(superMethodCall);
}
});
return classMethod;
}
/**
* Replaces `this._super(...arguments)` calls in actions with the appropriate
* class `super` call and a helpful migration message.
*
* @example
*
* ```
* // TODO: This call to super is within an action, and has to refer to the parent
* // class's actions to be safe. This should be refactored to call a normal method
* // on the parent class. If the parent class has not been converted to native
* // classes, it may need to be refactored as well. See
* // https://github.com/ember-codemods/ember-native-class-codemod/blob/master/README.md
* // for more details.
* super.actions.baz.call(this, ...arguments);
* ```
*/
export function replaceActionSuperExpressions(
classMethod: AST.ClassMethod,
replaceWithUndefined: boolean
): AST.ClassMethod {
return replaceSuperExpressions(
classMethod,
replaceWithUndefined,
(superMethodArgs) => {
const superMethodCall = j.callExpression(
j.memberExpression(
j.memberExpression(
j.memberExpression(j.super(), j.identifier(ACTIONS_NAME)),
classMethod.key
),
j.identifier('call')
),
[j.thisExpression(), ...superMethodArgs]
);
superMethodCall.comments = createLineComments(
ACTION_SUPER_EXPRESSION_COMMENT
);
return superMethodCall;
}
);
}
/**
* Replaces `this._super(...arguments)` calls with the appropriate class `super`
* call for a getter or setter.
*
* @example
* ```
* super.getterName;
* ```
*/
export function replaceGetterSetterSuperExpressions(
classMethod: AST.ClassMethod,
replaceWithUndefined: boolean,
identifier: AST.Identifier
): AST.ClassMethod {
return replaceSuperExpressions(classMethod, replaceWithUndefined, () =>
j.memberExpression(j.super(), identifier)
);
}
/**
* Replaces `this._super(...arguments)` calls with the appropriate class `super`
* call for a method.
*
* @example
* ```
* super.methodName(...arguments);
* ```
*/
export function replaceMethodSuperExpressions(
classMethod: AST.ClassMethod,
replaceWithUndefined: boolean
): AST.ClassMethod {
return replaceSuperExpressions(
classMethod,
replaceWithUndefined,
(superMethodArgs) => {
return j.callExpression(
j.memberExpression(j.super(), classMethod.key),
superMethodArgs
);
}
);
}
/** Create import statement */
export function createImportDeclaration(
specifiers: Array<AST.ImportSpecifier | AST.ImportDefaultSpecifier>,
path: string
): AST.ImportDeclaration {
return j.importDeclaration(specifiers, j.literal(path));
}
/**
* Matches the decorators for the current path with the `decoratorsToImport`,
* and creates import specifiers for the matching decorators
*/
export function createEmberDecoratorSpecifiers(
pathSpecifiers: string[] = [],
decoratorsToImport: string[] = []
): AST.ImportSpecifier[] {
return pathSpecifiers
.filter((specifier) => decoratorsToImport.includes(specifier))
.map((specifier) => {
const importedSpecifier =
specifier === LAYOUT_DECORATOR_LOCAL_NAME
? LAYOUT_DECORATOR_NAME
: specifier;
return j.importSpecifier(
j.identifier(importedSpecifier),
j.identifier(specifier)
);
});
}