ast_tools icon indicating copy to clipboard operation
ast_tools copied to clipboard

逗号表达式处理 else for 语法时候取不到body bug

Open lee003 opened this issue 2 years ago • 3 comments

https://github.com/sml2h3/ast_tools/blob/cf3bca84b1c8f8ffbe7fca14003c2cb33d512a6e/libs/common/VariableDeclaratorFix.js#L68 这段代码在处理逗号表达式的时候,如果碰到 else for 这种写法 由于else 的内容没有用大括号包裹, 会导致在拿for的父对象的body 插 AssignmentExpression 的时候 取不到 body 导致报错, 感觉应该对所有的 else 都先对 alternate 检查 如果 不是 BlockStatement 都加上一个BlockStatement 包裹住 下面为测试代码 if (void 0 !== e[r(n(1163, 1110))]) { u = r(""); } else for (var f = [r("ESEAPx0Y"), r(n(1230, 1247)), r("Czc"), r("CQ")], v = 0; v < f[r(n(1346, 1251))]; v++) { if (void 0 !== e[f[v] + r("Li0GMBEC")]) { u = f[v]; break; } }

lee003 avatar Apr 09 '22 03:04 lee003

好的,这个我会检查一下,因为在这之前应该有个修复if else的block的模块,可以将没有被block包裹的部分用block包裹起来,你可以尝试在执行[VariableDeclaratorFix之前先行执行一下IfWithExpressFix

sml2h3 avatar Apr 11 '22 01:04 sml2h3

IfWithExpressFix 做了两种类型的判断 isExpressionStatement isReturnStatement 不晓得这里是否有特殊情况 我直接改成了

if (types.isIfStatement(node)){
        if (node.consequent != null && !types.isBlockStatement(node.consequent)){
            path.node.consequent = types.blockStatement([node.consequent])
        }
        else if(node.consequent === null){
            path.node.consequent = types.blockStatement([])
        }
        
        if (node.alternate != null && !types.isBlockStatement(node.alternate)){
            path.node.alternate = types.blockStatement([node.alternate])
        }
    }

在我处理的代码里面 可以运行。

lee003 avatar Apr 11 '22 02:04 lee003

好的,我看看

IfWithExpressFix 做了两种类型的判断 isExpressionStatement isReturnStatement 不晓得这里是否有特殊情况 我直接改成了

if (types.isIfStatement(node)){
        if (node.consequent != null && !types.isBlockStatement(node.consequent)){
            path.node.consequent = types.blockStatement([node.consequent])
        }
        else if(node.consequent === null){
            path.node.consequent = types.blockStatement([])
        }
        
        if (node.alternate != null && !types.isBlockStatement(node.alternate)){
            path.node.alternate = types.blockStatement([node.alternate])
        }
    }

在我处理的代码里面 可以运行。

sml2h3 avatar Apr 13 '22 07:04 sml2h3