diff --git a/lib/rules/unit-no-unknown/README.md b/lib/rules/unit-no-unknown/README.md index 2d41213bab..3d11d5ddff 100644 --- a/lib/rules/unit-no-unknown/README.md +++ b/lib/rules/unit-no-unknown/README.md @@ -33,19 +33,19 @@ The following patterns are *not* considered violations: ```css a { width: 10px; -} +} ``` ```css a { width: 10Px; -} +} ``` ```css a { width: 10pX; -} +} ``` ```css @@ -83,3 +83,43 @@ a { width: 10my-other-unit; } ``` + +### `ignoreFunctions: ["/regex/", "string"]` + +Given: + +```js +["image-set", "/^my-/", "/^YOUR-/i"] +``` + +The following patterns are *not* considered violations: + +```css +a { + background-image: image-set( + '/images/some-image-1x.jpg' 1x, + '/images/some-image-2x.jpg' 2x, + '/images/some-image-3x.jpg' 3x + ); +} +``` + +```css +a { + background-image: my-image-set( + '/images/some-image-1x.jpg' 1x, + '/images/some-image-2x.jpg' 2x, + '/images/some-image-3x.jpg' 3x + ); +} +``` + +```css +a { + background-image: YoUr-image-set( + '/images/some-image-1x.jpg' 1x, + '/images/some-image-2x.jpg' 2x, + '/images/some-image-3x.jpg' 3x + ); +} +``` diff --git a/lib/rules/unit-no-unknown/__tests__/index.js b/lib/rules/unit-no-unknown/__tests__/index.js index e984faa7b4..8e5b1ac055 100644 --- a/lib/rules/unit-no-unknown/__tests__/index.js +++ b/lib/rules/unit-no-unknown/__tests__/index.js @@ -472,3 +472,30 @@ testRule(rule, { } ] }); + +testRule(rule, { + ruleName, + config: [true, { ignoreFunctions: ["image-set", "/^my-/", "/^YOUR-/i"] }], + + accept: [ + { + code: + "a { background-image: image-set('/images/some-image-1x.jpg' 1x,'/images/some-image-2x.jpg' 2x,'/images/some-image-3x.jpg' 3x,); }" + }, + { + code: + "a { background-image: my-image-set('/images/some-image-1x.jpg' 1x,'/images/some-image-2x.jpg' 2x,'/images/some-image-3x.jpg' 3x,); }" + }, + { + code: + "a { background-image: YoUr-image-set('/images/some-image-1x.jpg' 1x,'/images/some-image-2x.jpg' 2x,'/images/some-image-3x.jpg' 3x,); }" + } + ], + + reject: [ + { + code: "a { margin: calc(10pixels + 1pixels); }", + message: messages.rejected("pixels") + } + ] +}); diff --git a/lib/rules/unit-no-unknown/index.js b/lib/rules/unit-no-unknown/index.js index a36ab16d9b..9674fc8a07 100644 --- a/lib/rules/unit-no-unknown/index.js +++ b/lib/rules/unit-no-unknown/index.js @@ -26,7 +26,8 @@ const rule = function(actual, options) { { actual: options, possible: { - ignoreUnits: [_.isString] + ignoreUnits: [_.isString], + ignoreFunctions: [_.isString] }, optional: true } @@ -42,9 +43,11 @@ const rule = function(actual, options) { value = value.replace(/\*/g, ","); valueParser(value).walk(function(valueNode) { // Ignore wrong units within `url` function + // and within functions listed in the `ignoreFunctions` option if ( valueNode.type === "function" && - valueNode.value.toLowerCase() === "url" + (valueNode.value.toLowerCase() === "url" || + optionsMatches(options, "ignoreFunctions", valueNode.value)) ) { return false; }