Skip to content

Support more page patterns #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export function getRawValue(node: ESTree.Node) {
return node.type === 'Literal' ? node.raw : undefined;
}

export function isIdentifier(node: ESTree.Node, name: string) {
return node.type === 'Identifier' && node.name === name;
export function isIdentifier(node: ESTree.Node, name: string | RegExp) {
return (
node.type === 'Identifier' &&
(typeof name === 'string' ? node.name === name : name.test(node.name))
);
}

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

/**
* Digs through a series of MemberExpressions and CallExpressions to find an
* Identifier with the given name.
*/
function dig(node: ESTree.Node, identifier: string | RegExp): boolean {
return node.type === 'MemberExpression'
? dig(node.property, identifier)
: node.type === 'CallExpression'
? dig(node.callee, identifier)
: node.type === 'Identifier'
? isIdentifier(node, identifier)
: false;
}

export function isPageMethod(node: ESTree.CallExpression, name: string) {
return (
node.callee.type === 'MemberExpression' &&
(node.callee.object.type === 'MemberExpression'
? isIdentifier(node.callee.object.property, 'page')
: isIdentifier(node.callee.object, 'page')) &&
dig(node.callee.object, /(^page|Page$)/) &&
isPropertyAccessor(node.callee, name)
);
}
2 changes: 1 addition & 1 deletion test/spec/no-element-handle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ runRuleTester('no-element-handle', rule, {
test('this.$("a");'),
test('this["$"]("a");'),
test('this[`$`]("a");'),
test('internalPage.$("a");'),
test('something.$("a");'),
test('this.page.$$$("div");'),
test('page.$$$("div");'),
],
Expand Down
74 changes: 74 additions & 0 deletions test/spec/no-wait-for-timeout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ runRuleTester('no-wait-for-timeout', rule, {
'(async function() { await page.waitForSelector("#foo"); })();',
'page.waitForSelector("#foo");',
'page["waitForSelector"]("#foo");',
'rampage.waitForTimeout(2000);',
'myPage2.waitForTimeout(2000);',
],
invalid: [
{
Expand Down Expand Up @@ -170,5 +172,77 @@ runRuleTester('no-wait-for-timeout', rule, {
},
],
},
{
code: 'foo.page().waitForTimeout(2000)',
errors: [
{
messageId,
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
line: 1,
column: 1,
endColumn: 32,
},
],
},
{
code: 'this.foo().page().waitForTimeout(2000)',
errors: [
{
messageId,
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
line: 1,
column: 1,
endColumn: 39,
},
],
},
{
code: 'page2.waitForTimeout(2000)',
errors: [
{
messageId,
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
line: 1,
column: 1,
endColumn: 27,
},
],
},
{
code: 'this.page2.waitForTimeout(2000)',
errors: [
{
messageId,
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
line: 1,
column: 1,
endColumn: 32,
},
],
},
{
code: 'myPage.waitForTimeout(2000)',
errors: [
{
messageId,
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
line: 1,
column: 1,
endColumn: 28,
},
],
},
{
code: 'this.myPage.waitForTimeout(2000)',
errors: [
{
messageId,
suggestions: [{ messageId: 'removeWaitForTimeout', output: '' }],
line: 1,
column: 1,
endColumn: 33,
},
],
},
],
});