Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 12 additions & 18 deletions packages/babel-sugar-functional-vue/src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import syntaxJsx from '@babel/plugin-syntax-jsx'

/**
* Check if expression is in method
* @param t
* @param path
* @param parentLimitPath
* @returns boolean
*/
const isInMethod = (t, path, parentLimitPath) => {
if (!path || path === parentLimitPath) {
return false
}
if (t.isObjectMethod(path)) {
return true
}
return isInMethod(t, path.parentPath, parentLimitPath)
}

/**
* Check path has JSX
* @param t
Expand All @@ -25,13 +8,24 @@ const isInMethod = (t, path, parentLimitPath) => {
*/
const hasJSX = (t, path) => {
let hasJSX = false
let parentScope

path.traverse({
JSXElement(elPath) {
if (!isInMethod(t, elPath, path)) {
if (parentScope === elPath.scope) {
hasJSX = true
}
},

ArrowFunctionExpression(blockPath) {
const isParent = blockPath.parentPath === path

if (!isParent) {
return
}

parentScope = blockPath.scope
},
})

return hasJSX
Expand Down
17 changes: 17 additions & 0 deletions packages/babel-sugar-functional-vue/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ const tests = [
}

});`,
},
{
name: 'Wrapper of function does not compile',
from: `const Wrapped = () => wrap(() => <span />)`,
to: `const Wrapped = () => wrap(() => <span />);`,
},
{
name: 'If JSX in function arguments does not compile',
from: `const Wrapped = (jsx = () => <span />) => jsx`,
to: `const Wrapped = (jsx = () => <span />) => jsx;`,
},
{
name: 'If JSX in nested function does not compile',
from: `const Wrapped = () => function () { return <span /> }`,
to: `const Wrapped = () => function () {
return <span />;
};`,
},
{
name: 'Default export',
Expand Down