Permalink
Browse files

Put comment after declaration semicolon

  • Loading branch information...
1 parent ef14193 commit 2ded0bf6ae4d17c5a6aedc39075ac96ce16c92cd @ai ai committed Jul 23, 2016
Showing with 135 additions and 13 deletions.
  1. +34 −13 parser.es6
  2. +4 −0 stringifier.es6
  3. +4 −0 test/cases/comments.css
  4. +90 −0 test/cases/comments.json
  5. +3 −0 test/cases/comments.sss
View
@@ -59,19 +59,7 @@ export default class Parser {
let node = new Comment();
this.init(node, part);
node.source.end = { line: token[4], column: token[5] };
-
- let text = token[1];
- if ( token[6] === 'inline' ) {
- node.raws.inline = true;
- text = text.slice(2);
- } else {
- text = text.slice(2, -2);
- }
-
- let match = text.match(/^(\s*)([^]*[^\s])(\s*)\n?$/);
- node.text = match[2];
- node.raws.left = match[1];
- node.raws.inlineRight = match[3];
+ this.commentText(node, token);
}
atrule(part) {
@@ -133,6 +121,24 @@ export default class Parser {
next = this.parts[this.pos + 1];
}
+ let last = value[value.length - 1];
+ if ( last && last[0] === 'comment' ) {
+ value.pop();
+ let comment = new Comment();
+ this.current.push(comment);
+ comment.source = {
+ input: this.input,
+ start: { line: last[2], column: last[3] },
+ end: { line: last[4], column: last[5] }
+ };
+ let prev = value[value.length - 1];
+ if ( prev && prev[0] === 'space' ) {
+ value.pop();
+ comment.raws.before = prev[1];
+ }
+ this.commentText(comment, last);
+ }
+
for ( let i = value.length - 1; i > 0; i-- ) {
let t = value[i][0];
if ( t === 'word' && value[i][1] === '!important' ) {
@@ -286,6 +292,21 @@ export default class Parser {
return part;
}
+ commentText(node, token) {
+ let text = token[1];
+ if ( token[6] === 'inline' ) {
+ node.raws.inline = true;
+ text = text.slice(2);
+ } else {
+ text = text.slice(2, -2);
+ }
+
+ let match = text.match(/^(\s*)([^]*[^\s])(\s*)\n?$/);
+ node.text = match[2];
+ node.raws.left = match[1];
+ node.raws.inlineRight = match[3];
+ }
+
// Errors
error(msg, line, column) {
View
@@ -73,6 +73,10 @@ export default class Stringifier {
let child = node.nodes[i];
let before = child.raws.before.replace(/[^\n]*$/, '') +
this.indent(node, indent);
+ if ( child.type === 'comment' &&
+ child.raws.before.indexOf('\n') === -1 ) {
+ before = child.raws.before;
+ }
if ( before ) this.builder(before);
this.stringify(child);
}
@@ -1,3 +1,7 @@
/* multi
line */
/* inline */
+a {
+ color: black; /* last */
+ z-index: 1
+}
@@ -1,5 +1,6 @@
{
"raws": {
+ "indent": " ",
"after": "\n"
},
"type": "root",
@@ -48,6 +49,95 @@
}
},
"text": "inline"
+ },
+ {
+ "raws": {
+ "before": "\n"
+ },
+ "type": "rule",
+ "nodes": [
+ {
+ "raws": {
+ "before": "\n ",
+ "between": ": "
+ },
+ "type": "decl",
+ "source": {
+ "start": {
+ "line": 5,
+ "column": 3
+ },
+ "input": {
+ "file": "comments.sss"
+ },
+ "end": {
+ "line": 5,
+ "column": 14
+ }
+ },
+ "prop": "color",
+ "value": "black"
+ },
+ {
+ "raws": {
+ "before": " ",
+ "inline": true,
+ "left": " ",
+ "inlineRight": ""
+ },
+ "type": "comment",
+ "source": {
+ "input": {
+ "file": "comments.sss"
+ },
+ "start": {
+ "line": 5,
+ "column": 16
+ },
+ "end": {
+ "line": 5,
+ "column": 22
+ }
+ },
+ "text": "last"
+ },
+ {
+ "raws": {
+ "before": "\n ",
+ "between": ": "
+ },
+ "type": "decl",
+ "source": {
+ "start": {
+ "line": 6,
+ "column": 3
+ },
+ "input": {
+ "file": "comments.sss"
+ },
+ "end": {
+ "line": 6,
+ "column": 12
+ }
+ },
+ "prop": "z-index",
+ "value": "1"
+ }
+ ],
+ "source": {
+ "start": {
+ "line": 4,
+ "column": 1
+ },
+ "input": {
+ "file": "comments.sss"
+ },
+ "end": {
+ "line": 4,
+ "column": 1
+ }
+ },
+ "selector": "a"
}
],
"source": {
@@ -1,3 +1,6 @@
/* multi
line */
// inline
+a
+ color: black // last
+ z-index: 1

0 comments on commit 2ded0bf

Please sign in to comment.