Skip to content

Commit 5398e39

Browse files
authored
Support more page patterns (#132)
* Add tests * Dig until we find a match * Docs
1 parent 741e802 commit 5398e39

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

src/utils/ast.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ export function getRawValue(node: ESTree.Node) {
1818
return node.type === 'Literal' ? node.raw : undefined;
1919
}
2020

21-
export function isIdentifier(node: ESTree.Node, name: string) {
22-
return node.type === 'Identifier' && node.name === name;
21+
export function isIdentifier(node: ESTree.Node, name: string | RegExp) {
22+
return (
23+
node.type === 'Identifier' &&
24+
(typeof name === 'string' ? node.name === name : name.test(node.name))
25+
);
2326
}
2427

2528
function isLiteral<T>(node: ESTree.Node, type: string, value?: T) {
@@ -148,12 +151,24 @@ export function getMatchers(
148151
return chain;
149152
}
150153

154+
/**
155+
* Digs through a series of MemberExpressions and CallExpressions to find an
156+
* Identifier with the given name.
157+
*/
158+
function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
159+
return node.type === 'MemberExpression'
160+
? dig(node.property, identifier)
161+
: node.type === 'CallExpression'
162+
? dig(node.callee, identifier)
163+
: node.type === 'Identifier'
164+
? isIdentifier(node, identifier)
165+
: false;
166+
}
167+
151168
export function isPageMethod(node: ESTree.CallExpression, name: string) {
152169
return (
153170
node.callee.type === 'MemberExpression' &&
154-
(node.callee.object.type === 'MemberExpression'
155-
? isIdentifier(node.callee.object.property, 'page')
156-
: isIdentifier(node.callee.object, 'page')) &&
171+
dig(node.callee.object, /(^page|Page$)/) &&
157172
isPropertyAccessor(node.callee, name)
158173
);
159174
}

test/spec/no-element-handle.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ runRuleTester('no-element-handle', rule, {
1111
test('this.$("a");'),
1212
test('this["$"]("a");'),
1313
test('this[`$`]("a");'),
14-
test('internalPage.$("a");'),
14+
test('something.$("a");'),
1515
test('this.page.$$$("div");'),
1616
test('page.$$$("div");'),
1717
],

test/spec/no-wait-for-timeout.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ runRuleTester('no-wait-for-timeout', rule, {
1111
'(async function() { await page.waitForSelector("#foo"); })();',
1212
'page.waitForSelector("#foo");',
1313
'page["waitForSelector"]("#foo");',
14+
'rampage.waitForTimeout(2000);',
15+
'myPage2.waitForTimeout(2000);',
1416
],
1517
invalid: [
1618
{
@@ -170,5 +172,77 @@ runRuleTester('no-wait-for-timeout', rule, {
170172
},
171173
],
172174
},
175+
{
176+
code: 'foo.page().waitForTimeout(2000)',
177+
errors: [
178+
{
179+
messageId,
180+
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
181+
line: 1,
182+
column: 1,
183+
endColumn: 32,
184+
},
185+
],
186+
},
187+
{
188+
code: 'this.foo().page().waitForTimeout(2000)',
189+
errors: [
190+
{
191+
messageId,
192+
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
193+
line: 1,
194+
column: 1,
195+
endColumn: 39,
196+
},
197+
],
198+
},
199+
{
200+
code: 'page2.waitForTimeout(2000)',
201+
errors: [
202+
{
203+
messageId,
204+
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
205+
line: 1,
206+
column: 1,
207+
endColumn: 27,
208+
},
209+
],
210+
},
211+
{
212+
code: 'this.page2.waitForTimeout(2000)',
213+
errors: [
214+
{
215+
messageId,
216+
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
217+
line: 1,
218+
column: 1,
219+
endColumn: 32,
220+
},
221+
],
222+
},
223+
{
224+
code: 'myPage.waitForTimeout(2000)',
225+
errors: [
226+
{
227+
messageId,
228+
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
229+
line: 1,
230+
column: 1,
231+
endColumn: 28,
232+
},
233+
],
234+
},
235+
{
236+
code: 'this.myPage.waitForTimeout(2000)',
237+
errors: [
238+
{
239+
messageId,
240+
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
241+
line: 1,
242+
column: 1,
243+
endColumn: 33,
244+
},
245+
],
246+
},
173247
],
174248
});

0 commit comments

Comments
 (0)