-
Notifications
You must be signed in to change notification settings - Fork 43
Add no eval rule #41
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
mskelton
merged 20 commits into
playwright-community:master
from
elaichenkov:add-no-eval-rule
Apr 12, 2022
Merged
Add no eval rule #41
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
83ca91d
rule: add new noElementHandle rule
elaichenkov 09acdee
chore: move end range to const
elaichenkov bf274cc
fix: getRange method for non-awaiting statements
elaichenkov 6ba89e1
test: add additional tests for the noElementHandle rule
elaichenkov e151e3c
chore: improve noElementHandle rule
elaichenkov fc8c9b3
docs: update the wording of noElementHandle rule
elaichenkov 3e8c4e8
rule: update message to match with other rule naming conventions
elaichenkov 54725f3
rule: change noElementHandle to warn level
elaichenkov bad4c71
rule: noElementHandle change fixable to suggestion
elaichenkov 4890c75
test: add valid tests for noElementHandle rule
elaichenkov 7bb335b
rule: change type for noElementHandle to suggestion
elaichenkov 03f016e
rule: update noElementHandle rule's message for suggestions
elaichenkov c776eed
rule: update noElementHandle with getRange method
elaichenkov 1651e94
Merge branch 'playwright-community:master' into no-element-handle-rule
elaichenkov bee9294
rule: add new noEval rule
elaichenkov 9c5b8cc
utils: add ast helpers
elaichenkov a240ec7
chore: add newline at end of file
elaichenkov cedf204
chore: change double quote to single
elaichenkov 44c6dca
chore: add link for the noEval rule
elaichenkov 46214cd
utils: add isCalleeProperty function
elaichenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { isPageIdentifier, isEvalIdentifier, isEvalAllIdentifier } = require('../utils/ast'); | ||
|
||
module.exports = { | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (isPageIdentifier(node) && (isEvalIdentifier(node) || isEvalAllIdentifier(node))) { | ||
context.report({ messageId: isEvalIdentifier(node) ? 'noEval' : 'noEvalAll', node }); | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Possible Errors', | ||
description: | ||
'The use of `page.$eval` and `page.$$eval` are discouraged, use `locator.evaluate` or `locator.evaluateAll` instead', | ||
recommended: true, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright#no-eval', | ||
}, | ||
messages: { | ||
noEval: 'Unexpected use of page.$eval().', | ||
noEvalAll: 'Unexpected use of page.$$eval().', | ||
}, | ||
type: 'problem', | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
function isPageIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.type === 'MemberExpression' && | ||
callee.object.type === 'Identifier' && | ||
callee.object.name === 'page' | ||
); | ||
} | ||
|
||
function isEvalIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === '$eval' | ||
); | ||
} | ||
|
||
function isEvalAllIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === '$$eval' | ||
); | ||
} | ||
|
||
function isElementHandleIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === '$' | ||
); | ||
} | ||
|
||
function isElementHandlesIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === '$$' | ||
); | ||
} | ||
|
||
function isPauseIdentifier({ callee }) { | ||
return ( | ||
callee && | ||
callee.property && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === 'pause' | ||
); | ||
} | ||
|
||
module.exports = { | ||
isPageIdentifier, | ||
isElementHandleIdentifier, | ||
isElementHandlesIdentifier, | ||
isEvalIdentifier, | ||
isEvalAllIdentifier, | ||
isPauseIdentifier, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const { RuleTester } = require('eslint'); | ||
const rule = require('../lib/rules/no-eval'); | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
}); | ||
|
||
const wrapInTest = (input) => `test('verify noEval rule', async () => { ${input} })`; | ||
|
||
const invalid = (code) => ({ | ||
code: wrapInTest(code), | ||
errors: [{ messageId: code.includes('page.$eval') ? 'noEval' : 'noEvalAll' }], | ||
}); | ||
|
||
const valid = (code) => ({ | ||
code: wrapInTest(code), | ||
}); | ||
|
||
new RuleTester().run('no-eval', rule, { | ||
invalid: [ | ||
// $eval with no arguments as const | ||
invalid('const searchValue = await page.$eval("#search", el => el.value);'), | ||
|
||
// $eval | ||
invalid('await page.$eval("#search", el => el.value);'), | ||
|
||
// $$eval | ||
invalid('await page.$$eval("#search", el => el.value);'), | ||
|
||
// $eval with arguments as function | ||
invalid('const html = await page.$eval(".main-container", (e, suffix) => e.outerHTML + suffix, "hello");'), | ||
|
||
// $$eval with no arguments as const | ||
invalid('const divCounts = await page.$$eval("div", (divs, min) => divs.length >= min, 10);'), | ||
], | ||
valid: [ | ||
// locator evaluate | ||
valid('await page.locator(".tweet").evaluate(node => node.innerText)'), | ||
|
||
// element handle evaluate | ||
valid('await (await page.$(".tweet")).$eval(".like", node => node.innerText)'), | ||
|
||
// element handle evaluate all | ||
valid('await (await page.$(".tweet")).$$eval(".like", node => node.innerText)'), | ||
|
||
// locator evaluateAll | ||
valid('await page.locator("div").evaluateAll((divs, min) => divs.length >= min, 10);'), | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.