Skip to content
This repository was archived by the owner on Feb 9, 2023. It is now read-only.

Commit 048aa24

Browse files
authored
Merge pull request #1 from gucong3000/submodule
submodule
2 parents f949672 + bfdfae5 commit 048aa24

29 files changed

+707
-629
lines changed

.editorconfig

Lines changed: 0 additions & 22 deletions
This file was deleted.

.eslintrc.json

Lines changed: 0 additions & 73 deletions
This file was deleted.

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ language: node_js
44

55
node_js:
66
- stable
7-
- 4
7+
8+
script:
9+
- nyc mocha --no-timeouts
810

911
after_script:
10-
- npm run report-coverage
12+
- codecov

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ postcss([stylelint({ fix: true })]).process(source, { syntax: syntax }).then(fun
3434

3535
input:
3636
```javascript
37-
import glm from 'jsx';
37+
import glm from 'glamorous';
3838
const Component1 = glm.a({
3939
flexDirectionn: 'row',
4040
display: 'inline-block',
@@ -44,7 +44,7 @@ const Component1 = glm.a({
4444

4545
output:
4646
```javascript
47-
import glm from 'jsx';
47+
import glm from 'glamorous';
4848
const Component1 = glm.a({
4949
color: '#fff',
5050
display: 'inline-block',

camel-case.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
function camelCase (str) {
3+
if (str.startsWith("--")) {
4+
return str;
5+
}
6+
return str.replace(/(^|\s|\W)-(ms-)/g, "$1$2").replace(/-+(\w)/g, (s, char) => s.length > 2 ? s : char.toUpperCase());
7+
}
8+
9+
module.exports = camelCase;

lib/extract.js renamed to extract.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const traverse = require("@babel/traverse").default;
33
const t = require("@babel/types").default;
44
const parse = require("babylon").parse;
5+
const getTemplate = require("./get-template");
56

67
const isTopLevelExpression = path =>
78
path.isObjectExpression() && !path.findParent(p => p.isObjectExpression());
@@ -64,15 +65,15 @@ function getOptions (opts, attribute) {
6465
};
6566
}
6667

67-
function literalParser (source, opts) {
68+
function literalParser (source, opts, styles) {
6869
let ast;
6970
try {
7071
ast = parse(source, getOptions(opts));
7172
} catch (ex) {
7273
if (opts.from && /\.(?:m?[jt]sx?|es\d*|pac)(\?.*|$)/i.test(opts.from)) {
73-
return [];
74+
return styles || [];
7475
}
75-
return;
76+
return styles;
7677
}
7778

7879
let glamorousImport;
@@ -133,30 +134,34 @@ function literalParser (source, opts) {
133134

134135
objects = objects.map(path => {
135136
const objectSyntax = require("./object-syntax");
136-
const root = objectSyntax.parse(path.node, source, opts);
137-
const startIndex = root.first.raws.node.start;
138-
const endIndex = root.last.raws.node.end;
137+
const endNode = path.node;
138+
const syntax = objectSyntax(endNode);
139+
let startNode = endNode;
140+
if (startNode.leadingComments && startNode.leadingComments.length) {
141+
startNode = startNode.leadingComments[0];
142+
}
139143
return {
140-
root,
141-
startIndex,
142-
endIndex,
143-
syntax: objectSyntax,
144+
startIndex: startNode.start,
145+
endIndex: endNode.end,
146+
skipConvert: true,
147+
content: source,
148+
syntax,
144149
lang: "object-literal",
145150
};
146151
});
147152

148-
let styledSyntax;
153+
let templateSyntax;
149154

150-
function getStyledSyntax () {
151-
if (!styledSyntax) {
152-
const getSyntax = require("postcss-syntax/lib/get-syntax");
155+
function getTemplateSyntax () {
156+
if (!templateSyntax) {
157+
const getSyntax = require("postcss-syntax/get-syntax");
153158
const cssSyntax = getSyntax("css", opts);
154-
styledSyntax = {
155-
parse: require(cssSyntax.parse.name === "safeParse" ? "./template-safe-parse" : "./template-parse"),
159+
templateSyntax = {
160+
parse: require("postcss-styled/template-" + (cssSyntax.parse.name === "safeParse" ? "safe-parse" : "parse")),
156161
stringify: cssSyntax.stringify,
157162
};
158163
}
159-
return styledSyntax;
164+
return templateSyntax;
160165
}
161166

162167
tpls = tpls.filter(path => (
@@ -165,28 +170,28 @@ function literalParser (source, opts) {
165170
))
166171
)).map(path => {
167172
const quasis = path.node.quasis;
168-
const startIndex = quasis[0].start;
169-
const strSource = source.slice(startIndex, quasis[quasis.length - 1].end);
173+
const value = getTemplate(path.node, source);
170174

171-
if (!strSource.trim()) {
175+
if (value.length === 1 && !value[0].trim()) {
172176
return;
173177
}
174178

175179
const style = {
176-
startIndex: startIndex,
177-
content: strSource,
180+
startIndex: quasis[0].start,
181+
endIndex: quasis[quasis.length - 1].end,
182+
content: value.join(""),
178183
ignoreErrors: true,
179184
};
180-
if (/(^|\s|\{|\}|;|:)\$\{/m.test(strSource)) {
181-
style.syntax = getStyledSyntax();
185+
if (value.length > 1) {
186+
style.syntax = getTemplateSyntax();
182187
style.lang = "template-literal";
183188
} else {
184189
style.lang = "css";
185190
}
186191
return style;
187192
}).filter(Boolean);
188193

189-
return objects.concat(tpls);
194+
return (styles || []).concat(objects).concat(tpls);
190195
};
191196

192197
module.exports = literalParser;

get-template.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
function getTemplate (node, source) {
3+
const result = [];
4+
node.quasis.forEach((node, i) => {
5+
result[i << 1] = node.value.cooked;
6+
});
7+
const quasis = node.quasis;
8+
node.expressions.forEach((node, i) => {
9+
const index = (i << 1) + 1;
10+
result[index] = source.slice(quasis[i].end, quasis[i + 1].start);
11+
});
12+
13+
return result;
14+
}
15+
module.exports = getTemplate;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
22
const extract = require("./extract");
3-
const syntax = require("postcss-syntax/lib/syntax")(extract);
3+
const syntax = require("postcss-syntax/syntax")(extract);
44

55
module.exports = syntax;

0 commit comments

Comments
 (0)