【问题标题】:VS Code not indenting to 4 spaces but only for a single fileVS Code 不缩进 4 个空格,但仅用于单个文件
【发布时间】:2020-06-02 21:46:49
【问题描述】:

我有一个使用 VS Code 编辑的 Typescript 项目。 VS Code 在其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,
  },
};

...还有一个.prettierrc:

{
  "singleQuote": true,
  "trailingComma": "all"
  "tabWidth": 4,
}

我的项目主要由.ts Typescript 扩展文件组成。现在,总而言之,这一切都很好。我可以更改我的代码,保存后,VS Code 会自动格式化代码。

但是,我特别有一个文件拒绝自动格式化为 4 格制表符并坚持使用 2 格制表符。

app.controller.ts

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) { }

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

首先,出于某种原因,保存在 VS Code 中不会对该文件进行格式更改。我试过运行npm run lint,它确实捕获了缩进错误。

然后,运行 npm run lint --fix 会导致:

@Controller()
export class AppController {
    constructor(private readonly appService: AppService) { }

  @Get()
    getHello(): string {
        return this.appService.getHello();
    }
}

如您所知,constructorgetHello 函数缩进正确,但装饰器没有缩进。此外,进入文件并进行编辑,我仍然得到一个标签大小为 2。

如何让这个文件遵守 4 的制表符大小?

提醒一下,这适用于除此文件之外的所有其他文件。

【问题讨论】:

    标签: node.js typescript eslint lint


    【解决方案1】:

    尝试在编辑器中强制标签宽度:

    settings.json

    "[typescript]": {
        "editor.tabSize": 4,
        "editor.detectIndentation": false,
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 2018-04-20
      • 2018-07-31
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      相关资源
      最近更新 更多