Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript Union | not surrounded by spaces in type declaration #49858

Open
JakubGIT mannequin opened this issue May 27, 2021 · 4 comments
Open

TypeScript Union | not surrounded by spaces in type declaration #49858

JakubGIT mannequin opened this issue May 27, 2021 · 4 comments
Labels
awaiting-review Has pending Phabricator review bugzilla Issues migrated from bugzilla clang-format enhancement Improving things as opposed to bug fixing, e.g. new or missing feature javascript typescript

Comments

@JakubGIT
Copy link
Mannequin

JakubGIT mannequin commented May 27, 2021

Bugzilla Link 50514
Version 12.0
OS Windows NT
@JakubGIT
Copy link
Mannequin Author

JakubGIT mannequin commented May 27, 2021

function foo(x: A|B)
{
}

const sample = string | number;

my setting:

---
Language:        JavaScript
BasedOnStyle:    Chromium
ColumnLimit:     100
BreakBeforeBraces: Allman

AlignConsecutiveAssignments: 'true'
AlignConsecutiveMacros: 'true'
AlignConsecutiveDeclarations: 'true'
AlignOperands: 'true'
AlignTrailingComments: 'true'
AllowShortBlocksOnASingleLine: 'false'
AllowShortCaseLabelsOnASingleLine: 'false'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLambdasOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
BinPackArguments: 'false'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializers: BeforeColon
AlwaysBreakTemplateDeclarations: Yes
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
DerivePointerAlignment: 'false'
IncludeBlocks: Preserve
IndentCaseLabels: 'false'
IndentWidth: '4'
KeepEmptyLinesAtTheStartOfBlocks: 'true'
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: 'None'
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
SortIncludes: 'true'
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpacesInAngles: false
SpacesInContainerLiterals: false
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInParentheses: 'false'
TabWidth: '4'
UseTab: Never

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@mydeveloperday
Copy link
Contributor

Can you explain what you are seeing and what you expected? A|B is correctly detecting the | as a JsTypeOperator

 M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=75 Name=identifier L=17 PPK=2 FakeLParens= FakeRParens=0 II=0x19e323d7650 Text='A'
 M=0 C=0 T=JsTypeOperator S=0 F=0 B=0 BK=0 P=46 Name=pipe L=18 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='|'
 M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=46 Name=identifier L=19 PPK=2 FakeLParens= FakeRParens=0 II=0x19e323d7680 Text='B'
 M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=r_paren L=20 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'

Its the string | number that's incorrectly seeing it as a BinaryOperator

 M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=const L=5 PPK=2 FakeLParens=2/ FakeRParens=0 II=0x19e323e08d8 Text='const'
 M=0 C=1 T=StartOfName S=1 F=0 B=0 BK=0 P=220 Name=identifier L=12 PPK=2 FakeLParens= FakeRParens=0 II=0x19e323d76b0 Text='sample'
 M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=22 Name=equal L=14 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='='
 M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=22 Name=identifier L=21 PPK=2 FakeLParens=6/ FakeRParens=0 II=0x19e323d70e0 Text='string'
 M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=26 Name=pipe L=23 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='|'
 M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=26 Name=identifier L=30 PPK=2 FakeLParens= FakeRParens=2 II=0x19e323d76e0 Text='number'
 M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=semi L=31 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=';'

Whilst we could fix it so there are spaces around the JsTypeOperator (all the time), or even add an option to control that, I think the string | number would be ALOT harder to get right in the situation where you don't want spaces

not so much in the string and number case but in the const sample = A | B case, any thoughts?

@mydeveloperday
Copy link
Contributor

There are unit tests to assert there is no space between them, if yo wanted

let x: A | B = A | B;

Then I think we'd need to add a new option

TEST_F(FormatTestJS, UnionIntersectionTypes) {
  verifyFormat("let x: A|B = A | B;");
  verifyFormat("let x: A&B|C = A & B;");
  verifyFormat("let x: Foo<A|B> = new Foo<A|B>();");
  verifyFormat("function(x: A|B): C&D {}");
  verifyFormat("function(x: A|B = A | B): C&D {}");
  verifyFormat("function x(path: number|string) {}");
  verifyFormat("function x(): string|number {}");
  verifyFormat("type Foo = Bar|Baz;");
  verifyFormat("type Foo = Bar<X>|Baz;");
  verifyFormat("type Foo = (Bar<X>|Baz);");
  verifyFormat("let x: Bar|Baz;");
  verifyFormat("let x: Bar<X>|Baz;");
  verifyFormat("let x: (Foo|Bar)[];");
  verifyFormat("type X = {\n"
               "  a: Foo|Bar;\n"
               "};");
  verifyFormat("export type X = {\n"
               "  a: Foo|Bar;\n"
               "};");
}

@mydeveloperday mydeveloperday added awaiting-review Has pending Phabricator review enhancement Improving things as opposed to bug fixing, e.g. new or missing feature labels Jan 13, 2022
@mydeveloperday
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review Has pending Phabricator review bugzilla Issues migrated from bugzilla clang-format enhancement Improving things as opposed to bug fixing, e.g. new or missing feature javascript typescript
Projects
None yet
Development

No branches or pull requests

1 participant