如何解决Javascript 中的 Prettier 强制变量定义到下一行
我正在使用 typescript、prettier 和 eslint,我正在尝试定义一个多行字符串变量,如下所示:
const text = 'Some really long interesting text' +
' some more really really long text' +
' and some even more really long text';
然而,当我使用 fix 选项运行 eslint 时,prettier 迫使我的代码看起来像这样。
const text =
'Some really long interesting text' +
' some more really long interesting text' +
' and some even more really long text';
我希望将 +
运算符放在文本之前,但我明白,由于它的意见,更漂亮不会让我这样做。美好的。但我不明白从下一行开始的变量定义。我以前从未见过变量定义从下一行开始。
谁能帮我回答以下任何一个问题:
- 这是从哪里来的?我以前从未见过这个标准,Prettier 是如何决定采用它的?关于此的文档相当乏味。
- 无论如何我可以覆盖它吗?
这是我的 .eslintrc.js:
module.exports = {
parser: '@typescript-eslint/parser',parserOptions: {
ecmaVersion: 2020,// Allows for the parsing of modern ECMAScript features
sourceType: 'module',// Allows for the use of imports
},plugins: ['@typescript-eslint'],extends: [
'plugin:@typescript-eslint/recommended',// Uses the recommended rules from the @typescript-eslint/eslint-plugin
'plugin:prettier/recommended','prettier/@typescript-eslint',],rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
// e.g. "@typescript-eslint/explicit-function-return-type": "off",'@typescript-eslint/ban-ts-comment': 'off'
},};
这是我的 .prettierrc.js:
module.exports = {
semi: true,trailingComma: "es5",singleQuote: true,printWidth: 120,tabWidth: 4
};
谢谢!
解决方法
您可能无法更改这一点,因为更漂亮的插件禁用了所有与 eslint 冲突的规则。
您可能想试试 prettier-eslint。它首先通过 prettier 格式化代码,然后传递给 eslint。
这会通过 prettier 格式化您的代码,然后将其结果传递给 eslint --fix。这样既可以享受到 prettier 优越的格式化能力的好处,又可以受益于 eslint 的配置能力。
添加 eslint operator-linebreak 规则和 a
应该完成工作。
规则
--fix
这是从哪里来的?我以前从未见过这个标准,Prettier 是如何决定采用它的?关于此的文档相当暗淡。
这不是标准。这是 Prettier 采用的一种风格。 javascript 中接受换行符。换行符被忽略直到
- 当下一行以破坏当前行的代码开头时(代码可以在多行上产生)
- 当下一行以}开头时,关闭当前块
- 到达源代码文件末尾时
- 当它自己的一行有返回语句时
- 当它自己的一行有一个 break 语句时
- 当在它自己的行上有一个 throw 语句时
- 当在它自己的行上有一个 continue 语句时
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。