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.

Unnecessary new lines injected by typescript parser.

See original GitHub issue

Prettier 1.19.1 Playground link

--parser typescript
--print-width 120

Input:

export const Foo = forwardRef((props: FooProps, ref: Ref<HTMLElement>): JSX.Element => {
  return <div />;
});

export const Bar = forwardRef((props: BarProps, ref: Ref<HTMLElement>): JSX.Element | null => {
  return <div />;
});

users.map((user: User): User => {
  return user;
})

Output:

export const Foo = forwardRef(
  (props: FooProps, ref: Ref<HTMLElement>): JSX.Element => {
    return <div />;
  }
);

export const Bar = forwardRef((props: BarProps, ref: Ref<HTMLElement>): JSX.Element | null => {
  return <div />;
});

users.map(
  (user: User): User => {
    return user;
  }
);

Expected behavior: Prettier leaves the code as is.

This formatting issue only occurs when using the typescript parser. If you switch over to the babel parser, the code remains untouched.

The extra lines that prettier injects seem overkill, especially for the users.map example.

It’s interesting though how that functions that have a union return type, don’t get rewritten to a new line.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
whitneyitcommented, Feb 5, 2020

If this is considered a bug, I would be happy to work on a fix

1reaction
whitneyitcommented, Feb 12, 2020

~I’m keen to work on it as well. I’m not sure if it is deemed a bug or not 🤷‍♂️~

I didn’t see the label come through via email 🤦‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript static code analysis: JavaScript parser failure
TypeScript static code analysis. Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your TYPESCRIPT code.
Read more >
ESLint - Configuring "no-unused-vars" for TypeScript
So here's the latest update of my .eslintrc file which works for me :) { "parser": "@typescript-eslint/parser" ...
Read more >
Writing custom TypeScript Eslint rules with unit tests for ...
After reading this article you will know how to create a custom Eslint rule in Typescript for Angular project and how to test...
Read more >
How to fix Definition for rule typescript-eslint no-use-before ...
I am new to eslint and I cannot figure out how to solve this issue. The beginning of my imports is always underlined...
Read more >
Prettier 2.1: new --embedded-language-formatting option and ...
Fix inconsistent parsing of injected expressions in ... technically necessary to do this for strings which are not on the first line of...
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