You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Disable formatting a region of code using directives #18261
Feature Request
Add a way to disable formatting of a region of code. An example of this is js beatuify's preserve directive:
// Use preserve when the content is not javascript, but you don't want it reformatted./* beautify preserve:start */{browserName: 'internet explorer',platform: 'Windows 7',version: '8'}/* beautify preserve:end */
Hi there,
It will be great to have ability to prevent specific pieces of code from being formatted.
In my case JS code can contains 3-rd party snippets that are minified pieces of JS (Google analytics, conversion scripts etc.). I'd prefer to not format them on paste/update by specifying directives or something.
There are formatters in the marketplace that support this, but I don't like many of them for various other issues. How's the support for the native formatter going?
I wonder, what is the "More Feedback" that is expected? Can we have the flag removed?
Reacted by Niko Fellner, Janosch Müller, Daniel, Bob Huckins, Anurag Roy, tomhanax, J++ and Antonio Mudarra Machuca
No news about this?. Is a nice feature that sometimes is required because the formatter is not the best in some situations, but looks like the idea is abandoned.
Some more feedback here. Possible directive structure for the feature:
Disables formatting until the closure end. If placed within the file's top level, then until the end of the file. If placed at the beginning of the file, that implies disabling formatter for the whole file.
// format-disable
Clears any formatting disables active until this point.
// format-enable
Just for the next line. Additional lines of multi-line statements will be formatted.
// format-disable-next-line
let ugly = {very:'ugly' } // <- this won't be formatted
let uglier = { [ 'moreUgly']:'nope'} // <- this will be formatted
Just for the current line (left from the comment). Additional lines of multi-line statements will be formatted.
let ugliest = ugly??uglier // format-disable-line <- disables for this line
? 'haha' : 'nope' // <- this will be formatted
For the whole statement, includes multiple lines.
// format-disable-next-statement
const x = // format-disable-statement
isA ? a()
: isB ? b()
: throw 'Nope'
Disables until closure end
// format-disable-next-closure
{
// format-disable-closure // <- basically just an alias to format-disable. For clarity.
}
// formatting is enabled here
Closing notes:
Not sure how tied this is to TypeScript itself and the // @ts-ignore etc comments, but // ts-format-disable or even // @ts-format-disable and all the variants mentioned above don't look too bad neither.
The problem I see here that the whole idea seems not to be interesting enough to be considering / work on.
I can not understand why, for us it is "must have" or no automatic formatter at all.
Reacted by Kjetil Gloppen, Luis Marroquin, Alek, TomasB, bonsembiante, John Piepers-Smits, Kevin Gilliam, Qwerty (Vítězslav Ackermann Ferko) and Mylloon
Hi all, I implemented format ignore directivies in vscode plugin
Great news, finally!
I've already tried it and it works perfectly. Thank you.
However I have one request - could it work also with space before the comment line? Like // @ts-format-ignore-region? Because we use the comments this way and our linter is unhappy without the space.
Also - is it possible to make it as standalone extension? There are plenty of features and it is more complicated to recommend this the all-in-one type of extension to the team.
Sorry hotfix was there, but forgot to release it instantly.. Now its definitely here!
Wow, I say that's customer service I like :) Thank you, tested and works perfectly. Regarding this issue, as for me, I consider this a "greater half" of the work done. I very much like that the command comments are consistent with original comments, this IMHO should be implemented there in the native formatter in the first place, but whatever... Now we have it anyway, finally!
The standalone extension will be definitely very welcome, but in the meantime I use the whole bundle and I see no problems so far, so good job and again, thank you.
Agree & thanks!
From: tomhanax ***@***.***>
Reply-To: microsoft/TypeScript ***@***.***>
Date: Sunday, February 5, 2023 at 10:25 PM
To: microsoft/TypeScript ***@***.***>
Cc: Josh Kramer ***@***.***>, Comment ***@***.***>
Subject: Re: [microsoft/TypeScript] Disable formatting a region of code using directives (#18261)
Sorry hotfix was there, but forgot to release it instantly.. Now its definitely here!
Wow, I say that's customer service I like :) Thank you, tested and works perfectly. Regarding this issue, as for me, I consider this a "greater half" of the work done. I very much like that the command comments are consistent with original comments, this IMHO should be implemented there in the native formatter in the first place, but whatever... Now we have it anyway, finally!
The standalone extension will be definitely very welcome, but in the meantime I use the whole bundle and I see no problems so far, so good job and again, thank you.
—
Reply to this email directly, view it on GitHub<#18261 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ACCIEBPZIL7HKE3XPTZ353TWWCKM7ANCNFSM4DZWFP4A>.
You are receiving this because you commented.Message ID: ***@***.***>
I have one question, it only works if I disable Typescript plugin. Is that the right way? Or I have to configure it somehow to work with Typescript plugin enabled?
I'd love to have some status on this issue, as it's been sitting here for almost 8 years now with a lot of people clearly expressing interest in the feature...
Activity
anmiles commentedon Oct 31, 2018
Hi there,
It will be great to have ability to prevent specific pieces of code from being formatted.
In my case JS code can contains 3-rd party snippets that are minified pieces of JS (Google analytics, conversion scripts etc.). I'd prefer to not format them on paste/update by specifying directives or something.
lfg6000 commentedon Feb 4, 2019
Hi,
oh it would be great to have the ability to prevent specific pieces of code from being formatted.
Louie
stevenvergenz commentedon May 30, 2019
I would want to use this for multiline strings, which need very careful whitespace inclusions.
PapsOu commentedon Jul 25, 2019
What about doing a similar stuff than PhpStorm ? :
ackvf commentedon Jan 27, 2020
There are formatters in the marketplace that support this, but I don't like many of them for various other issues. How's the support for the native formatter going?
I wonder, what is the "More Feedback" that is expected? Can we have the flag removed?
watana2 commentedon Jan 17, 2022
// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)
//https://prettier.io/docs/en/ignore.html
Danixu commentedon May 22, 2022
No news about this?. Is a nice feature that sometimes is required because the formatter is not the best in some situations, but looks like the idea is abandoned.
ackvf commentedon May 31, 2022
Some more feedback here. Possible directive structure for the feature:
Disables formatting until the closure end. If placed within the file's top level, then until the end of the file. If placed at the beginning of the file, that implies disabling formatter for the whole file.
Clears any formatting disables active until this point.
Just for the next line. Additional lines of multi-line statements will be formatted.
Just for the current line (left from the comment). Additional lines of multi-line statements will be formatted.
For the whole statement, includes multiple lines.
Disables until closure end
Closing notes:
Not sure how tied this is to TypeScript itself and the
// @ts-ignoreetc comments, but// ts-format-disableor even// @ts-format-disableand all the variants mentioned above don't look too bad neither.tomhanax commentedon May 31, 2022
The problem I see here that the whole idea seems not to be interesting enough to be considering / work on.
I can not understand why, for us it is "must have" or no automatic formatter at all.
ktgilliam commentedon Oct 25, 2022
Would very much like to have this feature available as well.
ivanjd commentedon Nov 17, 2022
4 remaining items
feat: add ability to Disable formatting a region of code using ts dir…
zardoy commentedon Feb 2, 2023
I made a vscode EXTENSION with format ignore directivies in vscode plugin (patches native ts formatter), it currently works like so:
Only
b: 3,will be formatted, obviously//@ts-format-ignore-regioncan be used on top of files to ignore them.And I wonder whether 3 directives suggested by Qwerty (Vítězslav Ackermann Ferko) (@ackvf) are enough?
tomhanax commentedon Feb 2, 2023
Great news, finally!
I've already tried it and it works perfectly. Thank you.
However I have one request - could it work also with space before the comment line? Like
// @ts-format-ignore-region? Because we use the comments this way and our linter is unhappy without the space.Also - is it possible to make it as standalone extension? There are plenty of features and it is more complicated to recommend this the all-in-one type of extension to the team.
Thanks!
zardoy commentedon Feb 5, 2023
tomhanax commentedon Feb 6, 2023
Wow, I say that's customer service I like :) Thank you, tested and works perfectly. Regarding this issue, as for me, I consider this a "greater half" of the work done. I very much like that the command comments are consistent with original comments, this IMHO should be implemented there in the native formatter in the first place, but whatever... Now we have it anyway, finally!
The standalone extension will be definitely very welcome, but in the meantime I use the whole bundle and I see no problems so far, so good job and again, thank you.
grapevinegizmos commentedon Feb 6, 2023
alexbezhan commentedon Feb 6, 2023
Vitaly (@zardoy) thank you, that's amazing.
I have one question, it only works if I disable Typescript plugin. Is that the right way? Or I have to configure it somehow to work with Typescript plugin enabled?
zardoy commentedon Feb 7, 2023
Aleksandr Bezhan (@alexbezhan)
What TypeScript plugin are you talking about?? If you disable aforementioned extension it obviously won't work. Or do you mean something else?
alexbezhan commentedon Feb 7, 2023
I mean this one https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-typescript-next
zardoy commentedon Feb 15, 2023
Aleksandr Bezhan (@alexbezhan) Good question! You see, there is following in TS 5.0 changelog:
And it even currently breaks plugin in so many places 🤣
But I changed format features implementation, so should be good now here.
soapy-spacetime commentedon Feb 16, 2023
with clangFormat for C/C++ you can use
george-thomas-hill commentedon Jul 2, 2024
Abdullah-AlAttar commentedon Jul 1, 2025
i-am-shodan commentedon Jul 4, 2025
ChristianLW commentedon Jul 17, 2025
I'd love to have some status on this issue, as it's been sitting here for almost 8 years now with a lot of people clearly expressing interest in the feature...