Skip to content

Commit

Permalink
feat: make validate addon compile
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 24, 2018
1 parent 440b4f3 commit a9f1adc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .storybook/rule.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const {linkTo} = require('@storybook/addon-links');
const {create} = require('../index');
const {addon} = require('../addon/rule');

const renderer = create();
addon(renderer);
const {rule} = renderer;
const nano = create();
addon(nano);
const {rule} = nano;

const className1 = rule({
border: '1px solid red'
Expand Down
19 changes: 19 additions & 0 deletions .storybook/validate.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {createElement as h} from 'react';
import {storiesOf} from '@storybook/react';
const {action} = require('@storybook/addon-actions');
const {linkTo} = require('@storybook/addon-links');
const {create} = require('../index');

const nano = create();
require('../addon/rule').addon(nano);
require('../addon/validate').addon(nano);
const {rule} = nano;

const className1 = rule({
border: '1px solid red'
}, 'validate1');

storiesOf('Addons/validate', module)
.add('No error', () =>
h('div', {className: className1}, 'Hello world')
)
2 changes: 1 addition & 1 deletion .storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
},

resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx'],
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
enforceExtension: false
}
};
55 changes: 52 additions & 3 deletions addon/validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
'use strict';

var validateString = require('csstree-validator').validateString;
var csstree = require('css-tree');
var syntax = csstree.lexer;

function validate(css, filename) {
var errors = [];
var ast = csstree.parse(css, {
filename: filename,
positions: true,
onParseError: function(error) {
errors.push(error);
}
});

csstree.walk(ast, {
visit: 'Declaration',
enter: function(node) {
var match = syntax.matchDeclaration(node);
var error = match.error;

if (error) {
var message = error.rawMessage || error.message || error;

// ignore errors except those which make sense
if (error.name !== 'SyntaxMatchError' &&
error.name !== 'SyntaxReferenceError') {
return;
}

if (message === 'Mismatch') {
message = 'Invalid value for `' + node.property + '`';
} else if (message === 'Uncomplete match') {
message = 'The rest part of value can\'t be matched to `' + node.property + '` syntax';
}

errors.push({
name: error.name,
node: node,
loc: error.loc || node.loc,
line: error.line || node.loc && node.loc.start && node.loc.start.line,
column: error.column || node.loc && node.loc.start && node.loc.start.column,
property: node.property,
message: message,
error: error
});
}
}
});

return errors;
}

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

renderer.putRaw = function (rawCssRule) {
var errors = validateString(rawCssRule)['<unknown>'];
var errors = validate(rawCssRule);

if (errors.length) {
errors.forEach(function (error) {
Expand All @@ -28,6 +77,6 @@ exports.addon = function (renderer) {
});
}

putRaw(rawCssRule);
return putRaw.apply(renderer, arguments);
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"stylis": "3.5.0",
"inline-style-prefixer": "^4.0.0",
"rtl-css-js": "^1.9.0",
"csstree-validator": "^1.3.1"
"css-tree": "^1.0.0-alpha.28"
},
"devDependencies": {
"@types/react": "16.0.40",
Expand Down

0 comments on commit a9f1adc

Please sign in to comment.