-
Notifications
You must be signed in to change notification settings - Fork 87
/
require-data-selectors.js
45 lines (41 loc) · 1.44 KB
/
require-data-selectors.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
44
45
/**
* @fileoverview Use data-* attribute selectors instead of classes or tag names
* @author Sandeep Baldawa
*/
'use strict'
module.exports = {
meta: {
docs: {
description: 'Use data-* attributes to provide context to your selectors and insulate them from CSS or JS changes https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements',
category: 'Possible Errors',
recommended: false,
url: 'https://docs.cypress.io/guides/references/best-practices.html#Selecting-Elements',
},
schema: [],
messages: {
unexpected: 'use data-* attribute selectors instead of classes or tag names',
},
},
create (context) {
return {
CallExpression (node) {
if (isCallingCyGet(node) && !isDataArgument(node)) {
context.report({ node, messageId: 'unexpected' })
}
},
}
},
}
function isCallingCyGet (node) {
return node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'cy' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'get'
}
function isDataArgument (node) {
return node.arguments.length > 0 &&
((node.arguments[0].type === 'Literal' && String(node.arguments[0].value).startsWith('[data-')) ||
(node.arguments[0].type === 'TemplateLiteral' && String(node.arguments[0].quasis[0].value.cooked).startsWith('[data-'))
)
}