Browse Source

Skip non-styled-jsx style elements (#94)

Fixes errors happen when there is a non-styled-jsx style element.
add-plugins-support
Naoyuki Kanezawa 8 years ago
committed by Giuseppe
parent
commit
54fe1537d0
  1. 5
      src/babel.js
  2. 7
      test/fixtures/non-styled-jsx-style.js
  3. 6
      test/fixtures/non-styled-jsx-style.out.js
  4. 6
      test/index.js

5
src/babel.js

@ -281,14 +281,13 @@ export default function ({types: t}) {
// next visit will be: JSXOpeningElement // next visit will be: JSXOpeningElement
}, },
exit(path, state) { exit(path, state) {
const el = path.node.openingElement const isGlobal = isGlobalEl(path.node.openingElement)
const isGlobal = isGlobalEl(el)
if (state.hasJSXStyle && (!--state.ignoreClosing && !isGlobal)) { if (state.hasJSXStyle && (!--state.ignoreClosing && !isGlobal)) {
state.hasJSXStyle = null state.hasJSXStyle = null
} }
if (!state.hasJSXStyle || !el.name || el.name.name !== 'style') { if (!state.hasJSXStyle || !isStyledJsx(path)) {
return return
} }

7
test/fixtures/non-styled-jsx-style.js

@ -0,0 +1,7 @@
export default () => (
<div>
<p>woot</p>
<style dangerouslySetInnerHTML={{ __html: `body { margin: 0; }` }}></style>
<style jsx>{'p { color: red }'}</style>
</div>
)

6
test/fixtures/non-styled-jsx-style.out.js

@ -0,0 +1,6 @@
import _JSXStyle from 'styled-jsx/style';
export default (() => <div data-jsx={188072295}>
<p data-jsx={188072295}>woot</p>
<style dangerouslySetInnerHTML={{ __html: `body { margin: 0; }` }}></style>
<_JSXStyle styleId={188072295} css={"p[data-jsx=\"188072295\"] {color: red }"} />
</div>);

6
test/index.js

@ -88,6 +88,12 @@ test('works with expressions in template literals', async t => {
t.is(code, out.trim()) t.is(code, out.trim())
}) })
test('works with non styled-jsx styles', async t => {
const {code} = await transform('./fixtures/non-styled-jsx-style.js')
const out = await read('./fixtures/non-styled-jsx-style.out.js')
t.is(code, out.trim())
})
test('throws when using `props` or constants ' + test('throws when using `props` or constants ' +
'defined in the closest scope', async t => { 'defined in the closest scope', async t => {
[1, 2, 3, 4].forEach(i => { [1, 2, 3, 4].forEach(i => {

Loading…
Cancel
Save