我有一个打字稿项目,我正在编辑与VS代码。VS代码的settings.json中有以下设置
{
"window.zoomLevel": 0,
"extensions.ignoreRecommendations": false,
"editor.formatOnSave": true,
"javascript.updateImportsOnFileMove.enabled": "always",
"files.autoSave": "off",
"workbench.iconTheme": "material-icon-theme"
"editor.tabSize" : 4
}此外,该项目使用ESLint,.eslintrc.js:
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
"prefer-const": 0,
"no-use-before-define": 0,
"no-useless-escape": 0,
"no-lone-blocks": 0,
"no-empty-pattern": 0,
"no-sequences": 0,
"no-path-concat": 0,
"no-extend-native": 0,
"import/no-duplicates": 0,
"no-unexpected-multiline": 0,
"camelcase": 0,
"func-call-spacing": [2, "never"],
"space-before-function-paren": [2, { "anonymous": "always", "named": "never", "asyncArrow": "always" }],
"no-array-constructor": 0,
"no-mixed-operators": 0,
"no-prototype-builtins": 0,
"brace-style": [2, "1tbs"],
"block-spacing": 0,
"indent": [2, 4, {
"SwitchCase": 1,
"VariableDeclarator": 1,
"outerIIFEBody": 1,
"MemberExpression": 1,
"FunctionDeclaration": { "parameters": 1, "body": 1 },
"FunctionExpression": { "parameters": 1, "body": 1 },
"CallExpression": { "arguments": 1 },
"ArrayExpression": 1,
"ObjectExpression": 1,
"ImportDeclaration": 1,
"flatTernaryExpressions": false,
"ignoreComments": false
}],
"eqeqeq": 0,
"semi": 0,
"prefer-promise-reject-errors": 0,
"no-undef": 0,
"no-return-assign": 0,
"no-unused-vars": 0,
"no-unused-expressions": 0,
},
};...and a .prettierrc
{
"singleQuote": true,
"trailingComma": "all"
"tabWidth": 4,
}我的项目主要由.ts类型记录扩展名文件组成。现在,所有这些都很好。我可以更改我的代码,并在保存时,VS代码将自动格式化代码相应.
然而,我有一个文件,特别是拒绝自动格式化为4空间选项卡,并坚持使用2空间选项卡。
app.controller.ts
@Controller()
export class AppController {
constructor(private readonly appService: AppService) { }
@Get()
getHello(): string {
return this.appService.getHello();
}
}首先,保存在VS代码中不会因为某种原因对此文件进行格式化更改。我尝试过运行npm run lint,它确实捕获了缩进错误。
然后,运行npm run lint --fix将得到以下结果:
@Controller()
export class AppController {
constructor(private readonly appService: AppService) { }
@Get()
getHello(): string {
return this.appService.getHello();
}
}如您所知,constructor和getHello函数被适当缩进,但装饰器没有。此外,进入文件和编辑,我仍然有一个2的标签大小。
如何使该文件符合4的选项卡大小?
提醒您,除了这个文件之外,其他所有文件都可以使用这个文件。
发布于 2021-01-04 05:57:57
还可以尝试在编辑器中强制使用选项卡宽度:
settings.json
"[typescript]": {
"editor.tabSize": 4,
"editor.detectIndentation": false,
}https://stackoverflow.com/questions/62161901
复制相似问题