Skip to content

Commit

Permalink
Add SpaceAfterComment linter
Browse files Browse the repository at this point in the history
  • Loading branch information
ivantsepp authored and sds committed Aug 18, 2016
1 parent 7bb17f7 commit 21d2444
Show file tree
Hide file tree
Showing 5 changed files with 580 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fix `--exclude-linter` flag
* Modify `ColorKeyword` linter to allow color keywords to be used as arguments
in `map-*`-related function calls
* Add `SpaceAfterComment` which checks for spacing after comment literal

## 0.49.0

Expand Down
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ linters:
enabled: true
style: one_space # or 'no_space', or 'at_least_one_space'

SpaceAfterComment:
enabled: true
style: at_least_one_space # or 'no_space', or 'at_least_one_space'
allow_empty_comments: true

SpaceAfterPropertyColon:
enabled: true
style: one_space # or 'no_space', or 'at_least_one_space', or 'aligned'
Expand Down
25 changes: 25 additions & 0 deletions lib/scss_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Below is a list of linters supported by `scss-lint`, ordered alphabetically.
* [SingleLinePerProperty](#singlelineperproperty)
* [SingleLinePerSelector](#singlelineperselector)
* [SpaceAfterComma](#spaceaftercomma)
* [SpaceAfterComment](#spaceaftercomment)
* [SpaceAfterPropertyColon](#spaceafterpropertycolon)
* [SpaceAfterPropertyName](#spaceafterpropertyname)
* [SpaceAfterVariableColon](#spaceaftervariablecolon)
Expand Down Expand Up @@ -1460,6 +1461,30 @@ Configuration Option | Description
---------------------|---------------------------------------------------------
`style` | `one_space`, or `no_space` or `at_least_one_space` (default **one_space**)

## SpaceAfterComment

Comment literals should be followed by a space.

**Bad: no space after comment literal**
```scss
//no space
/*no space*/
/*!no space for loud comment*/
```

**Good: comment literals followed by a space**
```scss
// one space
/* one space*/
/*! one space for loud comment*/
```

The `style` option allows you to specify a different preferred style.

Configuration Option | Description
-----------------------|---------------------------------------------------------
`allow_empty_comments` | Allow empty comments for `//` style (default **true**)
`style` | `one_space`, or `no_space` or `at_least_one_space` (default **one_space**)

## SpaceAfterPropertyColon

Expand Down
69 changes: 69 additions & 0 deletions lib/scss_lint/linter/space_after_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module SCSSLint
# Checks for a space after comment literals
class Linter::SpaceAfterComment < Linter
include LinterRegistry

def visit_comment(node)
source = source_from_range(node.source_range).strip
check_method = "check_#{node.type}_comment"
send(check_method, node, source)
end

private

def check_silent_comment(node, source)
source.split("\n").each_with_index do |line, index|
next if config['allow_empty_comments'] && line.strip.length <= 2
whitespace = whitespace_after_comment(line, 2)
check_for_space(node.line + index, whitespace)
end
end

def check_normal_comment(node, source)
whitespace = whitespace_after_comment(source, 2)
check_for_space(node, whitespace)
end

def check_loud_comment(node, source)
whitespace = whitespace_after_comment(source, 3)
check_for_space(node, whitespace)
end

def check_for_no_spaces(node_or_line, whitespace)
return if whitespace == 0
add_lint(node_or_line, 'Comment literal should not be followed by any spaces')
end

def check_for_one_space(node_or_line, whitespace)
return if whitespace == 1
add_lint(node_or_line, 'Comment literal should be followed by one space')
end

def check_for_at_least_one_space(node_or_line, whitespace)
return if whitespace >= 1
add_lint(node_or_line, 'Comment literal should be followed by at least one space')
end

def check_for_space(node_or_line, spaces)
case config['style']
when 'one_space'
check_for_one_space(node_or_line, spaces)
when 'no_space'
check_for_no_spaces(node_or_line, spaces)
when 'at_least_one_space'
check_for_at_least_one_space(node_or_line, spaces)
end
end

def whitespace_after_comment(source, offset)
whitespace = 0

while [' ', "\t"].include? source[offset]
whitespace += 1
offset += 1
end

whitespace
end
end
end
Loading

0 comments on commit 21d2444

Please sign in to comment.