【发布时间】:2019-06-05 21:55:22
【问题描述】:
我有一个 Javascript 浏览器项目拆分为多个文件,并且无法让 ESLint 在同一全局范围内对我的 HTML 文件的脚本标签进行 lint,以便一个文件中的类和函数的声明和调用在另一个文件中被识别。
这是我的项目结构:
这是 foo.js 的内容:
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
demo@1.0.0 /Users/alexmcdermott/GitHub/Demo
├── eslint@5.12.0
├── eslint-config-airbnb-base@13.1.0
├── eslint-plugin-html@5.0.0
└── eslint-plugin-import@2.14.0
我正在使用 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 的重点?如果有人对我做错了什么或如何解决这个问题有任何意见,我将不胜感激,因为我多年来一直坚持这一点。如果我需要提供更多信息,请告诉我,我是新手。
【问题讨论】:
标签: javascript visual-studio-code eslint lint