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

Jsx2mp/dev #1128

Merged
merged 36 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
67a9a01
feat: runtime of jsx2mp add createStyle with tests
wssgcg1213 Jul 4, 2019
fa548fb
feat: jsx-compiler support create style
wssgcg1213 Jul 4, 2019
4a6a36c
fix: condition judge jsxElement
wssgcg1213 Jul 4, 2019
6bc810c
chore: pass eslint
wssgcg1213 Jul 4, 2019
0fa23e3
feat: support compile css file to css object
wssgcg1213 Jul 4, 2019
f862ae8
feat: support logical expression with &&
wssgcg1213 Jul 4, 2019
c18607e
fix: add identifier to dynamic value
wssgcg1213 Jul 4, 2019
f261b3e
fix: test cases and fix consequense
wssgcg1213 Jul 4, 2019
e7f0ec6
chore: bump versions
wssgcg1213 Jul 9, 2019
324e55f
fix: eslint
wssgcg1213 Jul 9, 2019
c06a994
Merge branch 'master' into jsx2mp/dev
wssgcg1213 Jul 9, 2019
acbe853
fix: createStyle support undefined and null
wssgcg1213 Jul 9, 2019
207cfca
feat: support transform hooks
wssgcg1213 Jul 11, 2019
d18867c
fix: props pass values
wssgcg1213 Jul 11, 2019
d2325bc
chore: bump version
wssgcg1213 Jul 11, 2019
dc9cc17
fix: conditional judge identifier should be listed in dynamic value
wssgcg1213 Jul 11, 2019
f15e383
feat: support pass value from props
wssgcg1213 Jul 11, 2019
6ed9279
feat: add parse multiple ternary expression
Jul 11, 2019
3c6b053
chore: remove redundancy code
SoloJiang Jul 11, 2019
37c8e9a
chore: remove redundancy code
SoloJiang Jul 11, 2019
8b74a83
chore: remove redundancy code
SoloJiang Jul 11, 2019
f7ad527
fix: skip double props
wssgcg1213 Jul 11, 2019
cb5f143
chore: lint
SoloJiang Jul 11, 2019
818aa49
fix: hooks and bump version
wssgcg1213 Jul 12, 2019
1242f59
chore: add dynamic value
SoloJiang Jul 12, 2019
ef02541
chore: lint
SoloJiang Jul 12, 2019
93c7ee7
Merge branch 'jsx2mp/dev' into feat/jsx2mp-ternary
SoloJiang Jul 12, 2019
80b600d
chore: support transform class properties
wssgcg1213 Jul 12, 2019
d8de355
Merge pull request #1132 from alibaba/feat/jsx2mp-ternary
SoloJiang Jul 12, 2019
ebc74e5
chore: update and bump a version
wssgcg1213 Jul 12, 2019
add3e92
Merge branch 'jsx2mp/dev' of github.com:alibaba/rax into jsx2mp/dev
wssgcg1213 Jul 12, 2019
ffa1ab7
fix: dynamic value is shared
SoloJiang Jul 12, 2019
c5b9693
fix: ci
SoloJiang Jul 12, 2019
6c63d2a
fix: test
SoloJiang Jul 12, 2019
4229be0
feat: support event binding
wssgcg1213 Jul 12, 2019
a4f63ea
Merge branch 'jsx2mp/dev' of github.com:alibaba/rax into jsx2mp/dev
wssgcg1213 Jul 12, 2019
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
2 changes: 1 addition & 1 deletion packages/jsx-compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsx-compiler",
"version": "0.1.5",
"version": "0.1.6",
"license": "BSD-3-Clause",
"description": "Parser for Rax JSX Statements.",
"main": "src/index.js",
Expand Down
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 @@ -12,4 +12,34 @@ describe('Transform condition', () => {

expect(genCode(ast).code).toEqual('<View><block a:if="{{foo}}"><View /></block><block a:else><Text /></block></View>');
});

it("transform conditional's consequent is conditional expression", () => {
const ast = parseExpression(`
<View>{foo ? bar ? <Bar /> : <View /> : <Text />}</View>
`);
_transformTemplate(ast, adapter, {});

expect(genCode(ast).code).toEqual('<View><block a:if="{{foo}}"><block a:if="{{bar}}"><Bar /></block><block a:else><View /></block></block><block a:else><Text /></block></View>');
});

