Vscode: TS formatter use format settings from tsfmt.json/tslint.json

Created on 20 Dec 2016  路  14Comments  路  Source: microsoft/vscode

Does Code use typescript-formatter for code formatting? If not, how do we sync up tslint settings with the built-in formatter?

*extension-candidate feature-request typescript

Most helpful comment

Any update on this?

All 14 comments

@tomdavidson

Does Code use typescript-formatter for code formatting?

Is your question about the VS Code source code and how we enforce consistent formatting in our code base?

In the VS Code code base we are using the typescript-formatter in a git commit hook. We use tslint as well, but we do not use tslint for formatting but only for style checking:
https://github.com/Microsoft/vscode/blob/master/tsfmt.json
https://github.com/Microsoft/vscode/blob/master/tslint.json

If the question is how to do this in general, then the answer is that it is on you to ensure that the formatter settings align with the tslint settings. Since you can use any formatter with VS Code (e.g. the formatter provided by the Beautify extension) we cannot make any assumptions about the settings provided by the formatter and vice versa the formatter should not make any assumption about the tslint settings.

Does this answer your question?

@egamma thanks for the info and I apologize my question was ambiguous.
The question is about my code written with VS Code rather than VS Code's code.
VS Code can format on save for line change - I am referencing the ("built-in" formatter)[https://code.visualstudio.com/Docs/languages/typescript#_format-code] for TypeScript rather than a community extension such as Beautify (that does not work with TypeScript)

tsfmt has several file based config options and I am wanting the VS Code formatter to use the same rules as tslint and as static code analysis CI step. For example, when I run tslint in our pipeline, I use tslint.json. tsfmt can also use tslint.json rather than attempt to keep multiple config files in sync.

@tomdavidson got it. You are asking that the VS Code TypeScript formatter settings support to honor or refer to the formatting settings from a tsfmt.json or tslint.json file.

FYI @waderyan @mjbvz

Thank you for adding the feature request label. I was actually hoping it was already available. Does VS Code use tsfmt to format my code or does it have its own formatter?

This would be a very nice feature. At the moment, when I do Shift+Alt+f, it introduces tslint warning every now and then.

Any update on this?

@mjbvz,

Is it possible to configure VSCode to read a project's tslint.json file when auto formatting after pasting?

Cheers 馃嵒

Hello, any answer on "Does VS Code use tsfmt to format my code or does it have its own formatter?"

This is kinda important :-/

Any resolution?

Nope, ended up writing my own extension to do it https://github.com/OneOfOne/vscode-save-runner

    "save-runner.commands": [
        {
            "enabled": true,
            "include": "\\.[tj]sx?$",
            "exclude": "/node_modules/",
            "pre": "eslint_d --fix ${tmpFile}"
        }
    ]

Note that I'm personally using elint_d (elint) instead of tslint, you can adapt it to tslint.

Any updates on this 6 months later?

Is this there now? @egamma

I believe this can now be implemented by a typescript typescript service vscode extension. See the typescript-tslint plugin for an example of this

TypeScript service plugins allow you to intercept requests made against the TS server. You could use this to intercept formatting requests and inject the formatting settings that have been loaded from the local tsfmt/tslint file. Alternatively, you could entirely override the TypeScript formatter. Let me know if you need pointers on getting started. The styled-plugin override the formatter so it may be a good initial reference

Closing as extension candidate

Was this page helpful?
0 / 5 - 0 ratings