Skip to content

Commit

Permalink
Merge pull request #159 from morishitter/v4
Browse files Browse the repository at this point in the history
Changed not allow to format lines and spaces before and after comments
  • Loading branch information
Masaaki Morishita committed Jun 23, 2016
2 parents b028d7a + f8abb81 commit a035a64
Show file tree
Hide file tree
Showing 28 changed files with 97 additions and 120 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## v4.0.0

- Remove the default formatting rules for comments. [#158](https://github.com/morishitter/stylefmt/issues/158)

## v3.5.0

- Add `--config` option to specific configuration file
Expand Down
19 changes: 9 additions & 10 deletions README.md
Expand Up @@ -44,9 +44,7 @@
</p>
<br>

:tada: CSSfmt was renamed to stylefmt

stylefmt is a tool that automatically formats your stylesheets.
stylefmt is a tool that automatically formats CSS according to [stylelint](http://stylelint.io/) rules.

stylefmt'd code is:

Expand All @@ -73,6 +71,7 @@ Input (input.css):
--mainColor :#12345678;
--highlightColor:hwb(190, 35%, 20%);
}

/* custom media queries */
@custom-media

Expand All @@ -88,6 +87,7 @@ padding: calc((var(--fontSize) / 2) + 1px)}
@media (--viewport-medium) {
body {font-size: calc(var(--fontSize) * 1.2); }
}

/* custom selectors */
@custom-selector :--heading h1,h2,h3, h4,h5,h6;
:--heading { margin-top:0 }
Expand All @@ -105,6 +105,7 @@ a:any-link { color:color(var(--highlightColor) blackness(+20%)) }
h2 {font-variant-caps:small-caps;
}table{font-variant-numeric: lining-nums;
}

/* filters */
.blur{filter:blur(4px)}.sepia{
filter: sepia(.8);}
Expand Down Expand Up @@ -192,7 +193,6 @@ Input (input.scss):
// mixin for clearfix



