Skip to content
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

Sublime formatting won't work if file is not saved #30

Open
zekedroid opened this issue Nov 15, 2016 · 10 comments
Open

Sublime formatting won't work if file is not saved #30

zekedroid opened this issue Nov 15, 2016 · 10 comments

Comments

@zekedroid
Copy link

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.

@AoDev
Copy link
Contributor

AoDev commented Jul 12, 2017

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.

@TheSavior
Copy link
Owner

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.

@AoDev
Copy link
Contributor

AoDev commented Jul 12, 2017

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?

@TheSavior
Copy link
Owner

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.

TheSavior pushed a commit that referenced this issue Jul 12, 2017
)

* docs(readme): add note about current behaviour, need to save first.

See Github issue #30

* Wording
@s7726
Copy link
Collaborator

s7726 commented Jun 1, 2018

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

@s7726
Copy link
Collaborator

s7726 commented Jun 1, 2018

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
Copy link
Collaborator

s7726 commented Jun 1, 2018

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

@TheSavior
Copy link
Owner

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.

@s7726
Copy link
Collaborator

s7726 commented Jun 2, 2018

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.

@katerlouis
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants