Skip to content

Request: quotemark option in ts.FormatCodeOptions of pretty printer in Typescript language service #11320

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

Closed
darcyparker opened this issue Oct 3, 2016 · 4 comments
Labels
API Relates to the public API for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@darcyparker
Copy link

Feature Request/Suggestion:

As a user, I'd like to have option to configure the quotemark (single or double) in ts.FormatCodeOptions interface that can be used with the pretty printer in the TypeScript Language Service.

Code generators and other utilities rely on the pretty printing capability to format typescript according to user preferences and most leverage the pretty printer capability in the . Some projects prefer single quotes and others double quotes.

The need was expressed a typescript-formatter issue and I have also found the need for configuring the preferred quotes in json-schema-to-typescript. I am sure others would like to be able to configure the quote preference too. I am open to suggestions to alternatives for formatting typescript, but I think it would be best if this was provided by the typescript language service.

TypeScript Version: Currently using 2.0.3

Expected behavior:

An option is available in the ts.FormatCodeOptions interface for the quotemark (single or double) similar to tslint. And output of pretty printer follows the quotemark defined.

Actual behavior:
I am not aware of an option to choose the quote mark.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 3, 2016

Formatting is really just about changing white spaces, that is spaces, tabs and new lines. it does not do any stylistic changes to the code. for instance, it does not add semi-colons where ASI kicks in, nor does it add curlies to if statements if they do not have ones.

I would recommend adding a "fix-up" tree walk (something like this) to replace all string literals using single quotes for instance to double quotes.

@mhegazy mhegazy added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript API Relates to the public API for TypeScript labels Oct 3, 2016
@darcyparker
Copy link
Author

This is disappointing to here. For other languages, formatting APIs that do more than just change white spaces. I was hoping the typescript language service could take ownership of serializing the AST with a variety of formatting options.

Thanks for the pointer to the AST walker. I understand the approach. I guess the community will need to take this on.

@aluanhaddad
Copy link
Contributor

@darcyparker the tslint plugin for VS Code has a function to automatically change all errant quote marks to the preferred style, but it is sometimes problematic as it breaks bad but valid code. It's a semantic change so it has to be done on a case-by-case basis.

@darcyparker
Copy link
Author

@aluanhaddad - thanks for the suggestion. I am sure others will appreciate trying that plugin. I currently use tsformatter with vim, but I am also looking for an API to serialize the AST with formatting options that I can use with various code-generators.

https://github.com/nojvek/tslint-fix is another solution that has some potential short term. But it uses regexes, which is probably not as robust as walking an AST. I want something that gets the escaping rules right.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
API Relates to the public API for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants