注意:我已经检查了 StackOverflow 上的各种答案,但它们主要关注 ES6 导入根本不起作用或 ReactJS 特定问题。这个问题专门询问与项目相关的导入。
我有一个包含以下内容的文件:
import { AUTH_COOKIE } from "./use-auth";
Run Code Online (Sandbox Code Playgroud)
当我用 eslint 检查时一切正常。
$ npx eslint lib/file2.js
/home/mike/Code/myapp/frontend/lib/file2.js
(no errors)
Run Code Online (Sandbox Code Playgroud)
但是我想使用项目相关的导入,例如:
import { AUTH_COOKIE } from "lib/use-auth";
Run Code Online (Sandbox Code Playgroud)
这会导致错误:
$ npx eslint lib/file1.js
/home/mike/Code/myapp/frontend/lib/file1.js
1:29 error Unable to resolve path to module 'lib/use-auth' import/no-unresolved
Run Code Online (Sandbox Code Playgroud)
不过,Webpack 仍然可以正确构建项目 - 这只是一个 eslint 错误。
我已检查了import/no-unresolved 的文档,但看不到指定项目根目录的设置。
.ts
文件还具有与项目相关的工作导入如何使用 eslint 'import/no-unresolved' 进行项目相对导入?
我的 。eslintrc
在下面。
// Note eslintrc does not use booleans to turn features on/off! …
Run Code Online (Sandbox Code Playgroud) 当我尝试在我正在处理的一个存储库上运行 ESLint 时,出现错误。我将问题归结为,出于某种原因,ESLint 会在一些特定文件上引发错误,因为它们包含枚举。我相信存储库中的其他文件也包含枚举,但由于某种原因,只有这些文件会导致 linter 崩溃。当我注释掉枚举时,lint 运行得很好。我已经研究了一下这个问题,但我不确定解决方案是什么。该.eslintrc
文件有no-unused-vars
和@typescript-eslint/no-unused-vars
,我读过这是一个问题,但我已经搞乱了单独启用和禁用它们中的每一个,但无济于事。将它们中的任何一个设置为活动状态都会产生相同的错误,而 linter 运行的唯一方法是禁用两者。
这是运行 lint 时抛出的错误:
AssertionError [ERR_ASSERTION]: Node must be provided when reporting error if location is not provided
at assertValidNodeInfo (/home/simon/code/janus-api/node_modules/eslint/lib/linter/report-translator.js:98:9)
at /home/simon/code/janus-api/node_modules/eslint/lib/linter/report-translator.js:311:9
at Object.report (/home/simon/code/janus-api/node_modules/eslint/lib/linter/linter.js:920:41)
at Program:exit (/home/simon/code/janus-api/node_modules/eslint/lib/rules/no-unused-vars.js:622:33)
at /home/simon/code/janus-api/node_modules/eslint/lib/linter/safe-emitter.js:45:58
at Array.forEach (<anonymous>)
at Object.emit (/home/simon/code/janus-api/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
at NodeEventGenerator.applySelector (/home/simon/code/janus-api/node_modules/eslint/lib/linter/node-event-generator.js:254:26)
at NodeEventGenerator.applySelectors (/home/simon/code/janus-api/node_modules/eslint/lib/linter/node-event-generator.js:283:22)
at NodeEventGenerator.leaveNode (/home/simon/code/janus-api/node_modules/eslint/lib/linter/node-event-generator.js:306:14)
Run Code Online (Sandbox Code Playgroud)
当运行时--debug
我收到此错误:
eslint:linter Line: 1 +0ms
eslint:linter Parser Options: {
ecmaFeatures: { globalReturn: false },
ecmaVersion: 11,
sourceType: 'module', …
Run Code Online (Sandbox Code Playgroud) error 'x' is not defined no-undef
从 typescript 3.8.2 升级到 typescript 4.4.2 后出现很多错误。其中一个例子是error 'JSX' is not defined no-undef
,我进行了更深入的研究,大部分是从declarations.d.ts
我如何解决它?
"typescript": "4.4.2"
"@typescript-eslint/eslint-plugin": "4.31.0",
"@typescript-eslint/parser": "4.31.0",
Run Code Online (Sandbox Code Playgroud) 我在使用命令npm run start时遇到错误:
错误:--config 中的 ESLint 配置无效:- 意外的顶级属性“compilerOptions”。
我无法找出错误的原因。我尝试添加root:true和我偶然发现的许多其他解决方案。
我的 package.json 文件:
{
"name": "discord-bot",
"version": "1.0.0",
"description": "",
"main": "dist/WebServer.js",
"scripts": {
"prebuild": "eslint . --ext .js,.jsx,.ts,.tsx -c tsconfig.json --fix",
"lint": "eslint . --ext .ts,.tsx",
"lint-and-fix": "eslint . --ext .ts,.tsx --fix",
"build": "tsc",
"prestart": "npm run build",
"start": "node .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Anup",
"license": "ISC",
"dependencies": {
"express": "^4.17.2"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.9",
"@typescript-eslint/eslint-plugin": …
Run Code Online (Sandbox Code Playgroud) 编辑:我没有意识到错误消息甚至不是来自ESLint,而是来自Visual Studio Code中的 [js] .我会删除这个问题,但它可能会帮助其他人误解ESLint的[js]输出.仍然很想知道这[js]
件事是什么样的过程.
我被告知要定义一个项目范围的.eslintrc.js文件,以确保团队的代码符合通用标准.
使用预设运行ESLint eslint:recommended
会给出以下错误消息
The property "parentNode" does not exist for the type "EventTarget".
Run Code Online (Sandbox Code Playgroud)
这是有问题的代码:
window.addEventListener('keydown', event => {
const currentTag = event.target.parentNode.tagName;
/* ... */
})
Run Code Online (Sandbox Code Playgroud)
我知道可能有event.target
s 不是 HTMLElement
s,但我应该如何编码 - 或配置ESLint - 所以这一行不再产生ESLint错误?只要ESLint报告任何错误,我们就计划让构建失败,因此必须解决这个问题.
导致类似错误的第二行代码:
document.activeElement.click();
Run Code Online (Sandbox Code Playgroud)
导致ESLint错误
The property "click" does not exist for the type "Element".
Run Code Online (Sandbox Code Playgroud)
第三个例子:
const rows = this.childNodes[1].querySelectorAll('tr');
Run Code Online (Sandbox Code Playgroud)
投
The property "querySelectorAll" does not exist for the type "Node & ChildNode". …
Run Code Online (Sandbox Code Playgroud) 我想关闭更漂亮的规则,在其中换行内联注释。我的ESLint规则no-inline-comments
已设置为关闭或警告,因此可以正常使用。事实证明,Prettier仍然想换行和内联评论:
我在VSCode中有一个设置,其中ESLint正在处理JS的漂亮语言,而Prettier扩展名正在处理所有其他语言。我也用了airbnb-base
。这是我相关的配置:
.eslintrc.json
:
{
"extends": ["airbnb-base", "plugin:prettier/recommended"],
"rules": {
"no-console": 0,
"no-plusplus": 0,
"no-inline-comments": "off",
"no-undef": "warn",
"no-use-before-define": "warn",
"no-restricted-syntax": [
"warn",
{
"selector": "ForOfStatement",
"message": "frowned upon using For...Of"
}
]
// "line-comment-position": ["warn", { "position": "above" }]
},
"env": {
"browser": true,
"webextensions": true
}
}
Run Code Online (Sandbox Code Playgroud)
VSCode settings.json
:
// all auto-save configs
"editor.formatOnSave": true,
// turn off for native beautifyjs
"[javascript]": {
"editor.formatOnSave": false
},
"eslint.autoFixOnSave": true,
"eslint.alwaysShowStatus": true,
"prettier.disableLanguages": ["js"],
"prettier.trailingComma": …
Run Code Online (Sandbox Code Playgroud) eslint visual-studio-code eslint-config-airbnb prettier eslintrc
我搜索了SO和Google。但是我找不到执行此规则的规则:
const object = {
methods:
{
}
}
Run Code Online (Sandbox Code Playgroud)
const object = {
methods: {
}
}
Run Code Online (Sandbox Code Playgroud)
它似乎也不是大括号风格的。
我希望no-restricted-imports
在我的代码库中强制执行特定导入的规则。但是,我需要为某些路径抛出错误并为其他路径抛出警告。看起来我不能只做:
'no-restricted-imports': [
'warn',
{
paths: [
{
name: 'd3',
message: 'd3 would be deprecated soon, please consider moving away from it'
}
]
},
'error',
{
paths: [
{
name: 'moment',
message: 'Use of moment is not allowed',
},
]
]
Run Code Online (Sandbox Code Playgroud)
实现这种行为的最佳方法是什么?
我已经尝试过no-restricted-imports
两次定义规则,一次用于错误,一次用于警告,但由于它是一个对象,因此被覆盖。
ESLint 具有来自所有环境的全局设置:
{
"env": {
"es6": true,
"es2017": true,
"es2020": true
}
}
Run Code Online (Sandbox Code Playgroud)
ESLint 全局变量是否来自所有环境es6
、es2017
、 以及es2020
累积或增量设置?
换句话说,是让es2020
支持受益于es6
和es2017
,还是必须单独启用每个 JS 语言版本支持?
我正在使用很棒的 typescript-eslint和 ESLint。
问题描述:TypeScript ESLint 解析器抱怨src/module.spec.ts
不是项目的一部分,这是正确的。我spec.ts
从 TypeScripttsconfig.json
文件中排除了所有文件,因为它们不需要被转译。
如何使src/module.spec.ts
未转译但仍针对 ESLint进行检查?
my-project\src\module.spec.ts 0:0 错误解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”。该文件与您的项目配置不匹配:src\module.spec.ts。该文件必须至少包含在提供的项目之一中
我的.eslint.json
(精简):
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"project": "tsconfig.json"
},
"plugins": [
"@typescript-eslint"
]
}
Run Code Online (Sandbox Code Playgroud)
我的tsconfig.json
:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"declaration": true,
"sourceMap": false,
"outDir": "./dist",
"rootDir": "./src",
"removeComments": true,
"strict": true,
"skipLibCheck": …
Run Code Online (Sandbox Code Playgroud) eslintrc ×10
eslint ×8
javascript ×4
typescript ×4
ecmascript-6 ×1
enums ×1
formatter ×1
json ×1
prettier ×1
tsconfig ×1
webpack ×1