Skip to content

Commit

Permalink
fix new bindthis
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyuki committed Jan 18, 2024
1 parent 8e6b208 commit d657c4b
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions packages/webpack-plugin/lib/template-compiler/bind-this.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function checkDelAndGetPath (path) {
let replace = false

// 确定删除路径
while (!t.isBlockStatement(current)) {
while (!t.isBlockStatement(current) && !t.isProgram(current)) {
// case: !!a
if (t.isUnaryExpression(current.parent) && current.key === 'argument') {
delPath = current.parentPath
Expand Down Expand Up @@ -113,7 +113,7 @@ function checkDelAndGetPath (path) {
}

// 确定是否可删除
while (!t.isBlockStatement(current) && canDel) {
while (!t.isBlockStatement(current) && !t.isProgram(current)) {
const { key, listKey, parent } = current

if (t.isIfStatement(parent) && key === 'test') {
Expand Down Expand Up @@ -244,7 +244,7 @@ module.exports = {
!path.scope.hasBinding(path.node.name)
) {
if (isProps) {
propKeySet.add(path.node.property.name)
propKeySet.add(path.node.name)
}
const { keyPath } = getCollectPath(path)
collectKeySet.add(keyPath)
Expand Down Expand Up @@ -282,19 +282,22 @@ module.exports = {
const propKeySet = new Set()
let isProps = false

const collectVisitor = {
BlockStatement: {
enter (path) { // 收集作用域下所有变量(keyPath)
bindingsMap.set(path, {
parent: currentBlock,
bindings: {}
})
currentBlock = path
},
exit (path) {
currentBlock = bindingsMap.get(path).parent
}
const blockCollectVisitor = {
enter (path) { // 收集作用域下所有变量(keyPath)
bindingsMap.set(path, {
parent: currentBlock,
bindings: {}
})
currentBlock = path
},
exit (path) {
currentBlock = bindingsMap.get(path).parent
}
}

const collectVisitor = {
Program: blockCollectVisitor,
BlockStatement: blockCollectVisitor,
Identifier (path) {
if (
checkBindThis(path) &&
Expand Down Expand Up @@ -347,18 +350,21 @@ module.exports = {
}
}

const bindThisVisitor = {
BlockStatement: {
enter (path) {
const scope = bindingsMap.get(path)
const parentScope = bindingsMap.get(scope.parent)
scope.pBindings = parentScope ? Object.assign({}, parentScope.bindings, parentScope.pBindings) : {}
currentBlock = path
},
exit (path) {
currentBlock = bindingsMap.get(path).parent
}
const blockBindVisitor = {
enter (path) {
const scope = bindingsMap.get(path)
const parentScope = bindingsMap.get(scope.parent)
scope.pBindings = parentScope ? Object.assign({}, parentScope.bindings, parentScope.pBindings) : {}
currentBlock = path
},
exit (path) {
currentBlock = bindingsMap.get(path).parent
}
}

const bindVisitor = {
Program: blockBindVisitor,
BlockStatement: blockBindVisitor,
// 标记收集props数据
CallExpression: {
enter (path) {
Expand Down Expand Up @@ -439,7 +445,7 @@ module.exports = {
}

traverse(ast, collectVisitor)
traverse(ast, bindThisVisitor)
traverse(ast, bindVisitor)

return {
code: generate(ast).code,
Expand Down

0 comments on commit d657c4b

Please sign in to comment.