Skip to content

Commit f1d7efc

Browse files
authored
Add document.lang (#26)
1 parent 9c732eb commit f1d7efc

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

index.js

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/syntax")(extract);
3+
const syntax = require("postcss-syntax/syntax")(extract, "jsx");
44

55
module.exports = syntax;

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-styled",
3-
"version": "0.28.0",
3+
"version": "0.30.0",
44
"description": "PostCSS syntax for parsing styled components",
55
"repository": {
66
"type": "git",
@@ -34,7 +34,7 @@
3434
},
3535
"peerDependencies": {
3636
"postcss": ">=5.0.0",
37-
"postcss-syntax": ">=0.28.0"
37+
"postcss-syntax": ">=0.30.0"
3838
},
3939
"devDependencies": {
4040
"chai": "^4.1.2",
@@ -43,6 +43,6 @@
4343
"nyc": "^12.0.2",
4444
"postcss": "^6.0.22",
4545
"postcss-safe-parser": "^3.0.1",
46-
"postcss-syntax": ">=0.28.0"
46+
"postcss-syntax": ">=0.30.0"
4747
}
4848
}

test/styled.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ describe("styled-components", () => {
88
const file = require.resolve("./fixtures/styled");
99
let code = fs.readFileSync(file);
1010

11-
const root = syntax.parse(code, {
11+
const document = syntax.parse(code, {
1212
from: file,
1313
});
1414

1515
code = code.toString();
16-
expect(root.toString(), code);
16+
expect(document.toString(), code);
17+
expect(document.source).to.haveOwnProperty("lang", "jsx");
1718

18-
expect(root.nodes).to.have.lengthOf(1);
19-
expect(root.first.nodes).to.have.lengthOf(8);
19+
expect(document.nodes).to.have.lengthOf(1);
20+
expect(document.first.nodes).to.have.lengthOf(8);
2021

2122
const lines = code.match(/^.+$/gm).slice(3).map(line => (line.replace(/^\s*(.+?);?\s*$/, "$1")));
22-
root.first.nodes.forEach((decl, i) => {
23+
document.first.nodes.forEach((decl, i) => {
2324
if (i) {
2425
expect(decl).to.have.property("type", "decl");
2526
} else {
@@ -37,70 +38,76 @@ describe("styled-components", () => {
3738
"}",
3839
"",
3940
].join("\n");
40-
const root = syntax.parse(code, {
41+
const document = syntax.parse(code, {
4142
from: "empty_template_literal.js",
4243
});
43-
expect(root.toString()).to.equal(code);
44-
expect(root.nodes).to.have.lengthOf(0);
44+
expect(document.toString()).to.equal(code);
45+
expect(document.source).to.haveOwnProperty("lang", "jsx");
46+
expect(document.nodes).to.have.lengthOf(0);
4547
});
4648

4749
it("skip javascript syntax error", () => {
4850
const code = "\\`";
49-
const root = syntax.parse(code, {
51+
const document = syntax.parse(code, {
5052
from: "syntax_error.js",
5153
});
52-
expect(root.toString()).to.equal(code);
53-
expect(root.nodes).to.have.lengthOf(0);
54+
expect(document.toString()).to.equal(code);
55+
expect(document.source).to.haveOwnProperty("lang", "jsx");
56+
expect(document.nodes).to.have.lengthOf(0);
5457
});
5558

5659
it("illegal template literal", () => {
5760
const code = "`$\n{display: block}\n${g} {}`";
58-
const root = syntax.parse(code, {
61+
const document = syntax.parse(code, {
5962
from: "illegal_template_literal.js",
6063
});
61-
expect(root.toString()).to.equal(code);
62-
expect(root.nodes).to.have.lengthOf(1);
63-
expect(root.first.nodes).to.have.lengthOf(2);
64-
expect(root.first.first).have.property("type", "rule");
65-
expect(root.first.first).have.property("selector", "$");
66-
expect(root.last.last).have.property("type", "rule");
67-
expect(root.last.last).have.property("selector", "${g}");
64+
expect(document.toString()).to.equal(code);
65+
expect(document.source).to.haveOwnProperty("lang", "jsx");
66+
expect(document.nodes).to.have.lengthOf(1);
67+
expect(document.first.nodes).to.have.lengthOf(2);
68+
expect(document.first.first).have.property("type", "rule");
69+
expect(document.first.first).have.property("selector", "$");
70+
expect(document.last.last).have.property("type", "rule");
71+
expect(document.last.last).have.property("selector", "${g}");
6872
});
6973

7074
it("skip CSS syntax error", () => {
7175
const code = "`a{`";
72-
const root = syntax.parse(code, {
76+
const document = syntax.parse(code, {
7377
from: "css_syntax_error.js",
7478
});
75-
expect(root.toString()).to.equal(code);
76-
expect(root.nodes).to.have.lengthOf(0);
79+
expect(document.toString()).to.equal(code);
80+
expect(document.source).to.haveOwnProperty("lang", "jsx");
81+
expect(document.nodes).to.have.lengthOf(0);
7782
});
7883

7984
it("fix CSS syntax error", () => {
8085
const code = "`a{`";
81-
const root = syntax({
86+
const document = syntax({
8287
css: "safe-parser",
8388
}).parse(code, {
8489
from: "postcss-safe-parser.js",
8590
});
86-
expect(root.toString()).to.equal("`a{}`");
87-
expect(root.nodes).to.have.lengthOf(1);
88-
expect(root.first.nodes).to.have.lengthOf(1);
89-
expect(root.first.first).have.property("type", "rule");
90-
expect(root.first.first).have.property("selector", "a");
91+
expect(document.toString()).to.equal("`a{}`");
92+
expect(document.source).to.haveOwnProperty("lang", "jsx");
93+
expect(document.nodes).to.have.lengthOf(1);
94+
expect(document.first.nodes).to.have.lengthOf(1);
95+
expect(document.first.first).have.property("type", "rule");
96+
expect(document.first.first).have.property("selector", "a");
9197
});
9298

9399
it("fix styled syntax error", () => {
94100
const code = "`${ a } {`";
95-
const root = syntax({
101+
const document = syntax({
96102
css: "safe-parser",
97103
}).parse(code, {
98104
from: "styled-safe-parse.js",
99105
});
100-
expect(root.toString()).to.equal("`${ a } {}`");
101-
expect(root.nodes).to.have.lengthOf(1);
102-
expect(root.first.nodes).to.have.lengthOf(1);
103-
expect(root.first.first).have.property("type", "rule");
104-
expect(root.first.first).have.property("selector", "${ a }");
106+
expect(document.toString()).to.equal("`${ a } {}`");
107+
expect(document.source).to.haveOwnProperty("lang", "jsx");
108+
expect(document.nodes).to.have.lengthOf(1);
109+
expect(document.first.nodes).to.have.lengthOf(1);
110+
expect(document.first.first).have.property("type", "rule");
111+
expect(document.first.first).have.property("selector", "${ a }");
105112
});
106113
});

0 commit comments

Comments
 (0)