[PATCH] D117197: [clang-format] Add new option to support adding a space between Javascript Union and Intersection types

Martin Probst via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 14 08:42:58 PST 2022


mprobst added inline comments.


================
Comment at: clang/lib/Format/TokenAnnotator.cpp:3522
+      if ((Left.is(TT_JsTypeOperator) && Right.isTypeOrIdentifier()) ||
+          (Left.isTypeOrIdentifier() || Left.is(TT_TemplateCloser)) &&
+              Right.is(TT_JsTypeOperator))
----------------
MyDeveloperDay wrote:
> mprobst wrote:
> > Why do we need this further qualification here? I'd have expect that you can simply "return Style.SpacesInJavaScriptUnion;"? identifier || template closer also sounds oddly specific, why exactly those?
> Left =  template closer, Right =JsTypeOperator is for this case
> 
> ```
> type Foo = Bar<X> | Baz;
>                ^^^^^
> ```
> 
> Left =  type or identifier, Right =JsTypeOperator is for this case
> 
> 
> ```
> let x: A | B = A | B;
>     ^^^^^^^^^
> ```
What about:

```
let x: {foo: string}&{bar: number};
```

I'm not sure including curlies would be comprehensive. It might be worth checking the TS grammar.

But coming back, did you try this:

```
if (Left.is(TT_JsTypeOperator) || Right.is(TT_JsTypeOperator)) {
  return Style.SpacesInJavaScriptUnion;
```

(We might also need to do something in `spaceRequiredBefore`, maybe that fires earlier?)




CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117197/new/

https://reviews.llvm.org/D117197



More information about the cfe-commits mailing list