@mixin clearfix () { &:before,
&:after {
content:" ";
Expand All @@ -202,8 +202,9 @@ Input (input.scss):
}.class
{
padding:10px;@include clearfix();}
.base { color: red; } // placeholder
.base { color: red; }

// placeholder
%base
{

Expand Down Expand Up @@ -356,15 +357,13 @@ and we can also format from the other stylelint's configration files or packages
- require `;` in last declaration
- require 1 space between values and `!important`
- disallow any spaces between `!` and `important`
- open 1 blank line between rules
- open 1 blank line between rules in atrules
- open 1 blank lines before comments
- require new line between a comment and rules
- leave 1 blank line between rules
- leave 1 blank line between rules in atrules
- disallow any brank lines between `@import`

### for nested selector syntax

- open 1 line between declarations and nested rules
- leave 1 line between declarations and nested rules

### SCSS

Expand Down
18 changes: 10 additions & 8 deletions lib/formatAtRules.js
@@ -1,3 +1,4 @@
var repeatString = require('repeat-string')
var formatAtRuleParams = require('./formatAtRuleParams')
var formatDecls = require('./formatDecls')
var getIndent = require('./getIndent')
Expand Down Expand Up @@ -29,12 +30,6 @@ function formatAtRules (root, params) {
var atruleBefore
var indentation = getIndent(atrule, indentWidth)

var hasComment = false
var prev = atrule.prev()
if (prev && prev.type === 'comment') {
hasComment = true
}

if (index === 0 && parentType === 'root') {
atruleBefore = ''
} else {
Expand All @@ -51,15 +46,22 @@ function formatAtRules (root, params) {
atruleBefore = '\n\n' + indentation
}

if (hasComment || atrule.name === 'import') {
if (atrule.name === 'import') {
atruleBefore = '\n' + indentation
}

if (atrule.name === 'else') {
atruleBefore = ' '
}
}

var prev = atrule.prev()
if (prev && prev.type === 'comment') {
var nlCount = atrule.raws.before.split('\n').length - 1
if (nlCount) {
atruleBefore = repeatString('\n', nlCount) + indentation
}
}
}

atrule.params = formatAtRuleParams(atrule, params)
atrule.raws.before = atruleBefore
Expand Down
47 changes: 11 additions & 36 deletions lib/formatComments.js
@@ -1,50 +1,25 @@
var repeatString = require('repeat-string')
var getIndent = require('./getIndent')

function formatComments (root, params) {
var indentWidth = params.indentWidth

root.walkComments(function (comment, index) {
var parentType = comment.parent.type
var isInline = comment.raws.inline === true
var isPrevInline = comment.prev() && comment.prev().raws.inline
var isPrevDecl = comment.prev() && comment.prev().type === 'decl'
var isPrevAtRule = comment.prev() && comment.prev().type === 'atrule'
var hasLineBreakBefore = /[\n]/.test(comment.raws.before)
var commentBefore
var indentation = getIndent(comment, indentWidth)

if (index === 0 && parentType === 'root') {
commentBefore = ''
} else if ((isPrevDecl || isPrevAtRule) && !hasLineBreakBefore) {
commentBefore = ' '
} else {
if (parentType === 'atrule') {
if (comment.parent.first === comment) {
commentBefore = '\n' + indentation
} else {
commentBefore = '\n\n' + indentation
}
}

if (parentType === 'rule') {
if (isInline && (comment.parent.first === comment || isPrevInline)) {
commentBefore = '\n' + indentation
} else {
commentBefore = '\n\n' + indentation
}
var nlCount = comment.raws.before.split('\n').length - 1
var spaceCount = comment.raws.before.split(' ').length - 1
if (parentType !== 'root') {
if (nlCount) {
comment.raws.before = repeatString('\n', nlCount) + indentation
}

if (parentType === 'root') {
// Handle multiline inline comments.
if (isInline && isPrevInline) {
commentBefore = '\n' + indentation
} else {
commentBefore = '\n\n' + indentation
}
} else {
if (nlCount) {
comment.raws.before = repeatString('\n', nlCount) + indentation
} else if (spaceCount){
comment.raws.before = repeatString(' ', spaceCount)
}
}

comment.raws.before = commentBefore
})

return root
Expand Down
15 changes: 7 additions & 8 deletions lib/formatRules.js
@@ -1,3 +1,4 @@
var repeatString = require('repeat-string')
var formatSelectors = require('./formatSelectors')
var formatDecls = require('./formatDecls')
var getIndent = require('./getIndent')
Expand All @@ -14,12 +15,6 @@ function formatRules (root, params) {
var parentType = rule.parent.type
var indentation = getIndent(rule, indentWidth)

var hasComment = false
var prev = rule.prev()
if (prev && prev.type === 'comment') {
hasComment = true
}

if (index === 0 && parentType === 'root') {
ruleBefore = ''
} else {
Expand All @@ -43,8 +38,12 @@ function formatRules (root, params) {
ruleBefore = '\n\n' + indentation
}

if (hasComment) {
ruleBefore = '\n' + indentation
var prev = rule.prev()
if (prev && prev.type === 'comment') {
var nlCount = rule.raws.before.split('\n').length - 1
if (nlCount) {
ruleBefore = repeatString('\n', nlCount) + indentation
}
}
}

Expand Down
24 changes: 13 additions & 11 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "stylefmt",
"version": "3.5.0",
"description": "stylefmt is a tool that automatically formats stylesheets code.",
"version": "4.0.0",
"description": "stylefmt is a tool that automatically formats CSS according to stylelint rules",
"main": "index.js",
"scripts": {
"test": "tape test/*.js"
Expand All @@ -19,27 +19,29 @@
"formatter",
"code style",
"beautifier",
"postcss-plugin"
"postcss-plugin",
"postcss",
"stylelint"
],
"author": "Masaaki Morishita",
"license": "MIT",
"dependencies": {
"cosmiconfig": "^1.1.0",
"css-color-list": "0.0.1",
"editorconfig": "^0.13.2",
"minimist": "^1.1.2",
"minimist": "^1.2.0",
"object-assign": "^4.1.0",
"postcss": "^5.0.6",
"postcss-scss": "^0.1.0",
"recursive-readdir": "^1.2.1",
"repeat-string": "^1.5.2",
"postcss": "^5.0.21",
"postcss-scss": "^0.1.8",
"recursive-readdir": "^2.0.0",
"repeat-string": "^1.5.4",
"resolve-from": "^2.0.0",
"stdin": "0.0.1",
"tmp": "0.0.26"
"tmp": "0.0.28"
},
"devDependencies": {
"each-series": "^1.0.0",
"klaw": "^1.2.0",
"tape": "^4.0.2"
"klaw": "^1.3.0",
"tape": "^4.6.0"
}
}
1 change: 0 additions & 1 deletion test/fixtures/comment-in-rules/comment-in-rules.out.css
@@ -1,6 +1,5 @@
.class {
display: inline-block;

/* comment */
float: left;
}
2 changes: 2 additions & 0 deletions test/fixtures/comment/comment.css
Expand Up @@ -15,6 +15,8 @@
/*
at rule
*/


@media only screen {
/* inner at rule comment */
.foo { display: none; }
Expand Down
8 changes: 6 additions & 2 deletions test/fixtures/comment/comment.out.css
@@ -1,24 +1,28 @@
/**
* comment
*/

.class {
padding: 10px;
}

/*comment*/


.foo {
color: red;
}


/*
at rule
*/


@media only screen {
/* inner at rule comment */
.foo {
display: none;
}

/*
* another comment
*/
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cssnext-example/cssnext-example.css
Expand Up @@ -3,6 +3,7 @@
--mainColor :#12345678;
--highlightColor:hwb(190, 35%, 20%);
}

/* custom media queries */
@custom-media

Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/cssnext-example/cssnext-example.out.css
Expand Up @@ -22,7 +22,6 @@ body {
font-size: calc(var(--fontSize) * 1.2);
}
}

/* custom selectors */
@custom-selector :--heading h1, h2, h3, h4, h5, h6;

Expand Down Expand Up @@ -56,7 +55,6 @@ h2 {
table {
font-variant-numeric: lining-nums;
}

/* filters */
.blur {
filter: blur(4px);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/inline-comment/inline-comment.css
@@ -1,5 +1,5 @@

// inline comments
// inline comments



Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/inline-comment/inline-comment.out.css
@@ -1,4 +1,8 @@

// inline comments



.class {
color: red;
}
7 changes: 2 additions & 5 deletions test/fixtures/readme/readme.css
@@ -1,7 +1,4 @@
// mixin for clearfix



@mixin clearfix () { &:before,
&:after {
content:" ";
Expand All @@ -20,10 +17,10 @@
padding: 12px
}

.foo{
.foo{
@extend .base;}

.bar
.bar
{ @extend %base;

}
3 changes: 1 addition & 2 deletions test/fixtures/readme/readme.out.css
Expand Up @@ -18,9 +18,8 @@

.base {
color: red;
}
} // placeholder

// placeholder
%base {
padding: 12px;
}
Expand Down
2 changes: 1 addition & 1 deletion test/sass/sass-comment/sass-comment.css
Expand Up @@ -15,7 +15,7 @@ color: red; }

.bar {
// Lorem ipsum dolor sit amet, consectetur adipiscing elit
// Donec a diam lectus. Sed sit amet ipsum mauris
// Donec a diam lectus. Sed sit amet ipsum mauris
display: none;
//bar
margin:auto;
Expand Down

0 comments on commit a035a64

Please sign in to comment.