Formatting breaks @ts-ignore comments
See original GitHub issueedited by @thorn0, citing this comment:
@ts-ignore
ignores one line of code, not one expression or statement (a behavior diametrically opposed to code beautifiers which operate on the AST). It therefore almost always affects code where Prettier condenses or spreads out TypeScript code.
Prettier 1.19.1 Playground link
--parser typescript
Input:
// @ts-ignore
const result = evaluate(chain, opts.filename, args.map(arg => arg.node.value))
Output:
// @ts-ignore
const result = evaluate(
chain,
opts.filename,
args.map(arg => arg.node.value)
);
The original code example is below. Prettier doesn’t break it anymore, but the general problem with @ts-ignore
comments hasn’t been solved.
original code example
Prettier 1.15.1 Playground link
# Options (if any):
--single-quote
Input:
export class WebpackTranslateLoader implements TranslateLoader {
public getTranslation<T>(lang: string): Observable<T> {
return from(
// @ts-ignore
import('dinamicModule')
);
}
}
Output:
export class WebpackTranslateLoader implements TranslateLoader {
public getTranslation<T>(lang: string): Observable<T> {
return from(
import(// @ts-ignore
"dinamicModule")
);
}
}
Expected behavior:
Same as input. The comment is mean to ignore the function call, but by moving the comment inside the function, the meaning of the comment is lost.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:12
- Comments:43 (25 by maintainers)
Top Results From Across the Web
Ignoring Code - Prettier
Use .prettierignore to ignore (i.e. not reformat) certain files and folders completely. Use “prettier-ignore” comments to ignore parts of files.
Read more >395:9 error Do not use // @ts-ignore comments because they ...
Step 1: Add this rule to `.eslintrc` file Step 2: Restart local server */ "rules": { "@typescript-eslint/ban-ts-ignore": "off" } ...
Read more >ts-ignore not working prettier - You.com | The AI Search ...
And inside of it I've tried package.json or whole folder src/ even paths to the specific files but nothing works as expected. no...
Read more >How to use `@ts-ignore` for a block? - Stack Overflow
So to disable checking for a block (function, class, etc.), you can move it into its own file, then use the comment/flag above....
Read more >Reformat and rearrange code - WebStorm - JetBrains
Switch to the Formatter tab and enable the Turn formatter on/off with markers in code comments option. In the editor, at the beginning...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a workaround, this might work:
Prettier 1.16.4 Playground link
Input:
Output:
@glen-84 It’s not worth the trouble.
@ts-ignore
is like thewith
statement or the comma operator. We just don’t want Prettier to break them. That’s it. We shouldn’t really care how they look in the end. That said, if you feel like trying to implement a more sophisticated solution, I’ll be glad to review the PR.