首页 > 解决方案 > 如何配置 ESLint 以捕获未声明的局部变量

问题描述

在下面的代码中,我希望 ESLint 给我一个 undeclared variable 的编译错误v1

有没有这样的选择?

<!DOCTYPE html> 
<html> 

<head> 
    <title>HTML Doctypes</title> 
    <script type="module">

    function f1() {
        v1=2//I want eslint to give me a 'compile-time' error for this line
    }
    if(performance.now == 12345){f1()}
    </script>
</head> 

<body> 
<p>HTML is easy to learn.</p> 
</body> 

</html> 

标签: javascripteslintstatic-analysis

解决方案


@ElAoutarHamza 我认为您的意思是 no-undef 而不是 no-unused-vars。这对我有用:

... 
<script type="module"> 
/*eslint no-var: "error"*/ 
/*eslint-env es6*/ 
/*eslint-env browser*/ 
/*eslint no-unused-vars: ["error", { "args": "all" }]*/ 
/*eslint no-undef: "error"*/
... 

这是输出:

sohrab@sohrab-HP-Notebook:~/proj/test$ eslint --ext .html testUseStrict.html
/home/sohrab/proj/test/testUseStrict.html 17:13 error 'v1' is not defined no-undef

推荐阅读