Skip to content

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
merged 20 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
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 Apr 10, 2022
09acdee
chore: move end range to const
elaichenkov Apr 10, 2022
bf274cc
fix: getRange method for non-awaiting statements
elaichenkov Apr 10, 2022
6ba89e1
test: add additional tests for the noElementHandle rule
elaichenkov Apr 10, 2022
e151e3c
chore: improve noElementHandle rule
elaichenkov Apr 10, 2022
fc8c9b3
docs: update the wording of noElementHandle rule
elaichenkov Apr 11, 2022
3e8c4e8
rule: update message to match with other rule naming conventions
elaichenkov Apr 11, 2022
54725f3
rule: change noElementHandle to warn level
elaichenkov Apr 11, 2022
bad4c71
rule: noElementHandle change fixable to suggestion
elaichenkov Apr 11, 2022
4890c75
test: add valid tests for noElementHandle rule
elaichenkov Apr 11, 2022
7bb335b
rule: change type for noElementHandle to suggestion
elaichenkov Apr 11, 2022
03f016e
rule: update noElementHandle rule's message for suggestions
elaichenkov Apr 11, 2022
c776eed
rule: update noElementHandle with getRange method
elaichenkov Apr 11, 2022
1651e94
Merge branch 'playwright-community:master' into no-element-handle-rule
elaichenkov Apr 11, 2022
bee9294
rule: add new noEval rule
elaichenkov Apr 11, 2022
9c5b8cc
utils: add ast helpers
elaichenkov Apr 11, 2022
a240ec7
chore: add newline at end of file
elaichenkov Apr 11, 2022
cedf204
chore: change double quote to single
elaichenkov Apr 11, 2022
44c6dca
chore: add link for the noEval rule
elaichenkov Apr 11, 2022
46214cd
utils: add isCalleeProperty function
elaichenkov Apr 12, 2022
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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,27 @@ Example of **correct** code for this rule:
const buttonLocator = page.locator('button');
await buttonLocator.click();
```

### `no-eval`

Disallow usage of `page.$eval` and `page.$$eval`.

Examples of **incorrect** code for this rule:

```js
const searchValue = await page.$eval('#search', el => el.value);

const divCounts = await page.$$eval('div', (divs, min) => divs.length >= min, 10);

await page.$eval('#search', el => el.value);

await page.$$eval('#search', el => el.value);
```

Example of **correct** code for this rule:

```js
await page.locator('button').evaluate(node => node.innerText);

await page.locator('div').evaluateAll((divs, min) => divs.length >= min, 10);
```
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const missingPlaywrightAwait = require("./rules/missing-playwright-await");
const noPagePause = require("./rules/no-page-pause");
const noElementHandle = require("./rules/no-element-handle");
const noEval = require("./rules/no-eval");

module.exports = {
configs: {
Expand All @@ -14,6 +15,7 @@ module.exports = {
"playwright/missing-playwright-await": "error",
"playwright/no-page-pause": "warn",
"playwright/no-element-handle": "warn",
"playwright/no-eval": "warn",
},
},
"jest-playwright": {
Expand Down Expand Up @@ -52,6 +54,7 @@ module.exports = {
rules: {
"missing-playwright-await": missingPlaywrightAwait,
"no-page-pause": noPagePause,
"no-element-handle": noElementHandle
"no-element-handle": noElementHandle,
"no-eval": noEval,
},
};
27 changes: 1 addition & 26 deletions lib/rules/no-element-handle.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,4 @@
function isPageIdentifier({ callee }) {
return (
callee &&
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'page'
);
}

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 === '$$'
);
}
const { isPageIdentifier, isElementHandleIdentifier, isElementHandlesIdentifier } = require('../utils/ast');

function getRange(node) {
const start = node.parent && node.parent.type === 'AwaitExpression'
Expand Down
27 changes: 27 additions & 0 deletions lib/rules/no-eval.js
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',
},
};
6 changes: 4 additions & 2 deletions lib/rules/no-page-pause.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { isPageIdentifier, isPauseIdentifier } = require("../utils/ast");

module.exports = {
create(context) {
return {
MemberExpression(node) {
if (node.object.name === "page" && node.property.name === "pause") {
CallExpression(node) {
if (isPageIdentifier(node) && isPauseIdentifier(node)) {
context.report({ messageId: "noPagePause", node });
}
},
Expand Down
62 changes: 62 additions & 0 deletions lib/utils/ast.js
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,
};
51 changes: 51 additions & 0 deletions test/no-eval.spec.js
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);'),
],
});