-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the
StrictLocals
linter following Rails' recommendations for u…
…sing local variables in partials. Close #486
- Loading branch information
Showing
5 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module HamlLint | ||
# Checks for the presence of a `locals` magic comment at the beginning of a partial. | ||
class Linter::StrictLocals < Linter | ||
include LinterRegistry | ||
|
||
DummyNode = Struct.new(:line) | ||
|
||
# Enables the linter if the tree is for the right file type. | ||
# | ||
# @param [HamlLint::Tree::RootNode] the root of a syntax tree | ||
# @return [true, false] whether the linter is enabled for the tree | ||
def visit_root(root) | ||
return unless enabled?(root) | ||
|
||
first_children = root.children.first | ||
return if first_children.is_a?(HamlLint::Tree::HamlCommentNode) && | ||
first_children.is_strict_locals? | ||
|
||
record_lint(DummyNode.new(1), failure_message) | ||
end | ||
|
||
private | ||
|
||
# Checks whether the linter is enabled for the file. | ||
# | ||
# @api private | ||
# @return [true, false] | ||
def enabled?(root) | ||
matcher.match(File.basename(root.file)) ? true : false | ||
end | ||
|
||
# The type of files the linter is configured to check. | ||
# | ||
# @api private | ||
# @return [String] | ||
def file_types | ||
@file_types ||= config['file_types'] || 'partial' | ||
end | ||
|
||
# The matcher to use for testing whether to check a file by file name. | ||
# | ||
# @api private | ||
# @return [Regexp] | ||
def matcher | ||
@matcher ||= Regexp.new(config['matchers'][file_types] || '\A_.*\.haml\z') | ||
end | ||
|
||
# The error message when an `locals` comment is not found. | ||
# | ||
# @api private | ||
# @return [String] | ||
def failure_message | ||
'Expected a strict `-# locals: ()` comment at the beginning of the file' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe HamlLint::Linter::StrictLocals do | ||
include_context 'linter' | ||
|
||
context 'when the file name does not match the matcher' do | ||
let(:haml) do | ||
[ | ||
'%p= greeting', | ||
'%p{ title: greeting }', | ||
':ruby', | ||
' x = greeting' | ||
].join("\n") | ||
end | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'when the file name matches the matcher' do | ||
let(:options) do | ||
{ | ||
config: HamlLint::ConfigurationLoader.default_configuration, | ||
file: '_partial.html.haml' | ||
} | ||
end | ||
|
||
context 'and there is a strict locals comment' do | ||
let(:haml) do | ||
[ | ||
'-# locals: (greeting:)', | ||
'%p Hello, world' | ||
].join("\n") | ||
end | ||
|
||
it { should_not report_lint } | ||
end | ||
|
||
context 'and there is no strict locals comment' do | ||
let(:haml) { '%p Hello, world' } | ||
|
||
it { should report_lint line: 1 } | ||
end | ||
end | ||
|
||
context 'with a custom matcher' do | ||
let(:haml) { '%p= @greeting' } | ||
let(:full_config) do | ||
HamlLint::Configuration.new( | ||
'linters' => { | ||
'StrictLocals' => { | ||
'file_types' => 'my_custom', | ||
'matchers' => { | ||
'my_custom' => '\Apartial_.*\.haml\z' | ||
} | ||
} | ||
} | ||
) | ||
end | ||
|
||
let(:options) do | ||
{ | ||
config: full_config, | ||
file: file | ||
} | ||
end | ||
|
||
context 'that matches the file name' do | ||
let(:file) { 'partial_view.html.haml' } | ||
|
||
it { should report_lint line: 1 } | ||
end | ||
|
||
context 'that does not match the file name' do | ||
let(:file) { 'view.html.haml' } | ||
|
||
it { should_not report_lint } | ||
end | ||
end | ||
end |