Skip to content

Commit e38d715

Browse files
Modified file-level rules handling to follow jshint-style specificity.
1 parent 9ffc29e commit e38d715

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

src/cli/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function cli(api){
5959
if (ignore) {
6060
ruleset = CSSLint.getRuleset();
6161
ignore.split(",").forEach(function(value){
62-
delete ruleset[value];
62+
ruleset[value] = 0;
6363
});
6464
}
6565

src/core/CSSLint.js

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,48 +65,37 @@ var CSSLint = (function(){
6565
};
6666

6767
/**
68-
* Returns a ruleset configuration object with all current rules, modified by inline embedded rules.
68+
* Returns a ruleset object based on embedded rules.
6969
* @param {String} text A string of css containing embedded rules.
70-
* @return A ruleset object.
70+
* @param {Object} ruleset A ruleset object to modify.
71+
* @return {Object} A ruleset object.
7172
* @method getEmbeddedRuleset
7273
*/
73-
api.getEmbeddedRuleset = function(text){
74+
function applyEmbeddedRuleset(text, ruleset){
7475
var valueMap,
75-
ruleset = this.getRuleset(),
7676
embedded = text && text.match(embeddedRuleset),
7777
rules = embedded && embedded[1];
7878

79-
if (!rules) {
80-
return ruleset;
79+
if (rules) {
80+
valueMap = {
81+
"true": 2, // true is error
82+
"false": 0, // false is ignore
83+
"2": 2,
84+
"1": 1,
85+
"0": 0
86+
};
87+
88+
rules.toLowerCase().split(",").forEach(function(rule){
89+
var pair = rule.split(":"),
90+
property = pair[0] || "",
91+
value = pair[1] || "";
92+
93+
ruleset[property.trim()] = valueMap[value.trim()];
94+
});
8195
}
8296

83-
valueMap = {
84-
'true': 2, // true is error
85-
'false': 0, // false is ignore
86-
'2': 2,
87-
'1': 1,
88-
'0': 0
89-
};
90-
91-
rules.toLowerCase().split(',').forEach(function(rule){
92-
var pair = rule.split(':'),
93-
property = (pair[0] || '').trim(), // normalize properties
94-
value = valueMap[(pair[1] || '').trim()]; // normalize values
95-
96-
if (!ruleset.hasOwnProperty(property)){
97-
return;
98-
}
99-
100-
if (!value) {
101-
delete ruleset[property];
102-
return;
103-
}
104-
105-
ruleset[property] = value;
106-
});
107-
10897
return ruleset;
109-
};
98+
}
11099

111100
//-------------------------------------------------------------------------
112101
// Formatters
@@ -190,20 +179,19 @@ var CSSLint = (function(){
190179
// normalize line endings
191180
lines = text.replace(/\n\r?/g, "$split$").split('$split$');
192181

193-
// always perfer file-level rulesets
194-
if (embeddedRuleset.test(text)){
195-
ruleset = this.getEmbeddedRuleset(text);
196-
}
197-
198182
if (!ruleset){
199183
ruleset = this.getRuleset();
200184
}
201185

186+
if (embeddedRuleset.test(text)){
187+
ruleset = applyEmbeddedRuleset(text, ruleset);
188+
}
189+
202190
reporter = new Reporter(lines, ruleset);
203191

204192
ruleset.errors = 2; //always report parsing errors as errors
205193
for (i in ruleset){
206-
if(ruleset.hasOwnProperty(i)){
194+
if(ruleset.hasOwnProperty(i) && ruleset[i]){
207195
if (rules[i]){
208196
rules[i].init(parser, reporter);
209197
}

tests/core/CSSLint.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
},
1919

2020
"Embedded ruleset should be honored": function(){
21-
var result = CSSLint.verify(
22-
"/*csslint adjoining-classes:true, box-sizing:false */\n.foo.bar{}",
23-
{ 'text-indent': 2 }
24-
);
21+
var result = CSSLint.verify("/*csslint bogus, adjoining-classes:true, box-sizing:false */\n.foo.bar{}", {
22+
'text-indent': 1,
23+
'box-sizing': 1
24+
});
2525

2626
Assert.areEqual(2, result.ruleset['adjoining-classes']);
27-
Assert.areEqual(undefined, result.ruleset['box-sizing']);
2827
Assert.areEqual(1, result.ruleset['text-indent']);
28+
Assert.areEqual(0, result.ruleset['box-sizing']);
2929
}
3030

3131
}));

0 commit comments

Comments
 (0)