ESLint-Formatter icon indicating copy to clipboard operation
ESLint-Formatter copied to clipboard

Sublime formatting won't work if file is not saved

Open zekedroid opened this issue 9 years ago • 10 comments

Minor bug, not that important but still an issue: formatting doesn't work (no errors, just doesn't do anything) if the file is not saved first.

zekedroid avatar Nov 15 '16 17:11 zekedroid

Not exactly the same issue but probably the same cause. I can only format with the option format_on_save: true.

It seems it's not running against the current altered version of the file.

AoDev avatar Jul 12 '17 14:07 AoDev

The formatter doesn't run on the buffer, it runs on the underlying file. If you save the file and then run the command to format it, it will format the underlying file which will update the buffer. You should then be able to format without format_on_save: true.

elicwhite avatar Jul 12 '17 16:07 elicwhite

We understood that so far. But that's not what people expect. If we run the command, we expect it to run against the buffer, state of the code that we are seeing.

If for some reason it is not possible, I don't know sublime API, then it would help to mention it in the docs. No?

AoDev avatar Jul 12 '17 18:07 AoDev

I believe it would be possible by having a command that takes the buffer, writes it to a tmp file in the current directory, runs eslint on it, and copies the file contents back to the buffer and cleans up the temp.

Happy to accept a PR adding that functionality or updating the docs explaining how it currently works.

elicwhite avatar Jul 12 '17 18:07 elicwhite

I believe this is typically handled by sending the contents of the buffer to the 'executable' via stdin.

s7726 avatar Jun 01 '18 05:06 s7726

So some investigating revealed that eslint won't run the fix option if the stdin option is given. And the fix-dry-run option doesn't seem to actually output anything... Unless you tell it to do the output in json. Which you could grab and write back to the buffer.

cat filename.js | eslint --fix-dry-run --format json --stdin --stdin-filename /full/path/to/the/filename.js

results in a json file on stdout.

[{"filePath":"/full/path/to/the/filename.js","messages":[],"errorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"output":"FIXED_TEXT_OF_INPUT"}]

There is a spot in the eslint engine file where it grabs the current working directory. There may be a need to file a pull request/issue to get that cwd exposed as a command line option, that way all the proper config files and things would be pulled relative to /full/path/to/the/ I managed to hack in stdout by modifying only ~two lines of code in eslint. More would be needed to properly cover the options and what not. But it's not too far off from the capability.

s7726 avatar Jun 01 '18 07:06 s7726

Getting this working would also allow for the formatting of inline <script>s in html

s7726 avatar Jun 01 '18 07:06 s7726

Wow, thanks for digging into this! It sounds like there might be some work on the ESLint side to support this functionality. @s7726, I've also gone ahead and made you a collaborator on this project. The investigation you did makes me trust you. :P Feel empowered to modify and add the functionality to this plugin you find necessary.

elicwhite avatar Jun 01 '18 07:06 elicwhite

Just noticed eslint_d has a --fix-to-stdout option that works in conjunction with the --stdin option.

@TheSavior Thanks for the Maintainorship. I'm still looking into this on the side of the project that prompted the issue.

s7726 avatar Jun 02 '18 20:06 s7726

just spent over an hour figuring out why this bloody file wouldn't get formatted .. (after even more hours trying to resolve node path issues..)

I haven't read the entire issue, but my 2 cents on the matter are: It should definitely format unsaved files; but if it can't, atleast throw an error with an explanation, so I know what's going on :F

katerlouis avatar Jan 21 '20 15:01 katerlouis