8000 Simplify QuoteConsistency by utilizing html matcher by jarommadsen · Pull Request #197 · sds/slim-lint · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Simplify QuoteConsistency by utilizing html matcher #197

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

Merged
merged 1 commit into from
Mar 4, 2025
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
33 changes: 5 additions & 28 deletions lib/slim_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,9 @@ Long lines are harder to read and usually indicative of complexity.

Reports inconsistent quote usage.

| Option | Description |
| ----------------- | ------------------------------------------- |
| `enforced_style` | Style to enforce. (default `single_quotes`) |
| `skip_ruby_lines` | Skips linting Ruby lines. (default `true`) |
| Option | Description |
| ---------------- | ------------------------------------------- |
| `enforced_style` | Style to enforce. (default `single_quotes`) |

### Enforced Style

Expand Down Expand Up @@ -271,35 +270,13 @@ p style="{ color: red; }" Something
p style='color: red;' Something
```

### Skip Ruby Lines
### Nested Quotes

By default, the linter will skip Ruby lines. This is to prevent issues with
RuboCop checking the same lines.

#### skip_ruby_lines: true

**Good**

```slim
- title = "Hello World"
```

#### skip_ruby_lines: false

**Bad**

```slim
- title = "Hello World"
```

### Exceptions

Comments and nested quotes are exempt from this linter.
Nested quotes are exempt from this linter.

The following is valid:

```slim
/ This is a "comment"
a href='javascript:void(0)' >
```

Expand Down
43 changes: 16 additions & 27 deletions lib/slim_lint/linter/quote_consistency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,22 @@ class Linter::QuoteConsistency < Linter

MSG = 'Inconsistent quote style. %s'

on_start do |_sexp|
dummy_node = Struct.new(:line)
document.source_lines.each_with_index do |line, index|
# Skip lines without any quotes
next unless line =~ /['"]/

# Skip comments
next if line =~ %r{^\s*(/{1,3})}

# Skip Ruby lines that RuboCop will check
next if skip_ruby_lines && line =~ /^\s*[=-]/

# Find all quoted strings in attributes (ignoring nested quotes)
single_quotes = line.scan(/^(?:[^'"]*'[^'"]*'[^'"]*)?(?:[^'"]*)('[^'"]*')/)
double_quotes = line.scan(/^(?:[^'"]*'[^'"]*'[^'"]*)?(?:[^'"]*)("[^'"]*")/)

if enforced_style == :single_quotes && double_quotes.any?
report_lint(dummy_node.new(index + 1),
format(MSG, "Use single quotes for attribute values (')"))
elsif enforced_style == :double_quotes && single_quotes.any?
report_lint(dummy_node.new(index + 1),
format(MSG, 'Use double quotes for attribute values (")'))
end
on [:html, :attrs] do |node|
line = document.source_lines[node.line - 1]

# Skip lines without any quotes
next unless line =~ /['"]/

# Find all quoted strings in attributes (ignoring nested quotes)
single_quotes = line.scan(/^(?:[^'"]*'[^'"]*'[^'"]*)?(?:[^'"]*)('[^'"]*')/)
double_quotes = line.scan(/^(?:[^'"]*'[^'"]*'[^'"]*)?(?:[^'"]*)("[^'"]*")/)

if enforced_style == :single_quotes && double_quotes.any?
report_lint(node,
format(MSG, "Use single quotes for attribute values (')"))
elsif enforced_style == :double_quotes && single_quotes.any?
report_lint(node,
format(MSG, 'Use double quotes for attribute values (")'))
end
end

Expand All @@ -38,9 +31,5 @@ class Linter::QuoteConsistency < Linter
def enforced_style
config['enforced_style']&.to_sym || :single_quotes
end

def skip_ruby_lines
config.fetch('skip_ruby_lines', true)
end
end
end
14 changes: 2 additions & 12 deletions spec/slim_lint/linter/quote_consistency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

context 'when line has multiple quoted strings' do
let(:slim) { <<-SLIM }
.input name='name' value="value"
.input name='test-name' value="test-value"
SLIM

it { should report_lint line: 1 }
Expand All @@ -86,16 +86,6 @@
- title = "Hello World"
SLIM

context 'when skip_ruby_lines is true' do
let(:config) { { 'skip_ruby_lines' => true } }

it { should_not report_lint }
end

context 'when skip_ruby_lines is false' do
let(:config) { { 'skip_ruby_lines' => false } }

it { should report_lint line: 1 }
end
it { should_not report_lint }
end
end
0