-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule to require specific attributes
- Loading branch information
Ben Edwards
committed
Aug 4, 2015
1 parent
a9de52b
commit 93a9fe3
Showing
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var assert = require('assert') | ||
, utils = require('../utils') | ||
|
||
module.exports = function () {} | ||
|
||
module.exports.prototype = | ||
{ name: 'requireSpecificAttributes' | ||
|
||
, configure: function (options) { | ||
|
||
assert(typeof options === 'object' | ||
, this.name + ' option requires array value or should be removed') | ||
|
||
this._requiredAttributes = options | ||
} | ||
|
||
, lint: function (file, errors) { | ||
|
||
this._requiredAttributes.forEach(function (attribute) { | ||
file.iterateTokensByFilter(function (token) { | ||
return token.type === 'tag' && utils.ownProperty(attribute, token.val) | ||
}, function (token) { | ||
var lineNumber = token.line | ||
, tag = token.val | ||
, requiredAttributes = utils.ownProperty(attribute, tag) | ||
, hasAttributes | ||
|
||
if (typeof requiredAttributes === 'string') { | ||
requiredAttributes = [ requiredAttributes ] | ||
} | ||
|
||
file.iterateTokensByFilter(function (token) { | ||
if (token.type === 'attrs' && token.line === lineNumber) { | ||
hasAttributes = true | ||
return true | ||
} | ||
|
||
return false | ||
}, function (token) { | ||
var attributeNames = [] | ||
|
||
token.attrs.forEach(function (attribute) { | ||
attributeNames.push(attribute.name.toLowerCase()) | ||
}) | ||
|
||
requiredAttributes.forEach(function (attribute) { | ||
if (attributeNames.indexOf(attribute.toLowerCase()) === -1) { | ||
errors.add('Tag "' + tag + '" must have attribute "' + attribute + '"', lineNumber) | ||
} | ||
}) | ||
}) | ||
|
||
if (!hasAttributes) { | ||
errors.add('Tag "' + tag + '" must has attributes "' + requiredAttributes.join('", "') + '"', lineNumber) | ||
} | ||
}) | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
abbr(title='title', lang='en-gb') | ||
img(title='title') | ||
abbr | ||
img(alt='alt') | ||
abbr(title='title') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module.exports = createTest | ||
|
||
var assert = require('assert') | ||
|
||
function createTest(linter, fixturesPath) { | ||
|
||
describe('requireSpecificAttributes', function () { | ||
|
||
describe('object', function () { | ||
|
||
var options = | ||
[ { 'img': 'alT' } | ||
, { 'Abbr': [ 'title', 'laNg' ] } | ||
] | ||
|
||
before(function () { | ||
linter.configure({ requireSpecificAttributes: options }) | ||
}) | ||
|
||
it('should report missing required attributes', function () { | ||
assert.equal(linter.checkString('img(title=\'title\')').length, 1) | ||
}) | ||
|
||
it('should not report existing attributes', function () { | ||
assert.equal(linter.checkString('img(alt=\'alt\')').length, 0) | ||
}) | ||
|
||
it('should report multiple errors found in file', function () { | ||
var result = linter.checkFile(fixturesPath + 'require-specific-attributes.jade') | ||
|
||
assert.equal(result.length, 3) | ||
assert.equal(result[0].code, 'JADE:LINT_REQUIRESPECIFICATTRIBUTES') | ||
assert.equal(result[0].line, 2) | ||
assert.equal(result[1].line, 3) | ||
assert.equal(result[2].line, 5) | ||
}) | ||
|
||
}) | ||
|
||
}) | ||
|
||
} |