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.

SyntaxError when TypeScript casting

See original GitHub issue

Your Environment

  • prettier-plugin-sort-imports version: 3.1.1
  • Prettier version: 2.4.1
  • node version: 16.9.1
  • package manager: npm@7
  • IDE: CLI

Describe the bug

When casting in TypeScript with the <> operator, prettier-plugin-sort-imports makes prettier fail with SyntaxError. Searching through closed issues, I found this which looks similar #45 But it appears to not be completely fixed, since I am using version 3. I tried uninstalling prettier-plugin-sort-imports and prettier ran just fine. Reinstalling it again, and the error resumed.

To Reproduce

npx prettier -c test.ts

test.cs:

const value = <string>('');

test.cs with a workaround and an explanation for why <> casting is necessary:

interface ExternalInterface {
  test(): Promise<string | number>;
}

class MyImplementation implements ExternalInterface {
  public async test(): Promise<string | number> {
    return 'foo';
  }
}

export async function run() {
  const instance = new MyImplementation();
  const value = <string>( // breaks prettier
      await instance.test()
  );

  const workaround = (
      await instance.test()
  ) as string; // works, but now the type is far away from the variable definition, especially for multiline calls
}

Expected behavior

Prettier checks the formatting.

Configuration File (cat .prettierrc, prettier.config.js, .prettier.js)

.prettierrc.yaml:

singleQuote: true
printWidth: 100
trailingComma: all

xmlWhitespaceSensitivity: ignore

importOrder:
  - '^@foo-.*$'
  - '^[./]'
importOrderSeparation: true
importOrderSortSpecifiers: true

overrides:
  - files: '*.jmx'
    options: { 'parser': 'xml' }

Error log

Single line test.ts: Checking formatting… test.ts[error] test.ts: SyntaxError: Unterminated JSX contents. (1:22)

Larger test.ts: Checking formatting… test.ts[error] test.ts: SyntaxError: Unterminated JSX contents. (13:24)

There may be two issues… Before I recreated this with the example code above, it failed with a different message on our actual code (but still on lines that did <> casting):

Checking formatting… redacted.ts[error] redacted.ts: SyntaxError: Unexpected token (79:2)

Contribute to @trivago/prettier-plugin-sort-imports

  • I’m willing to fix this bug 🥇

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

3reactions
daniel-walterscommented, Aug 25, 2022

@StringKe, I had the same issue which I fixed by incorporating @byara’s solution into an override for only .ts files ie.

"overrides": [
  {
    "files": "*.ts",
    "options": {
      "importOrderParserPlugins": ["typescript"]
    }
  }
]

The reason is due to a technical limitation of babel when mixing the ‘jsx’ and ‘typescript’ plugins and is documented here

2reactions
byaracommented, Dec 14, 2021

Have you tried to add typescript in importOrderParserPlugins?

importOrderParserPlugins: ['typescript']

Reason is, you are using typescript and the content of this file you are formatting is passed to Babel parser. With adding that option you let babel know it is facing TS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript cast (assertion) results in Syntax Error compiler ...
When the TypeScript compiler sees this line, it complains that The property 'SetElqContent' does not exist on value of type 'Window' . I...
Read more >
Type Assertions & Type Casting | Typescript Tutorial for ...
Learn Type Assertions & Type Casting in this Typescript tutorial for ... 2: Type Assertion Corrections: (13:50) Syntax error : document.
Read more >
Documentation - TypeScript 3.8
SyntaxError ! // '#foo' needs to be declared before writing to it. this.#foo = foo;. } } JavaScript has always allowed users to...
Read more >
Using Casting in TypeScript. One of the things that are very common ...
you will get syntax error in the following constructor line: this.span = document.createElement('span');. The error occurs because the document.createElement ...
Read more >
Type Casting in TypeScript | JavaScript in Plain English
The code above will actually throw an error, which is Object is of type 'unknown'. . To solve this problem, we need to...
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