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}; }`); }); }); 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 ee8a9079..2babb801 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; @@ -157,6 +158,12 @@ function transformIdentifierComponentName(path, alias, dynamicValue, parsed, opt if (importedComponent) { 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/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) } } 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