Comparing version
@@ -5,2 +5,4 @@ 'use strict'; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
@@ -11,2 +13,6 @@ | ||
var PLUGIN_OPTION_COMMENT = 'doiuse-'; | ||
var DISABLE_FEATURE_COMMENT = PLUGIN_OPTION_COMMENT + 'disable'; | ||
var ENABLE_FEATURE_COMMENT = PLUGIN_OPTION_COMMENT + 'enable'; | ||
/* | ||
@@ -45,2 +51,3 @@ * str: string to search in. | ||
* feature: {} // caniuse-db feature slug | ||
* ignore: {} // caniuse-db feature to ignore in current file | ||
* } | ||
@@ -54,2 +61,3 @@ * ``` | ||
this.features = _.pick(features, featureList); | ||
this.ignore = []; | ||
} | ||
@@ -65,3 +73,3 @@ | ||
if (!values || values.filter(isFoundIn(_decl.value)).length > 0) { | ||
cb({ usage: _decl, feature: feat }); | ||
cb({ usage: _decl, feature: feat, ignore: this.ignore }); | ||
} | ||
@@ -77,7 +85,7 @@ } | ||
if (selectors.filter(isFoundIn(_rule.selector)).length > 0) { | ||
cb({ usage: _rule, feature: feat }); | ||
cb({ usage: _rule, feature: feat, ignore: this.ignore }); | ||
} | ||
} | ||
this.process(_rule, cb); | ||
this.node(_rule, cb); | ||
} | ||
@@ -92,3 +100,3 @@ }, { | ||
if (!params || params.filter(isFoundIn(_atrule.params)).length > 0) { | ||
cb({ usage: _atrule, feature: feat }); | ||
cb({ usage: _atrule, feature: feat, ignore: this.ignore }); | ||
} | ||
@@ -98,20 +106,54 @@ } | ||
this.process(_atrule, cb); | ||
this.node(_atrule, cb); | ||
} | ||
}, { | ||
key: 'process', | ||
value: function process(node, cb) { | ||
var self = this; | ||
node.each(function (child) { | ||
key: 'comment', | ||
value: function comment(_comment, cb) { | ||
var text = _comment.text.toLowerCase(); | ||
if (_.startsWith(text, PLUGIN_OPTION_COMMENT)) { | ||
var option = text.split(' ', 1)[0]; | ||
var value = text.replace(option, '').trim(); | ||
switch (option) { | ||
case DISABLE_FEATURE_COMMENT: | ||
if (value === '') { | ||
this.ignore = _.keysIn(this.features); | ||
} else { | ||
this.ignore = _.uniq([].concat(_toConsumableArray(this.ignore), _toConsumableArray(value.split(',').map(function (feat) { | ||
return feat.trim(); | ||
})))); | ||
} | ||
break; | ||
case ENABLE_FEATURE_COMMENT: | ||
if (value === '') { | ||
this.ignore = []; | ||
} else { | ||
this.ignore = _.without.apply(_, [this.ignore].concat(_toConsumableArray(value.split(',').map(function (feat) { | ||
return feat.trim(); | ||
})))); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
}, { | ||
key: 'node', | ||
value: function node(_node, cb) { | ||
var _this = this; | ||
_node.each(function (child) { | ||
switch (child.type) { | ||
case 'rule': | ||
self.rule(child, cb); | ||
_this.rule(child, cb); | ||
break; | ||
case 'decl': | ||
self.decl(child, cb); | ||
_this.decl(child, cb); | ||
break; | ||
case 'atrule': | ||
self.atrule(child, cb); | ||
_this.atrule(child, cb); | ||
break; | ||
case 'comment': | ||
_this.comment(child, cb); | ||
break; | ||
@@ -123,2 +165,11 @@ default: | ||
} | ||
}, { | ||
key: 'process', | ||
value: function process(node, cb) { | ||
// Reset ignoring rules specified by inline comments per each file | ||
this.ignore = []; | ||
// Recursively walk nodes in file | ||
this.node(node, cb); | ||
} | ||
}]); | ||
@@ -125,0 +176,0 @@ |
@@ -11,3 +11,3 @@ 'use strict'; | ||
var onFeatureUsage = options.onFeatureUsage; | ||
var ignore = options.ignore; | ||
var ignoreOptions = options.ignore; | ||
var ignoreFiles = options.ignoreFiles; | ||
@@ -38,2 +38,3 @@ | ||
var usage = _ref.usage; | ||
var ignore = _ref.ignore; | ||
@@ -43,2 +44,5 @@ if (ignore && ignore.indexOf(feature) !== -1) { | ||
} | ||
if (ignoreOptions && ignoreOptions.indexOf(feature) !== -1) { | ||
return; | ||
} | ||
@@ -45,0 +49,0 @@ if (ignoreFiles && Multimatch(usage.source.input.from, ignoreFiles).length > 0) { |
{ | ||
"name": "doiuse", | ||
"version": "2.2.2", | ||
"version": "2.3.0", | ||
"description": "Lint CSS for browser support against caniuse database.", | ||
@@ -5,0 +5,0 @@ "main": "lib/doiuse.js", |
@@ -93,3 +93,3 @@ [](https://travis-ci.org/anandthakker/doiuse) | ||
Refer to the data in /src/data/features.js. | ||
<a name="features-list"></a>Refer to the data in [/data/features.js](data/features.js). | ||
@@ -149,2 +149,14 @@ - If a feature in that dataset only specifies `properties`, we just use those | ||
## Ignoring file-specific rules: | ||
For disabling some checks you can use just-in-place comments | ||
##### `/* doiuse-disable */` | ||
Disables checks of _all [features](#features-list)_ | ||
##### `/* doiuse-disable feature */` | ||
Disables checks of _specified [feature(s)](#features-list)_ (can be comma separated list) | ||
##### `/* doiuse-enable */` | ||
Re-enables checks of _all [features](#features-list)_ | ||
##### `/* doiuse-enable feature */` | ||
Enables checks of _specified [feature(s)](#features-list)_ (can be comma separated list) | ||
- for following lines in file | ||
# [Contributing](CONTRIBUTING.md) | ||
@@ -151,0 +163,0 @@ |
@@ -110,1 +110,31 @@ var fs = require('fs') | ||
}) | ||
test('ignores rules specified in comments', function (t) { | ||
var count, ignoreCss, ignoreCssPath, processCss, processCssPath | ||
ignoreCssPath = require.resolve('./cases/ignore-comment.css') | ||
ignoreCss = fs.readFileSync(ignoreCssPath) | ||
processCssPath = require.resolve('./cases/ignore-file.css') | ||
processCss = fs.readFileSync(processCssPath) | ||
count = 0 | ||
var processor = postcss([atImport(), | ||
doiuse({ | ||
browsers: ['ie 6'], | ||
onFeatureUsage: function (usageInfo) { | ||
count++ | ||
} | ||
})]) | ||
processor.process(ignoreCss, {from: ignoreCssPath}) | ||
.then(function () { | ||
t.equal(count, 2) | ||
}).then(function () { | ||
processor.process(processCss, {from: processCssPath}) | ||
.then(function () { | ||
t.equal(count, 3, 'inline css disabing rules must apply only to current file') | ||
t.end() | ||
}) | ||
}) | ||
}) |
65250
6.25%71
1.43%1809
5.48%171
7.55%