Skip to content

Commit

Permalink
space-tab-mixed-disabled(rule): Support space and tab mode, for check…
Browse files Browse the repository at this point in the history
… only space or tab
  • Loading branch information
yaniswang committed Oct 7, 2015
1 parent b9d8082 commit 83afc1d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
HTMLHint change log
====================

## ver 0.9.8 (2015-10-6)
## ver 0.9.8 (2015-10-7)

add:

Expand All @@ -10,6 +10,7 @@ add:
3. Support hint any file without `.html` or `.htm` extension, just like: `htmlhint test.xhtml`
4. Support json raw format in cli
5. tag-pair(rule): Show the line of the start tag
6. space-tab-mixed-disabled(rule): Support space and tab mode, for check only space or tab

fix:

Expand Down
13 changes: 10 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ module.exports = function(grunt) {
},
exec: {
test: {
command: '"./node_modules/.bin/mocha" --recursive',
stdout: true,
stderr: true
},
cover: {
command: '"./node_modules/.bin/istanbul" cover "./node_modules/mocha/bin/_mocha" -- --recursive',
stdout: true,
stderr: true
Expand All @@ -48,7 +53,7 @@ module.exports = function(grunt) {
}
},
replace: {
htmlhint: {
version: {
files: {
'lib/htmlhint.js':'lib/htmlhint.js'
},
Expand All @@ -68,8 +73,10 @@ module.exports = function(grunt) {
}
});

grunt.registerTask('dev', ['jshint', 'clean', 'concat', 'exec:test']);
grunt.registerTask('build', ['jshint', 'clean', 'concat']);

grunt.registerTask('dev', ['build', 'exec:test']);

grunt.registerTask('default', ['dev', 'uglify', 'replace']);
grunt.registerTask('default', ['build', 'exec:cover', 'uglify', 'replace:version']);

};
2 changes: 1 addition & 1 deletion lib/htmlhint.js

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions src/rules/space-tab-mixed-disabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@
HTMLHint.addRule({
id: 'space-tab-mixed-disabled',
description: 'Do not mix tabs and spaces for indentation.',
init: function(parser, reporter){
init: function(parser, reporter, options){
var self = this;
parser.addListener('text', function(event){
var raw = event.raw;
var reMixed = /(^|\r?\n)( +\t|\t+ )/g;
var reMixed = /(^|\r?\n)([ \t]+)/g;
var match;
while((match = reMixed.exec(raw))){
var fixedPos = parser.fixPos(event, match.index + match[1].length);
reporter.warn('Do not mix tabs and spaces for indentation.', fixedPos.line, 1, self, event.raw);
if(options === 'space' && /^ +$/.test(match[2]) === false){
reporter.warn('Please use space for indentation.', fixedPos.line, 1, self, event.raw);
}
else if(options === 'tab' && /^\t+$/.test(match[2]) === false){
reporter.warn('Please use tab for indentation.', fixedPos.line, 1, self, event.raw);
}
else if(/ +\t|\t+ /.test(match[2]) === true){
reporter.warn('Do not mix tabs and spaces for indentation.', fixedPos.line, 1, self, event.raw);
}
}
});
}
});
});
74 changes: 62 additions & 12 deletions test/rules/space-tab-mixed-disabled.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
/**
* Copyright (c) 2014, Yanis Wang <[email protected]>
* Copyright (c) 2014-2015, Yanis Wang <[email protected]>
* MIT Licensed
*/

var expect = require("expect.js");

var HTMLHint = require("../../index").HTMLHint;

var ruldId = 'space-tab-mixed-disabled',
ruleOptions = {};
var ruldId = 'space-tab-mixed-disabled';
var ruleMixOptions = {};
var ruleSpaceOptions = {};
var ruleTabOptions = {};

ruleOptions[ruldId] = true;
ruleMixOptions[ruldId] = true;
ruleSpaceOptions[ruldId] = 'space';
ruleTabOptions[ruldId] = 'tab';

describe('Rules: '+ruldId, function(){

it('Spaces and tabs mixed in front of line should result in an error', function(){
// space before tab
var code = ' <a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
var messages = HTMLHint.verify(code, ruleMixOptions);
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(1);
// tab before space
code = ' <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleOptions);
messages = HTMLHint.verify(code, ruleMixOptions);
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(1);
// multi line
code = '<div>\r\n <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleOptions);
messages = HTMLHint.verify(code, ruleMixOptions);
expect(messages.length).to.be(1);
expect(messages[0].rule.id).to.be(ruldId);
expect(messages[0].line).to.be(2);
Expand All @@ -40,18 +44,64 @@ describe('Rules: '+ruldId, function(){

it('Only spaces in front of line should not result in an error', function(){
var code = ' <a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
var messages = HTMLHint.verify(code, ruleMixOptions);
expect(messages.length).to.be(0);

code = '<div>\r\n <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleOptions);
messages = HTMLHint.verify(code, ruleMixOptions);
expect(messages.length).to.be(0);
});

it('Only tabs in front of line should not result in an error', function(){
var code = ' <a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleOptions);
var messages = HTMLHint.verify(code, ruleMixOptions);
expect(messages.length).to.be(0);
});

});

it('Not only space in front of line should result in an error', function(){
// mixed 1
var code = ' <a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleSpaceOptions);
expect(messages.length).to.be(1);

// mixed 2
code = ' <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleSpaceOptions);
expect(messages.length).to.be(1);

// only tab
code = ' <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleSpaceOptions);
expect(messages.length).to.be(1);
});

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);
expect(messages.length).to.be(0);
});

it('Not only tab in front of line should result in an error', function(){
// mixed 1
var code = ' <a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleTabOptions);
expect(messages.length).to.be(1);

// mixed 2
code = ' <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleTabOptions);
expect(messages.length).to.be(1);

// only space
code = ' <a href="a">bbb</a>';
messages = HTMLHint.verify(code, ruleTabOptions);
expect(messages.length).to.be(1);
});

it('Only tab in front of line should not result in an error', function(){
// only tab
var code = ' <a href="a">bbb</a>';
var messages = HTMLHint.verify(code, ruleTabOptions);
expect(messages.length).to.be(0);
});
});

0 comments on commit 83afc1d

Please sign in to comment.