question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[typescript] union types incorrectly indented after comments

See original GitHub issue

Prettier 1.16.4 Playground link

--parser typescript

Input:

export type TransformedSource = {
  code: string;
  map?:
    // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

export type TransformedSource = {
  code: string,
  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
  | {
        version: number,
        sources: string[],
        names: string[],
        sourceRoot?: string,
        sourcesContent?: string[],
        mappings: string,
        file: string
      }
    | string
    | null
};

Output:

export type TransformedSource = {
  code: string;
  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379

    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

export type TransformedSource = {
  code: string;
  map?:  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

Second Output:

export type TransformedSource = {
  code: string;
  map?:  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

export type TransformedSource = {
  code: string;
  map?:  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

Expected behavior: For the output to become the second result after a single prettier run. This happens using babylon and flow parser as well.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, Feb 11, 2019

Yeah, that’s what happens on the second run, in addition to a deletion of a blank line.

Maybe simpler to see:

Start:

export type TransformedSource = {
  code: string;
  map?:

  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

Diff after running prettier once:

@@ -1,8 +1,7 @@
 export type TransformedSource = {
   code: string;
-  map?:
+  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
 
-  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
     | {
         version: number;
         sources: string[];

Diff after running prettier twice:

@@ -1,8 +1,6 @@
 export type TransformedSource = {
   code: string;
-  map?:
-
-  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
+  map?:  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
     | {
         version: number;
         sources: string[];

Diff between first and second run of prettier:

@@ -1,7 +1,6 @@
 export type TransformedSource = {
   code: string;
-  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
-
+  map?:  // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
     | {
         version: number;
         sources: string[];
0reactions
thorn0commented, May 7, 2020

The idempotence issue is gone, but the indentation after the comment is off:

Prettier 2.0.5 Playground link

--arrow-parens avoid
--parser typescript
--trailing-comma none

Input:

export type TransformedSource = {
  code: string;
  map?:
    // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
    | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

export type TransformedSource = {
  code: string,
  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
  | {
        version: number,
        sources: string[],
        names: string[],
        sourceRoot?: string,
        sourcesContent?: string[],
        mappings: string,
        file: string
      }
    | string
    | null
};

Output:

export type TransformedSource = {
  code: string;
  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
  | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

export type TransformedSource = {
  code: string;
  map?: // copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/363cdf403a74e0372e87bbcd15eb1668f4c5230b/types/babel__core/index.d.ts#L371-L379
  | {
        version: number;
        sources: string[];
        names: string[];
        sourceRoot?: string;
        sourcesContent?: string[];
        mappings: string;
        file: string;
      }
    | string
    | null;
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

[indent] incorrect treatment of multi-line parameter types #880
This problem compounds a bit when you've got typescript specific nodes within typescript specific nodes. As you've then got hacked together ...
Read more >
Handbook - Unions and Intersection Types - TypeScript
Union Types ​​ * If 'padding' is a number, then that number of spaces is added to the left side. The problem with...
Read more >
Typescript union and intersection give unintuitive and wrong ...
Ultimately your Animal type is a huge awful union, and if you assign a value that matches none of those, the compiler is...
Read more >
Google TypeScript Style Guide
Omit comments that are redundant with TypeScript. For example, do not declare types in @param or @return blocks, do not write @implements ,...
Read more >
Why not de-indent regions? it'll look so much more ... - Reddit
A place where a comment is not always enough to delimit an area as you ... typescript and have clearly defined string options...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found