forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-rules.ts
200 lines (165 loc) · 5.81 KB
/
js-rules.ts
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
191
192
193
194
195
196
197
198
199
200
import {
Visitor,
namedTypes as n,
builders as b,
} from 'ast-types'
import { ExpressionKind } from 'ast-types/gen/kinds'
// use `globalThis` instead of `window`, `self`... to lower chances of scope conflict
// users can technically override even this, but it would be very rude
// "[globalThis] provides a way for polyfills/shims, build tools, and portable code to have a reliable non-eval means to access the global..."
// @see https://github.com/tc39/proposal-global/blob/master/NAMING.md
const globalIdentifier = b.identifier('globalThis')
/**
* Generate a CallExpression for Cypress.resolveWindowReference
* @param accessedObject object being accessed
* @param prop name of property being accessed
* @param maybeVal if an assignment is being made, this is the RHS of the assignment
*/
function resolveWindowReference (accessedObject: ExpressionKind, prop: string, maybeVal?: ExpressionKind) {
const args = [
globalIdentifier,
accessedObject,
b.stringLiteral(prop),
]
if (maybeVal) {
args.push(maybeVal)
}
return b.callExpression(
b.memberExpression(
b.memberExpression(
b.memberExpression(
globalIdentifier,
b.identifier('top'),
),
b.identifier('Cypress'),
),
b.identifier('resolveWindowReference'),
),
args,
)
}
/**
* Generate a CallExpression for Cypress.resolveLocationReference
*/
function resolveLocationReference () {
return b.callExpression(
b.memberExpression(
b.memberExpression(
b.memberExpression(
globalIdentifier,
b.identifier('top'),
),
b.identifier('Cypress'),
),
b.identifier('resolveLocationReference'),
),
[globalIdentifier],
)
}
/**
* Given an Identifier or a Literal, return a property name that should use `resolveWindowReference`.
* @param node
*/
function getReplaceablePropOfMemberExpression (node: n.MemberExpression) {
const { property } = node
// something.(top|parent)
if (n.Identifier.check(property) && ['parent', 'top', 'location'].includes(property.name)) {
return property.name
}
// something['(top|parent)']
if (n.Literal.check(property) && ['parent', 'top', 'location'].includes(String(property.value))) {
return String(property.value)
}
// NOTE: cases where a variable is used for the prop will not be replaced
// for example, `bar = 'top'; window[bar];` will not be replaced
// this would most likely be too slow
return
}
/**
* An AST Visitor that applies JS transformations required for Cypress.
* @see https://github.com/benjamn/ast-types#ast-traversal for details on how the Visitor is implemented
* @see https://astexplorer.net/#/gist/7f1e645c74df845b0e1f814454e9bbdf/f443b701b53bf17fbbf40e9285cb8b65a4066240
* to explore ASTs generated by recast
*/
export const jsRules: Visitor<{}> = {
// replace member accesses like foo['top'] or bar.parent with resolveWindowReference
visitMemberExpression (path) {
const { node } = path
const prop = getReplaceablePropOfMemberExpression(node)
if (!prop) {
return this.traverse(path)
}
path.replace(resolveWindowReference(path.get('object').node, prop))
return false
},
// replace lone identifiers like `top`, `parent`, with resolveWindowReference
visitIdentifier (path) {
const { node } = path
if (path.parentPath) {
const parentNode = path.parentPath.node
// like `identifer = 'foo'`
const isAssignee = n.AssignmentExpression.check(parentNode) && parentNode.left === node
if (isAssignee && node.name === 'location') {
// `location = 'something'`, rewrite to intercepted href setter since relative urls can break this
path.replace(b.memberExpression(resolveLocationReference(), b.identifier('href')))
return false
}
// some Identifiers do not refer to a scoped variable, depending on how they're used
if (
// like `var top = 'foo'`
(n.VariableDeclarator.check(parentNode) && parentNode.id === node)
|| (isAssignee)
|| (
[
'LabeledStatement', // like `top: foo();`
'ContinueStatement', // like 'continue top'
'BreakStatement', // like 'break top'
'Property', // like `{ top: 'foo' }`
'FunctionDeclaration', // like `function top()`
'RestElement', // like (...top)
'ArrowFunctionExpression', // like `(top, ...parent) => { }`
'ArrowExpression', // MDN Parser docs mention this being used for () => {}
'FunctionExpression', // like `(function top())`,
].includes(parentNode.type)
)
) {
return false
}
}
if (path.scope.declares(node.name)) {
// identifier has been declared in local scope, don't care about replacing
return this.traverse(path)
}
if (node.name === 'location') {
path.replace(resolveLocationReference())
return false
}
if (['parent', 'top'].includes(node.name)) {
path.replace(resolveWindowReference(globalIdentifier, node.name))
return false
}
this.traverse(path)
},
visitAssignmentExpression (path) {
const { node } = path
const finish = () => {
this.traverse(path)
}
if (!n.MemberExpression.check(node.left)) {
return finish()
}
const propBeingSet = getReplaceablePropOfMemberExpression(node.left)
if (!propBeingSet) {
return finish()
}
if (node.operator !== '=') {
// in the case of +=, -=, |=, etc., assume they're not doing something like
// `window.top += 4` since that would be invalid anyways, just continue down the RHS
this.traverse(path.get('right'))
return false
}
const objBeingSetOn = node.left.object
path.replace(resolveWindowReference(objBeingSetOn, propBeingSet, node.right))
return false
},
}