1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports.default = function visitModules(visitor, options) {
|
16 | const ignore = options && options.ignore;
|
17 | const amd = !!(options && options.amd);
|
18 | const commonjs = !!(options && options.commonjs);
|
19 |
|
20 | const esmodule = !!Object.assign({ esmodule: true }, options).esmodule;
|
21 |
|
22 | const ignoreRegExps = ignore == null ? [] : ignore.map((p) => new RegExp(p));
|
23 |
|
24 |
|
25 | function checkSourceValue(source, importer) {
|
26 | if (source == null) { return; }
|
27 |
|
28 |
|
29 | if (ignoreRegExps.some((re) => re.test(String(source.value)))) { return; }
|
30 |
|
31 |
|
32 | visitor(source, importer);
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 | function checkSource(node) {
|
38 | checkSourceValue(node.source, node);
|
39 | }
|
40 |
|
41 |
|
42 |
|
43 | function checkImportCall(node) {
|
44 |
|
45 | let modulePath;
|
46 |
|
47 | if (node.type === 'ImportExpression') {
|
48 | modulePath = node.source;
|
49 | } else if (node.type === 'CallExpression') {
|
50 |
|
51 | if (node.callee.type !== 'Import') { return; }
|
52 | if (node.arguments.length !== 1) { return; }
|
53 |
|
54 | modulePath = node.arguments[0];
|
55 | } else {
|
56 | throw new TypeError('this should be unreachable');
|
57 | }
|
58 |
|
59 | if (modulePath.type !== 'Literal') { return; }
|
60 | if (typeof modulePath.value !== 'string') { return; }
|
61 |
|
62 | checkSourceValue(modulePath, node);
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function checkCommon(call) {
|
69 | if (call.callee.type !== 'Identifier') { return; }
|
70 | if (call.callee.name !== 'require') { return; }
|
71 | if (call.arguments.length !== 1) { return; }
|
72 |
|
73 | const modulePath = call.arguments[0];
|
74 | if (modulePath.type !== 'Literal') { return; }
|
75 | if (typeof modulePath.value !== 'string') { return; }
|
76 |
|
77 | checkSourceValue(modulePath, call);
|
78 | }
|
79 |
|
80 |
|
81 | function checkAMD(call) {
|
82 | if (call.callee.type !== 'Identifier') { return; }
|
83 | if (call.callee.name !== 'require' && call.callee.name !== 'define') { return; }
|
84 | if (call.arguments.length !== 2) { return; }
|
85 |
|
86 | const modules = call.arguments[0];
|
87 | if (modules.type !== 'ArrayExpression') { return; }
|
88 |
|
89 | for (const element of modules.elements) {
|
90 | if (!element) { continue; }
|
91 | if (element.type !== 'Literal') { continue; }
|
92 | if (typeof element.value !== 'string') { continue; }
|
93 |
|
94 | if (
|
95 | element.value === 'require'
|
96 | || element.value === 'exports'
|
97 | ) {
|
98 | continue;
|
99 | }
|
100 |
|
101 | checkSourceValue(element, element);
|
102 | }
|
103 | }
|
104 |
|
105 | const visitors = {};
|
106 | if (esmodule) {
|
107 | Object.assign(visitors, {
|
108 | ImportDeclaration: checkSource,
|
109 | ExportNamedDeclaration: checkSource,
|
110 | ExportAllDeclaration: checkSource,
|
111 | CallExpression: checkImportCall,
|
112 | ImportExpression: checkImportCall,
|
113 | });
|
114 | }
|
115 |
|
116 | if (commonjs || amd) {
|
117 | const currentCallExpression = visitors.CallExpression;
|
118 | visitors.CallExpression = function (call) {
|
119 | if (currentCallExpression) { currentCallExpression(call); }
|
120 | if (commonjs) { checkCommon(call); }
|
121 | if (amd) { checkAMD(call); }
|
122 | };
|
123 | }
|
124 |
|
125 | return visitors;
|
126 | };
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | function makeOptionsSchema(additionalProperties) {
|
133 |
|
134 | const base = {
|
135 | type: 'object',
|
136 | properties: {
|
137 | commonjs: { type: 'boolean' },
|
138 | amd: { type: 'boolean' },
|
139 | esmodule: { type: 'boolean' },
|
140 | ignore: {
|
141 | type: 'array',
|
142 | minItems: 1,
|
143 | items: { type: 'string' },
|
144 | uniqueItems: true,
|
145 | },
|
146 | },
|
147 | additionalProperties: false,
|
148 | };
|
149 |
|
150 | if (additionalProperties) {
|
151 | for (const key in additionalProperties) {
|
152 |
|
153 | base.properties[key] = additionalProperties[key];
|
154 | }
|
155 | }
|
156 |
|
157 | return base;
|
158 | }
|
159 | exports.makeOptionsSchema = makeOptionsSchema;
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | exports.optionsSchema = makeOptionsSchema();
|