【发布时间】:2019-03-14 15:23:24
【问题描述】:
我在保存 JSX 或 JS 文件时遇到自动缩进问题。当我保存文件时,它缩进不正确并导致 linting 错误。
这里是原始的js文件。
Question_4.js
import React from 'react';
import './Question_4.scss';
class Question_4 extends React.Component {
constructor(props) {
super(props);
}
handleClick = () => {
console.log('this is question 4 clicked:', this);
}
render() {
return (
<button className="button-redirect" onClick={this.handleClick}>
<div className="home-cta-image home-cta-image--neighborhood"></div>
</button>
);
}
}
export default Question_4;
然后当我保存时 (ctrl+s)。它显示在渲染函数中不正确的缩进下方。
import React from 'react';
import './Question_4.scss';
class Question_4 extends React.Component {
constructor(props) {
super(props);
}
handleClick = () => {
console.log('this is question 4 clicked:', this);
}
render() {
return ( <
button className = "button-redirect"
onClick = { this.handleClick } >
<
div className = "home-cta-image home-cta-image--neighborhood" > < /div> <
/button>
);
}
}
export default Question_4;
我不确定我的 eslint 文件或其他地方导致问题的原因。
.eslintrc.json 文件
{
"extends": [
"airbnb"
],
"parser": "babel-eslint",
"env": {
"browser": true
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"react/prefer-stateless-function": 0,
"react/jsx-indent": ["error", 4],
"react/jsx-indent-props": ["error", 4],
"react/jsx-no-undef": ["2", { "allowGlobals": true }],
"indent": ["error", 4],
"comma-dangle": 0,
"no-tabs": 0,
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
"function-paren-newline": 0,
"react/jsx-filename-extension": [1, { "extensions": [".js", ".jsx"] }]
}
}
【问题讨论】:
-
您可以在不复制/粘贴的情况下共享实际文件(通过在要点上发布或上传到某处)您使用的是哪个编辑器?有没有试过把规则一一去掉,测试一下?
-
你能在你的 package.json 文件中分享与 eslint 和 prettier(如果你正在使用的话)相关的包版本吗? (如 eslint、eslint-config-airbnb、eslint-config-prettier)
-
感谢您的建议,当然,文件的要点位于下面的链接中。我正在使用 VS 代码。我删除了 .eslintrc.json 中的所有规则,这似乎没有任何效果。我还更改了 VS Code Workspace 设置中的设置(autofixonsave: true -> false),也没有效果。 gist.github.com/eliaahadi/24daca14bbbe38792f3de5d08d0e8a4d
标签: javascript reactjs jsx eslint eslintrc