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

Added control statement spacing linter #81 #82

Merged
merged 4 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/slim_lint/linter/control_statement_spacing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module SlimLint
# Checks for missing or superfluous spacing before and after control statements.
class Linter::ControlStatementSpacing < Linter
include LinterRegistry

MESSAGE = 'Please add a space before and after the `=`'

on [:html, :tag, anything, [],
[:slim, :output, anything, capture(:ruby, anything)]] do |sexp|

# Fetch original Slim code that contains an element with a control statement.
line = document.source_lines[sexp.line() - 1]

# Remove any Ruby code, because our regexp below must not match inside Ruby.
ruby = captures[:ruby]
line = line.sub(ruby, 'x')

next if line =~ /[^ ] = [^ ]/
report_lint(sexp, MESSAGE)
end
end
end
177 changes: 177 additions & 0 deletions spec/slim_lint/linter/control_statement_spacing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# frozen_string_literal: true

require 'spec_helper'

describe SlimLint::Linter::ControlStatementSpacing do
include_context 'linter'

context 'element missing space before =' do
let(:slim) { 'div= bad' }

it { should report_lint }
end

context 'element missing space after =' do
let(:slim) { 'div =bad' }

it { should report_lint }
end

context 'element missing space around =' do
let(:slim) { 'div=bad' }

it { should report_lint }
end

context 'element too much space before =' do
let(:slim) { 'div =bad' }

it { should report_lint }
end

context 'element too much space after =' do
let(:slim) { 'div= bad' }

it { should report_lint }
end

context 'id missing space before =' do
let(:slim) { '#submit= bad' }

it { should report_lint }
end

context 'id missing space after =' do
let(:slim) { '#submit =bad' }

it { should report_lint }
end

context 'id missing space around =' do
let(:slim) { '#submit=bad' }

it { should report_lint }
end

context 'id and class missing space around =' do
let(:slim) { '.some-class#submit=bad' }

it { should report_lint }
end

context 'id too much space before =' do
let(:slim) { '#submit =bad' }

it { should report_lint }
end

context 'id too much space after =' do
let(:slim) { '#submit= bad' }

it { should report_lint }
end

context 'class missing space before =' do
let(:slim) { '.klass= bad' }

it { should report_lint }
end

context 'class missing space after =' do
let(:slim) { '.klass =bad' }

it { should report_lint }
end

context 'class missing space around =' do
let(:slim) { '.klass=bad' }

it { should report_lint }
end

context 'class too much space before =' do
let(:slim) { '.klass =bad' }

it { should report_lint }
end

context 'class too much space after =' do
let(:slim) { '.klass= bad' }

it { should report_lint }
end

context 'class with hyphen missing space before =' do
let(:slim) { '.some-klass= bad' }

it { should report_lint }
end

context 'class with hyphen missing space after =' do
let(:slim) { '.some-klass =bad' }

it { should report_lint }
end

context 'class with hyphen missing space around =' do
let(:slim) { '.some-klass=bad' }

it { should report_lint }
end

context 'class with hyphen too much space before =' do
let(:slim) { '.some-klass =bad' }

it { should report_lint }
end

context 'class with hyphen too much space after =' do
let(:slim) { '.some-klass= bad' }

it { should report_lint }
end

context 'ruby code that contains a properly formatted equal sign' do
let(:slim) { 'div =bad = 1' }

it { should report_lint }
end

context 'ruby code that contains a properly formatted equal sign' do
let(:slim) { 'div= bad = 1' }

it { should report_lint }
end

context 'ruby code that contains a properly formatted equal sign' do
let(:slim) { 'div = bad = 1' }

it { should report_lint }
end

# OK

context 'ruby code that contains an equal sign without spacing' do
let(:slim) { 'div = ok=1' }

it { should_not report_lint }
end

context 'element with hyphen' do
let(:slim) { 'div - ok' }

it { should_not report_lint }
end

context 'control statement without element' do
let(:slim) { '= ok' }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this doesn't have any opinion on =ok which, according to my source files, does not seem to raise an error. In my opinion, = ok (with the space) should be enforced. Any opinions from the author or @sds?

Copy link
Contributor Author

@mmzoo mmzoo Oct 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consistently use the space as it's more readable. Slim's official snippets also keep that standard. I thought this was already enforced 😄

I just checked, you're right and it is not enforced, the following test fails (i.e. nothing is reported):

  context 'control statement without element and without space' do
    let(:slim) { '=ok' }

    it { should report_lint }
  end


it { should_not report_lint }
end

context 'attribute with equal sign without spacing' do
let(:slim) { 'a href=ok' }

it { should_not report_lint }
end
end