From 9e1af907739f4771bd7765aa0e14cd7eac569ea0 Mon Sep 17 00:00:00 2001 From: tusaeff Date: Tue, 26 Nov 2019 18:34:32 +0300 Subject: [PATCH 1/2] chore: add absent tests for some cases containing JSX --- .../babel-sugar-functional-vue/test/test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/babel-sugar-functional-vue/test/test.js b/packages/babel-sugar-functional-vue/test/test.js index abee254..12ccd96 100644 --- a/packages/babel-sugar-functional-vue/test/test.js +++ b/packages/babel-sugar-functional-vue/test/test.js @@ -75,6 +75,23 @@ const tests = [ } });`, + }, + { + name: 'Wrapper of function does not compile', + from: `const Wrapped = () => wrap(() => )`, + to: `const Wrapped = () => wrap(() => );`, + }, + { + name: 'If JSX in function arguments does not compile', + from: `const Wrapped = (jsx = () => ) => jsx`, + to: `const Wrapped = (jsx = () => ) => jsx;`, + }, + { + name: 'If JSX in nested function does not compile', + from: `const Wrapped = () => function () { return }`, + to: `const Wrapped = () => function () { + return ; +};`, }, { name: 'Default export', From 11ec1077ef4f6f66d84010ed6bbdcff63ff8f7d7 Mon Sep 17 00:00:00 2001 From: tusaeff Date: Tue, 26 Nov 2019 18:54:57 +0300 Subject: [PATCH 2/2] fix: do check if JSX is in arrow function scope instead of check if it not in method for better documentation compliance --- .../babel-sugar-functional-vue/src/index.js | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/packages/babel-sugar-functional-vue/src/index.js b/packages/babel-sugar-functional-vue/src/index.js index 985a85f..55c5513 100644 --- a/packages/babel-sugar-functional-vue/src/index.js +++ b/packages/babel-sugar-functional-vue/src/index.js @@ -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 @@ -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