1
| var a = "\x48\x65\x6c\x6c\x6f\x2c\x4e\x69\x67\x68\x74\x54\x65\x61\x6d\x21";
|
十六进制还原十进制
AST-TREE分析
由上图可知影响结果的节点是 extra.raw ,接下来对这个节点进行操作
1 2 3 4 5 6 7 8 9
| const vistor = { VariableDeclarator(path){ const init = path.get('init'); if (!init.isStringLiteral()) return; const node = init.node; let {value, extra} = node; extra.raw = '"' + value + ':)"'; } }
|
方式二:直接遍历type为StringLiteral的路径。
1 2 3 4 5 6
| const vistor = { StringLiteral(path){ let {value, extra} = path.node; extra.raw = '"' + value + ':)"'; } }
|
方式三:使用新建节点替换
1 2 3 4 5 6 7 8 9
| const vistor = { StringLiteral(path){ let {value} = path.node; path.replaceWith(t.StringLiteral(value + ":(")); path.stop(); } }
|
方式四:删除raw节点
1 2 3 4 5 6
| const vistor = { StringLiteral(path){ let {extra} = path.node; delete extra.raw; } }
|
通用步骤 (十六进制数值/字符串还原十进制)
1 2 3 4 5 6
| const vistor = { "StringLiteral|NumericLiteral"(path){ delete path.node.extra } }
|