Skip to content

Commit

Permalink
fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
ielgnaw committed Oct 27, 2016
1 parent 2bd6e34 commit c95d22f
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 27 deletions.
37 changes: 15 additions & 22 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
*/

import Manis from 'manis';
import {join, basename} from 'path';
import stripJSONComments from 'strip-json-comments';
import {join} from 'path';
import yaml from 'js-yaml';

'use strict';

let STORAGE = null;

const JSON_YAML_REG = /.+\.(json|yml)$/i;

/**
* 获取 merge 后的配置文件
* 用户自定义的配置文件优先级 .lesslintrc > config.yml > config.json
Expand All @@ -30,29 +27,18 @@ export function loadConfig(filePath, refresh) {

let manis = new Manis({
files: [
'.lesslintrc',
'config.yml',
'config.json'
'.lesslintrc'
],
loader(content, filePath) {
loader(content) {
if (!content) {
return '';
}

if (basename(filePath) === '.lesslintrc') {
return JSON.parse(stripJSONComments(content));
}

let match = filePath.match(JSON_YAML_REG);
if (match) {
let suffix = match[1];
if (suffix === 'json') {
return JSON.parse(stripJSONComments(content));
}
else if (suffix === 'yml') {
return yaml.load(content);
}
let ret;
try {
ret = yaml.load(content);
}
catch (e) {}
return ret;
}
});

Expand All @@ -62,3 +48,10 @@ export function loadConfig(filePath, refresh) {

return STORAGE;
}

/**
* 清空 STORAGE
*/
export function clearStorage() {
STORAGE = null;
}
5 changes: 4 additions & 1 deletion src/rule/require-before-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export const check = postcss.plugin(RULENAME, opts =>
if (realRuleVal.length) {
css.walkRules(rule => {
// 只有 { 时,才能用 between 处理,其他符号的 require-before-space 规则还未实现
if (rule.raws.between === '' && realRuleVal.indexOf('{') !== -1) {
if (
!rule.ruleWithoutBody // 排除 mixin 调用
&& rule.raws.between === '' && realRuleVal.indexOf('{') !== -1
) {
const source = rule.source;
const line = source.start.line;
const col = source.start.column + rule.selector.length;
Expand Down
5 changes: 4 additions & 1 deletion src/rule/require-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export const check = postcss.plugin(RULENAME, opts =>
if (realRuleVal.indexOf('selector') > -1) {
css.walkRules(rule => {
const selector = rule.selector;
if (PATTERN_NOTLF.test(selector)) {
if (
!rule.ruleWithoutBody // 排除 mixin 调用
&& PATTERN_NOTLF.test(selector)
) {
const source = rule.source;
const line = source.start.line;
const lineContent = getLineContent(line, source.input.css);
Expand Down
1 change: 1 addition & 0 deletions test/fixture/.lesslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import: false
6 changes: 6 additions & 0 deletions test/fixture/require-before-space.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ body{
}
color: #ff0;
}

.foo {
.transition(width 1s);
.size(30px, 20px);
.clearfix();
}
6 changes: 6 additions & 0 deletions test/fixture/require-newline.less
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ p,
i,.cc {
width: 100px;height: 100px;
}

.foo {
.transition(width 1s);
.size(30px, 20px);
.clearfix();
}
5 changes: 3 additions & 2 deletions test/spec/checker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ describe('checker test suite\n', () => {
it('should be return right result', () => {
const filePath = 'path/to/file.css';
const fileContent = '\np {\n height: 0px;}\n\n';
return checker.checkString(fileContent, filePath, config.loadConfig(filePath, true)).then(invalidList => {
return checker.checkString(fileContent, filePath, config.loadConfig(filePath, false)).then(invalidList => {
expect(invalidList[0].messages.length).to.equal(1);
});
});

it('should catch error with line', () => {
const filePath = 'path/to/file.css';
const fileContent = '\np {\nheight: 0px\n\n';
return checker.checkString(fileContent, filePath, config.loadConfig(filePath, true)).then(() => {
return checker.checkString(fileContent, filePath, config.loadConfig(filePath, false)).then(() => {
}).catch(invalidList => {
const messages = invalidList[0].messages;
expect(messages[0].line).to.equal(2);
Expand Down Expand Up @@ -57,6 +57,7 @@ describe('checker test suite\n', () => {
content: '\np {\n height: 0px\ncolor: ccc;\n\n'
};
return checker.check(file, errors, () => {}).then(() => {
config.clearStorage();
const messages = errors[0].messages;
expect(messages[0].line).to.equal(3);
expect(messages[0].ruleName).to.equal('CssSyntaxError');
Expand Down
8 changes: 7 additions & 1 deletion test/spec/rule.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import fs from 'fs';
import path from 'path';

let checker = require(path.join(__dirname, '../../src', 'checker'));
let config = require(path.join(__dirname, '../../src', 'config'));

const expect = chai.expect;

Expand Down Expand Up @@ -52,13 +53,18 @@ describe('rule test suite\n', () => {

const errors = [];
return checker.check(file, errors, () => {}).then(() => {
expect(errors[0].messages.length).to.equal(10);
expect(errors[0].messages.length).to.equal(9);
});
});
});

describe('import: ', () => {
it('should return right message length', () => {
// 清空 config storage,设置成默认的 config
// 因为 fixture 目录下的 .lesslintrc 中配置了 import: false
config.clearStorage();
config.loadConfig('.', true);

const filePath = path.join(__dirname, '../fixture/import.less');
const fileContent = fs.readFileSync(
path.join(__dirname, '../fixture/import.less'),
Expand Down
14 changes: 14 additions & 0 deletions test/spec/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ describe('util test suite\n', () => {
];
const candidates2 = util.getCandidates(['rule.spec.js', '.'], patterns2);
expect(candidates2[0]).to.equal('rule.spec.js');

const patterns3 = [
'**/*.js',
'!**/{output,node_modules,asset,dist,release,doc,dep,report,*.bak}/**'
];
const candidates3 = util.getCandidates(['not-exist.js', '.'], patterns3);
expect(candidates3.length).to.equal(0);

const patterns4 = [
'**/*.js',
'!**/{output,node_modules,asset,dist,release,doc,dep,report,*.bak}/**'
];
const candidates4 = util.getCandidates(['aaa', '.'], patterns4);
expect(candidates4.length).to.equal(0);
});
});

Expand Down

0 comments on commit c95d22f

Please sign in to comment.