Skip to content

Commit

Permalink
Add isStandardProperty and change isStandardDeclaration criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
jeddy3 committed Apr 25, 2016
1 parent aaa9a0f commit 26fa669
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 56 deletions.
7 changes: 3 additions & 4 deletions src/rules/declaration-block-no-duplicate-properties/index.js
@@ -1,7 +1,7 @@
import {
isStandardDeclaration,
optionsHaveIgnored,
isCustomProperty,
isStandardProperty,
optionsHaveIgnored,
report,
ruleMessages,
validateOptions,
Expand Down Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions 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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions src/rules/function-comma-space-after/index.js
@@ -1,7 +1,6 @@
import valueParser from "postcss-value-parser"
import {
declarationValueIndex,
isStandardDeclaration,
isStandardFunction,
report,
ruleMessages,
Expand Down Expand Up @@ -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 }

Expand Down
3 changes: 0 additions & 3 deletions src/rules/function-parentheses-newline-inside/index.js
Expand Up @@ -2,7 +2,6 @@ import valueParser from "postcss-value-parser"
import {
declarationValueIndex,
isSingleLineString,
isStandardDeclaration,
isStandardFunction,
report,
ruleMessages,
Expand Down Expand Up @@ -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 => {
Expand Down
2 changes: 0 additions & 2 deletions src/rules/function-parentheses-space-inside/index.js
Expand Up @@ -2,7 +2,6 @@ import valueParser from "postcss-value-parser"
import {
declarationValueIndex,
isSingleLineString,
isStandardDeclaration,
isStandardFunction,
report,
ruleMessages,
Expand Down Expand Up @@ -36,7 +35,6 @@ export default function (expectation) {
if (!validOptions) { return }

root.walkDecls(decl => {
if (!isStandardDeclaration(decl)) { return }

if (decl.value.indexOf("(") === -1) { return }

Expand Down
7 changes: 3 additions & 4 deletions 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,
Expand All @@ -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 }

Expand Down
5 changes: 2 additions & 3 deletions src/rules/property-case/index.js
@@ -1,6 +1,6 @@
import {
isStandardDeclaration,
isCustomProperty,
isStandardProperty,
report,
ruleMessages,
validateOptions,
Expand All @@ -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()
Expand Down
8 changes: 3 additions & 5 deletions 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"
Expand All @@ -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({
Expand Down
5 changes: 2 additions & 3 deletions src/rules/root-no-standard-properties/index.js
@@ -1,6 +1,6 @@
import selectorParser from "postcss-selector-parser"
import {
isStandardDeclaration,
isStandardProperty,
isCustomProperty,
report,
ruleMessages,
Expand All @@ -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({
Expand Down
6 changes: 0 additions & 6 deletions 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,
Expand Down Expand Up @@ -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) ||
Expand Down
13 changes: 1 addition & 12 deletions 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")
Expand All @@ -29,20 +28,10 @@ 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) {
postcss().process(css).then(result => {
result.root.walkDecls(cb)
})
}

function lessRules(css, cb) {
postcss().process(css, { syntax: less }).then(result => {
result.root.walkDecls(cb)
})
}
12 changes: 12 additions & 0 deletions 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()
})
1 change: 1 addition & 0 deletions src/utils/index.js
Expand Up @@ -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"
Expand Down
8 changes: 2 additions & 6 deletions src/utils/isStandardDeclaration.js
Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions 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
}

0 comments on commit 26fa669

Please sign in to comment.