TypeScript 类装饰器的格式不正确

jjhzyzn0  于 2个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(36)

microsoft/vscode#21723

TypeScript 版本: 2.2.1
代码

function Component(x) {
  return x
}

@Component
export class Foo {bar(): void {
return 2
}

}

设置格式化选项为两个空格并运行文档格式

预期行为:

类使用两个空格进行格式化,如下所示:

@Component
export class Foo {
  bar(): void {
    return 2
  }

}

实际行为:

类使用四个空格进行格式化:

@Component
export class Foo {
    bar(): void {
      return 2
    }

}

再次运行格式化后,返回正确的两个空格格式化

[Trace - 5:55:14 PM] Sending request: configure (12). Response expected: yes. Current queue length: 0
Arguments: {
    "file": "/Users/matb/projects/sand/x.ts",
    "formatOptions": {
        "tabSize": 2,
        "indentSize": 2,
        "convertTabsToSpaces": true,
        "newLineCharacter": "\n",
        "insertSpaceAfterCommaDelimiter": true,
        "insertSpaceAfterSemicolonInForStatements": true,
        "insertSpaceBeforeAndAfterBinaryOperators": true,
        "insertSpaceAfterKeywordsInControlFlowStatements": true,
        "insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,
        "insertSpaceBeforeFunctionParenthesis": false,
        "insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
        "insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets": false,
        "insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces": false,
        "insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces": false,
        "placeOpenBraceOnNewLineForFunctions": false,
        "placeOpenBraceOnNewLineForControlBlocks": false
    }
}
[Trace - 5:55:14 PM] Response received: configure (12). Request took 4 ms. Success: true 
[Trace - 5:55:14 PM] Sending request: format (13). Response expected: yes. Current queue length: 0
Arguments: {
    "file": "/Users/matb/projects/sand/x.ts",
    "line": 1,
    "offset": 1,
    "endLine": 10,
    "endOffset": 2
}
[Trace - 5:55:14 PM] Response received: format (13). Request took 36 ms. Success: true 
Result: [
    {
        "start": {
            "line": 1,
            "offset": 20
        },
        "end": {
            "line": 1,
            "offset": 20
        },
        "newText": " "
    },
    {
        "start": {
            "line": 1,
            "offset": 21
        },
        "end": {
            "line": 1,
            "offset": 21
        },
        "newText": " "
    },
    {
        "start": {
            "line": 6,
            "offset": 19
        },
        "end": {
            "line": 6,
            "offset": 19
        },
        "newText": "\n"
    },
    {
        "start": {
            "line": 6,
            "offset": 19
        },
        "end": {
            "line": 6,
            "offset": 19
        },
        "newText": "    "
    },
    {
        "start": {
            "line": 7,
            "offset": 1
        },
        "end": {
            "line": 7,
            "offset": 1
        },
        "newText": "      "
    },
    {
        "start": {
            "line": 8,
            "offset": 1
        },
        "end": {
            "line": 8,
            "offset": 1
        },
        "newText": "    "
    }
]
64jmpszr

64jmpszr1#

当格式化程序将ClassMember节点移动到新行时,它会错误地计算缩进。这就是为什么第一次格式化时缩进是错误的,然后第二次自动修复。这个问题只在格式化程序插入换行符时发生。

当子节点和父节点开始于不同的行时,假设父节点需要悬挂缩进,因此子节点会被双倍缩进。在这种情况下,ClassMember从第2行开始,ClassDeclaration(包括装饰器)从第1行开始。然而,你可以通过将类声明分成两行来重现编辑(不带装饰器)的问题:

export                   // ClassDeclaration starts on line 1
class Foo {bar(): void { // ClassMember starts on line 2

我假设格式化程序会双倍缩进 bar ,以便将其与class上的悬挂缩进区分开来。

// After formatting once
export
    class Foo {        // hanging indent
        bar(): void {  // children are double-indented in the presence of hanging indent

然而,当你再次格式化时,双倍缩进就被忘记了。

export
    class Foo {    // hanging indent
    bar(): void {  // children are now single-indented

格式化程序的预期行为是什么?当父节点跨多行开始时,子节点应该被双倍缩进吗?还是说子节点应该始终保持单倍缩进,而不受影响?

相关问题