Skip to content

Commit

Permalink
fix #73
Browse files Browse the repository at this point in the history
space-tab-mixed-disabled plugin: add space length require
  • Loading branch information
yaniswang committed May 1, 2016
1 parent 3c39073 commit 46208bd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
HTMLHint change log
====================

## ver 0.9.13 (2015-10-30)
## ver 0.9.13 (2016-5-1)

add:

1. change cli parameter: `--plugin` to `--rulesdir`
2. add formatter directory support
3. add formatters: compact, markdown
4. add cli parameter:`--nocolor`, disable color in cli
5. space-tab-mixed-disabled plugin: add space length require

fix:

1. fix: report error evidence if tag attrs include `\r\n`
2. fix: space-tab-mixed-disabled issue #119

## ver 0.9.10 (2015-10-12)

Expand Down
2 changes: 1 addition & 1 deletion lib/htmlhint.js

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions src/rules/space-tab-mixed-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ HTMLHint.addRule({
description: 'Do not mix tabs and spaces for indentation.',
init: function(parser, reporter, options){
var self = this;
var indentMode = 'nomix';
var spaceLengthRequire = null;
if(typeof options === 'string'){
var match = options.match(/^([a-z]+)(\d+)?/);
indentMode = match[1];
spaceLengthRequire = match[2] && parseInt(match[2], 10);
}
parser.addListener('text', function(event){
var raw = event.raw;
var reMixed = /(^|\r?\n)([ \t]+)/g;
Expand All @@ -16,13 +23,23 @@ HTMLHint.addRule({
if(fixedPos.col !== 1){
continue;
}
if(options === 'space' && /^ +$/.test(match[2]) === false){
reporter.warn('Please use space for indentation.', fixedPos.line, 1, self, event.raw);
var whiteSpace = match[2];
if(indentMode === 'space'){
if(spaceLengthRequire){
if(/^ +$/.test(whiteSpace) === false || whiteSpace.length % spaceLengthRequire !== 0){
reporter.warn('Please use space for indentation and keep '+spaceLengthRequire+' length.', fixedPos.line, 1, self, event.raw);
}
}
else{
if(/^ +$/.test(whiteSpace) === false){
reporter.warn('Please use space for indentation.', fixedPos.line, 1, self, event.raw);
}
}
}
else if(options === 'tab' && /^\t+$/.test(match[2]) === false){
else if(indentMode === 'tab' && /^\t+$/.test(whiteSpace) === false){
reporter.warn('Please use tab for indentation.', fixedPos.line, 1, self, event.raw);
}
else if(/ +\t|\t+ /.test(match[2]) === true){
else if(/ +\t|\t+ /.test(whiteSpace) === true){
reporter.warn('Do not mix tabs and spaces for indentation.', fixedPos.line, 1, self, event.raw);
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/rules/space-tab-mixed-disabled.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ var HTMLHint = require("../../index").HTMLHint;
var ruldId = 'space-tab-mixed-disabled';
var ruleMixOptions = {};
var ruleSpaceOptions = {};
var ruleSpace4Options = {};
var ruleSpace5Options = {};
var ruleTabOptions = {};

ruleMixOptions[ruldId] = true;
ruleSpaceOptions[ruldId] = 'space';
ruleSpace4Options[ruldId] = 'space4';
ruleSpace5Options[ruldId] = 'space5';
ruleTabOptions[ruldId] = 'tab';

describe('Rules: '+ruldId, function(){
Expand Down Expand Up @@ -75,6 +79,40 @@ describe('Rules: '+ruldId, function(){
expect(messages.length).to.be(1);
});

it('Not only space and 4 length in front of line should result in an error', function(){

var code = ' <a href="a"> bbb</a>';
var messages = HTMLHint.verify(code, ruleSpace4Options);
expect(messages.length).to.be(1);
expect(messages[0].message).to.be('Please use space for indentation and keep 4 length.');

});

it('Only space and 4 length in front of line should not result in an error', function(){

var code = ' <a href="a"> bbb</a>';
var messages = HTMLHint.verify(code, ruleSpace4Options);
expect(messages.length).to.be(0);

});

it('Not only space and 5 length in front of line should result in an error', function(){

var code = ' <a href="a"> bbb</a>';
var messages = HTMLHint.verify(code, ruleSpace5Options);
expect(messages.length).to.be(1);
expect(messages[0].message).to.be('Please use space for indentation and keep 5 length.');

});

it('Only space and 5 length in front of line should not result in an error', function(){

var code = ' <a href="a"> bbb</a>';
var messages = HTMLHint.verify(code, ruleSpace5Options);
expect(messages.length).to.be(0);

});

it('Only space in front of line should not result in an error', function(){
var code = ' <a href="a"> bbb</a>';
var messages = HTMLHint.verify(code, ruleSpaceOptions);
Expand Down

0 comments on commit 46208bd

Please sign in to comment.