1 | "use strict"
|
2 | exports.__esModule = true
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | exports.default = function visitModules(visitor, options) {
|
14 |
|
15 | options = Object.assign({ esmodule: true }, options)
|
16 |
|
17 | let ignoreRegExps = []
|
18 | if (options.ignore != null) {
|
19 | ignoreRegExps = options.ignore.map(p => new RegExp(p))
|
20 | }
|
21 |
|
22 | function checkSourceValue(source) {
|
23 | if (source == null) return
|
24 |
|
25 |
|
26 | if (ignoreRegExps.some(re => re.test(source.value))) return
|
27 |
|
28 |
|
29 | visitor(source)
|
30 | }
|
31 |
|
32 |
|
33 | function checkSource(node) {
|
34 | checkSourceValue(node.source)
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 | function checkCommon(call) {
|
40 | if (call.callee.type !== 'Identifier') return
|
41 | if (call.callee.name !== 'require') return
|
42 | if (call.arguments.length !== 1) return
|
43 |
|
44 | const modulePath = call.arguments[0]
|
45 | if (modulePath.type !== 'Literal') return
|
46 | if (typeof modulePath.value !== 'string') return
|
47 |
|
48 | checkSourceValue(modulePath)
|
49 | }
|
50 |
|
51 | function checkAMD(call) {
|
52 | if (call.callee.type !== 'Identifier') return
|
53 | if (call.callee.name !== 'require' &&
|
54 | call.callee.name !== 'define') return
|
55 | if (call.arguments.length !== 2) return
|
56 |
|
57 | const modules = call.arguments[0]
|
58 | if (modules.type !== 'ArrayExpression') return
|
59 |
|
60 | for (let element of modules.elements) {
|
61 | if (element.type !== 'Literal') continue
|
62 | if (typeof element.value !== 'string') continue
|
63 |
|
64 | if (element.value === 'require' ||
|
65 | element.value === 'exports') continue
|
66 |
|
67 | checkSourceValue(element)
|
68 | }
|
69 | }
|
70 |
|
71 | const visitors = {}
|
72 | if (options.esmodule) {
|
73 | Object.assign(visitors, {
|
74 | 'ImportDeclaration': checkSource,
|
75 | 'ExportNamedDeclaration': checkSource,
|
76 | 'ExportAllDeclaration': checkSource,
|
77 | })
|
78 | }
|
79 |
|
80 | if (options.commonjs || options.amd) {
|
81 | visitors['CallExpression'] = function (call) {
|
82 | if (options.commonjs) checkCommon(call)
|
83 | if (options.amd) checkAMD(call)
|
84 | }
|
85 | }
|
86 |
|
87 | return visitors
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | exports.optionsSchema = {
|
96 | 'type': 'object',
|
97 | 'properties': {
|
98 | 'commonjs': { 'type': 'boolean' },
|
99 | 'amd': { 'type': 'boolean' },
|
100 | 'esmodule': { 'type': 'boolean' },
|
101 | 'ignore': {
|
102 | 'type': 'array',
|
103 | 'minItems': 1,
|
104 | 'items': { 'type': 'string' },
|
105 | 'uniqueItems': true,
|
106 | },
|
107 | },
|
108 | 'additionalProperties': false,
|
109 | }
|