Skip to content

Commit

Permalink
Add maximumLineLength rule
Browse files Browse the repository at this point in the history
This rule reports lines in Pug file that exceed the specified length.

Related to pugjs#3
  • Loading branch information
adrienverge committed Apr 13, 2017
1 parent d87bbb3 commit 1909828
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/rules.md
Expand Up @@ -333,6 +333,12 @@ h1= translate(`${title} text`)
h1= translate(`${title} text`)
```

# maximumLineLength: `int`

## e.g.: `80`

Lines in Pug file must not exceed the specified length.

# maximumNumberOfLines: `int`

Pug files should be at most the number of lines specified.
Expand Down
33 changes: 33 additions & 0 deletions lib/rules/maximum-line-length.js
@@ -0,0 +1,33 @@
// # maximumLineLength: `int`
//
// ## e.g.: `80`
//
// Lines in Pug file must not exceed the specified length.

var assert = require('assert');

module.exports = function () {};

module.exports.prototype = {
name: 'maximumLineLength',

schema: {
type: ['null', 'integer']
},

configure: function (options) {
assert(typeof options === 'number', this.name + ' option requires number value or should be removed');

this._maximumLineLength = options;
},

lint: function (file, errors) {
var max = this._maximumLineLength;

file.getLines().forEach(function (line, index) {
if (line.length > max) {
errors.add('Line length exceeds the maximum of ' + max + ' chars', index + 1);
}
});
}
};
8 changes: 8 additions & 0 deletions schemas/pug-lintrc-schema.json
Expand Up @@ -243,6 +243,14 @@
"description": "Pug must not contain template strings. `true` only fails when a template\nstring is used directly; `'all'` fails when template strings are used at\nall.",
"documentation": "# disallowTemplateString: `true` | `'all'`\n\nPug must not contain template strings. `true` only fails when a template\nstring is used directly; `'all'` fails when template strings are used at\nall.\n\n## e.g. `true`\n\n```pug\n//- Invalid\nh1= `${title} text`\n\n//- Valid\nh1= translate(`${title} text`)\n```\n\n## e.g. `'all'`\n\n```pug\n//- Invalid\nh1= translate(`${title} text`)\n```\n"
},
"maximumLineLength": {
"type": [
"null",
"integer"
],
"description": "Lines in Pug file must not exceed the specified length.",
"documentation": "# maximumLineLength: `int`\n\n## e.g.: `80`\n\nLines in Pug file must not exceed the specified length.\n"
},
"maximumNumberOfLines": {
"type": [
"null",
Expand Down
48 changes: 48 additions & 0 deletions test/rules/maximum-line-length.test.js
@@ -0,0 +1,48 @@
module.exports = createTest;

var assert = require('assert');

function createTest(linter) {
describe('maximumLineLength', function () {
describe('int', function () {
before(function () {
linter.configure({maximumLineLength: 40});
});

var ok = 'html\n' +
' body\n' +
' div This is a 40-char-long line ----\n' +
' p other line\n';
var notOk = 'html\n' +
' body\n' +
' div This is a 41-char-long line -----\n' +
' p other line\n';
var notOk2 = 'html\n' +
' body\n' +
' div This is a 41-char-long line -----\n' +
' p other line\n' +
' div This is a 45-char-long line ---------\n' +
' p other line\n';

it('should not report normal lines', function () {
assert.equal(linter.checkString(ok).length, 0);
});

it('should report lines too long', function () {
assert.equal(linter.checkString(notOk).length, 1);
});

it('should report multiple errors', function () {
var result = linter.checkString(notOk2);

assert.equal(result.length, 2);
assert.equal(result[0].code, 'PUG:LINT_MAXIMUMLINELENGTH');
assert.equal(result[0].line, 3);
assert.equal(result[0].column, undefined);
assert.equal(result[1].code, 'PUG:LINT_MAXIMUMLINELENGTH');
assert.equal(result[1].line, 5);
assert.equal(result[1].column, undefined);
});
});
});
}

0 comments on commit 1909828

Please sign in to comment.