Skip to content

Commit

Permalink
Added "alwaysUseNoneOverZero" formatting option
Browse files Browse the repository at this point in the history
  • Loading branch information
Anantachai Saothong (Manta) committed Apr 28, 2017
1 parent 8e995ae commit 91a757e
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 8 deletions.
9 changes: 6 additions & 3 deletions docs/format.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions docs/index.html

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions edge/createFormattingOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,20 @@ const schema = {
`
}
},
alwaysUseNoneOverZero: {
description: 'Replace <code>0</code> (regardless of its unit) with <code>none</code> for <code>border</code> and <code>outline</code> properties, or do nothing.',
type: 'boolean',
default: false,
example: {
values: [true, false],
code: `
.class1
border 0px
`
}
},
alwaysUseZeroWithoutUnit: {
description: 'Replace <code>0px</code>, <code>0%</code>, <code>0em</code> and so on with <code>0</code> without units, or do nothing.',
description: 'Replace <code>0</code> (regardless of its unit) with <code>0</code> (without units), or do nothing.',
type: 'boolean',
default: false,
example: {
Expand All @@ -268,7 +280,7 @@ const schema = {
}
},
reduceMarginAndPaddingValues: {
description: 'Reduce <code>margin</code> and <code>padding</code> duplicate values by converting <code>margin x x x x</code> to <code>margin x</code>, <code>margin x y x y</code> to <code>margin x y</code>, and <code>margin x y y y</code> to <code>margin x y y</code> where <code>x</code>, <code>y</code> is a property value.',
description: 'Reduce <code>margin</code> and <code>padding</code> duplicate values by converting <code>margin x x x x</code> to <code>margin x</code>, <code>margin x y x y</code> to <code>margin x y</code>, and <code>margin x y y y</code> to <code>margin x y y</code> where <code>x</code>, <code>y</code> is a unique property value.',
type: 'boolean',
default: false,
example: {
Expand Down
1 change: 1 addition & 0 deletions edge/createFormattingOptionsFromStylint.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const stylintOptionMap = {
'extendPref': value => ['alwaysUseExtends', value === '@extends'],
'indentPref': value => value > 0 ? ['tabStopChar', _.repeat(' ', value)] : [],
'leadingZero': createAdapterForAlwaysNeverFalse('insertLeadingZeroBeforeFraction'),
'none': createAdapterForAlwaysNeverFalse('alwaysUseNoneOverZero'),
'parenSpace': createAdapterForAlwaysNeverFalse('insertSpaceInsideParenthesis'),
'quotePref': value => value === 'single' && ['quoteChar', '\''] || value === 'double' && ['quoteChar', '"'], // the values "single" and "double" will be converted to "'" and "\"" respectively.
'semicolons': createAdapterForAlwaysNeverFalse('insertSemicolons'),
Expand Down
4 changes: 4 additions & 0 deletions edge/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ function format(content, options = {}) {
} else if (propertyValues.length === 4 && propertyValues[0] !== propertyValues[2] && propertyValues[1] === propertyValues[3]) {
propertyValues = [propertyValues[0], propertyValues[1], propertyValues[2]]
}
} else if (propertyName === 'border' || propertyName === 'outline') {
if (options.alwaysUseNoneOverZero && propertyValues.length === 1 && /^0(\.0*)?(\w+|\%)?/.test(propertyValues[0])) {
propertyValues = ['none']
}
}

// Insert the property value(s) without the last portion of comments
Expand Down
1 change: 1 addition & 0 deletions spec/createFormattingOptionsFromStylint.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('createFormattingOptionsFromStylint', () => {
insertLeadingZeroBeforeFraction: false,
insertSemicolons: false,
sortProperties: 'alphabetical',
alwaysUseNoneOverZero: false,
alwaysUseZeroWithoutUnit: true,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"alwaysUseNoneOverZero": false,
"newLineChar": "\r\n"
}
6 changes: 6 additions & 0 deletions spec/option-always-use-none-over-zero-false/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body
border 0
border 0px
border none
outline 0%
outline none
7 changes: 7 additions & 0 deletions spec/option-always-use-none-over-zero-false/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
border: 0;
border: 0px;
border: none;
outline: 0%;
outline: none;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"alwaysUseNoneOverZero": true,
"newLineChar": "\r\n"
}
6 changes: 6 additions & 0 deletions spec/option-always-use-none-over-zero-true/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
body
border 0
border 0px
border none
outline 0%
outline none
7 changes: 7 additions & 0 deletions spec/option-always-use-none-over-zero-true/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
body {
border: none;
border: none;
border: none;
outline: none;
outline: none;
}

0 comments on commit 91a757e

Please sign in to comment.