Skip to content

Commit

Permalink
M26656 Create linter rule for id/name checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Krautz committed Jan 25, 2017
1 parent c2f722a commit 088ab44
Show file tree
Hide file tree
Showing 5 changed files with 3,887 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ var errors = {
E045: 'tags in <html> may not be duplicated',
E046: '<head> tag must come before <body> in <html>',
E047: 'the only tags allowed in the <head> are base, link, meta, noscript, script, style, template, and title',
E048: 'invalid value for option <%= option %>: <%= value =>'
E048: 'invalid value for option <%= option %>: <%= value =>',
E049: 'tag must have an associated name',
};

module.exports.errors = {};
Expand Down
1 change: 1 addition & 0 deletions lib/presets/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
'focusable-tabindex-style' : false,
'input-radio-req-name': true,
'input-req-label': false,
'tag-req-id-or-name': false,
'table-req-caption': false,
'table-req-header': false
};
27 changes: 27 additions & 0 deletions lib/rules/tag-req-id-or-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var knife = require('../knife'),
Issue = require('../issue'),
proc = require('../processOption');

module.exports = {
name: 'tag-req-name',
on: ['dom'],
filter: ['tag', 'style', 'script'],
desc: [
'The value of this option is a list of strings, each of which is a tag',
'name. Tags with any of the given names are disallowed.'
].join('\n'),
process: proc.arrayOfStr
};

module.exports.lint = function (element, opts) {
var format = opts[this.name];
if (format.indexOf(element.name) < 0) {
return [];
}

if (knife.hasNonEmptyAttr(element, 'id') || knife.hasNonEmptyAttr(element, 'name')) {
return [];
}

return new Issue('E016', element.openLineCol, { tag: element.name });
};
92 changes: 92 additions & 0 deletions test/functional/tag-req-id-or-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module.exports = [
{
desc: 'should pass when set to false',
input: '<input type="radio">',
opts: { 'tag-req-name': false },
output: 0
},
{
desc: 'should pass when tag not listed in options',
input: '<input type="radio">',
opts: { 'tag-req-name': ['div'] },
output: 0
},
{
desc: 'should fail when input has no type',
input: '<input>',
opts: { 'tag-req-name': ['input'] },
output: 1
},
{
desc: 'should fail when input has no type value',
input: '<input type>',
opts: { 'tag-req-name': ['input'] },
output: 1
},
{
desc: 'should fail when input has type text',
input: '<input type="text">',
opts: { 'tag-req-name': ['input'] },
output: 1
},
{
desc: 'should fail when radio input has no id or name',
input: '<input type="radio" >',
opts: { 'tag-req-name': ['input'] },
output: 1
},
{
desc: 'should fail when button has no id or name',
input: '<button>',
opts: { 'tag-req-name': ['button'] },
output: 1
},
{
desc: 'should fail when anchor has no id or name',
input: '<a></a>',
opts: { 'tag-req-name': ['a'] },
output: 1
},
{
desc: 'should fail when form has no id or name',
input: '<form></form>',
opts: { 'tag-req-name': ['form'] },
output: 1
},
{
desc: 'should fail when radio input has empty name',
input: '<input type="radio" name>',
opts: { 'tag-req-name': ['input'] },
output: 1
},
{
desc: 'should fail when radio input has name with no length',
input: '<input type="radio" name="">',
opts: { 'tag-req-name': ['input'] },
output: 1
},
{
desc: 'should pass when input has a name',
input: '<input type="radio" name="hello">',
opts: { 'tag-req-name': ['input'] },
output: 0
},
{
desc: 'should pass when button has a name',
input: '<button name="hello">',
opts: { 'tag-req-name': ['button'] },
output: 0
},
{
desc: 'should pass when form has a name',
input: '<form name="hello"></form>',
opts: { 'tag-req-name': ['form'] },
output: 0
},
{
desc: 'should pass when anchor has a name',
input: '<a name="hello"></a>',
opts: { 'tag-req-name': ['a'] },
output: 0
}
];
Loading

0 comments on commit 088ab44

Please sign in to comment.