SublimeLinter / SublimeLinter-rubocop

SublimeLinter 3 plugin for Ruby, using rubocop.
MIT License
159 stars 40 forks source link

Rubocop incorrectly runs on HAML files #61

Closed sman591 closed 6 years ago

sman591 commented 6 years ago

When saving a change to a file, it appears SublimeLinter kicks off multiple, duplicate executions rubocop for HAML files.

For example, when I save a one-line file change to a HAML file, the Sublime Text console shows:

SublimeLinter: sublime_linter.py:250: Delay buffer 31 for 2.0s
SublimeLinter: linter.py:887: Checking lint mode background vs lint reason None
SublimeLinter: linter.py:887: Checking lint mode background vs lint reason None
SublimeLinter: linter.py:887: Checking lint mode background vs lint reason None
SublimeLinter: #923 linter.py:907: 'annotations' is linting '_form.html.haml'
SublimeLinter: #924 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #925 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #926 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #927 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #929 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #928 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #930 linter.py:907: 'rubocop' is linting '_form.html.haml'
SublimeLinter: #959 linter.py:907: 'haml' is linting '_form.html.haml'
SublimeLinter: #925 linter.py:1263: Running ...

(with "debug": true)

Not only does this kick off duplicate rubocop lints, it emits entirely incorrect lint errors such as:

Lint/Syntax: unexpected token $end (Using Ruby 2.5 parser; configure using `TargetRubyVersion` parameter, under `AllCops`)

For almost ever line of haml:

screenshot 2018-07-11 17 44 26

kaste commented 6 years ago

This must be fixed via the selector. Currently we have 'source.ruby - text.html'. We probably also need to deselect haml files.

Can you check which main selector haml files use?

LeEnno commented 6 years ago

I can confirm the multiple executions bug, but not the one with the incorrect lint errors. Output for one of my haml files:

SublimeLinter: sublime_linter.py:250: Delay buffer 25 for 0.0s
SublimeLinter: linter.py:886: Checking lint mode background vs lint reason on_load
SublimeLinter: #1 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #2 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #3 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #4 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #1 linter.py:1262: Running ...
SublimeLinter: #2 linter.py:1262: Running ...
SublimeLinter: #3 linter.py:1262: Running ...
SublimeLinter: #4 linter.py:1262: Running ...
SublimeLinter: #5 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #5 linter.py:1262: Running ...
SublimeLinter: #6 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #6 linter.py:1262: Running ...
SublimeLinter: #7 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #8 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #7 linter.py:1262: Running ...
SublimeLinter: #8 linter.py:1262: Running ...
SublimeLinter: #9 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #9 linter.py:1262: Running ...
SublimeLinter: #10 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #10 linter.py:1262: Running ...
SublimeLinter: #11 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #11 linter.py:1262: Running ...
SublimeLinter: #12 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #12 linter.py:1262: Running ...
SublimeLinter: #13 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #13 linter.py:1262: Running ...
SublimeLinter: #14 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #14 linter.py:1262: Running ...
SublimeLinter: #15 linter.py:906: 'rubocop' is linting 'index.html.haml'
SublimeLinter: #15 linter.py:1262: Running ...
SublimeLinter: sublime_linter.py:444: Linting buffer 25 took 10.14s

Can you check which main selector haml files use?

If you tell me how to check that I'll gladly do.

kaste commented 6 years ago

Tools > Developer > Show Scope Name will open a popup. Make sure the cursor is at the first char of the buffer.

LeEnno commented 6 years ago

If file starts with a Ruby statement: text.haml meta.line.ruby.haml
If file starts with a Haml expression: text.haml meta.tag.haml punctuation.definition.tag.haml

kaste commented 6 years ago

So I guess setting selector to source.ruby - text.html - text.haml is what you want.

https://github.com/SublimeLinter/SublimeLinter-rubocop/blob/90da4e351c9d32ee812783326c48f6a9a74434e5/linter.py#L7

Please, do a PR and test it, I don't use haml and thus can't do a functional test.

sman591 commented 6 years ago

I can confirm the multiple executions bug, but not the one with the incorrect lint errors.

@LeEnno if you add a Ruby error to your Haml, does the error get displayed by SublimeLinter?

I thought this linter originally worked for catching Ruby errors within Haml too, but I could be wrong.

LeEnno commented 6 years ago

@LeEnno if you add a Ruby error to your Haml, does the error get displayed by SublimeLinter?

No.

LeEnno commented 6 years ago

Please, do a PR and test it, I don't use haml and thus can't do a functional test.

I'll gladly do, just need some further assistance. I have 0 experience in Python or Sublime development, so please forgive my cluelessness.

  1. How do I create a .sublime-package file out of the linter.py file?
  2. I normally add packages via Package Control. How do I get Sublime to recognize the newly created .sublime-package file as a package? Simply add it to Packages/User?
kaste commented 6 years ago

@braver to take it from here. I'm 🏥 🤒

braver commented 6 years ago

@LeEnno Well, to test this a PR isn't really necessary, we just need confirmation that setting the selector to source.ruby - text.html - text.haml solves the problem. For that, you can just set the "selector" setting for this specific linter: http://www.sublimelinter.com/en/stable/linter_settings.html#selector

If this results in the expected behaviour, ie. it lints ruby but not haml, let me know and I can just update the plugin to make that the default.


If you want to know more about developing packages for Sublime Text, some pointers:

A .sublime-package is really just a zip file that's used as a delivery mechanism. Package Control puts them in "Installed Packages". If you want to make your own package/plugin you can put your files in a subdirectory in "Packages" and ST will read that (i.e. you don't have to zip them into a .sublime-package file). So, "User" is also a package. You use that directory for settings and overrides, not for developing packages.

More info in https://code.tutsplus.com/tutorials/how-to-create-a-sublime-text-2-plugin--net-22685 and https://www.sublimetext.com/docs/3/

sman591 commented 6 years ago

Just tested the manual selector override and it works! Doesn't run the rubocop linter at all for .html.haml files. I've since installed haml-lint to properly lint haml.

braver commented 6 years ago

Doesn't run the rubocop linter at all for .html.haml files

Does it still run for ruby?

LeEnno commented 6 years ago

@braver Thanks a lot for your explanation 👍

I can confirm that changing the selector to source.ruby - text.html - text.haml makes rubocop stop linting .html.haml files.

.rb files still get linted by rubocop.

braver commented 6 years ago

Thanks all!