-
Notifications
You must be signed in to change notification settings - Fork 14
/
get-locator.js
43 lines (37 loc) · 1.12 KB
/
get-locator.js
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
'use strict'
/**
* @fileoverview Utility function to extract the "by" locator values. Also handles the "$" and "$$" shortcuts.
* @author Alexander Afanasyev
*/
function onlyLiteralArguments (node) {
return node.arguments.every(function (arg) {
return arg.type === 'Literal'
})
}
module.exports = function (node) {
var object = node.callee.object
var property = node.callee.property
var insideBy = object && property && object.name === 'by'
var dollarShortcuts = node.callee.name === '$' || node.callee.name === '$$'
var chainedDollarShortcuts = property && (property.name === '$' || property.name === '$$')
// handling by.smth calls
if (insideBy) {
var hasArgument = node.arguments && node.arguments.length
if (hasArgument && onlyLiteralArguments(node)) {
return {
by: property.name,
value: node.arguments.map(function (arg) {
return arg.value
})
}
}
}
// handling $ and $$ calls
if (dollarShortcuts || chainedDollarShortcuts) {
var value = node.arguments[0].value
return {
by: 'css',
value: value ? [value] : undefined
}
}
}