-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
192 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @file jscs checker | ||
* @author chris<wfsr@foxmail.com> | ||
*/ | ||
|
||
var through = require('through2'); | ||
var HTMLHint = require('htmlhint').HTMLHint; | ||
var RcLoader = require('rcloader2'); | ||
|
||
/** | ||
* 根据文件路径中扩展名判断当前能否处理 | ||
* | ||
* @param {string} path 文件路径 | ||
* @return {boolean} 是否可处理 | ||
*/ | ||
function canHandle(path) { | ||
return /\.html$/.test(path); | ||
} | ||
|
||
|
||
/** | ||
* 负责代码风格检查的转换流 | ||
* | ||
* @param {Object} options 配置项 | ||
* @return {Transform} 转换流 | ||
*/ | ||
module.exports = function (options) { | ||
var util = require('../util'); | ||
|
||
var config = require('./config'); | ||
var defaultHTMLHintConfig = config.htmlhint; | ||
var htmlhintRcloader = new RcLoader('.htmlhintrc', defaultHTMLHintConfig, {loader: util.parseJSON}); | ||
var checked = {}; | ||
|
||
return through( | ||
{ | ||
objectMode: true | ||
}, | ||
|
||
function (file, enc, cb) { | ||
if (checked[file.path] || !canHandle(file.path) || file.isNull()) { | ||
cb(null, file); | ||
return; | ||
} | ||
|
||
file.errors = []; | ||
checked[file.path] = true; | ||
|
||
var htmlhintConfig = options.lookup | ||
? htmlhintRcloader.for(file.path) | ||
: htmlhintRcloader; | ||
|
||
try { | ||
|
||
var contents = file.contents.toString(); | ||
|
||
HTMLHint.verify(contents, htmlhintConfig).forEach(function (error) { | ||
file.errors.push( | ||
util.parseError( | ||
{ | ||
type: 'html', | ||
checker: 'htmlhint', | ||
rule: error.rule.id | ||
}, | ||
error | ||
) | ||
); | ||
}); | ||
|
||
|
||
cb(null, file); | ||
|
||
} | ||
catch (error) { | ||
if (options.debug) { | ||
throw error; | ||
} | ||
|
||
file.errors.push( | ||
util.parseError( | ||
{ | ||
type: 'html', | ||
checker: 'htmlhint', | ||
code: '999' | ||
}, | ||
error | ||
) | ||
); | ||
cb(null, file); | ||
} | ||
|
||
|
||
}, | ||
|
||
function (cb) { | ||
htmlhintRcloader = null; | ||
checked = null; | ||
cb(); | ||
} | ||
); | ||
}; |
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,10 @@ | ||
/** | ||
* @file HTML 检查规则配置模块 | ||
* @author chris<wfsr@foxmail.com> | ||
*/ | ||
|
||
var util = require('../util'); | ||
|
||
// 读取当前目录下的 json 文件,以文件名为 key 对外 export | ||
// json 配置文件中可以使用 javascript 注释 | ||
util.readConfigs(__dirname, exports); |
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,24 @@ | ||
{ | ||
"attr-lowercase": true, | ||
"attr-no-duplication": true, | ||
"attr-unsafe-chars": true, | ||
"attr-value-double-quotes": true, | ||
// "attr-value-not-empty": false, | ||
// "csslint": {}, | ||
"doctype-first": true, | ||
"doctype-html5": true, | ||
"head-script-disabled": true, | ||
// "href-abs-or-rel": true, | ||
"id-class-ad-disabled": true, | ||
"id-class-value": "dash", | ||
"id-unique": true, | ||
"imgalt-require": true, | ||
// "jshint": {}, | ||
"space-tab-mixed-disabled": true, | ||
"spec-char-escape": true, | ||
"src-not-empty": true, | ||
"style-disabled": true, | ||
"tag-pair": true, | ||
// "tag-self-close": true, | ||
"tagname-lowercase": true | ||
} |
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,24 @@ | ||
{ | ||
"attr-lowercase": "017", | ||
"attr-no-duplication": "997", | ||
"attr-unsafe-chars": "998", | ||
"attr-value-double-quotes": "018", | ||
// "attr-value-not-empty": "019", | ||
// "csslint": true, | ||
"doctype-first": "998", | ||
"doctype-html5": "021", | ||
"head-script-disabled": "030", | ||
// "href-abs-or-rel": "997", | ||
"id-class-ad-disabled": "998", | ||
"id-class-value": "006", | ||
"id-unique": "005", | ||
"img-alt-require": "038", | ||
// "jshint": true, | ||
"space-tab-mixed-disabled": "001", | ||
"spec-char-escape": "997", | ||
"src-not-empty": "036", | ||
"style-disabled": "028", | ||
"tag-pair": "012", | ||
// "tag-self-close": "011", | ||
"tagname-lowercase": "010" | ||
} |
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