Skip to content

Commit ef5e251

Browse files
mskeltonCopilot
andauthored
Support addInitScript in no-unsafe-references (#369)
* no-unsafe-references for addInitScript * Update src/rules/no-unsafe-references.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a710bd7 commit ef5e251

File tree

4 files changed

+395
-44
lines changed

4 files changed

+395
-44
lines changed

docs/rules/no-unsafe-references.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
## Prevent unsafe variable references in `page.evaluate()` (`no-unsafe-references`)
1+
## Prevent unsafe variable references in `page.evaluate()` and `page.addInitScript()` (`no-unsafe-references`)
22

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

99
## Rule Details
1010

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

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

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

2426
const x = 7
2527
const y = 8
2628
await page.evaluate(([x, y]) => Promise.resolve(x * y), [x, y])
29+
await page.addInitScript(([x, y]) => Promise.resolve(x * y), [x, y])
2730
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"lint": "eslint .",
3636
"fmt": "prettier --write .",
3737
"fmt:check": "prettier --check .",
38-
"test": "vitest",
39-
"test:watch": "vitest --reporter=dot",
38+
"test": "vitest --run",
39+
"test:watch": "vitest --reporter=dot --run",
4040
"ts": "tsc --noEmit"
4141
},
4242
"peerDependencies": {

0 commit comments

Comments
 (0)