Skip to content

Commit

Permalink
Merge pull request #605 from gyoshev/feature/no-trailing-whitespace-rule
Browse files Browse the repository at this point in the history
New rule: no-trailing-whitespace
  • Loading branch information
DanPurdy committed Apr 19, 2016
2 parents 9656e0a + 3523f52 commit 45d5fb8
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/rules/no-trailing-whitespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# No Trailing Whitespace

Rule `no-trailing-whitespace` will enforce that trailing whitespace is not allowed.

## Examples

When enabled, the following are disallowed (\s denotes spaces or tabs):

```scss
.foo {\s
margin: 1.5rem;
}

.foo {
margin: .5rem;\s
}

.foo {
margin: .4rem;
}\s
```
1 change: 1 addition & 0 deletions lib/config/sass-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ rules:
no-mergeable-selectors: 1
no-misspelled-properties: 1
no-qualifying-elements: 1
no-trailing-whitespace: 1
no-trailing-zero: 1
no-transition-all: 1
no-url-protocols: 1
Expand Down
37 changes: 37 additions & 0 deletions lib/rules/no-trailing-whitespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

var helpers = require('../helpers');

module.exports = {
'name': 'no-trailing-whitespace',
'defaults': {},
'detect': function (ast, parser) {
var result = [];
var trailing = (/( |\t)+\n/);

ast.traverseByType('space', function (space, i, parent) {
var content = space.content;
var nextIndex = i + 1;
var next = parent.content[nextIndex];

while (next && (next.is('space') || next.is('declarationDelimeter'))) {
content += next.content;
nextIndex++;
next = parent.content[nextIndex];
}

if (trailing.test(content)) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'severity': parser.severity,
'line': space.start.line,
'column': space.start.column,
'message': 'No trailing whitespace allowed'
});
}
});

return result;
}
};

35 changes: 35 additions & 0 deletions tests/rules/no-trailing-whitespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var lint = require('./_lint');

//////////////////////////////
// SCSS syntax tests
//////////////////////////////
describe('no trailing whitespace - scss', function () {
var file = lint.file('no-trailing-whitespace.scss');

it('enforce', function (done) {
lint.test(file, {
'no-trailing-whitespace': 1
}, function (data) {
lint.assert.equal(5, data.warningCount);
done();
});
});
});

//////////////////////////////
// Sass syntax tests
//////////////////////////////
describe('no trailing whitespace - sass', function () {
var file = lint.file('no-trailing-whitespace.sass');

it('enforce', function (done) {
lint.test(file, {
'no-trailing-whitespace': 1
}, function (data) {
lint.assert.equal(5, data.warningCount);
done();
});
});
});
24 changes: 24 additions & 0 deletions tests/sass/no-trailing-whitespace.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.foo
margin: 1.5rem;

// Two trailing spaces after selector
.bar
margin: 1.5rem;

// Two trailing spaces after property
.baz
margin: 1.5rem;

// Two trailing spaces between rules
.qux
margin: 1.5rem;


// Trailing tab after selector
.cat
margin: 1.5rem;

// Trailing tab after property
.dog
margin: 1.5rem;

29 changes: 29 additions & 0 deletions tests/sass/no-trailing-whitespace.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
.foo {
margin: 1.5rem;
}

// Two trailing spaces after selector
.bar {
margin: 1.5rem;
}

// Two trailing spaces after property
.baz {
margin: 1.5rem;
}

// Two trailing spaces after rule
.qux {
margin: 1.5rem;
}

// Trailing tab after selector
.cat {
margin: 1.5rem;
}

// Trailing tab after property
.dog {
margin: 1.5rem;
}

0 comments on commit 45d5fb8

Please sign in to comment.