Skip to content

Commit

Permalink
visitor(split-member-object): add new visitor
Browse files Browse the repository at this point in the history
Signed-off-by: echo094 <[email protected]>
  • Loading branch information
echo094 committed Jul 6, 2024
1 parent 1655f1d commit 320cb7b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/visitor/split-member-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
function splitMemberObject(path) {
const object = path.get('object')
if (!object.isAssignmentExpression()) {
return
}
let insertPath = path
while (!insertPath?.listKey) {
if (insertPath.parentPath.isAssignmentExpression()) {
insertPath = insertPath.parentPath
continue
}
if (insertPath.parentPath.isExpressionStatement()) {
insertPath = insertPath.parentPath
continue
}
return
}
insertPath.insertBefore(object.node)
object.replaceWith(object.node.left)
insertPath.scope.crawl()
}

/**
* Split assignment operation in member object
*
* From:
* ```javascript
* (a = {})['b'] = c;
* ```
* To:
* ```javascript
* a = {}
* a['b'] = c;
* ```
*/
module.exports = {
MemberExpression: splitMemberObject,
}

0 comments on commit 320cb7b

Please sign in to comment.