diff --git a/src/rules/declaration-block-no-duplicate-properties/index.js b/src/rules/declaration-block-no-duplicate-properties/index.js index 1f15f341e2..45793a2c1c 100644 --- a/src/rules/declaration-block-no-duplicate-properties/index.js +++ b/src/rules/declaration-block-no-duplicate-properties/index.js @@ -1,7 +1,7 @@ import { - isStandardDeclaration, - optionsHaveIgnored, isCustomProperty, + isStandardProperty, + optionsHaveIgnored, report, ruleMessages, validateOptions, @@ -44,10 +44,9 @@ export default function (on, options) { } if (child.type !== "decl") { return } - if (!isStandardDeclaration(child)) { return } const { prop } = child - + if (!isStandardProperty(prop)) { return } if (isCustomProperty(prop)) { return } // Ignore the src property as commonly duplicated in at-fontface diff --git a/src/rules/declaration-block-properties-order/index.js b/src/rules/declaration-block-properties-order/index.js index 4442e29e74..d5fd04f200 100644 --- a/src/rules/declaration-block-properties-order/index.js +++ b/src/rules/declaration-block-properties-order/index.js @@ -1,8 +1,8 @@ import _ from "lodash" import { vendor } from "postcss" import { - isStandardDeclaration, isCustomProperty, + isStandardProperty, report, ruleMessages, validateOptions, @@ -55,11 +55,9 @@ export default function (expectation, options) { } if (child.type !== "decl") { return } - - if (!isStandardDeclaration(child)) { return } - + const { prop } = child - + if (!isStandardProperty(prop)) { return } if (isCustomProperty(prop)) { return } let unprefixedPropName = vendor.unprefixed(prop) diff --git a/src/rules/function-comma-space-after/index.js b/src/rules/function-comma-space-after/index.js index 7121da14ac..a7f67d5a70 100644 --- a/src/rules/function-comma-space-after/index.js +++ b/src/rules/function-comma-space-after/index.js @@ -1,7 +1,6 @@ import valueParser from "postcss-value-parser" import { declarationValueIndex, - isStandardDeclaration, isStandardFunction, report, ruleMessages, @@ -44,8 +43,6 @@ export default function (expectation) { export function functionCommaSpaceChecker({ locationChecker, root, result, checkedRuleName }) { root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } - valueParser(decl.value).walk(valueNode => { if (valueNode.type !== "function") { return } diff --git a/src/rules/function-parentheses-newline-inside/index.js b/src/rules/function-parentheses-newline-inside/index.js index b40c782aea..9f53599ca3 100644 --- a/src/rules/function-parentheses-newline-inside/index.js +++ b/src/rules/function-parentheses-newline-inside/index.js @@ -2,7 +2,6 @@ import valueParser from "postcss-value-parser" import { declarationValueIndex, isSingleLineString, - isStandardDeclaration, isStandardFunction, report, ruleMessages, @@ -33,8 +32,6 @@ export default function (expectation) { if (!validOptions) { return } root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } - if (decl.value.indexOf("(") === -1) { return } valueParser(decl.value).walk(valueNode => { diff --git a/src/rules/function-parentheses-space-inside/index.js b/src/rules/function-parentheses-space-inside/index.js index a2eb18e3cb..01e7dbc992 100644 --- a/src/rules/function-parentheses-space-inside/index.js +++ b/src/rules/function-parentheses-space-inside/index.js @@ -2,7 +2,6 @@ import valueParser from "postcss-value-parser" import { declarationValueIndex, isSingleLineString, - isStandardDeclaration, isStandardFunction, report, ruleMessages, @@ -36,7 +35,6 @@ export default function (expectation) { if (!validOptions) { return } root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } if (decl.value.indexOf("(") === -1) { return } diff --git a/src/rules/property-blacklist/index.js b/src/rules/property-blacklist/index.js index 08dae9e55e..4915e41587 100644 --- a/src/rules/property-blacklist/index.js +++ b/src/rules/property-blacklist/index.js @@ -1,9 +1,9 @@ import { vendor } from "postcss" import { isString } from "lodash" import { - isStandardDeclaration, - matchesStringOrRegExp, isCustomProperty, + isStandardProperty, + matchesStringOrRegExp, report, ruleMessages, validateOptions, @@ -25,9 +25,8 @@ export default function (blacklistInput) { if (!validOptions) { return } root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } - const { prop } = decl + if (!isStandardProperty(prop)) { return } if (isCustomProperty(prop)) { return } if (!matchesStringOrRegExp(vendor.unprefixed(prop), blacklist)) { return } diff --git a/src/rules/property-case/index.js b/src/rules/property-case/index.js index 48720bb4f2..28fad74191 100644 --- a/src/rules/property-case/index.js +++ b/src/rules/property-case/index.js @@ -1,6 +1,6 @@ import { - isStandardDeclaration, isCustomProperty, + isStandardProperty, report, ruleMessages, validateOptions, @@ -24,9 +24,8 @@ export default function (expectation) { if (!validOptions) { return } root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } - const { prop } = decl + if (!isStandardProperty(prop)) { return } if (isCustomProperty(prop)) { return } const expectedProp = expectation === "lower" ? prop.toLowerCase() : prop.toUpperCase() diff --git a/src/rules/property-whitelist/index.js b/src/rules/property-whitelist/index.js index 47d19e1e2d..b5385a9d97 100644 --- a/src/rules/property-whitelist/index.js +++ b/src/rules/property-whitelist/index.js @@ -1,12 +1,12 @@ import { vendor } from "postcss" import { isString } from "lodash" import { - isStandardDeclaration, isCustomProperty, + isStandardProperty, + matchesStringOrRegExp, report, ruleMessages, validateOptions, - matchesStringOrRegExp, } from "../../utils" export const ruleName = "property-whitelist" @@ -26,11 +26,9 @@ export default function (whitelistInput) { root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } - const { prop } = decl + if (!isStandardProperty(prop)) { return } if (isCustomProperty(prop)) { return } - if (matchesStringOrRegExp(vendor.unprefixed(prop), whitelist)) { return } report({ diff --git a/src/rules/root-no-standard-properties/index.js b/src/rules/root-no-standard-properties/index.js index 2720500eda..81d919fdeb 100644 --- a/src/rules/root-no-standard-properties/index.js +++ b/src/rules/root-no-standard-properties/index.js @@ -1,6 +1,6 @@ import selectorParser from "postcss-selector-parser" import { - isStandardDeclaration, + isStandardProperty, isCustomProperty, report, ruleMessages, @@ -26,10 +26,9 @@ export default function (actual) { if (ignoreRule(selectorAST)) { return } rule.walkDecls(function (decl) { - if (!isStandardDeclaration(decl)) { return } const { prop } = decl - + if (!isStandardProperty(prop)) { return } if (isCustomProperty(prop)) { return } report({ diff --git a/src/rules/shorthand-property-no-redundant-values/index.js b/src/rules/shorthand-property-no-redundant-values/index.js index c920f31eb1..d1f0a86a9c 100644 --- a/src/rules/shorthand-property-no-redundant-values/index.js +++ b/src/rules/shorthand-property-no-redundant-values/index.js @@ -1,8 +1,6 @@ import valueParser from "postcss-value-parser" import { vendor } from "postcss" import { - isStandardDeclaration, - isCustomProperty, report, ruleMessages, validateOptions, @@ -65,12 +63,8 @@ export default function (actual) { root.walkDecls(decl => { - if (!isStandardDeclaration(decl)) { return } - const { prop, value } = decl - if (isCustomProperty(prop)) { return } - // ignore not shorthandable properties, and math operations if ( isIgnoredCharacters(value) || diff --git a/src/utils/__tests__/isStandardDeclaration-test.js b/src/utils/__tests__/isStandardDeclaration-test.js index 4c47c5fe10..9d4130c998 100644 --- a/src/utils/__tests__/isStandardDeclaration-test.js +++ b/src/utils/__tests__/isStandardDeclaration-test.js @@ -1,11 +1,10 @@ import isStandardDeclaration from "../isStandardDeclaration" -import less from "postcss-less" import postcss from "postcss" import test from "tape" test("isStandardDeclaration", t => { - t.plan(8) + t.plan(7) rules("a { a: b }", decl => { t.ok(isStandardDeclaration(decl), "standard prop and value") @@ -29,10 +28,6 @@ test("isStandardDeclaration", t => { rules("$map: (value, value2)", decl => { t.notOk(isStandardDeclaration(decl), "scss map") }) - - lessRules("a { @var: b }", decl => { - t.notOk(isStandardDeclaration(decl), "less var") - }) }) function rules(css, cb) { @@ -40,9 +35,3 @@ function rules(css, cb) { result.root.walkDecls(cb) }) } - -function lessRules(css, cb) { - postcss().process(css, { syntax: less }).then(result => { - result.root.walkDecls(cb) - }) -} diff --git a/src/utils/__tests__/isStandardProperty-test.js b/src/utils/__tests__/isStandardProperty-test.js new file mode 100644 index 0000000000..ae4310957d --- /dev/null +++ b/src/utils/__tests__/isStandardProperty-test.js @@ -0,0 +1,12 @@ +import test from "tape" +import isStandardProperty from "../isStandardProperty" + +test("isStandardProperty", t => { + t.ok(isStandardProperty("top"), "single word") + t.ok(isStandardProperty("--custom-property"), "custom property") + t.ok(isStandardProperty("border-top-left-radius"), "hyphenated words") + t.ok(isStandardProperty("-webkit-appearance"), "vendor prefix") + t.notOk(isStandardProperty("$sass-variable"), "sass variable") + t.notOk(isStandardProperty("@less-variable"), "less variable") + t.end() +}) diff --git a/src/utils/index.js b/src/utils/index.js index c4cf9f49b6..09545c8304 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -17,6 +17,7 @@ export { default as isLowerSpecificity } from "./isLowerSpecificity" export { default as isSingleLineString } from "./isSingleLineString" export { default as isStandardDeclaration } from "./isStandardDeclaration" export { default as isStandardFunction } from "./isStandardFunction" +export { default as isStandardProperty } from "./isStandardProperty" export { default as isStandardRule } from "./isStandardRule" export { default as isStandardSelector } from "./isStandardSelector" export { default as isStandardTypeSelector } from "./isStandardTypeSelector" diff --git a/src/utils/isStandardDeclaration.js b/src/utils/isStandardDeclaration.js index ee39c0e514..4170c10230 100644 --- a/src/utils/isStandardDeclaration.js +++ b/src/utils/isStandardDeclaration.js @@ -5,13 +5,9 @@ * @return {boolean} If `true`, the declaration is standard */ export default function (decl) { - const { prop } = decl - // SCSS var (e.g. $var: x), list (e.g. $list: (x)) or map (e.g. $map: (key:value)) - if (prop[0] === "$") { return false } - - // Less var (e.g. @var: x) - if (prop[0] === "@") { return false } + // Declarations belong in a declaration block + if (decl.parent.type === "root") { return false } return true } diff --git a/src/utils/isStandardProperty.js b/src/utils/isStandardProperty.js new file mode 100644 index 0000000000..03972ceb44 --- /dev/null +++ b/src/utils/isStandardProperty.js @@ -0,0 +1,16 @@ +/** + * Check whether a property is standard + * + * @param {string} property + * @return {boolean} If `true`, the property is standard + */ +export default function (property) { + + // SCSS var (e.g. $var: x), list (e.g. $list: (x)) or map (e.g. $map: (key:value)) + if (property[0] === "$") { return false } + + // Less var (e.g. @var: x) + if (property[0] === "@") { return false } + + return true +}