|
8 | 8 | // Rule Definition |
9 | 9 | //------------------------------------------------------------------------------ |
10 | 10 |
|
11 | | -module.exports=function(context){ |
12 | | -constsourceCode=context.getSourceCode(); |
13 | | -constregexpStack=[]; |
14 | | -letregexpBuffer=[]; |
15 | | -letinRegExp=false; |
| 11 | +module.exports={ |
| 12 | +create(context){ |
| 13 | +constsourceCode=context.getSourceCode(); |
| 14 | +constregexpStack=[]; |
| 15 | +letregexpBuffer=[]; |
| 16 | +letinRegExp=false; |
16 | 17 |
|
17 | | -functionreport(node,startOffset){ |
18 | | -constindexOfDot=sourceCode.getIndexFromLoc(node.loc.start)+startOffset; |
19 | | -context.report({ |
20 | | - node, |
21 | | -loc: sourceCode.getLocFromIndex(indexOfDot), |
22 | | -message: 'Unescaped dot character in regular expression' |
23 | | -}); |
24 | | -} |
| 18 | +functionreport(node,startOffset){ |
| 19 | +constindexOfDot=sourceCode.getIndexFromLoc(node.loc.start)+startOffset; |
| 20 | +context.report({ |
| 21 | + node, |
| 22 | +loc: sourceCode.getLocFromIndex(indexOfDot), |
| 23 | +message: 'Unescaped dot character in regular expression' |
| 24 | +}); |
| 25 | +} |
| 26 | +constallowedModifiers=['+','*','?','{']; |
25 | 27 |
|
26 | | -constallowedModifiers=['+','*','?','{']; |
27 | | -functioncheckRegExp(nodes){ |
28 | | -letescaping=false; |
29 | | -letinCharClass=false; |
30 | | -for(letn=0;n<nodes.length;++n){ |
31 | | -constpair=nodes[n]; |
32 | | -constnode=pair[0]; |
33 | | -conststr=pair[1]; |
34 | | -for(leti=0;i<str.length;++i){ |
35 | | -switch(str[i]){ |
36 | | -case'[': |
37 | | -if(!escaping) |
38 | | -inCharClass=true; |
39 | | -else |
40 | | -escaping=false; |
41 | | -break; |
42 | | -case']': |
43 | | -if(!escaping){ |
44 | | -if(inCharClass) |
45 | | -inCharClass=false; |
46 | | -}else{ |
47 | | -escaping=false; |
48 | | -} |
49 | | -break; |
50 | | -case'\\': |
51 | | -escaping=!escaping; |
52 | | -break; |
53 | | -case'.': |
54 | | -if(!escaping){ |
55 | | -if(!inCharClass&& |
| 28 | +functioncheckRegExp(nodes){ |
| 29 | +letescaping=false; |
| 30 | +letinCharClass=false; |
| 31 | +for(letn=0;n<nodes.length;++n){ |
| 32 | +constpair=nodes[n]; |
| 33 | +constnode=pair[0]; |
| 34 | +conststr=pair[1]; |
| 35 | +for(leti=0;i<str.length;++i){ |
| 36 | +switch(str[i]){ |
| 37 | +case'[': |
| 38 | +if(!escaping) |
| 39 | +inCharClass=true; |
| 40 | +else |
| 41 | +escaping=false; |
| 42 | +break; |
| 43 | +case']': |
| 44 | +if(!escaping){ |
| 45 | +if(inCharClass) |
| 46 | +inCharClass=false; |
| 47 | +}else{ |
| 48 | +escaping=false; |
| 49 | +} |
| 50 | +break; |
| 51 | +case'\\': |
| 52 | +escaping=!escaping; |
| 53 | +break; |
| 54 | +case'.': |
| 55 | +if(!escaping){ |
| 56 | +if(!inCharClass&& |
56 | 57 | ((i+1)===str.length|| |
57 | 58 | allowedModifiers.indexOf(str[i+1])===-1)){ |
58 | | -report(node,i); |
| 59 | +report(node,i); |
| 60 | +} |
| 61 | +}else{ |
| 62 | +escaping=false; |
59 | 63 | } |
60 | | -}else{ |
61 | | -escaping=false; |
62 | | -} |
63 | | -break; |
64 | | -default: |
65 | | -if(escaping) |
66 | | -escaping=false; |
| 64 | +break; |
| 65 | +default: |
| 66 | +if(escaping) |
| 67 | +escaping=false; |
| 68 | +} |
67 | 69 | } |
68 | 70 | } |
69 | 71 | } |
70 | | -} |
71 | 72 |
|
72 | | -functioncheckRegExpStart(node){ |
73 | | -if(node.callee&&node.callee.name==='RegExp'){ |
74 | | -if(inRegExp){ |
75 | | -regexpStack.push(regexpBuffer); |
76 | | -regexpBuffer=[]; |
| 73 | +functioncheckRegExpStart(node){ |
| 74 | +if(node.callee&&node.callee.name==='RegExp'){ |
| 75 | +if(inRegExp){ |
| 76 | +regexpStack.push(regexpBuffer); |
| 77 | +regexpBuffer=[]; |
| 78 | +} |
| 79 | +inRegExp=true; |
77 | 80 | } |
78 | | -inRegExp=true; |
79 | 81 | } |
80 | | -} |
81 | 82 |
|
82 | | -functioncheckRegExpEnd(node){ |
83 | | -if(node.callee&&node.callee.name==='RegExp'){ |
84 | | -checkRegExp(regexpBuffer); |
85 | | -if(regexpStack.length){ |
86 | | -regexpBuffer=regexpStack.pop(); |
87 | | -}else{ |
88 | | -inRegExp=false; |
89 | | -regexpBuffer=[]; |
| 83 | +functioncheckRegExpEnd(node){ |
| 84 | +if(node.callee&&node.callee.name==='RegExp'){ |
| 85 | +checkRegExp(regexpBuffer); |
| 86 | +if(regexpStack.length){ |
| 87 | +regexpBuffer=regexpStack.pop(); |
| 88 | +}else{ |
| 89 | +inRegExp=false; |
| 90 | +regexpBuffer=[]; |
| 91 | +} |
90 | 92 | } |
91 | 93 | } |
92 | | -} |
93 | 94 |
|
94 | | -functioncheckLiteral(node){ |
95 | | -constisTemplate=(node.type==='TemplateLiteral'&&node.quasis&& |
| 95 | +functioncheckLiteral(node){ |
| 96 | +constisTemplate=(node.type==='TemplateLiteral'&&node.quasis&& |
96 | 97 | node.quasis.length); |
97 | | -if(inRegExp&& |
| 98 | +if(inRegExp&& |
98 | 99 | (isTemplate||(typeofnode.value==='string'&&node.value.length))){ |
99 | | -letp=node.parent; |
100 | | -while(p&&p.type==='BinaryExpression'){ |
101 | | -p=p.parent; |
102 | | -} |
103 | | -if(p&&(p.type==='NewExpression'||p.type==='CallExpression')&& |
| 100 | +letp=node.parent; |
| 101 | +while(p&&p.type==='BinaryExpression'){ |
| 102 | +p=p.parent; |
| 103 | +} |
| 104 | +if(p&&(p.type==='NewExpression'||p.type==='CallExpression')&& |
104 | 105 | p.callee&&p.callee.type==='Identifier'&& |
105 | 106 | p.callee.name==='RegExp'){ |
106 | | -if(isTemplate){ |
107 | | -constquasis=node.quasis; |
108 | | -for(leti=0;i<quasis.length;++i){ |
109 | | -constel=quasis[i]; |
110 | | -if(el.type==='TemplateElement'&&el.value&&el.value.cooked) |
111 | | -regexpBuffer.push([el,el.value.cooked]); |
| 107 | +if(isTemplate){ |
| 108 | +constquasis=node.quasis; |
| 109 | +for(leti=0;i<quasis.length;++i){ |
| 110 | +constel=quasis[i]; |
| 111 | +if(el.type==='TemplateElement'&&el.value&&el.value.cooked) |
| 112 | +regexpBuffer.push([el,el.value.cooked]); |
| 113 | +} |
| 114 | +}else{ |
| 115 | +regexpBuffer.push([node,node.value]); |
112 | 116 | } |
113 | | -}else{ |
114 | | -regexpBuffer.push([node,node.value]); |
115 | 117 | } |
| 118 | +}elseif(node.regex){ |
| 119 | +checkRegExp([[node,node.regex.pattern]]); |
116 | 120 | } |
117 | | -}elseif(node.regex){ |
118 | | -checkRegExp([[node,node.regex.pattern]]); |
119 | 121 | } |
120 | | -} |
121 | 122 |
|
122 | | -return{ |
123 | | -'TemplateLiteral': checkLiteral, |
124 | | -'Literal': checkLiteral, |
125 | | -'CallExpression': checkRegExpStart, |
126 | | -'NewExpression': checkRegExpStart, |
127 | | -'CallExpression:exit': checkRegExpEnd, |
128 | | -'NewExpression:exit': checkRegExpEnd |
129 | | -}; |
| 123 | + |
| 124 | +return{ |
| 125 | +'TemplateLiteral': checkLiteral, |
| 126 | +'Literal': checkLiteral, |
| 127 | +'CallExpression': checkRegExpStart, |
| 128 | +'NewExpression': checkRegExpStart, |
| 129 | +'CallExpression:exit': checkRegExpEnd, |
| 130 | +'NewExpression:exit': checkRegExpEnd |
| 131 | +}; |
| 132 | +} |
130 | 133 | }; |
0 commit comments