Skip to content

Commit

Permalink
Switching leading_underscore convention to option
Browse files Browse the repository at this point in the history
Rather than have this be an entire convention, create an option
`allow_leading_underscore` which regardless of convention specified
allows you to prefix names with underscores to denote them as private.

Change-Id: I380cdf84d8e5d263b76b033137a9e4a69025d9ca
Reviewed-on: http://gerrit.causes.com/45099
Tested-by: jenkins <[email protected]>
Reviewed-by: Shane da Silva <[email protected]>
  • Loading branch information
sds committed Dec 7, 2014
1 parent 0dc0def commit c530441
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
* Fix `DuplicateProperty` to report correct name of duplicated property
* Add `smacss` sort order option for `PropertySortOrder`
* Add `recess` sort order option for `PropertySortOrder`
* Add `leading_underscore` option for `NameFormat`
* Add `allow_leading_underscore` option to `NameFormat` to allow the leading
underscore convention to denote private functions

## 0.30.0

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ linters:

NameFormat:
enabled: true
allow_leading_underscore: true
convention: hyphenated_lowercase # or 'BEM', or a regex pattern

NestingDepth:
Expand Down
14 changes: 8 additions & 6 deletions lib/scss_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -565,16 +565,18 @@ The Sass parser automatically treats underscores and hyphens the same, so even
if you're using a library that declares a function with an underscore, you can
refer to it using the hyphenated form instead.

If you use underscores to denote private functions within your Sass, you can set
the `leading_underscore` option which will enforce all lowercase with hyphens
but allow a single leading underscore like `@function _private-function() {}`.
Depending on whether you use underscores to denote private functions within your
code, you can set the `allow_leading_underscore` option (enabled by default)
which will ignore leading underscores in names if they exist, allowing
declarations like `@function _private-function() { ... }`.

You can also prefer the [BEM](http://bem.info/method/) convention by setting the
`convention` option to `BEM`. Any other value will be treated as a regex.

Configuration Option | Description
---------------------|---------------------------------------------------------
`convention` | Name of convention to use (`hyphenated_lowercase` (default) or `BEM`), or a regex the name must match
Configuration Option | Description
---------------------------|---------------------------------------------------
`allow_leading_underscore` | Whether to allow names to start with a single underscore (default `true`)
`convention` | Name of convention to use (`hyphenated_lowercase` (default) or `BEM`), or a regex the name must match

## NestingDepth

Expand Down
15 changes: 11 additions & 4 deletions lib/scss_lint/linter/name_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ def visit_variable(node)
].to_set

def check_name(node, node_type, node_text = node.name)
node_text = trim_underscore_prefix(node_text)
return unless violation = violated_convention(node_text)

add_lint(node, "Name of #{node_type} `#{node_text}` should be " \
"written #{violation[:explanation]}")
end

# Removes underscore prefix from name if leading underscores are allowed.
def trim_underscore_prefix(name)
if config['allow_leading_underscore']
# Remove if there is a single leading underscore
name = name.gsub(/^_(?!_)/, '')
end

name
end

def check_placeholder(node)
extract_string_selectors(node.selector).any? do |selector_str|
check_name(node, 'placeholder', selector_str.gsub('%', ''))
Expand All @@ -63,10 +74,6 @@ def check_placeholder(node)
explanation: 'in all lowercase letters with hyphens instead of underscores',
validator: ->(name) { name !~ /[_A-Z]/ },
},
'leading_underscore' => {
explanation: 'hyphenated_lowercase with leading underscores permitted',
validator: ->(name) { name !~ /[^_][_A-Z]/ },
},
'BEM' => {
explanation: 'in BEM (Block Element Modifier) format',
validator: ->(name) { name !~ /[A-Z]|-{3}|_{3}|[^_]_[^_]/ },
Expand Down
8 changes: 4 additions & 4 deletions spec/scss_lint/linter/name_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@
}
CSS

it { should report_lint }
it { should_not report_lint }
end
end

context 'when the leading underscore style is specified' do
let(:linter_config) { { 'convention' => 'leading_underscore' } }
context 'when leading underscores are not allowed' do
let(:linter_config) { { 'allow_leading_underscore' => false } }

it_behaves_like 'hyphenated_lowercase'

Expand All @@ -190,7 +190,7 @@
}
CSS

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

Expand Down

0 comments on commit c530441

Please sign in to comment.