Skip to content

Commit

Permalink
feat: added shorthand support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyOGo committed Apr 27, 2020
1 parent e24fb76 commit 3cfd8af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @property {boolean} [ignoreFunctions=true] - Wheter or not to ignore function.
* @property {null|number|string|Array|object} [ignoreKeywords=null] - **DEPRECATED:** An ignored keywords config.
* @property {number|string|RegExp|Array|object} [ignoreValues=null] - An ignored values config.
* @property {boolean} [expandShorthand=true] - Wheter or not to expand shorthand CSS properties.
* @property {boolean} [recurseLonghand=false] - Wheter or not to expand longhand CSS properties recursivly - this is only useful for the border property.
* @property {string} [severity='error'] - Adjust severity of the rule, `'warning'` or `'error'` (default).
* @property {null|string} [message=null] - A custom message when a rule is violated, interpolated with `${types}`, `${value}` and `${property}`.
* @property {boolean} [disableFix=false] - Don't auto-fix if `--fix` option is applied.
Expand All @@ -20,6 +22,8 @@
* @property {boolean} [ignoreFunctions=true] - Ignore function by default.
* @property {null} [ignoreKeywords=null] - **DEPRECATED:** Forbid keywords by default.
* @property {null} [ignoreValues=null] - Forbid values by default.
* @property {boolean} [expandShorthand=true] - Expand shorthand CSS properties.
* @property {boolean} [recurseLonghand=false] - Do not expand longhand properties recursivly - only useful for `border`.
* @property {string} [severity='error'] - This rule's default severity is `'error'`.
* @property {null} [message=null] - Use default message.
* @property {null} [disableFix=false] - Don't auto-fix if `--fix` option is applied.
Expand All @@ -31,6 +35,8 @@ const defaults = {
ignoreFunctions: true,
ignoreKeywords: null,
ignoreValues: null,
expandShorthand: true,
recurseLonghand: false,
severity: 'error',
message: null,
disableFix: false,
Expand Down
37 changes: 32 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import stylelint from 'stylelint'
import shortCSS from 'shortcss'

import {
validProperties, validOptions, expected, getTypes, getIgnoredKeywords, getIgnoredValues, getAutoFixFunc,
Expand Down Expand Up @@ -106,7 +107,7 @@ const ruleFunction = (properties, options, context = {}) => (root, result) => {
...options,
}
const {
ignoreVariables, ignoreFunctions, ignoreKeywords, ignoreValues, message, disableFix, autoFixFunc,
ignoreVariables, ignoreFunctions, ignoreKeywords, ignoreValues, message, disableFix, autoFixFunc, expandShorthand, recurseLonghand,
} = config
const autoFixFuncNormalized = getAutoFixFunc(autoFixFunc)
const reKeywords = ignoreKeywords ? {} : null
Expand All @@ -122,7 +123,36 @@ const ruleFunction = (properties, options, context = {}) => (root, result) => {
}

// walk through all declarations filtered by configured properties
root.walkDecls(propFilter, lintDeclStrictValue)
root.walkDecls(filterDesls)

/**
* Filter declarations for matching properties and expand shorthand properties.
*
* @callback
* @param {object} node - A Declaration-Node from PostCSS AST-Parser.
*/
function filterDesls(node) {
const { value: originalValue, prop: originalProp } = node

// skip variable declarations
if (reSkipProp.test(originalProp)) return

if (originalProp === propFilter || (propFilter instanceof RegExp && propFilter.test(originalProp))) {
lintDeclStrictValue(node)
} else if (expandShorthand && shortCSS.isShorthand(originalProp)) {
const expandedProps = shortCSS.expand(originalProp, originalValue, recurseLonghand)

Object.keys(expandedProps).forEach((prop) => {
const value = expandedProps[prop]

if (prop === propFilter || (propFilter instanceof RegExp && propFilter.test(prop))) {
const { raws } = node

lintDeclStrictValue(node.clone({ value, prop, raws }))
}
})
}
}

/**
* Lint usages of declarations values againts, variables, functions
Expand All @@ -134,9 +164,6 @@ const ruleFunction = (properties, options, context = {}) => (root, result) => {
function lintDeclStrictValue(node) {
const { value, prop } = node

// skip variable declarations
if (reSkipProp.test(prop)) return

// falsify everything by default
let validVar = false
let validFunc = false
Expand Down

0 comments on commit 3cfd8af

Please sign in to comment.