我有一个 Javascript 浏览器项目分为多个文件,并且无法让 ESLint 在同一全局范围下对 HTML 文件的脚本标记进行 lint 处理,以便一个文件中的类和函数的声明和调用可以在另一个文件中识别。
这是我的项目结构:
这是食物的内容是:
sayHello();
和 bar.js:
function sayHello() {
console.log('Hello');
}
我已将它们链接到 HTML 文件中:
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="main.css">
<script src="libraries/bar.js" type="text/javascript"></script>
<script src="foo.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>
我认为解决这个问题的方法是使用 eslint-plugin-html 包,但尝试配置它但没有成功。这些是我在项目本地安装的软件包:
Alexs-MacBook-Pro:Demo alexmcdermott$ npm list --depth=0
[email protected] /cdn-cgi/l/email-protection /Users/alexmcdermott/GitHub/Demo
├── [email protected] /cdn-cgi/l/email-protection
├── [email protected] /cdn-cgi/l/email-protection
├── [email protected] /cdn-cgi/l/email-protection
└── [email protected] /cdn-cgi/l/email-protection
我正在使用 VSCode 编辑器,但在终端中也遇到了同样的问题:
Alexs-MacBook-Pro:Demo alexmcdermott$ npx eslint --ext .js,.html .
/Users/alexmcdermott/GitHub/Demo/foo.js
1:1 error 'sayHello' is not defined no-undef
/Users/alexmcdermott/GitHub/Demo/libraries/bar.js
1:10 error 'sayHello' is defined but never used no-unused-vars
2:3 warning Unexpected console statement no-console
✖ 3 problems (2 errors, 1 warning)
在 VSCode 中也是如此:
这是我的 ESLint 配置:
{
"env": {
"browser": true,
"es6": true
},
"extends": "airbnb-base",
"plugins": [ "html" ]
}
我还在 VSCode 用户设置中使用以下选项将 VSCode 配置为在 HTML 文件上运行 ESLint:
{
"eslint.autoFixOnSave": true,
"eslint.options": {
"extensions": [ ".js", ".html" ]
},
"eslint.validate": [
"javascript",
{
"language": "html",
"autoFix": true
}
]
}
虽然我一定做错了什么,或者错过了 eslint-plugin-html 的要点?如果有人对我做错了什么或如何解决这个问题有任何意见,我将非常感激,因为我多年来一直坚持这个问题。如果我需要提供更多信息,请告诉我,我对此很陌生。