Skip to content

Commit

Permalink
Add camel_case convention to NameFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
sds committed Apr 13, 2015
1 parent f73135a commit a2ddd68
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
this is a particular dialect which may not be what developers expect
* Allow `SelectorFormat` convention explanations to be customized via the
`#{type}_convention_explanation` and `convention_explanation` options
* Add `snake_case` convention to `NameFormat`
* Add `camel_case` and `snake_case` conventions to `NameFormat`
* Remove `BEM` convention from `NameFormat`, as it didn't make sense in this
context since `NameFormat` does not deal with selectors

Expand Down
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ linters:
NameFormat:
enabled: true
allow_leading_underscore: true
convention: hyphenated_lowercase # or 'BEM', or a regex pattern
convention: hyphenated_lowercase # or 'camel_case', or 'snake_case', or a regex pattern

NestingDepth:
enabled: true
Expand Down
6 changes: 3 additions & 3 deletions lib/scss_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,8 @@ Configuration Option | Description

## NameFormat

Functions, mixins, and variables should be declared with all lowercase letters
and hyphens instead of underscores.
Functions, mixins, variables, and placeholders should be declared with all
lowercase letters and hyphens instead of underscores.

**Bad: uppercase characters**
```scss
Expand Down Expand Up @@ -654,7 +654,7 @@ You can also prefer the [BEM](http://bem.info/method/) convention by setting the
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), `snake_case`, or `BEM`), or a regex the name must match (eg: `^[a-zA-Z]+$`)
`convention` | Name of convention to use (`hyphenated_lowercase` (default), `camel_case`, `snake_case`), or a regex the name must match (eg: `^[a-zA-Z]+$`)

## NestingDepth

Expand Down
8 changes: 6 additions & 2 deletions lib/scss_lint/linter/name_format.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SCSSLint
# Checks that the declared names of functions, mixins, and variables are all
# lowercase and use hyphens instead of underscores.
# Checks the format of declared names of functions, mixins, variables, and
# placeholders.
class Linter::NameFormat < Linter
include LinterRegistry

Expand Down Expand Up @@ -70,6 +70,10 @@ def check_placeholder(node)
end

CONVENTIONS = {
'camel_case' => {
explanation: 'should be written in camelCase format',
validator: ->(name) { name =~ /^[a-z][a-zA-Z0-9]*$/ },
},
'snake_case' => {
explanation: 'should be written in snake_case',
validator: ->(name) { name !~ /[^_a-z0-9]/ },
Expand Down
28 changes: 28 additions & 0 deletions spec/scss_lint/linter/name_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,34 @@
end
end

context 'when the camel_case convention is specified' do
let(:linter_config) { { 'convention' => 'camel_case' } }

context 'when a name contains all lowercase letters' do
let(:scss) { '$variable: 1;' }

it { should_not report_lint }
end

context 'when a name contains all lowercase letters and underscores' do
let(:scss) { '$my_variable: 1;' }

it { should report_lint }
end

context 'when a name contains all lowercase letters and hyphens' do
let(:scss) { '$my-variable: 1;' }

it { should report_lint }
end

context 'when a name is written in camelCase' do
let(:scss) { '$myVariable: 1;' }

it { should_not report_lint }
end
end

context 'when the snake_case convention is specified' do
let(:linter_config) { { 'convention' => 'snake_case' } }

Expand Down

0 comments on commit a2ddd68

Please sign in to comment.