-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
addToCypressConfigPlugin.ts
190 lines (175 loc) · 6.24 KB
/
addToCypressConfigPlugin.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
import type { ParserOptions, PluginItem, Visitor } from '@babel/core'
import * as t from '@babel/types'
/**
* Standardizes our approach to writing values into the existing
* Cypress config file. Attempts to handle the pragmatic cases,
* finding the
*
* @param toAdd k/v Object Property to append to the current object
* @returns
*/
export function addToCypressConfigPlugin (toAdd: t.ObjectProperty): PluginItem {
/**
* Based on the import syntax, we look for the "defineConfig" identifier, and whether it
* has been reassigned
*/
const defineConfigIdentifiers: Array<string | [string, string]> = []
/**
* Checks whether we've seen the identifier
*/
let seenConfigIdentifierCall = false
// Returns the ObjectExpression associated with the defineConfig call,
// so we can add in the "toAdd" object property
function getDefineConfigExpression (node: t.CallExpression): t.ObjectExpression | undefined {
for (const possibleIdentifier of defineConfigIdentifiers) {
if (typeof possibleIdentifier === 'string') {
if (t.isIdentifier(node.callee) && node.callee.name === possibleIdentifier && t.isObjectExpression(node.arguments[0])) {
return node.arguments[0]
}
} else if (Array.isArray(possibleIdentifier)) {
if (t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.object) &&
t.isIdentifier(node.callee.property) &&
node.callee.object.name === possibleIdentifier[0] &&
node.callee.property.name === possibleIdentifier[1] &&
t.isObjectExpression(node.arguments[0])
) {
return node.arguments[0]
}
}
}
return undefined
}
// Visits the program ahead-of-time, to know what transforms we need to do
// on the source when we output the addition
const nestedVisitor: Visitor = {
ImportDeclaration (path) {
// Skip "import type" for the purpose of finding the defineConfig identifier,
// and skip if we see a non "cypress" import, since that's the only one we care about finding
if (path.node.importKind === 'type' || path.node.source.value !== 'cypress') {
return
}
for (const specifier of path.node.specifiers) {
if (specifier.type === 'ImportNamespaceSpecifier' || specifier.type === 'ImportDefaultSpecifier') {
defineConfigIdentifiers.push([specifier.local.name, 'defineConfig'])
} else {
defineConfigIdentifiers.push(specifier.local.name)
}
}
},
VariableDeclaration (path) {
// We only care about the top-level variable declarations for requires
if (path.parent.type !== 'Program') {
return
}
const cyImportDeclarations = path.node.declarations.filter((d) => {
return (
t.isCallExpression(d.init) &&
t.isIdentifier(d.init.callee) &&
d.init.callee.name === 'require' &&
t.isStringLiteral(d.init.arguments[0]) &&
d.init.arguments[0].value === 'cypress'
)
})
for (const variableDeclaration of cyImportDeclarations) {
if (t.isIdentifier(variableDeclaration.id)) {
defineConfigIdentifiers.push([variableDeclaration.id.name, 'defineConfig'])
} else if (t.isObjectPattern(variableDeclaration.id)) {
for (const prop of variableDeclaration.id.properties) {
if (t.isObjectProperty(prop) && t.isIdentifier(prop.key) && t.isIdentifier(prop.value)) {
if (prop.key.name === 'defineConfig') {
defineConfigIdentifiers.push(prop.value.name)
}
}
}
}
}
},
CallExpression (path) {
if (getDefineConfigExpression(path.node)) {
seenConfigIdentifierCall = true
}
},
}
let didAdd = false
return {
name: 'addToCypressConfigPlugin',
manipulateOptions (t, parserOpts: ParserOptions) {
parserOpts.errorRecovery = true
if (
parserOpts.plugins.some(
(p: any) => (Array.isArray(p) ? p[0] : p) === 'typescript',
)
) {
return
}
parserOpts.plugins.push('typescript')
},
visitor: {
Program: {
enter (path) {
path.traverse(nestedVisitor)
},
exit () {
if (!didAdd) {
throw new Error('Unable to add the properties to the file')
}
},
},
CallExpression (path) {
if (seenConfigIdentifierCall && !didAdd) {
const defineConfigExpression = getDefineConfigExpression(path.node)
if (defineConfigExpression) {
defineConfigExpression.properties.push(toAdd)
didAdd = true
}
}
},
ExportDefaultDeclaration (path) {
// Exit if we've seen the defineConfig({ ... called elsewhere,
// since this is where we'll be adding the object props
if (seenConfigIdentifierCall || didAdd) {
return
}
// export default {}
if (t.isObjectExpression(path.node.declaration)) {
path.node.declaration.properties.push(toAdd)
didAdd = true
} else if (t.isExpression(path.node.declaration)) {
path.node.declaration = spreadResult(path.node.declaration, toAdd)
didAdd = true
}
},
AssignmentExpression (path) {
// Exit if we've seen the defineConfig({ ... called elsewhere,
// since this is where we'll be adding the object props
if (seenConfigIdentifierCall || didAdd) {
return
}
if (t.isMemberExpression(path.node.left) && isModuleExports(path.node.left)) {
if (t.isObjectExpression(path.node.right)) {
path.node.right.properties.push(toAdd)
didAdd = true
} else if (t.isExpression(path.node.right)) {
path.node.right = spreadResult(path.node.right, toAdd)
didAdd = true
}
}
},
},
}
}
function spreadResult (expr: t.Expression, toAdd: t.ObjectProperty): t.ObjectExpression {
return t.objectExpression([
t.spreadElement(expr),
toAdd,
])
}
function isModuleExports (node: t.MemberExpression) {
return (
t.isIdentifier(node.object) &&
node.object.name === 'module' &&
t.isIdentifier(node.property) &&
node.property.name === 'exports'
)
}