-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
102 lines (91 loc) · 2.71 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* @file 解析模块包含的依赖
* @author youngwind
* @content 使用esprima将模块文件解析成AST,然后逐个语句遍历,找到该模块都依赖了哪些模块
*/
const esprima = require('esprima')
function parse(source) {
let ast = esprima.parse(source, {
range: true
})
let module = {}
walkStatements(module, ast.body)
module.source = source
return module
}
function walkStatements(module, astTree) {
astTree.forEach(statement => {
walkStatement(module, statement)
})
}
/**
* 分析每一个语句
* @param {object} module 模块对象
* @param {object} statement AST语法树
*/
function walkStatement(module, statement) {
switch (statement.type) {
case 'BlockStatement':
walkStatements(module, statement.body)
break
case 'VariableDeclaration':
if (statement.declarations) {
walkVariableDeclarators(module, statement.declarations)
}
break
case 'ExpressionStatement':
walkExpression(module, statement.expression)
break
}
}
/**
* 处理定义变量的语句
* @param {object} module 模块对象
* @param {object} declarator
*/
function walkVariableDeclarators(module, declarators) {
declarators.forEach(declarator => {
if(declarator.type === 'VariableDeclarator' && declarator.init) {
walkExpression(module, declarator.init)
}
})
}
/**
* 处理表达式
* @param {object} module 模块对象
* @param {object} expression 表达式
*/
function walkExpression(module, expression) {
switch (expression.type) {
case 'CallExpression':
// 处理普通的require
if (expression.callee && expression.callee.name === 'require' && expression.callee.type === 'Identifier' &&
expression.arguments && expression.arguments.length === 1) {
module.requires = module.requires || []
let param = Array.from(expression.arguments)[0]
module.requires.push({
name: param.value,
nameRange: param.range
})
}
// 处理匿名表达式,如 require('b')()
if (expression.callee && !expression.callee.name) {
walkExpression(module, expression.callee)
// 处理连续调用的匿名表达式,如 require('a')(require('b'));
if(expression.arguments && expression.arguments[0] && expression.arguments[0].type === 'CallExpression') {
walkExpressions(module, expression.arguments)
}
}
break
case 'FunctionExpression':
if (expression.body.type === 'BlockStatement') {
walkStatement(module, expression.body)
}
}
}
function walkExpressions(module, expressions) {
expressions.forEach(expression => {
walkExpression(module, expression)
})
}
module.exports = parse