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

Regression with TypeScript "as const" object literal in 2.0.3 #7956

Closed
wincent opened this issue Apr 5, 2020 · 2 comments · Fixed by #7958
Closed

Regression with TypeScript "as const" object literal in 2.0.3 #7956

wincent opened this issue Apr 5, 2020 · 2 comments · Fixed by #7958
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program

Comments

@wincent
Copy link

wincent commented Apr 5, 2020

Playground hasn't updated to 2.0.3 yet, so this link was generated on 2.0.2 playground:

Prettier 2.0.2
Playground link

--parser typescript

Input:

const TYPE_MAP = {
    'character device': 'special',
    'character special file': 'special',
    directory: 'directory',
    'regular file': 'file',
    socket: 'socket',
    'symbolic link': 'link',
} as const;

Output:

const TYPE_MAP = {
  "character device": "special",
  "character special file": "special",
  directory: "directory",
  "regular file": "file",
  socket: "socket",
  "symbolic link": "link",
} as const;

Expected behavior:

The 2.0.2 behavior (above) is expected, but with 2.0.3, I get:

export const LOG_LEVEL =
    {
        EMERGENCY: 0,
        ALERT: 1,
        CRITICAL: 2,
        ERROR: 3,
        WARNING: 4,
        NOTICE: 5,
        INFO: 6,
        DEBUG: 7,
    } as const;

Only happens with as const qualifier. Without it, formatting continues to be like in 2.0.2:

export const LOG_LEVEL = {
    EMERGENCY: 0,
    ALERT: 1,
    CRITICAL: 2,
    ERROR: 3,
    WARNING: 4,
    NOTICE: 5,
    INFO: 6,
    DEBUG: 7,
};
@sosukesuzuki sosukesuzuki added lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) type:bug Issues identifying ugly output, or a defect in the program labels Apr 5, 2020
@sosukesuzuki
Copy link
Member

This happens for not only as const.
Playground
Input:

const TYPE_MAP = {
    'character device': 'special',
    'character special file': 'special',
    directory: 'directory',
    'regular file': 'file',
    socket: 'socket',
    'symbolic link': 'link',
} as Foo;

Output:

const TYPE_MAP =
  {
    "character device": "special",
    "character special file": "special",
    directory: "directory",
    "regular file": "file",
    socket: "socket",
    "symbolic link": "link",
  } as Foo;

wincent referenced this issue in wincent/wincent Apr 5, 2020
Prettier 2.0.3 just came out 2 hours ago, and because I am running with
`npx`, I get it as soon as I run `yarn format`.

I can't see anything obvious in the release notes about it, so there is
a chance that it's actually a regression:

https://github.com/prettier/prettier/blob/master/CHANGELOG.md#203
@Shinigami92
Copy link
Member

I don't know if it's the same problem, but I think it might fit here:

Somewhere in our code (real world example), only one example of eleven

v2.0.2:

    this.previewPlayerHandle = (setInterval(async () => {
      if (this.previewIsPlaying) {
        await this.fetchNextPreviews();
        this.currentPreviewIndex++;
      }
    }, this.refreshDelay) as unknown) as number;

v2.0.3:

    this.previewPlayerHandle =
      (setInterval(async () => {
        if (this.previewIsPlaying) {
          await this.fetchNextPreviews();
          this.currentPreviewIndex++;
        }
      }, this.refreshDelay) as
        unknown) as
      number;

v2.0.2 is MUCH more readable and less noisy


Another example

v2.0.2:

    this.intervalID = (setInterval(() => {
      self.step();
    }, 30) as unknown) as number;

v2.0.3:

    this.intervalID =
      (setInterval(() => {
        self.step();
      }, 30) as
        unknown) as
      number;

... I will skip v2.0.3 and wait for a patch in v2.0.4+

thorn0 added a commit to thorn0/prettier that referenced this issue Apr 6, 2020
@wincent wincent changed the title Possible regression with TypeScript "as const" object literal in 2.0.3 Regression with TypeScript "as const" object literal in 2.0.3 Apr 6, 2020
thorn0 added a commit that referenced this issue Apr 6, 2020
…xpression" (#7958)

* Revert "[TypeScript] format TSAsExpression with same logic as BinaryExpression (#7869)"

This reverts commit 8a48ca2.

* add tests from #7956 and #7957

* more tests
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jul 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:typescript Issues affecting TypeScript-specific constructs (not general JS issues) locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants