Skip to content

Commit 82df196

Browse files
author
Masaaki Morishita
authored
Merge pull request #221 from gucong3000/sync_stylelint_api
Supports all stylelint options.
2 parents be48dfd + 8afcf89 commit 82df196

17 files changed

+165
-154
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"no-console": 0,
99
"space-before-function-paren": [2, "always"],
1010
"semi": [2, "never"],
11-
"eqeqeq": [2, "always"],
11+
"eqeqeq": [2, "allow-null"],
1212
"block-spacing": [2, "always"]
1313
}
1414
}

CHANGELOG.md

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1+
## Head
2+
3+
- Fixed [Same issue in stylelint #218](https://github.com/stylelint/stylelint/issues/1881) use `stylelint` to load configuration file.
4+
- Fixed: `--config` option to CLI. same as [stylelint CLI](https://github.com/stylelint/stylelint/blob/master/src/cli.js#L44)
5+
- Added: `--config-basedir` option to CLI. same as [stylelint CLI](https://github.com/stylelint/stylelint/blob/master/src/cli.js#L57)
6+
- Fixed: `config` option to Node.js API. same as [stylelint API](http://stylelint.io/user-guide/node-api/#config)
7+
- Added: `configBasedir` option to Node.js API. same as [stylelint API](http://stylelint.io/user-guide/node-api/#configbasedir)
8+
- Added: `configFile` option to Node.js API. same as [stylelint API](http://stylelint.io/user-guide/node-api/#configfile)
9+
- Added: When `stylelint` configuration file not found, it will extends from `editorconfig`:
10+
- [indentation](http://stylelint.io/user-guide/rules/indentation/)
11+
- [no-missing-end-of-source-newline](http://stylelint.io/user-guide/rules/no-missing-end-of-source-newline/)
12+
- [no-eol-whitespace](http://stylelint.io/user-guide/rules/no-eol-whitespace/)
13+
- Fixed: rules that value is an array, can work properly:
14+
- [at-rule-semicolon-newline-after](http://stylelint.io/user-guide/rules/at-rule-semicolon-newline-after/)
15+
- [block-closing-brace-newline-after](http://stylelint.io/user-guide/rules/block-closing-brace-newline-after/)
16+
- [block-opening-brace-newline-before](http://stylelint.io/user-guide/rules/block-opening-brace-newline-before/)
17+
- [block-opening-brace-space-before](http://stylelint.io/user-guide/rules/block-opening-brace-space-before/)
18+
- [declaration-colon-space-after](http://stylelint.io/user-guide/rules/declaration-colon-space-after/)
19+
- [declaration-colon-space-before](http://stylelint.io/user-guide/rules/declaration-colon-space-before/)
20+
- [length-zero-no-unit](http://stylelint.io/user-guide/rules/length-zero-no-unit/)
21+
- [number-leading-zero](http://stylelint.io/user-guide/rules/number-leading-zero/)
22+
- [number-no-trailing-zeros](http://stylelint.io/user-guide/rules/number-no-trailing-zeros/)
23+
- [selector-combinator-space-after](http://stylelint.io/user-guide/rules/selector-combinator-space-after/)
24+
- [selector-combinator-space-before](http://stylelint.io/user-guide/rules/selector-combinator-space-before/)
25+
- [selector-list-comma-newline-after](http://stylelint.io/user-guide/rules/selector-list-comma-newline-after/)
26+
- [selector-list-comma-newline-before](http://stylelint.io/user-guide/rules/selector-list-comma-newline-before/)
27+
- [selector-list-comma-space-after](http://stylelint.io/user-guide/rules/selector-list-comma-space-after/)
28+
- [selector-list-comma-space-before](http://stylelint.io/user-guide/rules/selector-list-comma-space-before/)
29+
- [shorthand-property-no-redundant-values](http://stylelint.io/user-guide/rules/shorthand-property-no-redundant-values/)
30+
131
## v4.3.1
232

333
- Fixed [#208](https://github.com/morishitter/stylefmt/issues/208)

README.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,12 @@ Usage: stylefmt [options] input-file [output-file]
281281
282282
Options:
283283
284-
-d, --diff output diff against original file
285-
-R, --recursive format files recursively
286-
-c, --config path to a specific configuration file (JSON, YAML, or CommonJS)
287-
-V, --versions output the version number
288-
-h, --help output usage information
284+
-d, --diff output diff against original file
285+
-R, --recursive format files recursively
286+
-c, --config path to a specific configuration file (JSON, YAML, or CommonJS)
287+
-b, --config-basedir path to the directory that relative paths defining "extends"
288+
-V, --versions output the version number
289+
-h, --help output usage information
289290
```
290291

291292
stylefmt can also read a file from stdin if there are no input-fle as argument in CLI.

bin/cli.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var argv = minimist(process.argv.slice(2), {
1919
d: 'diff',
2020
l: 'list',
2121
R: 'recursive',
22+
b: 'config-basedir',
2223
c: 'config'
2324
}
2425
})
@@ -37,32 +38,42 @@ if (argv.h) {
3738
console.log('')
3839
console.log('Options:')
3940
console.log('')
40-
console.log(' -d, --diff output diff against original file')
41-
console.log(' -l, --list format list of space seperated files in place')
42-
console.log(' -R, --recursive format files recursively')
43-
console.log(' -c, --config path to a specific configuration file (JSON, YAML, or CommonJS)')
44-
console.log(' -v, --version output the version number')
45-
console.log(' -h, --help output usage information')
41+
console.log(' -d, --diff output diff against original file')
42+
console.log(' -l, --list format list of space seperated files in place')
43+
console.log(' -R, --recursive format files recursively')
44+
console.log(' -c, --config path to a specific configuration file (JSON, YAML, or CommonJS)')
45+
console.log(' -b, --config-basedir path to the directory that relative paths defining "extends"')
46+
console.log(' -v, --version output the version number')
47+
console.log(' -h, --help output usage information')
4648
process.exit()
4749
}
4850

4951

50-
var options ={}
52+
var options = {}
5153
if (argv.c) {
52-
options.config = argv.c
54+
options.configFile = argv.c
55+
}
56+
57+
if (argv.b) {
58+
options.configBasedir = (path.isAbsolute(argv.b))
59+
? argv.b
60+
: path.resolve(process.cwd(), argv.b)
5361
}
5462

5563
if (argv.l) {
5664
var files = [argv.l].concat(argv._)
5765
processMultipleFiles(files)
5866
} else if (argv._[0]) {
59-
var input = argv._[0]
67+
var input = path.resolve(process.cwd(), argv._[0])
6068
var output = argv._[1] || argv._[0]
6169

6270
var css = fs.readFileSync(input, 'utf-8')
6371

6472
postcss([stylefmt(options)])
65-
.process(css, { syntax: scss })
73+
.process(css, {
74+
from: input,
75+
syntax: scss
76+
})
6677
.then(function (result) {
6778
var formatted = result.css
6879
if (argv.d) {
@@ -105,7 +116,10 @@ function processMultipleFiles (files) {
105116
var css = fs.readFileSync(fullPath, 'utf-8')
106117

107118
postcss([stylefmt(options)])
108-
.process(css, { syntax: scss })
119+
.process(css, {
120+
from: fullPath,
121+
syntax: scss
122+
})
109123
.then(function (result) {
110124
var formatted = result.css
111125
if (css !== formatted) {

index.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ const formatSassVariables = require('./lib/formatSassVariables')
99

1010

1111
const stylefmt = postcss.plugin('stylefmt', function (options) {
12-
options = options || {}
13-
12+
var paramer = params(options)
1413
return function (root) {
15-
return params(options).then(function (params) {
14+
return paramer(root).then(function (params) {
1615
formatComments(root, params)
1716
formatAtRules(root, params)
1817
formatOrder(root, params)

lib/formatAtRules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ function blockOpeningBraceNewlineBefore (atrule, opts) {
207207
if (isIgnoreRule(opts.stylelint, atrule)) {
208208
return atrule.raws.between
209209
}
210-
switch (opts.stylelint['block-opening-brace-newline-before']) {
210+
switch (getProperty(opts.stylelint, 'block-opening-brace-newline-before')) {
211211
case 'always':
212212
return '\n' + opts.indentation
213213
case 'always-single-line':

lib/formatDecls.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var formatValues = require('./formatValues')
22
var hasDecls = require('./hasDecls')
3+
var getProperty = require('./util').getProperty
34

45
function formatDecls (rule, indent, indentWidth, stylelint) {
56

@@ -31,11 +32,11 @@ function formatDecls (rule, indent, indentWidth, stylelint) {
3132
}
3233
decl.raws.before = declBefore
3334

34-
if (stylelint && stylelint['declaration-colon-space-before']) {
35+
if (getProperty(stylelint, 'declaration-colon-space-before')) {
3536
decl.raws.between = declarationColonSpaceBefore(stylelint, decl.raws.between)
3637
}
3738

38-
if (stylelint && stylelint['declaration-colon-space-after']) {
39+
if (getProperty(stylelint, 'declaration-colon-space-after')) {
3940
decl.raws.between = declarationColonSpaceAfter(stylelint, decl.raws.between)
4041
}
4142

@@ -47,7 +48,7 @@ function formatDecls (rule, indent, indentWidth, stylelint) {
4748
}
4849

4950
function declarationColonSpaceBefore (stylelint, between) {
50-
switch (stylelint['declaration-colon-space-before']) {
51+
switch (getProperty(stylelint, 'declaration-colon-space-before')) {
5152
case 'always':
5253
return ' ' + between
5354
default:
@@ -56,7 +57,7 @@ function declarationColonSpaceBefore (stylelint, between) {
5657
}
5758

5859
function declarationColonSpaceAfter (stylelint, between) {
59-
switch (stylelint['declaration-colon-space-after']) {
60+
switch (getProperty(stylelint, 'declaration-colon-space-after')) {
6061
case 'never':
6162
return between.trim()
6263
default:

lib/formatRules.js

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var formatSelectors = require('./formatSelectors')
22
var formatDecls = require('./formatDecls')
33
var getIndent = require('./getIndent')
4+
var getProperty = require('./util').getProperty
45

56
function formatRules (root, params) {
67
var stylelint = params.stylelint
@@ -75,24 +76,20 @@ function setRuleRawsConfig (rule, opts) {
7576
rule.raws.before = opts.ruleBefore
7677
rule.raws.afterName = ' '
7778
setPropertyForAtRuleSemicolonNewlineAfter(rule.raws, opts)
78-
if (opts.stylelint['block-closing-brace-newline-after']) {
79+
if (getProperty(opts.stylelint, 'block-closing-brace-newline-after')) {
7980
setPropertyForBlockClosingBraceNewlineAfter(rule.raws, opts)
8081
} else {
8182
setPropertyForAtRuleSemicolonNewlineAfter(rule.raws, opts)
8283
}
83-
if (opts.stylelint['block-opening-brace-newline-before']) {
84+
if (getProperty(opts.stylelint, 'block-opening-brace-newline-before')) {
8485
rule.raws.between = blockOpeningBraceNewlineBefore(rule, opts)
8586
} else {
8687
rule.raws.between = blockOpeningBraceSpaceBefore(rule, opts)
8788
}
8889
}
8990

9091
function setPropertyForAtRuleSemicolonNewlineAfter (raws, opts) {
91-
var ruleValue = opts.stylelint['at-rule-semicolon-newline-after']
92-
if (typeof ruleValue === 'object') {
93-
ruleValue = ruleValue[0]
94-
}
95-
switch (ruleValue) {
92+
switch (getProperty(opts.stylelint, 'at-rule-semicolon-newline-after')) {
9693
default:
9794
raws.after = '\n' + opts.indentation
9895
raws.semicolon = true
@@ -102,11 +99,7 @@ function setPropertyForAtRuleSemicolonNewlineAfter (raws, opts) {
10299

103100
function setPropertyForBlockClosingBraceNewlineAfter (raws, opts) {
104101
raws.semicolon = true
105-
var ruleValue = opts.stylelint['block-closing-brace-newline-after']
106-
if (typeof ruleValue === 'object') {
107-
ruleValue = ruleValue[0]
108-
}
109-
switch (ruleValue) {
102+
switch (getProperty(opts.stylelint, 'block-closing-brace-newline-after')) {
110103
case 'never-multi-line':
111104
raws.before = ''
112105
break
@@ -120,7 +113,7 @@ function blockOpeningBraceSpaceBefore (rule, opts) {
120113
if (isIgnoreRule(opts.stylelint, rule)) {
121114
return rule.raws.between
122115
}
123-
switch (opts.stylelint['block-opening-brace-space-before']) {
116+
switch (getProperty(opts.stylelint, 'block-opening-brace-space-before')) {
124117
case 'never':
125118
return ''
126119
case 'always-single-line':
@@ -152,7 +145,7 @@ function blockOpeningBraceNewlineBefore (rule, opts) {
152145
if (isIgnoreRule(opts.stylelint, rule)) {
153146
return rule.raws.between
154147
}
155-
switch (opts.stylelint['block-opening-brace-newline-before']) {
148+
switch (getProperty(opts.stylelint, 'block-opening-brace-newline-before')) {
156149
case 'always':
157150
return '\n' + opts.indentation
158151
case 'always-single-line':

lib/formatSelectors.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var getProperty = require('./util').getProperty
12
function formatSelectors (rule, indentation, stylelint) {
23
var tmp = []
34
var isSingleLine = false
@@ -24,7 +25,7 @@ function formatSelectors (rule, indentation, stylelint) {
2425
var separator = selectorListCommaSpaceBefore(stylelint, isSingleLine, indentation)
2526
+ selectorListCommaNewlineBefore(stylelint, isSingleLine, indentation)
2627
+ ','
27-
if (stylelint['selector-list-comma-space-after']) {
28+
if (getProperty(stylelint, 'selector-list-comma-space-after')) {
2829
separator += selectorListCommaSpaceAfter(stylelint, isSingleLine)
2930
} else {
3031
separator += selectorListCommaNewlineAfter(stylelint, isSingleLine)
@@ -43,7 +44,7 @@ function formatSelectors (rule, indentation, stylelint) {
4344
}
4445

4546
function selectorCombinatorSpaceBefore (stylelint, selector) {
46-
switch (stylelint['selector-combinator-space-before']) {
47+
switch (getProperty(stylelint, 'selector-combinator-space-before')) {
4748
case 'never':
4849
return selector.replace(/\s+(?=[+~>])/g, "")
4950
default:
@@ -52,7 +53,7 @@ function selectorCombinatorSpaceBefore (stylelint, selector) {
5253
}
5354

5455
function selectorCombinatorSpaceAfter (stylelint, selector) {
55-
switch (stylelint['selector-combinator-space-after']) {
56+
switch (getProperty(stylelint, 'selector-combinator-space-after')) {
5657
case 'never':
5758
return selector.replace(/([+~>])\s*/g, "$1")
5859
default:
@@ -61,7 +62,7 @@ function selectorCombinatorSpaceAfter (stylelint, selector) {
6162
}
6263

6364
function selectorListCommaNewlineBefore (stylelint, isSingleLine, indentation) {
64-
switch (stylelint['selector-list-comma-newline-before']) {
65+
switch (getProperty(stylelint, 'selector-list-comma-newline-before')) {
6566
case 'always':
6667
return '\n' + indentation
6768
case 'always-multi-line':
@@ -75,7 +76,7 @@ function selectorListCommaNewlineBefore (stylelint, isSingleLine, indentation) {
7576
}
7677

7778
function selectorListCommaNewlineAfter (stylelint, isSingleLine) {
78-
switch (stylelint['selector-list-comma-newline-after']) {
79+
switch (getProperty(stylelint, 'selector-list-comma-newline-after')) {
7980
case 'always-multi-line':
8081
if (isSingleLine) {
8182
return '\n'
@@ -89,7 +90,7 @@ function selectorListCommaNewlineAfter (stylelint, isSingleLine) {
8990
}
9091

9192
function selectorListCommaSpaceBefore (stylelint, isSingleLine, indentation) {
92-
switch (stylelint['selector-list-comma-space-before']) {
93+
switch (getProperty(stylelint, 'selector-list-comma-space-before')) {
9394
case 'always':
9495
return ' '
9596
case 'always-single-line':
@@ -108,7 +109,7 @@ function selectorListCommaSpaceBefore (stylelint, isSingleLine, indentation) {
108109
}
109110

110111
function selectorListCommaSpaceAfter (stylelint, isSingleLine) {
111-
switch (stylelint['selector-list-comma-space-after']) {
112+
switch (getProperty(stylelint, 'selector-list-comma-space-after')) {
112113
case 'never':
113114
return ''
114115
case 'always-single-line':

lib/formatShorthand.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var valueParser = require('postcss-value-parser')
2+
var getProperty = require('./util').getProperty
23

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

@@ -70,7 +71,7 @@ function isShorthandableProperty (prop) {
7071

7172

7273
function formatShorthand (decl, stylelint) {
73-
if (stylelint && stylelint['shorthand-property-no-redundant-values'] === true) {
74+
if (getProperty(stylelint, 'shorthand-property-no-redundant-values') === true) {
7475
if (
7576
hasIgnoredCharacters(decl.value)
7677
|| isIgnoredShorthandProperty(decl.prop)

lib/formatValues.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var formatTransforms = require('./formatTransforms')
22
var formatColors = require('./formatColors')
33
var formatZeros = require('./formatZeros')
44
var formatShorthand = require('./formatShorthand')
5+
var getProperty = require('./util').getProperty
56

67
function formatvalues (decl, stylelint) {
78
var isDataUrl = /data:.+\/(.+);base64,(.*)/.test(decl.value)
@@ -17,12 +18,13 @@ function formatvalues (decl, stylelint) {
1718
decl.value = decl.value.trim().replace(/\s+/g, ' ')
1819
}
1920

20-
if (stylelint && stylelint['string-quotes']) {
21-
if (stylelint['string-quotes'] === 'double') {
21+
switch (getProperty(stylelint, 'string-quotes')) {
22+
case 'double':
2223
decl.value = decl.value.replace(/'/g, '"')
23-
} else if (stylelint['string-quotes'] === 'single') {
24+
break
25+
case 'single':
2426
decl.value = decl.value.replace(/"/g, '\'')
25-
}
27+
break
2628
}
2729

2830
if (decl.prop === 'content') {

lib/formatZeros.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
var zeroWithUnitRegex = /^0[\.0]*(?:px|r?em|ex|ch|vh|vw|cm|mm|in|pt|pc|vmin|vmax)/g
2+
var getProperty = require('./util').getProperty
23

34
function formatZeros (value, stylelint) {
4-
if (stylelint && stylelint['length-zero-no-unit'] === true) {
5+
if (getProperty(stylelint, 'length-zero-no-unit') === true) {
56
value = value.replace(zeroWithUnitRegex, '0')
67
}
78

8-
if (stylelint && stylelint['number-leading-zero']) {
9-
if (stylelint['number-leading-zero'] === 'always') {
9+
switch (getProperty(stylelint, 'number-leading-zero')) {
10+
case 'always':
1011
value = value.replace(/(\D|^)(\.\d+)/g, '$10$2')
11-
} else if (stylelint['number-leading-zero'] === 'never') {
12+
break
13+
case 'never':
1214
value = value.replace(/(\D|^)(0)(\.\d+)/g, '$1$3')
13-
}
15+
break
1416
}
1517

16-
if (stylelint && stylelint['number-no-trailing-zeros'] === true) {
18+
if (getProperty(stylelint, 'number-no-trailing-zeros') === true) {
1719
value = value.replace(/(\d+)(\.[0-9]*[1-9]+)(0+)/g, '$1$2')
1820
value = value.replace(/(\d+)(\.0+)(?=\D|$)/g, '$1')
1921
}

0 commit comments

Comments
 (0)