-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbuilder.js
210 lines (175 loc) · 5.72 KB
/
builder.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import fs from 'fs';
import doctrine from 'doctrine';
import debug from 'debug';
import parse from 'eslint-module-utils/parse';
import visit from 'eslint-module-utils/visit';
import resolve from 'eslint-module-utils/resolve';
import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore';
import { hashObject } from 'eslint-module-utils/hash';
import * as unambiguous from 'eslint-module-utils/unambiguous';
import ExportMap from '.';
import childContext from './childContext';
import { isEsModuleInterop } from './typescript';
import { RemotePath } from './remotePath';
import ImportExportVisitorBuilder from './visitor';
const log = debug('eslint-plugin-import:ExportMap');
const exportCache = new Map();
/**
* The creation of this closure is isolated from other scopes
* to avoid over-retention of unrelated variables, which has
* caused memory leaks. See #1266.
*/
function thunkFor(p, context) {
// eslint-disable-next-line no-use-before-define
return () => ExportMapBuilder.for(childContext(p, context));
}
export default class ExportMapBuilder {
static get(source, context) {
const path = resolve(source, context);
if (path == null) { return null; }
return ExportMapBuilder.for(childContext(path, context));
}
static for(context) {
const { path } = context;
const cacheKey = context.cacheKey || hashObject(context).digest('hex');
let exportMap = exportCache.get(cacheKey);
// return cached ignore
if (exportMap === null) { return null; }
const stats = fs.statSync(path);
if (exportMap != null) {
// date equality check
if (exportMap.mtime - stats.mtime === 0) {
return exportMap;
}
// future: check content equality?
}
// check valid extensions first
if (!hasValidExtension(path, context)) {
exportCache.set(cacheKey, null);
return null;
}
// check for and cache ignore
if (isIgnored(path, context)) {
log('ignored path due to ignore settings:', path);
exportCache.set(cacheKey, null);
return null;
}
const content = fs.readFileSync(path, { encoding: 'utf8' });
// check for and cache unambiguous modules
if (!unambiguous.test(content)) {
log('ignored path due to unambiguous regex:', path);
exportCache.set(cacheKey, null);
return null;
}
log('cache miss', cacheKey, 'for path', path);
exportMap = ExportMapBuilder.parse(path, content, context);
// ambiguous modules return null
if (exportMap == null) {
log('ignored path due to ambiguous parse:', path);
exportCache.set(cacheKey, null);
return null;
}
exportMap.mtime = stats.mtime;
// If the visitor keys were not populated, then we shouldn't save anything to the cache,
// since the parse results may not be reliable.
if (exportMap.visitorKeys) {
exportCache.set(cacheKey, exportMap);
}
return exportMap;
}
static parse(path, content, context) {
const exportMap = new ExportMap(path);
const isEsModuleInteropTrue = isEsModuleInterop(context);
let ast;
let visitorKeys;
try {
const result = parse(path, content, context);
ast = result.ast;
visitorKeys = result.visitorKeys;
} catch (err) {
exportMap.errors.push(err);
return exportMap; // can't continue
}
exportMap.visitorKeys = visitorKeys;
let hasDynamicImports = false;
const remotePathResolver = new RemotePath(path, context);
function processDynamicImport(source) {
hasDynamicImports = true;
if (source.type !== 'Literal') {
return null;
}
const p = remotePathResolver.resolve(source.value);
if (p == null) {
return null;
}
const importedSpecifiers = new Set();
importedSpecifiers.add('ImportNamespaceSpecifier');
const getter = thunkFor(p, context);
exportMap.imports.set(p, {
getter,
declarations: new Set([{
source: {
// capturing actual node reference holds full AST in memory!
value: source.value,
loc: source.loc,
},
importedSpecifiers,
dynamic: true,
}]),
});
}
visit(ast, visitorKeys, {
ImportExpression(node) {
processDynamicImport(node.source);
},
CallExpression(node) {
if (node.callee.type === 'Import') {
processDynamicImport(node.arguments[0]);
}
},
});
const unambiguouslyESM = unambiguous.isModule(ast);
if (!unambiguouslyESM && !hasDynamicImports) { return null; }
// attempt to collect module doc
if (ast.comments) {
ast.comments.some((c) => {
if (c.type !== 'Block') { return false; }
try {
const doc = doctrine.parse(c.value, { unwrap: true });
if (doc.tags.some((t) => t.title === 'module')) {
exportMap.doc = doc;
return true;
}
} catch (err) { /* ignore */ }
return false;
});
}
const visitorBuilder = new ImportExportVisitorBuilder(
path,
context,
exportMap,
ExportMapBuilder,
content,
ast,
isEsModuleInteropTrue,
thunkFor,
);
ast.body.forEach(function (astNode) {
const visitor = visitorBuilder.build(astNode);
if (visitor[astNode.type]) {
visitor[astNode.type].call(visitorBuilder);
}
});
if (
isEsModuleInteropTrue // esModuleInterop is on in tsconfig
&& exportMap.namespace.size > 0 // anything is exported
&& !exportMap.namespace.has('default') // and default isn't added already
) {
exportMap.namespace.set('default', {}); // add default export
}
if (unambiguouslyESM) {
exportMap.parseGoal = 'Module';
}
return exportMap;
}
}