Last active
December 9, 2016 10:18
-
-
Save madeinfree/36763533934ae408ab79dc210dcd8bbb to your computer and use it in GitHub Desktop.
for babel to inject react debug tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default function (babel) { | |
| const { types: t } = babel; | |
| function injectMountHistory(cycleType) { | |
| return t.assignmentExpression( | |
| '=', | |
| t.memberExpression( | |
| t.Identifier('history'), | |
| t.UpdateExpression( | |
| '++', | |
| t.Identifier('step') | |
| ), | |
| true | |
| ), | |
| t.binaryExpression( | |
| '+', | |
| t.stringLiteral(cycleType), | |
| t.memberExpression( | |
| t.Identifier('componentConstructor'), | |
| t.Identifier('name') | |
| ) | |
| ) | |
| ) | |
| } | |
| return { | |
| name: "ast-transform", // not required | |
| visitor: { | |
| ClassMethod(path) { | |
| const { node } = path | |
| if (node.key.name === 'componentDidMount') { | |
| const cacheExpressionBody = node.body.body.map(b => b) | |
| const injectDidMountDebugExpression = t.blockStatement([ | |
| t.variableDeclaration('const', [t.variableDeclarator( | |
| t.Identifier('componentConstructor'), | |
| t.CallExpression(t.Identifier('descriptionState'), [t.ThisExpression()]) | |
| )]), | |
| t.ifStatement( | |
| t.binaryExpression( | |
| '===', | |
| t.memberExpression( | |
| t.Identifier('componentConstructor'), | |
| t.Identifier('debugID') | |
| ), | |
| t.NumericLiteral(1) | |
| ), | |
| t.BlockStatement([ | |
| t.expressionStatement( | |
| injectMountHistory('Mount-') | |
| ), | |
| t.expressionStatement( | |
| t.assignmentExpression( | |
| '=', | |
| t.memberExpression( | |
| t.Identifier('history'), | |
| t.UpdateExpression( | |
| '++', | |
| t.Identifier('step') | |
| ), | |
| true | |
| ), | |
| t.stringLiteral('------------- First Mount End -------------') | |
| ) | |
| ) | |
| ]), | |
| t.blockStatement([ | |
| t.expressionStatement( | |
| injectMountHistory('Mount-') | |
| ) | |
| ]) | |
| ) | |
| ].concat(cacheExpressionBody)) | |
| path.node.body = injectDidMountDebugExpression | |
| } | |
| if (node.key.name === 'componentWillMount') { | |
| } | |
| if (node.key.name === 'componentWillUnmount') { | |
| const cacheExpressionBody = node.body.body.map(b => b) | |
| console.log(cacheExpressionBody) | |
| } | |
| if (node.key.name === 'componentWillReceiveProps') { | |
| } | |
| if (node.key.name === 'shouldComponentUpdate') { | |
| } | |
| if (node.key.name === 'componentWillUpdate') { | |
| } | |
| if (node.key.name === 'componentDidUpdate') { | |
| } | |
| } | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment