Comparing version 0.1.19 to 0.1.20
@@ -145,3 +145,2 @@ /** | ||
// require('fs').writeFileSync(outputFilename, ast); | ||
try { | ||
@@ -151,4 +150,6 @@ tree.toCSS(); | ||
catch (e) { | ||
/* istanbul ignore next */ | ||
// parse css 错误 | ||
errMsg = e.message; | ||
/* istanbul ignore next */ | ||
if (e.extract) { | ||
@@ -159,3 +160,3 @@ errMsg += ': ' | ||
} | ||
/* istanbul ignore next */ | ||
errors.push({ | ||
@@ -179,4 +180,5 @@ path: filePath, | ||
}); | ||
/* istanbul ignore next */ | ||
deferred.reject(errors); | ||
/* istanbul ignore next */ | ||
return; | ||
@@ -183,0 +185,0 @@ } |
@@ -155,3 +155,7 @@ /** | ||
realVal.values.reduce(function (v, item) { | ||
var valueLineNum = util.getLine(item.value.index, me.fileData); | ||
var index = item.value.index; | ||
if (!index) { | ||
return; | ||
} | ||
var valueLineNum = util.getLine(index, me.fileData); | ||
if (valueLineNum > propertyLineNum) { | ||
@@ -158,0 +162,0 @@ var valueReg = new RegExp('^\\s{' + valueSpacesNum + '}[^\\s]+'); |
@@ -11,2 +11,4 @@ /** | ||
var colors = require('less').tree.colors; | ||
var msg = 'Color value can be abbreviated, must use the abbreviation form'; | ||
@@ -19,3 +21,4 @@ | ||
*/ | ||
var p2 = /\s*#([0-9a-fA-F]{0,6})\s*/g; | ||
// var p2 = /\s*#([0-9a-fA-F]{0,6})\s*/; | ||
var p2 = /#([0-9a-fA-F]{0,6})/; | ||
@@ -27,46 +30,68 @@ /** | ||
*/ | ||
var p3 = /^([\da-f])\1([\da-f])\2([\da-f])\3$/i; | ||
var p3 = /#([\da-f])\1([\da-f])\2([\da-f])\3/i; | ||
/** | ||
* 模块输出的接口 | ||
* 上下文对象是 LesslintVisitor 实例 | ||
* 处理属性的缩进 | ||
* | ||
* @param {Object} rule ast 节点中 rules 的每个 item | ||
* @param {string} ruleName 当前的规则名称 | ||
* @param {string} ruleName ruleName | ||
* @param {string} configVal 当前规则配置的值 | ||
*/ | ||
module.exports = function (rule, ruleName) { | ||
function dealProperty(rule, ruleName, configVal) { | ||
var me = this; | ||
if (rule.type === 'Comment' || !rule.value || rule.value.type !== 'Value') { | ||
return; | ||
} | ||
// 当前 less 文件的内容 | ||
var fileData = me.fileData; | ||
var name = rule.name; | ||
// 当前 rule 上的 realVal | ||
var realVal = rule.realVal; | ||
if (Array.isArray(name)) { | ||
name = name.reduce(function (value, item) { | ||
value += item.toCSS({}); | ||
return value; | ||
}, ''); | ||
} | ||
// 当前 rule 的索引 | ||
var index = realVal.index; | ||
var lineNum = util.getLine(rule.index, me.fileData); | ||
var lineContent = util.getLineContent(lineNum, me.fileData, true); | ||
var lineNum = util.getLine(index, fileData); | ||
var lineContent = util.getLineContent(lineNum, fileData); | ||
// 如何知道这个 rule 是颜色 | ||
// realVal.type === 'Color' | ||
// 或者 | ||
// realVal.type === 'Call' 且 realVal.refFuncs in colorFuncs | ||
// 或者 | ||
// realVal.values[index].value == '1px solid #fff' | ||
if (realVal.type === 'Color') { | ||
// 由于 less 的语法树上,如下两种 rule 是完全一样的 | ||
// @color1: white; | ||
// @color2: #ffffff; | ||
if (p2.test(lineContent)) { | ||
var $1 = RegExp.$1; | ||
if (p3.test($1)) { | ||
var values = rule.realVal.values; | ||
var i = -1; | ||
var len = values.length; | ||
var value; | ||
while (++i < len) { | ||
value = values[i]; | ||
if (value && value.value && typeof value.value === 'string') { | ||
// 说明是 hex 格式颜色值 | ||
if (p2.test(value.value)) { | ||
var hex = RegExp.$1; | ||
// 可以缩写 | ||
if (p3.test(value.value)) { | ||
me.invalidList.push({ | ||
uniqueFlag: ruleName + name + lineNum + '-hex', | ||
ruleName: ruleName, | ||
line: lineNum, | ||
col: lineContent.indexOf(hex), | ||
message: '`' | ||
+ lineContent | ||
+ '` ' | ||
+ msg, | ||
colorMessage: '`' | ||
+ lineContent.replace( | ||
('#' + hex), | ||
chalk.magenta('#' + hex) | ||
) | ||
+ '` ' | ||
+ chalk.grey(msg) | ||
}); | ||
} | ||
} | ||
// 说明是单词形式的颜色值 | ||
else if (colors[value.value]) { | ||
me.invalidList.push({ | ||
uniqueFlag: ruleName + name + lineNum + '-namedColor', | ||
ruleName: ruleName, | ||
line: lineNum, | ||
// col: lineContent.indexOf($1), | ||
col: lineContent.indexOf(value.value) + 1, | ||
message: '`' | ||
@@ -78,4 +103,4 @@ + lineContent | ||
+ lineContent.replace( | ||
('#' + $1), | ||
chalk.magenta('#' + $1) | ||
value.value, | ||
chalk.magenta(value.value) | ||
) | ||
@@ -88,32 +113,15 @@ + '` ' | ||
} | ||
else if (realVal.type !== 'Comment') { | ||
} | ||
var values = realVal.values; | ||
if (values.length) { | ||
for (var j = 0, jLen = realVal.values.length; j < jLen; j++) { | ||
var v = realVal.values[j]; | ||
p2.test(v.value); | ||
/* eslint-disable no-redeclare */ | ||
var $1 = RegExp.$1; | ||
/* eslint-enable no-redeclare */ | ||
if (p3.test($1)) { | ||
me.invalidList.push({ | ||
ruleName: ruleName, | ||
line: lineNum, | ||
message: '`' | ||
+ lineContent | ||
+ '` ' | ||
+ msg, | ||
colorMessage: '`' | ||
+ lineContent.replace( | ||
('#' + $1), | ||
chalk.magenta('#' + $1) | ||
) | ||
+ '` ' | ||
+ chalk.grey(msg) | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* 模块输出的接口 | ||
* 上下文对象是 LesslintVisitor 实例 | ||
* | ||
* @param {Object} rule ast 节点中 rules 的每个 item | ||
* @param {string} ruleName 当前的规则名称 | ||
* @param {string} configVal 当前规则配置的值 | ||
*/ | ||
module.exports = function (rule, ruleName, configVal) { | ||
var me = this; | ||
dealProperty.call(me, rule, ruleName, configVal); | ||
}; |
@@ -48,9 +48,11 @@ /** | ||
if (!name) { | ||
return; | ||
} | ||
if (Array.isArray(name)) { | ||
try { | ||
name = name[0].toCSS({}); | ||
} | ||
catch (e) { | ||
name = name[0].name; | ||
} | ||
name = name.reduce(function (value, item) { | ||
value += item.toCSS({}); | ||
return value; | ||
}, ''); | ||
} | ||
@@ -60,3 +62,3 @@ | ||
if (name && name.indexOf('@') === 0) { | ||
if (name.indexOf('@') === 0) { | ||
if (!pattern.test(name)) { | ||
@@ -63,0 +65,0 @@ me.invalidList.push({ |
@@ -132,30 +132,2 @@ /** | ||
/** | ||
* 根据索引获取位置 | ||
* from less/lib/less/parser.js | ||
* | ||
* @param {number} index 索引 | ||
* @param {string} inputStream 文件内容 | ||
* | ||
* @return {Object} 位置信息 | ||
*/ | ||
exports.getLocation = function (index, inputStream) { | ||
var n = index + 1; | ||
var line = null; | ||
var column = -1; | ||
while (--n >= 0 && inputStream.charAt(n) !== '\n') { | ||
column++; | ||
} | ||
if (typeof index === 'number') { | ||
line = (inputStream.slice(0, index).match(/\n/g) || '').length; | ||
} | ||
return { | ||
line: line, | ||
column: column | ||
}; | ||
}; | ||
/** | ||
* 根据参数以及模式匹配相应的文件 | ||
@@ -162,0 +134,0 @@ * |
{ | ||
"name": "lesslint", | ||
"description": "lint your less code", | ||
"version": "0.1.19", | ||
"version": "0.1.20", | ||
"maintainers": [ | ||
@@ -13,5 +13,5 @@ { | ||
"chalk": "^0.5.1", | ||
"edp-core": "^1.0.28", | ||
"edp-core": "^1.0.32", | ||
"less": "^1.7.5", | ||
"q": "^1.1.2" | ||
"q": "^1.4.1" | ||
}, | ||
@@ -18,0 +18,0 @@ "devDependencies": { |
84267
2266
Updatededp-core@^1.0.32
Updatedq@^1.4.1