import * as babel from '@babel/core'
import { describe, expect, it } from 'vitest'
import babelRestoreJSX from './babel-restore-jsx'
function jsx(code: string) {
return babel.transform(code, {
parserOpts: { plugins: ['jsx'] },
plugins: [babelRestoreJSX]
})?.code
}
// Tests adapted from: https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx/blob/63137b6/test/index.js
describe('babel-restore-jsx', () => {
it('should convert 1-argument calls', () => {
expect(jsx('React.createElement("h1")')).toMatchInlineSnapshot(`"
;"`)
expect(jsx('React.createElement(Foo)')).toMatchInlineSnapshot(`";"`)
expect(jsx('React.createElement(Foo.Bar)')).toMatchInlineSnapshot(
`";"`
)
expect(jsx('React.createElement(Foo.Bar.Baz)')).toMatchInlineSnapshot(
`";"`
)
})
it('should convert effective 1-argument calls (with null or undefined)', () => {
expect(jsx('React.createElement("h1", null)')).toMatchInlineSnapshot(
`";"`
)
expect(jsx('React.createElement("h2", null, null)')).toMatchInlineSnapshot(
`";"`
)
expect(jsx('React.createElement("h3", undefined)')).toMatchInlineSnapshot(
`";"`
)
})
it('should handle props without children', () => {
expect(jsx('React.createElement("h1", {hi: there})')).toMatchInlineSnapshot(
`";"`
)
expect(
jsx('React.createElement("h2", {"hi": there})')
).toMatchInlineSnapshot(`";"`)
expect(
jsx('React.createElement("h3", {hi: "there"})')
).toMatchInlineSnapshot(`";"`)
})
it('should handle spread props', () => {
expect(jsx('React.createElement("h1", props)')).toMatchInlineSnapshot(
`";"`
)
expect(jsx('React.createElement("h1", getProps())')).toMatchInlineSnapshot(
`";"`
)
})
it('should handle mixed props', () => {
expect(
jsx('React.createElement("h1", _extends({ hi: "there" }, props))')
).toMatchInlineSnapshot(`";"`)
expect(
jsx('React.createElement("h1", _extends({}, props, { hi: "there" }))')
).toMatchInlineSnapshot(`";"`)
expect(
jsx('React.createElement("h1", { ...props, hi: "there" })')
).toMatchInlineSnapshot(`";"`)
})
it('should handle props and ignore “null”/“undefined” children', () => {
expect(
jsx('React.createElement("h1", {hi: there}, null, undefined)')
).toMatchInlineSnapshot(`";"`)
})
it('should ignore “null”/“undefined” props and handle children', () => {
expect(
jsx('React.createElement("h1", null, "Header")')
).toMatchInlineSnapshot(`"Header
;"`)
//this can be created from e.g. 'Header{"harhar"}
', but i think there’s no downside to merging it
expect(
jsx('React.createElement("h2", null, "Header", "harhar")')
).toMatchInlineSnapshot(`"Headerharhar
;"`)
expect(
jsx('React.createElement("h3", null, React.createElement("i"))')
).toMatchInlineSnapshot(`"
;"`)
expect(
jsx('React.createElement("h4", null, "a", React.createElement("b"), "c")')
).toMatchInlineSnapshot(`"ac
;"`)
})
it('should handle props and children', () => {
//we extensively tested props and children separately, so only sth. basic
expect(
jsx('React.createElement("h1", {hi: there}, "Header")')
).toMatchInlineSnapshot(`"Header
;"`)
})
it('should ignore intermingled “null”/“undefined” children', () => {
expect(
jsx('React.createElement("h1", null, null, "Header", undefined)')
).toMatchInlineSnapshot(`"Header
;"`)
})
it('should handle children in nested expressions', () => {
expect(
jsx(
'React.createElement("h1", null, foo ? React.createElement("p") : null)'
)
).toMatchInlineSnapshot(`"{foo ?
: null};"`)
})
it('should handle lowercase component names', () => {
expect(jsx('React.createElement(aaa)')).toMatchInlineSnapshot(
`"React.createElement(aaa);"`
)
})
it('should not handle contains __self prop', () => {
expect(
jsx('React.createElement(Provider, { __self: this })')
).toMatchInlineSnapshot('";"')
})
it('should not handle contains __source prop', () => {
expect(
jsx(
'React.createElement(Provider, { __source: { fileName: _jsxFileName, lineNumber: 133 }})'
)
).toMatchInlineSnapshot('";"')
})
})