From 2eacfd81f334642c9485fe6a333e34e785194bc1 Mon Sep 17 00:00:00 2001 From: Tom Chen Date: Sun, 21 Jan 2018 14:43:00 +0800 Subject: [PATCH] feat(emSize): add support for missing width/height --- src/cli/__snapshots__/index.test.js.snap | 2 +- src/h2x/emSize.js | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/cli/__snapshots__/index.test.js.snap b/src/cli/__snapshots__/index.test.js.snap index c2d6397b..3cccb47d 100644 --- a/src/cli/__snapshots__/index.test.js.snap +++ b/src/cli/__snapshots__/index.test.js.snap @@ -4,7 +4,7 @@ exports[`cli --icon 1`] = ` "import React from \\"react\\"; const One = props => ( - + ); diff --git a/src/h2x/emSize.js b/src/h2x/emSize.js index 84b2eba5..d549709e 100644 --- a/src/h2x/emSize.js +++ b/src/h2x/emSize.js @@ -1,13 +1,23 @@ +import { JSXAttribute } from 'h2x-plugin-jsx' + +const makeSizeAttr = name => { + const attr = new JSXAttribute() + attr.name = name + attr.value = '1em' + attr.litteral = false + return attr +} + const emSize = () => ({ visitor: { - JSXAttribute: { + JSXElement: { enter(path) { - if ( - path.parent.name === 'svg' && - (path.node.name === 'width' || path.node.name === 'height') - ) { - path.node.value = '1em' - path.node.litteral = false + if (path.node.name === 'svg' && !path.node.attributes.some(attr => attr && attr.name === 'width' && attr.value === '1em') && !path.node.attributes.some(attr => attr && attr.name === 'height' && attr.value === '1em')) { + const nextAttrs = path.node.attributes.filter(attr => attr.name !== 'width' && attr.name !== 'height'); + nextAttrs.push(makeSizeAttr('width')); + nextAttrs.push(makeSizeAttr('height')); + path.node.attributes = nextAttrs; + path.replace(path.node); } }, },