From e70b5bb57260dacb07557534729573fe800ee9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Mon, 27 Sep 2021 12:06:00 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=94=AF=E6=8C=81if=20else=E5=90=8E?= =?UTF-8?q?=E9=9D=A2=E5=8F=AA=E6=9C=89=E4=B8=80=E6=9D=A1=E8=AF=AD=E5=8F=A5?= =?UTF-8?q?=E3=80=81=E6=B2=A1=E6=9C=89=E5=A4=A7=E6=8B=AC=E5=8F=B7=E7=9A=84?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/jsx-compiler/src/modules/condition.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jsx-compiler/src/modules/condition.js b/packages/jsx-compiler/src/modules/condition.js index 807002c7..46fa7f5d 100644 --- a/packages/jsx-compiler/src/modules/condition.js +++ b/packages/jsx-compiler/src/modules/condition.js @@ -44,8 +44,8 @@ function transformRenderFunction(renderPath, adapter) { } if (!alternatePath.isIfStatement() && alternatePath.node) { - const alternateBodyPath = alternatePath.get('body'); - if (alternateBodyPath) { + if (alternatePath.isBlockStatement()) { + const alternateBodyPath = alternatePath.get('body'); alternateBodyPath.map((statementPath) => { handleAlternate( statementPath.get('expression'), @@ -322,7 +322,7 @@ function handleConsequent(path, expressionPath, templateMap, renderScope, adapte // Remove only if the expression contains JSX expressionPath.remove(); } - } + } else handleAlternate(expressionPath, templateMap, adapter) } } From 1f5473d7b85a99ed7f86a951b8ffc0249f68d45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Mon, 27 Sep 2021 16:42:18 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0test=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/modules/__tests__/condition.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packages/jsx-compiler/src/modules/__tests__/condition.js b/packages/jsx-compiler/src/modules/__tests__/condition.js index 7b182b68..aea5c24a 100644 --- a/packages/jsx-compiler/src/modules/__tests__/condition.js +++ b/packages/jsx-compiler/src/modules/__tests__/condition.js @@ -238,6 +238,36 @@ describe('Transiform condition render function', () => { setState(a); }, []); return {a}; +}`); + }); + + it('statement without square bracket in alternate', () => { + const ast = parseExpression(`(function render(props) { + const [loggined, setLoggined] = useState(false); + useEffect(() => { + const { isLogin } = app; + if (isLogin) { + setLoggined(true); + } else setLoggined(false); + }, []) + return {loggined}; + }) + `); + + const tmpVars = _transformRenderFunction(ast, adapter); + expect(genExpression(tmpVars.vdom)).toEqual(''); + expect(genExpression(ast)).toEqual(`function render(props) { + const [loggined, setLoggined] = useState(false); + useEffect(() => { + const { + isLogin + } = app; + + if (isLogin) { + setLoggined(true); + } else setLoggined(false); + }, []); + return {loggined}; }`); }); }); From b7af9dc092cab5afcd0f42d03886f55fe1bd3b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E6=88=98?= Date: Wed, 24 Nov 2021 14:11:32 +0800 Subject: [PATCH 3/3] support native components event bind --- .../src/modules/__tests__/element.js | 30 +++++++++++++++++++ .../jsx-compiler/src/modules/components.js | 9 +++++- packages/jsx-compiler/src/modules/element.js | 3 +- .../jsx-compiler/src/utils/isBaseComponent.js | 10 +++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/jsx-compiler/src/utils/isBaseComponent.js diff --git a/packages/jsx-compiler/src/modules/__tests__/element.js b/packages/jsx-compiler/src/modules/__tests__/element.js index 167a46fa..b8eb39d0 100644 --- a/packages/jsx-compiler/src/modules/__tests__/element.js +++ b/packages/jsx-compiler/src/modules/__tests__/element.js @@ -526,4 +526,34 @@ describe('Transform JSXElement', () => { }).toThrowError(); }); }); + + it('should transform events in native components (defined in package.json -> miniappConfig) in wechat miniprogram', () => { + const ast = parseExpression(` + + `); + ast.openingElement.name.isCustom = true; + ast.openingElement.name.isNative = true; + _transform({ + templateAST: ast + }, wxAdapter); + expect(genInlineCode(ast).code).toEqual(''); + }); + + it('shouldn\'t transform events in non-native components (defined in package.json -> miniappConfig) in wechat miniprogram', () => { + const ast = parseExpression(` + + `); + ast.openingElement.name.isCustom = true; + ast.openingElement.name.isNative = false; + _transform({ + templateAST: ast + }, wxAdapter); + expect(genInlineCode(ast).code).toEqual(''); + }); }); diff --git a/packages/jsx-compiler/src/modules/components.js b/packages/jsx-compiler/src/modules/components.js index a7406c19..5087a363 100644 --- a/packages/jsx-compiler/src/modules/components.js +++ b/packages/jsx-compiler/src/modules/components.js @@ -38,7 +38,8 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt node.isCustomEl = alias.isCustomEl; node.name.isCustom = true; - if (!getCompiledComponents(options.adapter.platform)[componentTag]) { + const platform = options.adapter.platform; + if (!getCompiledComponents(platform)[componentTag]) { // let tagId; @@ -149,6 +150,12 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt importedComponent.isFromComponentLibrary = true; }); } + + /** + * Judge whether the component has native compiled component + */ + if (pkg && pkg.miniappConfig && pkg.miniappConfig[`main:${platform}`]) node.name.isNative = true; + else node.isNative = false; } } } diff --git a/packages/jsx-compiler/src/modules/element.js b/packages/jsx-compiler/src/modules/element.js index c9def662..ce4c1698 100644 --- a/packages/jsx-compiler/src/modules/element.js +++ b/packages/jsx-compiler/src/modules/element.js @@ -13,6 +13,7 @@ const isSlotScopeNode = require('../utils/isSlotScopeNode'); const { isDirectiveAttr, isEventHandlerAttr, isRenderPropsAttr, BINDING_REG } = require('../utils/checkAttr'); const handleValidIdentifier = require('../utils/handleValidIdentifier'); const isNativeComponent = require('../utils/isNativeComponent'); +const isBaseComponent = require('../utils/isBaseComponent'); const isDerivedFromProps = require('../utils/isDerivedFromProps'); const { componentCommonProps } = require('../adapter'); @@ -541,7 +542,7 @@ function transformTemplate( attr.name.name = attr.name.name.replace('on', 'bind').toLowerCase(); } }); - } else if (adapter.needTransformEvent && baseComponents.indexOf(name) > -1) { + } else if (adapter.needTransformEvent && isBaseComponent(componentTagNode)) { // Rax base component should add bind before onXXX // While events in custom component should not be changed node.attributes.forEach(attr => { diff --git a/packages/jsx-compiler/src/utils/isBaseComponent.js b/packages/jsx-compiler/src/utils/isBaseComponent.js new file mode 100644 index 00000000..179f3d6a --- /dev/null +++ b/packages/jsx-compiler/src/utils/isBaseComponent.js @@ -0,0 +1,10 @@ +const baseComponents = require('../baseComponents'); + +/** + * Judge whether node is base component + * @param {Object} node + */ +module.exports = function isBaseComponent(node) { + // Rax base components and native base components are recognized as base components + return baseComponents.indexOf(node.name) > -1 || node && node.isNative; +}; \ No newline at end of file