Skip to content
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
15 changes: 9 additions & 6 deletions docs/rules/no-unsafe-references.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## Prevent unsafe variable references in `page.evaluate()` (`no-unsafe-references`)
## Prevent unsafe variable references in `page.evaluate()` and `page.addInitScript()` (`no-unsafe-references`)

This rule prevents common mistakes when using `page.evaluate()` with variables
referenced from the parent scope. When referencing variables from the parent
scope with `page.evaluate()`, you must pass them as an argument so Playwright
can properly serialize and send them to the browser page where the function
being evaluated is executed.
This rule prevents common mistakes when using `page.evaluate()` or
`page.addInitScript()` with variables referenced from the parent scope. When
referencing variables from the parent scope with these methods, you must pass
them as an argument so Playwright can properly serialize and send them to the
browser page where the function being evaluated is executed.

## Rule Details

Expand All @@ -14,14 +14,17 @@ Example of **incorrect** code for this rule:
const x = 7
const y = 8
await page.evaluate(() => Promise.resolve(x * y), [])
await page.addInitScript(() => Promise.resolve(x * y), [])
```

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

```javascript
await page.evaluate(([x, y]) => Promise.resolve(x * y), [7, 8])
await page.addInitScript(([x, y]) => Promise.resolve(x * y), [7, 8])

const x = 7
const y = 8
await page.evaluate(([x, y]) => Promise.resolve(x * y), [x, y])
await page.addInitScript(([x, y]) => Promise.resolve(x * y), [x, y])
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"lint": "eslint .",
"fmt": "prettier --write .",
"fmt:check": "prettier --check .",
"test": "vitest",
"test:watch": "vitest --reporter=dot",
"test": "vitest --run",
"test:watch": "vitest --reporter=dot --run",
"ts": "tsc --noEmit"
},
"peerDependencies": {
Expand Down
Loading