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: 30 additions & 0 deletions packages/jsx-compiler/src/modules/__tests__/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,36 @@ describe('Transiform condition render function', () => {
setState(a);
}, []);
return <View>{a}</View>;
}`);
});

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 <View>{loggined}</View>;
})
`);

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 <View>{loggined}</View>;
}`);
});
});
30 changes: 30 additions & 0 deletions packages/jsx-compiler/src/modules/__tests__/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
<custom-element
onClick={onClick}
onChange={onChange}
/>
`);
ast.openingElement.name.isCustom = true;
ast.openingElement.name.isNative = true;
_transform({
templateAST: ast
}, wxAdapter);
expect(genInlineCode(ast).code).toEqual('<custom-element bindonClick="_e0" bindonChange="_e1" />');
});

it('shouldn\'t transform events in non-native components (defined in package.json -> miniappConfig) in wechat miniprogram', () => {
const ast = parseExpression(`
<custom-element
onClick={onClick}
onChange={onChange}
/>
`);
ast.openingElement.name.isCustom = true;
ast.openingElement.name.isNative = false;
_transform({
templateAST: ast
}, wxAdapter);
expect(genInlineCode(ast).code).toEqual('<custom-element onClick="_e0" onChange="_e1" />');
});
});
9 changes: 8 additions & 1 deletion packages/jsx-compiler/src/modules/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
// <tag __tagId="tagId" />

let tagId;
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions packages/jsx-compiler/src/modules/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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)
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/jsx-compiler/src/modules/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 => {
Expand Down
10 changes: 10 additions & 0 deletions packages/jsx-compiler/src/utils/isBaseComponent.js
Original file line number Diff line number Diff line change
@@ -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;
};