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:
1414const x = 7
1515const y = 8
1616await page .evaluate (() => Promise .resolve (x * y), [])
17+ await page .addInitScript (() => Promise .resolve (x * y), [])
1718```
1819
1920Example of ** correct** code for this rule:
2021
2122``` javascript
2223await page .evaluate (([x , y ]) => Promise .resolve (x * y), [7 , 8 ])
24+ await page .addInitScript (([x , y ]) => Promise .resolve (x * y), [7 , 8 ])
2325
2426const x = 7
2527const y = 8
2628await page .evaluate (([x , y ]) => Promise .resolve (x * y), [x, y])
29+ await page .addInitScript (([x , y ]) => Promise .resolve (x * y), [x, y])
2730```
0 commit comments