我刚刚创建了新的 react-native 应用程序,eslint 改变了这一点:
return (
<Component />
);
Run Code Online (Sandbox Code Playgroud)
到
return <Component />;
Run Code Online (Sandbox Code Playgroud)
我试图更改 .eslintrc 和 .prettierrc 文件,但没有任何效果。
我不明白为什么我需要在两个不同的参数中指定相同的信息,
both env: { es6: true }
和parserOptions: { ecmaVersion: 6 }
。
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [
'eslint:recommended'
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: {
ecmaVersion: 6
},
};
Run Code Online (Sandbox Code Playgroud) 是否有任何 Eslint 规则用于验证仅允许定义.js
文件中的大写变量。
Eslint 有一个Require CamelCase (camelcase)
规则并用于"properties": "always"
验证驼峰命名法导入。我想知道是否有类似类型的规则,或者我可以创建一个来验证下面的示例。
cosntant.js
// valid
export const EDIT_FORM = "EDIT_FORM"
// invalid 'warning'
export const edit_form = "EDIT_FORM"
Run Code Online (Sandbox Code Playgroud) 给定一个项目,其节点package.json
安装了 eslint 和一些插件,我怎么知道在哪里设置了特定规则?
我看到正在应用一条规则 ( space-before-function-paren
),但.eslintrc
在项目的任何文件或插件文档中都找不到它。
此外,我正在使用带有一些扩展的 VSCode,例如 ESLint 本身,这可能会干扰这里,但我再次不确定如何/在哪里检查哪个部分正在应用该规则(尽管我认为这不太可能是发生,npm run lint
如果代码不符合上述规则,则失败。
我正在发布 package.json 文件的相关部分:
{
// ...
"scripts": {
"watch": "NODE_ENV=development node build/build.js --watch",
"build": "NODE_ENV=development node build/build.js",
"build:prod": "NODE_ENV=production node build/build.js",
"unit": "NODE_ENV=test jest --config test/unit/jest.conf.js --coverage",
"test": "npm run unit",
"lint": "eslint --ext .js,.vue src test/unit/specs",
"ci-lint": "eslint --ext .js,.vue src test/unit/specs --format checkstyle --output-file lint_out/unit_timeline.xml"
},
"devDependencies": {
"@vue/test-utils": "^1.0.0-beta.27",
"autoprefixer": "^8.2.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.2.2",
"babel-jest": "^22.4.3",
"babel-loader": …
Run Code Online (Sandbox Code Playgroud) 我正在使用它构建一个网站create-react-app
,并且刚刚安装了eslint
它。
由于某种原因,本应显示为 eslint 警告的内容却显示为错误并导致npm run start
失败。
我怎样才能绕过这个问题并像以前一样将它们显示为警告?
我的 .eslintrc.js
env: {
browser: true,
es6: true,
jest: true,
},
extends: [
'airbnb-typescript',
'plugin:@typescript-eslint/recommended',
'prettier/react',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json',
},
plugins: ['react', '@typescript-eslint'],
rules: {
'class-methods-use-this': 'off',
'additional-rule': 'warn',
},
ignorePatterns: ['**/node_modules/*', '**/build/*', 'config-overrides.js'],
};
Run Code Online (Sandbox Code Playgroud)
我的package.json
"name": "device-protection-renewal-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Eslint 设置构建一个 React 应用程序。在我的应用程序中,我使用 GraphQL @apollo/client 依赖项。
当我尝试做的时候import { setContext } from '@apollo/client/link/context'
我收到一个 eslint 错误
'@apollo/client/link/context' should be listed in the project's dependencies. Run 'npm i -S @apollo/client/link/context' to add it import/no-extraneous-dependencies
Run Code Online (Sandbox Code Playgroud)
我的 package.json 中确实有“@apollo/client”依赖项。根据 Apollo 文档,从 '@apollo/client/link/context' 导入是获取 'setContext' 的正确方法。
似乎import/no-extreaneous-dependencies
无法识别“@apollo/client”的嵌套路径。
当我设置"import/no-extraneous-dependencies": 0
.eslintrc.js 规则时,它会正常工作。但为了获得正确的解决方案,我假设我可能需要使用 .eslintrc.js 规则设置一些内容,而不是仅仅关闭 eslint 检查。
在这种情况下,我应该在 .eslintrc.js 中为我的规则编写哪些其他设置才能正确解决问题?
我的 package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"@apollo/client": "^3.3.19",
"@auth0/auth0-react": "^1.4.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10", …
Run Code Online (Sandbox Code Playgroud) eslint --fix
阻止 eslint(使用时)更改所需的规则和配置是什么:
return regex.test(foo) ? true : false
Run Code Online (Sandbox Code Playgroud)
进入这个:
return !!regex.test(foo)
Run Code Online (Sandbox Code Playgroud)
虽然我理解这条规则的作用,但我不喜欢它。我认为它可能是其中之一 - 然而,它要么不是,要么我只是无法理解如何正确配置它们。
return regex.test(foo) ? true : false
Run Code Online (Sandbox Code Playgroud) 我想了解有关 ESLinting 缓存的更多详细信息。
ESLinting 使用什么缓存机制?如果是的话,Windows和Linux上的缓存文件在哪里?
有没有办法在命令行上清理缓存?
React-TypeScript 项目有推荐的 ESLinting 设置吗?
我尝试在 javascript 中使用可选链接,但我的 eslint 规则导致错误。
错误:可选链的使用不安全。如果它与“未定义”短路,则评估将抛出 TypeError no-unsafe-optical-chaining
const { homeAddress, officeAddress} = Employee?.addresses;
Run Code Online (Sandbox Code Playgroud)
错误:可选链上的算术运算不安全。它可能会导致 NaN no-unsafe-Optional-chaining
const AddressesCount = homeAddress?.length + officeAddress?.length
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我不想违反规则
我试图弄清楚如何防止函数Public
在另一个文件中未使用时被创建。
我试图为此寻找规则,但我似乎找不到一个,最接近的是使用explicit-member-accessibility
.
到目前为止,我已经尝试通过这样做来解决它
"@typescript-eslint/explicit-member-accessibility": [
"warn",
{
"accessibility": "explicit",
"overrides": {
"accessors": "off",
"constructors": "no-public",
"methods": "explicit",
"properties": "off",
"parameterProperties": "off"
}
}
]
Run Code Online (Sandbox Code Playgroud)
但它的问题在于它要求您Public
在每个公共方法之前编写。这还需要在现有代码库中编辑大量代码。
如果可能的话,我希望 eslint 能够以这种方式格式化代码
class childClass extends baseClass {
//public method belonging to base class
//should be `public` since it is used in another file
baseClassMethod() {
//some code
}
//private method only accessible within the child class
//should be `private` since it isn't used outside the file
private childClassMethod() …
Run Code Online (Sandbox Code Playgroud) eslintrc ×10
eslint ×8
javascript ×4
reactjs ×3
typescript ×2
arrays ×1
ecmascript-6 ×1
graphql ×1
jsx ×1
node.js ×1
prettier ×1
react-native ×1
vue.js ×1