Skip to content

Commit

Permalink
🔍 Add max-line-length rule #833
Browse files Browse the repository at this point in the history
  • Loading branch information
DanPurdy committed Aug 25, 2016
1 parent 54c25a9 commit ecdcc98
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/rules/max-line-length.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Max Line Length

Rule `max-line-length` will enforce that lines do not exceed a max length / limit.

## Examples

When enabled, the following are disallowed:

```scss
.really--long--class-name--that-unfortunately--isnt--very--succint--and-looks-stupid {
color: red;
}

// ==============================================================================
//
// This comment is too long clearly, we should probably make sure we have a rule to
// determine when we breach this length
//
// ==============================================================================
```
1 change: 1 addition & 0 deletions lib/config/sass-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ rules:
hex-notation: 1
indentation: 1
leading-zero: 1
max-line-length: 0
nesting-depth: 1
property-sort-order: 1
pseudo-element: 1
Expand Down
32 changes: 32 additions & 0 deletions lib/rules/max-line-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

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

module.exports = {
'name': 'max-line-length',
'defaults': {
length: 80
},
'detect': function (ast, parser) {
var result = [];

ast.traverseByType('space', function (space) {
var lineLength = 0;
if (helpers.hasEOL(space.content)) {
lineLength = space.start.column - 1;
}

if (lineLength > parser.options.length) {
result = helpers.addUnique(result, {
'ruleId': parser.rule.name,
'severity': parser.severity,
'line': space.start.line,
'column': 0,
'message': 'line ' + space.start.line + ' exceeds the maximum line length of ' + parser.options.length
});
}
});

return result;
}
};
63 changes: 63 additions & 0 deletions tests/rules/max-line-length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

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

//////////////////////////////
// SCSS syntax tests
//////////////////////////////
describe('max-line-length - scss', function () {
var file = lint.file('max-line-length.scss');

it('enforce [default]', function (done) {
lint.test(file, {
'max-line-length': 1
}, function (data) {
lint.assert.equal(5, data.warningCount);
done();
});
});

it('enforce [length: 79]', function (done) {
lint.test(file, {
'max-line-length': [
1,
{
length: 79
}
]
}, function (data) {
lint.assert.equal(8, data.warningCount);
done();
});
});
});

//////////////////////////////
// Sass syntax tests
//////////////////////////////
describe('max-line-length - sass', function () {
var file = lint.file('max-line-length.sass');

it('enforce', function (done) {
lint.test(file, {
'max-line-length': 1
}, function (data) {
lint.assert.equal(5, data.warningCount);
done();
});
});

it('enforce [length: 79]', function (done) {
lint.test(file, {
'max-line-length': [
1,
{
length: 79
}
]
}, function (data) {
lint.assert.equal(8, data.warningCount);
done();
});
});
});
20 changes: 20 additions & 0 deletions tests/sass/max-line-length.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.really--long--class-name--that-unfortunately--isnt--very--succint--and-looks-stupid
color: red

@function($aReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariableName)
@return 'test'

// ==============================================================================
//
// This comment is too long clearly, we should probably make sure we have a rule to
// determine when we breach this length
//
// ==============================================================================
// =============================================================================
//
// This comment comment on the other hand should be the perfect length, unless a
// user decides to make their max line length === 79!
//
// =============================================================================
22 changes: 22 additions & 0 deletions tests/sass/max-line-length.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.really--long--class-name--that-unfortunately--isnt--very--succint--and-looks-stupid {
color: red;
}

@function($aReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongVariableName) {
@return 'test';
}

// ==============================================================================
//
// This comment is too long clearly, we should probably make sure we have a rule to
// determine when we breach this length
//
// ==============================================================================


// =============================================================================
//
// This comment comment on the other hand should be the perfect length, unless a
// user decides to make their max line length === 79!
//
// =============================================================================

0 comments on commit ecdcc98

Please sign in to comment.