typescript-formatter icon indicating copy to clipboard operation
typescript-formatter copied to clipboard

.editorconfig is not picked up with --baseDir

Open Eoksni opened this issue 7 years ago • 5 comments

Settings from .editorconfig are not picked up by tsfmt with --baseDir.

Here is file structure I'm using https://gist.github.com/Eoksni/49cbac2c0a6cfd17b1d93b699098ac8a.

Here is what I do

c:\temp\test-tsfmt\outer>tsfmt ..\project\test.ts
let q = function (name) {
  console.log("greetings" + name);
}

c:\temp\test-tsfmt\outer>tsfmt --baseDir=c:\temp\test-tsfmt\project test.ts
let q = function (name) {
    console.log("greetings" + name);
}

Both files project\test.ts and outer\test.ts are exactly equal, and by applying tsfmt to them with same baseDir I expect the same output, which is not the case here. Notice that settings from tsfmt.json were indeed picked up (space after anonymous function), but settings from .editorconfig were not (indentation should be 2 instead of default 4).

Eoksni avatar May 16 '17 10:05 Eoksni

sorry for late reply. It is expected behavior. editorconfig is directory oriented specifications. We should not ignoring it.

I recommend you to write indent settings to tsfmt.json.

https://github.com/vvakame/typescript-formatter/blob/888b51e85fa1460984ed36dde338d096b5bb9fab/lib/provider/editorconfig.ts#L11-L14

$ tsfmt --verbose --baseDir /tmp/49cbac2c0a6cfd17b1d93b699098ac8a/project test.ts
replace             : OFF
verify              : OFF
baseDir             : /tmp/49cbac2c0a6cfd17b1d93b699098ac8a/project
stdin               : OFF
files from tsconfig : OFF
tsconfig            : ON
tslint              : ON
editorconfig        : ON
vscode              : ON
tsfmt               : ON
read /tmp/49cbac2c0a6cfd17b1d93b699098ac8a/project/tsfmt.json for test.ts
editorconfig is not supported baseDir options
let q = function (name) {
    console.log("greetings" + name);
}

vvakame avatar May 24 '17 04:05 vvakame

I'm a bit confused. So it's expected, but not intended? In other words, support for editorconfig with baseDir is desired, but not implemented yet? Would you mind a pull request with a fix? I will have some spare time at the weekend and might wanna fix it, I think it shouldn't be too hard.

Eoksni avatar May 24 '17 20:05 Eoksni

I recommend to you use tsfmt.json in baseDir. Why do you want to use .editorconfig? If we support --baseDir option with .editorconfig, Editors can't pickup .editorconfig file from target dir. Is not that meaningless?

vvakame avatar May 30 '17 07:05 vvakame

My comment turned out to be a lot of text, so first I wanted to say that I applied some hack to my emacs which kinda solves this issue for me (thank god emacs is so customizable!). So if you think that this issue is unimportant or that you are right and I'm talking nonsense, then just close this issue. Still, I think I have some valid points so I'm talking about them below:

I don't quite understand what makes .editorconfig so special. The same argument that you make can apply to tsfmt.json (or other config sources files). There are editors which use settings from tsfmt.json for formatting. In fact, tsserver does it, so any editor which uses tsserver as backend also uses tsfmt.json (for example, emacs with tide-mode and I believe there is extension for Visual Studio Code to use tsserver as well and probably there are other editors).

You say that "editorconfig is directory oriented specifications". Same, I don't understand how it is different from tsfmt.json. Both of them contain a number of formatting settings which are intended to be applied to files in underlying directory. Can't you say that tsfmt.json is a directory oriented specification?

May be I just don't understand the purpose of baseDir. So I'll tell you what is the use case for this issue. I'm using spacemacs (it's a customized version of emacs) with typescript configuration layer, and it is set up to use tsfmt to do formatting on file save. It uses tsfmt in a certain way:

  • it copies current file to a tmp directory,
  • formats it using tsfmt with baseDir set to current directory,
  • and then replaces current file with a formatted file.

So the intention here is that setting baseDir to a certain directory makes tsfmt to behave exactly the same regardless of the fact that the file-to-be-formatted is located somewhere else (in tmp directory in this case). Clearly it is not currently the case with .editorconfig present.

As for your recommendation to use tsfmt.json for storing settings, I can't quite follow it, because I work with a team of users of Visual Studio Code, and they use built-in formatting, which doesn't support tsfmt.json. On the contrary, tsfmt does support VSCode settings, so its kinda not a problem except for indentation - it seems that tsfmt doesn't pick up indentation settings from VSCode config (I haven't actually checked it myself but that was my impression as in https://github.com/vvakame/typescript-formatter#read-settings-from-files in the section for VSCode there is no mention of indentation options, only for formatting ones). So we ended up using editorconfig for specifying indentation as it is supported both by VSCode (via plugin) and tsfmt.

Eoksni avatar May 30 '17 08:05 Eoksni

umm... I feel like We can use .editorconfig with --baseDir together. Could you send a PR?

BYW, tsfmt.json is typescript-formatter specific config file. It can use indentation settings.

{
  "indentSize": 2
}

vvakame avatar Jun 13 '17 09:06 vvakame