Ai
2 Star 0 Fork 0

mirrors_sourcegraph/javascript-idents

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
idents.js 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
Jannis R 提交于 2016-09-25 04:21 +08:00 . strict mode
'use strict';
const walk = require('acorn/dist/walk');
// inspect traverses the AST starting at node, calling found with each Identifier AST node it encounters.
const inspect = (ast, found) => {
walk.simple(ast, {
Identifier: (node) => {
found(node);
},
VariableDeclarator: (node) => {
if(node.id.type === 'Identifier') found(node.id);
},
Function: (node) => {
if(node.id && node.id.type === 'Identifier') found(node.id);
for (let param of node.params) {
if(param.type === 'Identifier') found(param);
}
},
AssignmentExpression: (node) => {
if(node.left.type === 'Identifier') found(node.left);
},
ObjectPattern: (node) => {
for (let property of node.properties) {
if (property.value.type === 'Identifier') found(property.value);
}
},
ArrayPattern: (node) => {
for (let element of node.elements) {
if (element.type === 'Identifier') found(element);
}
},
});
};
// all traverses the AST starting at node and returns an array of all Identifier AST nodes it encounters.
const all = (node) => {
const idents = [];
inspect(node, (ident) => {
idents.push(ident);
});
return idents;
};
module.exports = {inspect, all}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_sourcegraph/javascript-idents.git
git@gitee.com:mirrors_sourcegraph/javascript-idents.git
mirrors_sourcegraph
javascript-idents
javascript-idents
master

搜索帮助