it("transform condition's alternate is conditional expression", () => {
const ast = parseExpression(`
<View>{empty ? <Empty /> : loading ? null : 'xxx' }</View>
`);
_transformTemplate(ast, adapter, {});

expect(genCode(ast).code).toEqual('<View><block a:if="{{empty}}"><Empty /></block><block a:else><block a:if="{{loading}}"></block><block a:else>xxx</block></block></View>');
});

it('transform conditional expression in JSXContainer2', () => {
const ast = parseExpression(`
<View>{foo ? <View /> : <Text />}</View>
`);
const dynamicValue = _transformTemplate(ast, adapter, {});

expect(genCode(ast).code).toEqual('<View><block a:if="{{foo}}"><View /></block><block a:else><Text /></block></View>');
expect(Object.keys(dynamicValue)).toEqual(['foo']);
});
});


13 changes: 13 additions & 0 deletions packages/jsx-compiler/src/modules/__tests__/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ describe('Transform JSXElement', () => {

expect(genInlineCode(ast).code).toEqual('<View onClick="_e0" />');
});

it('bind methods', () => {
const ast = parseExpression(`
<View
onClick={onClick.bind(this, { a: 1 })}
onKeyPress={this.handleClick.bind(this, 'hello')}
/>
`);
const dynamicValue = _transform(ast);

expect(genInlineCode(ast).code).toEqual('<View onClick="_e0" onKeyPress="_e1" data-arg-context="this" data-arg-0="{{ a: 1 }}" data-arg-context="this" data-arg-0="{{\'hello\'}}" />');
expect(genDynamicAttrs(dynamicValue)).toEqual('{ _e0: onClick, _e1: this.handleClick }');
});
});

describe('element', () => {
Expand Down
38 changes: 36 additions & 2 deletions packages/jsx-compiler/src/modules/code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const t = require('@babel/types');
const genExpression = require('../codegen/genExpression');
const isClassComponent = require('../utils/isClassComponent');
const isFunctionComponent = require('../utils/isFunctionComponent');
const traverse = require('../utils/traverseNodePath');
Expand All @@ -17,6 +18,9 @@ const SAFE_CREATE_COMPONENT = '__create_component__';
const SAFE_CREATE_PAGE = '__create_page__';
const SAFE_CREATE_STYLE = '__create_style__';

const USE_EFFECT = 'useEffect';
const USE_STATE = 'useState';

const EXPORTED_DEF = '__def__';
const RUNTIME = 'jsx2mp-runtime';

Expand Down Expand Up @@ -83,13 +87,15 @@ module.exports = {
}
}

addDefine(parsed.ast, options.type, userDefineType, eventHandlers, parsed.useCreateStyle);
const hooks = transformHooks(parsed.renderFunctionPath);

addDefine(parsed.ast, options.type, userDefineType, eventHandlers, parsed.useCreateStyle, hooks);
removeRaxImports(parsed.ast);
removeDefaultImports(parsed.ast);
},
};

function addDefine(ast, type, userDefineType, eventHandlers, useCreateStyle) {
function addDefine(ast, type, userDefineType, eventHandlers, useCreateStyle, hooks) {
let safeCreateInstanceId;
let importedIdentifier;
switch (type) {
Expand Down Expand Up @@ -120,6 +126,12 @@ function addDefine(ast, type, userDefineType, eventHandlers, useCreateStyle) {
));
}

if (Array.isArray(hooks)) {
hooks.forEach(id => {
specifiers.push(t.importSpecifier(t.identifier(id), t.identifier(id)));
});
}

if (useCreateStyle) {
specifiers.push(t.importSpecifier(
t.identifier(SAFE_CREATE_STYLE),
Expand Down Expand Up @@ -197,3 +209,25 @@ function getReplacer(defaultExportedPath) {
return null;
}
}

function transformHooks(root) {
let ret = {};
traverse(root, {
CallExpression(path) {
const { node } = path;
if (t.isIdentifier(node.callee, { name: USE_STATE })) {
if (t.isVariableDeclarator(path.parentPath.node) && t.isArrayPattern(path.parentPath.node.id)) {
const firstId = path.parentPath.node.id.elements[0];
node.arguments[1] = t.stringLiteral(firstId.name);
ret[USE_STATE] = true;
} else {
console.warn(`useState should be called with following: const [foo, setFoo] = useState(originalFoo); instead of ${genExpression(path.parentPath.node)}`);
}
} else if (t.isIdentifier(node.callee, { name: USE_EFFECT })) {
ret[USE_EFFECT] = true;
}
}
});

return Object.keys(ret);
}
Loading