Skip to content

Commit

Permalink
feat: throw this.hub.buildError(id, , TypeError);
Browse files Browse the repository at this point in the history
                     ^

TypeError: Cannot read property 'buildError' of undefined error looks like a babel bug
  • Loading branch information
yklydxtt committed Jul 28, 2021
1 parent afed2f8 commit cd7453e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ ctfc -i react-demo.js -o fc.js
- getDerivedStateFromProps处理
- "state={}"写法兼容
- 解决this问题
- UNSAFE_componentWillMount问题
1 change: 0 additions & 1 deletion bin/ctfc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#! /usr/bin/env node

const chalk = require('chalk');
const program = require('commander');
const version = require('../package.json').version;

Expand Down
23 changes: 17 additions & 6 deletions src/Transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ class Transformer {
_self.handleClassFn(path);
}
})
// 生成useState
_self.state.forEach(item => {
const decl = t.arrayPattern([t.identifier(item.key), t.identifier(`set${item.key[0].toUpperCase()}${item.key.slice(1)}`)]);
const call = t.callExpression(t.identifier("useState"), [item.value])
console.log(`set${item.key[0].toUpperCase()}${item.key.slice(1)}`);
_self.componentBody.unshift(t.variableDeclaration("const", [t.variableDeclarator(decl, call)]))
})
});
const blockStatements = t.blockStatement(_self.componentBody);
// console.log(blockStatements);
path.replaceWith(t.functionDeclaration(path.node.id, [t.identifier('props')], blockStatements));
},
MemberExpression(path) {
Expand All @@ -69,7 +72,6 @@ class Transformer {
const blockBodyList = [];
for (let state in states) {
const statement = t.expressionStatement(t.callExpression(t.identifier(`set${state[0].toUpperCase()}${state.slice(1)}`), [states[state]]));
// blockStatement.body=[...blockStatement.body,statement];
blockBodyList.push(statement);
}
blockStatement.replaceWith&&blockStatement.replaceWith(...blockBodyList);
Expand Down Expand Up @@ -97,7 +99,16 @@ class Transformer {
const node = path.node;
const methodName = node.key.name;
if(node.value&&node.value.type==='ObjectExpression'){
// 处理对象模式
if(node.key.name==='state'){
node.value.properties.forEach(item => {
const state = {};
state.key = item.key.name;
state.value = item.value
console.log(item.value,'value');
this.state.push(state);
});
this.collectHooks('useState');
}
return;
}
if (cycle.indexOf(methodName) === -1) {
Expand All @@ -122,10 +133,10 @@ class Transformer {
expression.right.properties.forEach(item => {
const state = {};
state.key = item.key.name;
state.value = item.value
_self.state.push(state);
state.value = item.value;
this.state.push(state);
});
_self.collectHooks('useState');
this.collectHooks('useState');
}
return;
}
Expand Down

0 comments on commit cd7453e

Please sign in to comment.