JavaScript
js/no-eval
Never run strings as code - no eval, new Function, or a string as the first argument of setTimeout or setInterval.
poważnastatycznefe-javascript
Dlaczego
A string run as code executes with the page's full rights: if any part of it comes from a URL, storage or a server response, it is cross-site scripting. It also forces a Content Security Policy to allow unsafe-eval and stops the engine from optimising the surrounding code.
Poprawka
Pass functions, not strings: setTimeout(save, 500). Parse data with JSON.parse, look values up in an object or Map instead of building property names as code, and use a template or the DOM for markup.
Przykłady
// bad
setTimeout('refresh()', 1000)
const total = eval(`${a} + ${b}`)
// good
setTimeout(refresh, 1000)
const total = a + bŹródła
Fakty
- Identyfikator
js/no-eval- Waga
- poważna
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
jsxhtmlvuesvelteastro- Uczy jej skill
fe-javascriptJavaScript in the browser- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain js/no-eval