Skip to content

Commit dd851fd

Browse files
authored
handling indent in first line of style code block. (#28)
1 parent 218a73c commit dd851fd

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

extract.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,17 +289,21 @@ function literalParser (source, opts, styles) {
289289

290290
objLiteral = Array.from(objLiteral).map(endNode => {
291291
const objectSyntax = require("./object-syntax");
292-
const syntax = objectSyntax(endNode);
293292
let startNode = endNode;
294293
if (startNode.leadingComments && startNode.leadingComments.length) {
295294
startNode = startNode.leadingComments[0];
296295
}
296+
let startIndex = startNode.start;
297+
const before = source.slice(startNode.start - startNode.loc.start.column, startNode.start);
298+
if (/^\s+$/.test(before)) {
299+
startIndex -= before.length;
300+
}
297301
return {
298-
startIndex: startNode.start,
302+
startIndex,
299303
endIndex: endNode.end,
300304
skipConvert: true,
301305
content: source,
302-
syntax,
306+
syntax: objectSyntax(endNode),
303307
lang: "object-literal",
304308
};
305309
});

object-parser.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,21 @@ class objectParser {
7979
const startNode = root.first.raws.node;
8080
const endNode = root.last.raws.node;
8181

82-
root.source.start = startNode.loc.start;
82+
const start = {
83+
line: startNode.loc.start.line,
84+
};
85+
86+
let before = root.source.input.css.slice(startNode.start - startNode.loc.start.column, startNode.start);
87+
if (/^\s+$/.test(before)) {
88+
start.column = 1;
89+
} else {
90+
before = "";
91+
start.column = startNode.loc.start.column;
92+
}
8393

84-
root.source.input.css = root.source.input.css.slice(startNode.start, endNode.end);
94+
root.first.raws.before = before;
95+
root.source.input.css = before + root.source.input.css.slice(startNode.start, endNode.end);
96+
root.source.start = start;
8597

8698
this.root = root;
8799
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-jsx",
3-
"version": "0.32.0",
3+
"version": "0.33.0",
44
"description": "PostCSS syntax for parsing CSS in JS literals",
55
"repository": {
66
"type": "git",
@@ -44,11 +44,11 @@
4444
"@babel/core": "^7.0.0-rc.1"
4545
},
4646
"optionalDependencies": {
47-
"postcss-styled": ">=0.32.0"
47+
"postcss-styled": ">=0.33.0"
4848
},
4949
"peerDependencies": {
5050
"postcss": ">=5.0.0",
51-
"postcss-syntax": ">=0.32.0"
51+
"postcss-syntax": ">=0.33.0"
5252
},
5353
"devDependencies": {
5454
"autoprefixer": "^9.1.1",
@@ -60,6 +60,6 @@
6060
"postcss": "^7.0.2",
6161
"postcss-parser-tests": "^6.3.0",
6262
"postcss-safe-parser": "^4.0.1",
63-
"postcss-syntax": ">=0.32.0"
63+
"postcss-syntax": ">=0.33.0"
6464
}
6565
}

test/react.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,28 @@ describe("react", () => {
1717
expect(document.toString(syntax)).to.equal(code);
1818
expect(document.nodes).to.lengthOf(3);
1919
});
20+
21+
it("first line indentation handle", () => {
22+
const code = `
23+
export default <img style=
24+
{
25+
{
26+
transform: 'translate(1, 1)',
27+
}
28+
}
29+
/>;
30+
`;
31+
32+
const document = syntax.parse(code, {
33+
from: "before.js",
34+
});
35+
36+
expect(document.toString(syntax)).to.equal(code);
37+
expect(document.nodes).to.lengthOf(1);
38+
expect(document.first.source.input.css).to.match(/^\s+\{/);
39+
expect(document.first.source.start.column).to.equal(1);
40+
expect(document.first.raws.beforeStart).to.match(/\n$/);
41+
expect(document.first.first.raws.before).to.match(/^\s+$/);
42+
expect(document.first.first.source.start.column).to.greaterThan(1);
43+
});
2044
});

0 commit comments

Comments
 (0)