-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37cc8b7
commit a24ee3e
Showing
8 changed files
with
266 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
module.exports = { | ||
"type": "Program", | ||
"start": 0, | ||
"end": 57, | ||
"body": [{ | ||
"type": "VariableDeclaration", | ||
"start": 0, | ||
"end": 12, | ||
"declarations": [{ | ||
"type": "VariableDeclarator", | ||
"start": 4, | ||
"end": 11, | ||
"id": { | ||
"type": "Identifier", | ||
"start": 4, | ||
"end": 5, | ||
"name": "s" | ||
}, | ||
"init": { | ||
"type": "Literal", | ||
"start": 8, | ||
"end": 11, | ||
"value": 123, | ||
"raw": "123" | ||
} | ||
}], | ||
"kind": "var" | ||
}, { | ||
"type": "ExpressionStatement", | ||
"start": 13, | ||
"end": 28, | ||
"expression": { | ||
"type": "CallExpression", | ||
"start": 13, | ||
"end": 27, | ||
"callee": { | ||
"type": "MemberExpression", | ||
"start": 13, | ||
"end": 24, | ||
"object": { | ||
"type": "Identifier", | ||
"start": 13, | ||
"end": 20, | ||
"name": "console" | ||
}, | ||
"property": { | ||
"type": "Identifier", | ||
"start": 21, | ||
"end": 24, | ||
"name": "log" | ||
}, | ||
"computed": false | ||
}, | ||
"arguments": [{ | ||
"type": "Identifier", | ||
"start": 25, | ||
"end": 26, | ||
"name": "s" | ||
}] | ||
} | ||
}, { | ||
"type": "ExpressionStatement", | ||
"start": 29, | ||
"end": 57, | ||
"expression": { | ||
"type": "AssignmentExpression", | ||
"start": 29, | ||
"end": 56, | ||
"operator": "=", | ||
"left": { | ||
"type": "MemberExpression", | ||
"start": 29, | ||
"end": 43, | ||
"object": { | ||
"type": "Identifier", | ||
"start": 29, | ||
"end": 35, | ||
"name": "module" | ||
}, | ||
"property": { | ||
"type": "Identifier", | ||
"start": 36, | ||
"end": 43, | ||
"name": "exports" | ||
}, | ||
"computed": false | ||
}, | ||
"right": { | ||
"type": "ObjectExpression", | ||
"start": 46, | ||
"end": 56, | ||
"properties": [{ | ||
"type": "Property", | ||
"start": 50, | ||
"end": 54, | ||
"method": false, | ||
"shorthand": false, | ||
"computed": false, | ||
"key": { | ||
"type": "Identifier", | ||
"start": 50, | ||
"end": 51, | ||
"name": "s" | ||
}, | ||
"value": { | ||
"type": "Identifier", | ||
"start": 53, | ||
"end": 54, | ||
"name": "s" | ||
}, | ||
"kind": "init" | ||
}] | ||
} | ||
} | ||
}], | ||
"sourceType": "script" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// code -> AST | ||
const acorn = require('acorn') | ||
// 遍历AST | ||
const estraverse = require('estraverse') | ||
// AST -> code | ||
const escodegen = require('escodegen') | ||
|
||
module.exports = function (content) { | ||
// AST解析过程模拟 | ||
const AST_Object = acorn.parse(content) | ||
// 结果见 ./ast.js | ||
console.log("AST静态语法树:", AST_Object) | ||
// 遍历AST | ||
estraverse.traverse(AST_Object, { | ||
enter: function (node, parent) { | ||
if (node.type == 'VariableDeclaration') { | ||
console.log("遍历AST拿到const:", node.kind) | ||
node.kind = 'var' | ||
} | ||
} | ||
}); | ||
// AST -> code | ||
console.log('AST替换之后生成的代码:', escodegen.generate(AST_Object)) | ||
} |
This file contains 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
Oops, something went wrong.