Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds validation that jsx is in scope of arrow function instead of method check #89

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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