Skip to content

Commit

Permalink
Add support for presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Edwards committed Aug 4, 2015
1 parent f095b6d commit b6097f5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ Linter.prototype =

this._configuredRules = []

if (options && options.hasOwnProperty('preset')) {
var presetPath = __dirname + '/../presets/' + options.preset + '.json'

if (fs.existsSync(presetPath)) {
options = JSON.parse(JSON.minify(fs.readFileSync(presetPath, 'utf8')))
} else {
throw new Error('Preset "' + options.preset + '" does not exist')
}
}

glob.sync(__dirname + '/rules/*.js').forEach(function (file) {
var Rule = require(file)
, rule = new Rule()
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/presets/clock--invalid.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div.container
header(class='main-header')
a(href='/') Project Title
img(src='/')
4 changes: 4 additions & 0 deletions test/fixtures/presets/clock--valid.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.container
header.main-header
a(href='/') Project Title
img(src='/', alt='alt')
12 changes: 12 additions & 0 deletions test/linter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ describe('linter', function () {
assert.equal(linter.getConfiguredRules().length, 0)
})

it('should load configured rules for a preset', function () {
linter.configure({ preset: 'clock' })

assert.equal(linter.getConfiguredRules().length > 0, true)
})

it('should error for invalid preset', function () {
assert.throws(function () {
linter.configure({ preset: 'nonexistent' })
}, /Preset "nonexistent" does not exist/)
})

it('should not use disabled rules', function () {
linter.configure({ validateAttributeSeparator: null })

Expand Down
18 changes: 18 additions & 0 deletions test/presets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var glob = require('glob')
, Linter = require('../lib/linter')

describe('presets', function () {

var linter = new Linter()
, rules = []
, fixturesPath = __dirname + '/fixtures/presets/'

glob.sync(__dirname + '/presets/*.test.js').forEach(function (file) {
rules.push(require(file))
})

rules.forEach(function (rule) {
rule(linter, fixturesPath)
})

})
33 changes: 33 additions & 0 deletions test/presets/clock.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = createTest

var assert = require('assert')

function createTest(linter, fixturesPath) {

describe('clock', function () {

describe('true', function () {

before(function () {
linter.configure({ preset: 'clock' })

assert.equal(linter.getConfiguredRules().length, 19)
})

it('should report errors found in file', function () {
var result = linter.checkFile(fixturesPath + 'clock--invalid.jade')

assert.equal(result.length, 2)
})

it('should not report errors in valid file', function () {
var result = linter.checkFile(fixturesPath + 'clock--valid.jade')

assert.equal(result.length, 0)
})

})

})

}

0 comments on commit b6097f5

Please sign in to comment.