Skip to content

Commit a9f1adc

Browse files
committed
feat: make validate addon compile
1 parent 440b4f3 commit a9f1adc

File tree

5 files changed

+76
-8
lines changed

5 files changed

+76
-8
lines changed

.storybook/rule.stories.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const {linkTo} = require('@storybook/addon-links');
55
const {create} = require('../index');
66
const {addon} = require('../addon/rule');
77

8-
const renderer = create();
9-
addon(renderer);
10-
const {rule} = renderer;
8+
const nano = create();
9+
addon(nano);
10+
const {rule} = nano;
1111

1212
const className1 = rule({
1313
border: '1px solid red'

.storybook/validate.stories.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {createElement as h} from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
const {action} = require('@storybook/addon-actions');
4+
const {linkTo} = require('@storybook/addon-links');
5+
const {create} = require('../index');
6+
7+
const nano = create();
8+
require('../addon/rule').addon(nano);
9+
require('../addon/validate').addon(nano);
10+
const {rule} = nano;
11+
12+
const className1 = rule({
13+
border: '1px solid red'
14+
}, 'validate1');
15+
16+
storiesOf('Addons/validate', module)
17+
.add('No error', () =>
18+
h('div', {className: className1}, 'Hello world')
19+
)

.storybook/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
},
1010

1111
resolve: {
12-
extensions: ['.ts', '.tsx', '.js', '.jsx'],
12+
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
1313
enforceExtension: false
1414
}
1515
};

addon/validate.js

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
11
'use strict';
22

3-
var validateString = require('csstree-validator').validateString;
3+
var csstree = require('css-tree');
4+
var syntax = csstree.lexer;
5+
6+
function validate(css, filename) {
7+
var errors = [];
8+
var ast = csstree.parse(css, {
9+
filename: filename,
10+
positions: true,
11+
onParseError: function(error) {
12+
errors.push(error);
13+
}
14+
});
15+
16+
csstree.walk(ast, {
17+
visit: 'Declaration',
18+
enter: function(node) {
19+
var match = syntax.matchDeclaration(node);
20+
var error = match.error;
21+
22+
if (error) {
23+
var message = error.rawMessage || error.message || error;
24+
25+
// ignore errors except those which make sense
26+
if (error.name !== 'SyntaxMatchError' &&
27+
error.name !== 'SyntaxReferenceError') {
28+
return;
29+
}
30+
31+
if (message === 'Mismatch') {
32+
message = 'Invalid value for `' + node.property + '`';
33+
} else if (message === 'Uncomplete match') {
34+
message = 'The rest part of value can\'t be matched to `' + node.property + '` syntax';
35+
}
36+
37+
errors.push({
38+
name: error.name,
39+
node: node,
40+
loc: error.loc || node.loc,
41+
line: error.line || node.loc && node.loc.start && node.loc.start.line,
42+
column: error.column || node.loc && node.loc.start && node.loc.start.column,
43+
property: node.property,
44+
message: message,
45+
error: error
46+
});
47+
}
48+
}
49+
});
50+
51+
return errors;
52+
}
453

554
exports.addon = function (renderer) {
655
if (process.env.NODE_ENV !== 'production') {
@@ -17,7 +66,7 @@ exports.addon = function (renderer) {
1766
var putRaw = renderer.putRaw;
1867

1968
renderer.putRaw = function (rawCssRule) {
20-
var errors = validateString(rawCssRule)['<unknown>'];
69+
var errors = validate(rawCssRule);
2170

2271
if (errors.length) {
2372
errors.forEach(function (error) {
@@ -28,6 +77,6 @@ exports.addon = function (renderer) {
2877
});
2978
}
3079

31-
putRaw(rawCssRule);
80+
return putRaw.apply(renderer, arguments);
3281
};
3382
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"stylis": "3.5.0",
4040
"inline-style-prefixer": "^4.0.0",
4141
"rtl-css-js": "^1.9.0",
42-
"csstree-validator": "^1.3.1"
42+
"css-tree": "^1.0.0-alpha.28"
4343
},
4444
"devDependencies": {
4545
"@types/react": "16.0.40",

0 commit comments

Comments
 (0)