Skip to content

Commit

Permalink
Add require-strict-extensions rule
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu committed Jun 4, 2016
1 parent 98b6f75 commit 229ad1d
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,25 @@ div.class(class='class')
.class(class='class')
```

# validateExtensions: `true`

Pug template must use proper file extensions with inclusion and inheritance
(`.pug`).

```pug
//- Invalid
include a
include a.jade
extends a
extends a.txt
extends a.jade
//- Valid
include a.txt
include a.pug
extends a.pug
```

# validateIndentation: `int` | `"\t"`

## e.g.: `2`
Expand Down
57 changes: 57 additions & 0 deletions lib/rules/validate-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// # validateExtensions: `true`
//
// Pug template must use proper file extensions with inclusion and inheritance
// (`.pug`).
//
// ```pug
// //- Invalid
// include a
// include a.jade
// extends a
// extends a.txt
// extends a.jade
//
// //- Valid
// include a.txt
// include a.pug
// extends a.pug
// ```

var path = require('path');
var utils = require('../utils');

module.exports = function () {};

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

configure: function (options) {
utils.validateTrueOptions(this.name, options);
},

lint: function (file, errors) {
var tokens = file.getTokensByFilter(function (token) {
return token.type === 'include' || token.type === 'extends' || token.type === 'path';
});

tokens.forEach(function (token, i) {
var next = tokens[i + 1];

if (!next || next.type !== 'path') {
return;
}

if (token.type === 'include') {
if (path.basename(next.val).indexOf('.') === -1) {
errors.add('Included file path must have a file extension', next.line, next.col);
} else if (path.extname(next.val) === '.jade') {
errors.add('Included Pug file must end in .pug', next.line, next.col);
}
} else if (token.type === 'extends') {
if (path.extname(next.val) !== '.pug') {
errors.add('Extended Pug file must end in .pug', next.line, next.col);
}
}
});
}
};
8 changes: 8 additions & 0 deletions test/fixtures/rules/validate-extensions.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include a
include a.jade
include a.txt
include a.pug
extends a
extends a.jade
extends a.txt
extends a.pug
58 changes: 58 additions & 0 deletions test/rules/validate-extensions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module.exports = createTest;

var assert = require('assert');

function createTest(linter, fixturesPath) {
describe('validateExtensions', function () {
describe('true', function () {
before(function () {
linter.configure({validateExtensions: true});
});

it('should report include no extensions', function () {
assert.equal(linter.checkString('include a').length, 1);
});

it('should report include `.jade` extension', function () {
assert.equal(linter.checkString('include a.jade').length, 1);
});

it('should not report raw include extensions', function () {
assert.equal(linter.checkString('include a.txt').length, 0);
});

it('should not report include `.pug` extension', function () {
assert.equal(linter.checkString('include a.pug').length, 0);
});

it('should report extends no extensions', function () {
assert.equal(linter.checkString('extends a').length, 1);
});

it('should report extends `.jade` extension', function () {
assert.equal(linter.checkString('extends a.jade').length, 1);
});

it('should report other extends extensions', function () {
assert.equal(linter.checkString('extends a.txt').length, 1);
});

it('should not report extends `.pug` extension', function () {
assert.equal(linter.checkString('extends a.pug').length, 0);
});

it('should report multiple errors found in file', function () {
var result = linter.checkFile(fixturesPath + 'validate-extensions.pug');

assert.equal(result.length, 5);
assert.equal(result[0].code, 'PUG:LINT_VALIDATEEXTENSIONS');
assert.equal(result[0].line, 1);
assert.equal(result[0].column, 9);
assert.equal(result[1].line, 2);
assert.equal(result[2].line, 5);
assert.equal(result[3].line, 6);
assert.equal(result[4].line, 7);
});
});
});
}

0 comments on commit 229ad1d

Please sign in to comment.