Skip to content

Commit

Permalink
feat: added attr-no-unnecessary-whitespace rule (#385)
Browse files Browse the repository at this point in the history
* Added attr-no-unnecessary-whitespace rule

* add tests

* remove console.log statement

* fix: merge conflicts

Co-authored-by: pcfutures <me@jaketaylor.co>
  • Loading branch information
2 people authored and Christopher Quadflieg committed May 19, 2020
1 parent 518a1a2 commit f87aef0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/rules/attr-no-unnecessary-whitespace.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
export default {
id: 'attr-no-unnecessary-whitespace',
description: 'No spaces between attribute names and values.',
init: function (parser, reporter, options) {
var self = this
var exceptions = Array.isArray(options) ? options : []

parser.addListener('tagstart', function (event) {
var attrs = event.attrs
var col = event.col + event.tagName.length + 1

for (var i = 0; i < attrs.length; i++) {
if (exceptions.indexOf(attrs[i].name) === -1) {
var match = /(\s*)=(\s*)/.exec(attrs[i].raw.trim())
if (match && (match[1].length !== 0 || match[2].length !== 0)) {
reporter.error(
"The attribute '" +
attrs[i].name +
"' must not have spaces between the name and value.",
event.line,
col + attrs[i].index,
self,
attrs[i].raw
)
}
}
}
})
},
}
id: 'attr-no-unnecessary-whitespace',
description: 'No spaces between attribute names and values.',
init: function (parser, reporter, options) {
var self = this;
var exceptions = Array.isArray(options) ? options : [];
parser.addListener('tagstart', function (event) {
var attrs = event.attrs,
col = event.col + event.tagName.length + 1;
for (var i = 0; i < attrs.length; i++) {
if (exceptions.indexOf(attrs[i].name) === -1 && /[^=](\s+=\s+|=\s+|\s+=)/g.test(attrs[i].raw.trim())) {
reporter.error(
'The attribute \'' + attrs[i].name + '\' must not have spaces between the name and value.',
event.line,
col + attrs[i].index,
self,
attrs[i].raw
);
}
}
});
}
};
1 change: 1 addition & 0 deletions src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export { default as tagnameLowercase } from './tagname-lowercase';
export { default as tagnameSpecialChars } from './tagname-specialchars';
export { default as titleRequire } from './title-require';
export { default as tagsCheck } from './tags-check';
export { default as attrNoUnnecessaryWhitespace } from './attr-no-unnecessary-whitespace';
31 changes: 31 additions & 0 deletions test/rules/attr-no-unnecessary-whitespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var expect = require('expect.js');

var HTMLHint = require('../../dist/htmlhint.js').HTMLHint;

var ruldId = 'attr-no-unnecessary-whitespace',
ruleOptions = {};

ruleOptions[ruldId] = true;

describe('Rules: ' + ruldId, function () {
it('Attribute with spaces should result in an error', function () {
var codes = [
'<div title = "a" />',
'<div title= "a" />',
'<div title ="a" />',
];
for (var i = 0; i < codes.length; i++) {
var messages = HTMLHint.verify(codes[i], ruleOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(1);
expect(messages[0].col).to.be(5);
}
});

it('Attribute without spaces should not result in an error', function () {
var code = '<div title="a" />';
var messages = HTMLHint.verify(code, ruleOptions);
expect(messages.length).to.be(0);
});
});

0 comments on commit f87aef0

Please sign in to comment.