11'use strict'
2-
32module . exports = {
43 meta : {
54 type : 'suggestion' ,
@@ -16,9 +15,29 @@ module.exports = {
1615 } ,
1716
1817 create ( context ) {
18+ const variablesMap = new Map ( )
1919 return {
20+ VariableDeclarator ( node ) {
21+ if ( node . init && node . id && node . id . type === 'Identifier' ) {
22+ let selectorValue = null
23+
24+ if ( node . init . type === 'Literal' && typeof node . init . value === 'string' ) {
25+ selectorValue = node . init . value
26+ }
27+ else if ( node . init . type === 'TemplateLiteral'
28+ && node . init . expressions . length === 0
29+ && node . init . quasis . length === 1 ) {
30+ selectorValue = node . init . quasis [ 0 ] . value . cooked
31+ }
32+
33+ if ( selectorValue && isAliasOrDataSelector ( selectorValue ) ) {
34+ variablesMap . set ( node . id . name , selectorValue )
35+ }
36+ }
37+ } ,
38+
2039 CallExpression ( node ) {
21- if ( isCallingCyGet ( node ) && ! isDataArgument ( node ) ) {
40+ if ( isCallingCyGet ( node ) && ! isDataArgument ( node , variablesMap ) ) {
2241 context . report ( { node, messageId : 'unexpected' } )
2342 }
2443 } ,
@@ -34,12 +53,24 @@ function isCallingCyGet(node) {
3453 && node . callee . property . name === 'get'
3554}
3655
37- function isDataArgument ( node ) {
38- return node . arguments . length > 0
39- && (
40- ( node . arguments [ 0 ] . type === 'Literal' && isAliasOrDataSelector ( String ( node . arguments [ 0 ] . value ) ) )
41- || ( node . arguments [ 0 ] . type === 'TemplateLiteral' && isAliasOrDataSelector ( String ( node . arguments [ 0 ] . quasis [ 0 ] . value . cooked ) ) )
42- )
56+ function isDataArgument ( node , dataVariables ) {
57+ if ( node . arguments . length === 0 ) return false
58+
59+ const firstArg = node . arguments [ 0 ]
60+
61+ if ( firstArg . type === 'Literal' ) {
62+ return isAliasOrDataSelector ( String ( firstArg . value ) )
63+ }
64+
65+ if ( firstArg . type === 'TemplateLiteral' ) {
66+ return isAliasOrDataSelector ( String ( firstArg . quasis [ 0 ] . value . cooked ) )
67+ }
68+
69+ if ( firstArg . type === 'Identifier' ) {
70+ return dataVariables . has ( firstArg . name )
71+ }
72+
73+ return false
4374}
4475
4576function isAliasOrDataSelector ( selector ) {
0 commit comments