Skip to content

Commit b30abe3

Browse files
committed
Use postcss-value-parser, add units
1 parent 61ac715 commit b30abe3

File tree

14 files changed

+408
-463
lines changed

14 files changed

+408
-463
lines changed

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"description": "Convert CSS text to a React Native stylesheet object",
55
"main": "dist/index.js",
66
"scripts": {
7-
"build-grammar": "nearleyc src/grammar.ne -o src/grammar.js",
8-
"build": "npm run build-grammar; babel src --ignore test.js --out-dir dist",
9-
"test": "npm run build-grammar; jest",
7+
"build": "babel src --ignore test.js --out-dir dist",
8+
"test": "jest",
9+
"test:watch": "jest --watch",
1010
"prepublish": "npm run build"
1111
},
1212
"repository": {
@@ -35,9 +35,11 @@
3535
"jest": "^17.0.0"
3636
},
3737
"dependencies": {
38+
"css-color-keywords": "^1.0.0",
3839
"css-color-list": "0.0.1",
3940
"fbjs": "^0.8.5",
4041
"nearley": "^2.7.7",
42+
"postcss-value-parser": "^3.3.0",
4143
"postcss-values-parser": "^1.0.1"
4244
}
4345
}

pegjs.json

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

src/TokenStream.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module.exports = class TokenStream {
2+
constructor(nodes, parent) {
3+
this.nodes = nodes;
4+
this.parent = parent;
5+
this.lastFunction = null;
6+
this.lastValue = null;
7+
}
8+
9+
get node() {
10+
return this.nodes[0];
11+
}
12+
13+
hasTokens() {
14+
return this.nodes.length > 0;
15+
}
16+
17+
lookahead() {
18+
return new TokenStream(this.nodes.slice(1), this.parent);
19+
}
20+
21+
match(...tokenDescriptors) {
22+
const node = this.node;
23+
24+
if (!node) return null;
25+
26+
/* eslint-disable no-restricted-syntax */
27+
for (const tokenDescriptor of tokenDescriptors) {
28+
const value = tokenDescriptor(node);
29+
30+
if (value !== null) {
31+
this.nodes = this.nodes.slice(1);
32+
this.lastFunction = null;
33+
this.lastValue = value;
34+
return value;
35+
}
36+
}
37+
/* eslint-enable */
38+
39+
return null;
40+
}
41+
42+
expect(...tokenDescriptors) {
43+
const value = this.match(...tokenDescriptors);
44+
if (value !== null) return value;
45+
return this.throw();
46+
}
47+
48+
matchFunction() {
49+
const node = this.node;
50+
if (node.type !== 'function') return null;
51+
const value = new TokenStream(node.nodes, node);
52+
this.nodes = this.nodes.slice(1);
53+
this.lastFunction = value;
54+
this.lastValue = null;
55+
return value;
56+
}
57+
58+
expectFunction() {
59+
const value = this.matchFunction();
60+
if (value !== null) return value;
61+
return this.throw();
62+
}
63+
64+
expectEmpty() {
65+
if (this.hasTokens()) this.throw();
66+
}
67+
68+
throw() {
69+
throw new Error(`Unexpected token type: ${this.node.type}`);
70+
}
71+
};

src/grammar.ne

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

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable no-param-reassign */
2-
const parser = require('postcss-values-parser/lib/index');
2+
const parse = require('postcss-value-parser');
33
const camelizeStyleName = require('fbjs/lib/camelizeStyleName');
44
const transforms = require('./transforms');
5+
const TokenStream = require('./TokenStream');
56

67
const transformRawValue = input => (
78
(input !== '' && !isNaN(input))
@@ -10,8 +11,9 @@ const transformRawValue = input => (
1011
);
1112

1213
export const parseProp = (propName, value) => {
13-
const ast = parser(value).parse();
14-
return transforms[propName](ast);
14+
const ast = parse(value).nodes;
15+
const tokenStream = new TokenStream(ast);
16+
return transforms[propName](tokenStream);
1517
};
1618

1719
export const getStylesForProperty = (propName, inputValue, allowShorthand) => {

0 commit comments

Comments
 (0)