Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for vendor prefix #299

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
{
"extends": "eslint:recommended",
"env": {
"node": true,
"es6": true
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"no-console": 0,
"space-before-function-paren": [2, "always"],
"semi": [2, "never"],
"eqeqeq": [2, "allow-null"],
"block-spacing": [2, "always"]
"block-spacing": [
"error",
"always"
],
"comma-spacing": [
"error"
],
"eqeqeq": [
"error",
"smart"
],
"keyword-spacing": [
"error"
],
"no-console": [
"error",
{
"allow": [
"warn",
"error"
]
}
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
"space-before-function-paren": [
"error",
"always"
],
"valid-jsdoc": [
"error"
]
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules
npm-debug.log
.nyc_output/
coverage/
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ test
.eslintrc.json
.travis.yml
CHANGELOG.md
.nyc_output
coverage
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
sudo: false
language: node_js
node_js:
- "4"
- "stable"
- "6"
script:
- npm run lint
- npm test
- "4"
10 changes: 5 additions & 5 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

/* eslint no-console: "off" */
var fs = require('fs')
var path = require('path')
var stdin = require('stdin')
Expand Down Expand Up @@ -109,8 +109,8 @@ if (argv.r) {

function processMultipleFiles (files) {
files = files.filter(isCss).sort()
if(!files.length){
console.error("Files glob patterns specified did not match any css files.")
if (!files.length){
console.error('Files glob patterns specified did not match any css files.')
return
}

Expand Down Expand Up @@ -144,7 +144,7 @@ function processMultipleFiles (files) {
messages = messages.filter(function (file){
return file
})
if(messages.length){
if (messages.length){
messages = messages.join(', ') + '\n\n' + messages.length
} else {
messages = 'No'
Expand All @@ -170,7 +170,7 @@ function handleDiff (file, original, formatted) {

if (chalk.supportsColor) {
file = chalk.blue(file)
if(diff) {
if (diff) {
diff = chalk.gray(diff)
} else {
var JsDiff = require('diff')
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const stylefmt = postcss.plugin('stylefmt', function (options) {
var paramer = params(options)
return function (root, result) {
return paramer(root, result).then(function (params) {
if(params) {
if (params) {
formatComments(root, params)
formatAtRules(root, params)
formatRules(root, params)
Expand Down
34 changes: 28 additions & 6 deletions lib/formatAtRules.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var postcss = require('postcss')
var unprefixAtRule = require('postcss-unprefix/lib/clearAtRule')
var formatAtRuleParams = require('./formatAtRuleParams')
var formatDecls = require('./formatDecls')
var getIndent = require('./getIndent')
Expand All @@ -8,11 +10,31 @@ var util = require('./util')
var getProperty = util.getProperty
var getOptions = util.getOptions

function shouldClear (atrule, stylelint) {
if (postcss.vendor.prefix(atrule.name)) {
return getProperty(stylelint, 'at-rule-no-vendor-prefix')
} else if (atrule.name === 'media') {
return getProperty(stylelint, 'media-feature-name-no-vendor-prefix') && /\(\s*(?:-\w+-)?(?:\w+-)*device-pixel-ratio\b/.test(atrule.params)
} else if (atrule.name === 'supports') {
var propNoVendorPrefix = getProperty(stylelint, 'property-no-vendor-prefix')
var valueNoVendorPrefix = getProperty(stylelint, 'value-no-vendor-prefix')
return (propNoVendorPrefix && valueNoVendorPrefix)
|| (propNoVendorPrefix && /\(\s*(?:-\w+){2,}\b/.test(atrule.params))
|| (propNoVendorPrefix && /\(\s*\w+[\w\-]+\s*\:[^()]*[\s,]?-\w+-/.test(atrule.params))
}
}

function formatAtRules (root, params) {
var stylelint = params.stylelint
var indentWidth = params.indentWidth

root.walkAtRules(function (atrule, index) {
if (shouldClear(atrule, stylelint)) {
unprefixAtRule(atrule)
if (!atrule.parent) {
return
}
}

var parentType = atrule.parent.type
var sassAtBlockTypes = [
Expand Down Expand Up @@ -98,15 +120,15 @@ function formatAtRules (root, params) {
}

if (atrule.name === 'mixin') {
atrule.params = atrule.params.replace(/(^[\w|-]+)\s*\(/, "$1(")
atrule.params = atrule.params.replace(/(^[\w|-]+)\s*\(/, '$1(')
formatDecls(atrule, indentation, indentWidth, stylelint)
}

if (atrule.name === 'extend' ||
atrule.name === 'debug' ||
atrule.name === 'warn' ||
atrule.name === 'error' ) {
atrule.params = atrule.params.replace(/\s+/g, " ")
atrule.params = atrule.params.replace(/\s+/g, ' ')
atrule.raws.before = '\n' + indentation
atrule.raws.between = ''
}
Expand All @@ -123,7 +145,7 @@ function formatAtRules (root, params) {
}

if (atrule.name === 'include') {
atrule.params = atrule.params.replace(/(^[\w|-]+)\s*\(/, "$1(")
atrule.params = atrule.params.replace(/(^[\w|-]+)\s*\(/, '$1(')
atrule.params = atrule.params.replace(/\)\s*{/g, ') ')
if (!hasLineBreaksBefore) {
atrule.raws.before = '\n' + indentation
Expand Down Expand Up @@ -215,7 +237,7 @@ function formatAtRules (root, params) {
function isAfterComment (prev) {
return (
prev
&& prev.type === "comment"
&& prev.type === 'comment'
)
}

Expand All @@ -228,7 +250,7 @@ function isFirstNested (prev, atrule, isNested) {

function isBlocklessGroup (prev, atrule) {
return (
prev && prev.type === "atrule"
prev && prev.type === 'atrule'
&& !hasBlock(prev)
&& !hasBlock(atrule)
)
Expand All @@ -238,7 +260,7 @@ function isBlocklessAfterSameNameBlockless (prev, atrule) {
return (
!hasBlock(atrule)
&& prev && !hasBlock(prev)
&& prev.type === "atrule"
&& prev.type === 'atrule'
&& prev.name === atrule.name
)
}
Expand Down
19 changes: 17 additions & 2 deletions lib/formatDecls.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
var formatValues = require('./formatValues')
var hasDecls = require('./hasDecls')
var postcss = require('postcss')
var unprefixDecl = require('postcss-unprefix/lib/clearDecl')
var hasEmptyLine = require('stylelint/lib/utils/hasEmptyLine')
var isStandardSyntaxDeclaration = require('stylelint/lib/utils/isStandardSyntaxDeclaration')
var isCustomProperty = require('stylelint/lib/utils/isCustomProperty')
var util = require('./util')
var getProperty = util.getProperty
var getOptions = util.getOptions
var isSingleLineString = require("stylelint/lib/utils/isSingleLineString")
var isSingleLineString = require('stylelint/lib/utils/isSingleLineString')

function formatDecls (rule, indent, indentWidth, stylelint) {
const isSingleLine = isSingleLineString(rule)
if (hasDecls(rule)) {
var propNoVendorPrefix = getProperty(stylelint, 'property-no-vendor-prefix')
var valueNoVendorPrefix = getProperty(stylelint, 'value-no-vendor-prefix')

rule.walkDecls(function (decl) {
if ((propNoVendorPrefix && valueNoVendorPrefix)
|| (propNoVendorPrefix && postcss.vendor.prefix(decl.prop))
|| (valueNoVendorPrefix && /(?:^|,|\s)-\w+-/m.test(decl.value)))
{
unprefixDecl(decl)
if (!decl.parent) {
return
}
}

var isSassVal = /^\$/.test(decl.prop)
var isIEHack = (/(\*|_)$/).test(decl.raws.before)
if (decl.prop && !isCustomProperty(decl.prop) && !isSassVal) {
Expand Down Expand Up @@ -71,7 +86,7 @@ function declarationEmptyLineBefore (stylelint, decl, indent, indentWidth, isSin
var exceptOptions = getOptions(stylelint, 'declaration-empty-line-before', 'except') || []
var ignoreOptions = getOptions(stylelint, 'declaration-empty-line-before', 'ignore') || []

var expectEmptyLineBefore = declarationEmptyLineBeforeRule === "always"
var expectEmptyLineBefore = declarationEmptyLineBeforeRule === 'always'

if (ignoreOptions.indexOf('after-comment') !== -1 && prev && prev.type === 'comment') {
ignore = true
Expand Down
19 changes: 13 additions & 6 deletions lib/formatSelectors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
var unprefixRule = require('postcss-unprefix/lib/clearRule')
var getProperty = require('./util').getProperty
function formatSelectors (rule, indentation, stylelint) {
if (/\:+-\w+-/.test(rule.selector)) {
unprefixRule(rule)
if (!rule.parent) {
return
}
}
var tmp = []
var isSingleLine = false

rule.selectors.forEach(function (selector, i) {
// don't add extra spaces to :nth-child(5n+1) etc.
if (!hasPlusInsideParens(selector) && !isAttrSelector(selector)) {
selector = selector.replace(/\s*([+~>])\s*/g, " $1 ")
selector = selector.replace(/\s*([+~>])\s*/g, ' $1 ')
}

if (isAttrSelector(selector)) {
selector = selector.replace(/\[\s*(\S+)\s*\]/g, "[$1]")
selector = selector.replace(/\[\s*(\S+)\s*\]/g, '[$1]')
}

selector = selectorCombinatorSpaceBefore(stylelint, selector)
Expand Down Expand Up @@ -46,18 +53,18 @@ function formatSelectors (rule, indentation, stylelint) {
function selectorCombinatorSpaceBefore (stylelint, selector) {
switch (getProperty(stylelint, 'selector-combinator-space-before')) {
case 'never':
return selector.replace(/\s+(?=[+~>])/g, "")
return selector.replace(/\s+(?=[+~>])/g, '')
default:
return selector.replace(/^\s*([+~>])/g, " $1")
return selector.replace(/^\s*([+~>])/g, ' $1')
}
}

function selectorCombinatorSpaceAfter (stylelint, selector) {
switch (getProperty(stylelint, 'selector-combinator-space-after')) {
case 'never':
return selector.replace(/([+~>])\s*/g, "$1")
return selector.replace(/([+~>])\s*/g, '$1')
default:
return selector.replace(/^\s*([+~>])\s*/g, "$1 ")
return selector.replace(/^\s*([+~>])\s*/g, '$1 ')
}
}

Expand Down
32 changes: 16 additions & 16 deletions lib/formatShorthand.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
var valueParser = require('postcss-value-parser')
var getProperty = require('./util').getProperty

var ignoredCharacters = ["+", "-", "*", "/", "(", ")", "$", "@", "--", "var("]
var ignoredCharacters = ['+', '-', '*', '/', '(', ')', '$', '@', '--', 'var(']

var ignoredShorthandProperties = [
"background",
"font",
"border",
"border-top",
"border-bottom",
"border-left",
"border-right",
"list-style",
"transition",
'background',
'font',
'border',
'border-top',
'border-bottom',
'border-left',
'border-right',
'list-style',
'transition',
]

var shorthandableProperties = [
"margin",
"padding",
"border-width",
"border-style",
"border-color",
"border-radius",
'margin',
'padding',
'border-width',
'border-style',
'border-color',
'border-radius',
]

function condense (top, right, bottom, left) {
Expand Down
3 changes: 1 addition & 2 deletions lib/formatValues.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,10 @@ function formatvalues (decl, stylelint) {
decl.value = formatTransforms(decl.value)

if (decl.important) {
decl.raws.important = " !important"
decl.raws.important = ' !important'
}

return decl
}


module.exports = formatvalues
2 changes: 1 addition & 1 deletion lib/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function loadConfig (stylelint, file, options) {
throw err
}
}).then(function (rules) {
if(rules) {
if (rules) {
return {
indentWidth: getIndentationFromStylelintRules(rules),
hasScss: /\.scss$/i.test(file),
Expand Down
1 change: 0 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ function getOptions (stylelint, prop, option) {
return stylelintProperty
}


module.exports = {
inAtRule: inAtRule,
getNestedRulesNum: getNestedRulesNum,
Expand Down
Loading