-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathgetExports.js
404 lines (326 loc) · 10.7 KB
/
getExports.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import 'es6-symbol/implement'
import Map from 'es6-map'
import * as fs from 'fs'
import { createHash } from 'crypto'
import * as doctrine from 'doctrine'
import parse from './parse'
import resolve from './resolve'
import isIgnored from './ignore'
import { hashObject } from './hash'
const exportCache = new Map()
/**
* detect exports without a full parse.
* used primarily to ignore the import/ignore setting, iif it looks like
* there might be something there (i.e., jsnext:main is set).
* @type {RegExp}
*/
const hasExports = new RegExp('(^|[\\n;])\\s*export\\s[\\w{*]')
export default class ExportMap {
constructor(path) {
this.path = path
this.namespace = new Map()
// todo: restructure to key on path, value is resolver + map of names
this.reexports = new Map()
this.dependencies = new Map()
this.errors = []
}
get hasDefault() { return this.get('default') != null } // stronger than this.has
get size() {
let size = this.namespace.size + this.reexports.size
this.dependencies.forEach(dep => size += dep().size)
return size
}
static get(source, context) {
var path = resolve(source, context)
if (path == null) return null
return ExportMap.for(path, context)
}
static for(path, context) {
let exportMap
const cacheKey = hashObject(createHash('sha256'), {
settings: context.settings,
parserPath: context.parserPath,
parserOptions: context.parserOptions,
path,
}).digest('hex')
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?
}
const content = fs.readFileSync(path, { encoding: 'utf8' })
// check for and cache ignore
if (isIgnored(path, context) && !hasExports.test(content)) {
exportCache.set(cacheKey, null)
return null
}
exportMap = ExportMap.parse(path, content, context)
exportMap.mtime = stats.mtime
exportCache.set(cacheKey, exportMap)
return exportMap
}
static parse(path, content, context) {
var m = new ExportMap(path)
try {
var ast = parse(content, context)
} catch (err) {
m.errors.push(err)
return m // can't continue
}
// attempt to collect module doc
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')) {
m.doc = doc
return true
}
} catch (err) { /* ignore */ }
return false
})
const namespaces = new Map()
function remotePath(node) {
return resolve.relative(node.source.value, path, context.settings)
}
function resolveImport(node) {
const rp = remotePath(node)
if (rp == null) return null
return ExportMap.for(rp, context)
}
function getNamespace(identifier) {
if (!namespaces.has(identifier.name)) return
return function () {
return resolveImport(namespaces.get(identifier.name))
}
}
function addNamespace(object, identifier) {
const nsfn = getNamespace(identifier)
if (nsfn) {
Object.defineProperty(object, 'namespace', { get: nsfn })
}
return object
}
ast.body.forEach(function (n) {
if (n.type === 'ExportDefaultDeclaration') {
const exportMeta = captureDoc(n)
if (n.declaration.type === 'Identifier') {
addNamespace(exportMeta, n.declaration)
}
m.namespace.set('default', exportMeta)
return
}
if (n.type === 'ExportAllDeclaration') {
let remoteMap = remotePath(n)
if (remoteMap == null) return
m.dependencies.set(remoteMap, () => ExportMap.for(remoteMap, context))
return
}
// capture namespaces in case of later export
if (n.type === 'ImportDeclaration') {
let ns
if (n.specifiers.some(s => s.type === 'ImportNamespaceSpecifier' && (ns = s))) {
namespaces.set(ns.local.name, n)
}
return
}
if (n.type === 'ExportNamedDeclaration'){
// capture declaration
if (n.declaration != null) {
switch (n.declaration.type) {
case 'FunctionDeclaration':
case 'ClassDeclaration':
case 'TypeAlias': // flowtype with babel-eslint parser
m.namespace.set(n.declaration.id.name, captureDoc(n))
break
case 'VariableDeclaration':
n.declaration.declarations.forEach((d) =>
recursivePatternCapture(d.id, id => m.namespace.set(id.name, captureDoc(d, n))))
break
}
}
n.specifiers.forEach((s) => {
const exportMeta = {}
let local
switch (s.type) {
case 'ExportDefaultSpecifier':
if (!n.source) return
local = 'default'
break
case 'ExportNamespaceSpecifier':
m.namespace.set(s.exported.name, Object.defineProperty(exportMeta, 'namespace', {
get() { return resolveImport(n) },
}))
return
case 'ExportSpecifier':
if (!n.source) {
m.namespace.set(s.exported.name, addNamespace(exportMeta, s.local))
return
}
// else falls through
default:
local = s.local.name
break
}
// todo: JSDoc
m.reexports.set(s.exported.name, { local, getImport: () => resolveImport(n) })
})
}
})
return m
}
/**
* Note that this does not check explicitly re-exported names for existence
* in the base namespace, but it will expand all `export * from '...'` exports
* if not found in the explicit namespace.
* @param {string} name
* @return {Boolean} true if `name` is exported by this module.
*/
has(name) {
if (this.namespace.has(name)) return true
if (this.reexports.has(name)) return true
// default exports must be explicitly re-exported (#328)
if (name !== 'default') {
for (let dep of this.dependencies.values()) {
let innerMap = dep()
// todo: report as unresolved?
if (!innerMap) continue
if (innerMap.has(name)) return true
}
}
return false
}
/**
* ensure that imported name fully resolves.
* @param {[type]} name [description]
* @return {Boolean} [description]
*/
hasDeep(name) {
if (this.namespace.has(name)) return { found: true, path: [this] }
if (this.reexports.has(name)) {
const { local, getImport } = this.reexports.get(name)
, imported = getImport()
// if import is ignored, return explicit 'null'
if (imported == null) return { found: true, path: [this] }
// safeguard against cycles, only if name matches
if (imported.path === this.path && local === name) return { found: false, path: [this] }
const deep = imported.hasDeep(local)
deep.path.unshift(this)
return deep
}
// default exports must be explicitly re-exported (#328)
if (name !== 'default') {
for (let dep of this.dependencies.values()) {
let innerMap = dep()
// todo: report as unresolved?
if (!innerMap) continue
// safeguard against cycles
if (innerMap.path === this.path) continue
let innerValue = innerMap.hasDeep(name)
if (innerValue.found) {
innerValue.path.unshift(this)
return innerValue
}
}
}
return { found: false, path: [this] }
}
get(name) {
if (this.namespace.has(name)) return this.namespace.get(name)
if (this.reexports.has(name)) {
const { local, getImport } = this.reexports.get(name)
, imported = getImport()
// if import is ignored, return explicit 'null'
if (imported == null) return null
// safeguard against cycles, only if name matches
if (imported.path === this.path && local === name) return undefined
return imported.get(local)
}
// default exports must be explicitly re-exported (#328)
if (name !== 'default') {
for (let dep of this.dependencies.values()) {
let innerMap = dep()
// todo: report as unresolved?
if (!innerMap) continue
// safeguard against cycles
if (innerMap.path === this.path) continue
let innerValue = innerMap.get(name)
if (innerValue !== undefined) return innerValue
}
}
return undefined
}
forEach(callback, thisArg) {
this.namespace.forEach((v, n) =>
callback.call(thisArg, v, n, this))
this.reexports.forEach(({ getImport, local }, name) =>
callback.call(thisArg, getImport().get(local), name, this))
this.dependencies.forEach(dep => dep().forEach((v, n) =>
n !== 'default' && callback.call(thisArg, v, n, this)))
}
// todo: keys, values, entries?
reportErrors(context, declaration) {
context.report({
node: declaration.source,
message: `Parse errors in imported module '${declaration.source.value}': ` +
`${this.errors
.map(e => `${e.message} (${e.lineNumber}:${e.column})`)
.join(', ')}`,
})
}
}
/**
* parse JSDoc from the first node that has leading comments
* @param {...[type]} nodes [description]
* @return {{doc: object}}
*/
function captureDoc(...nodes) {
const metadata = {}
// 'some' short-circuits on first 'true'
nodes.some(n => {
if (!n.leadingComments) return false
// capture XSDoc
n.leadingComments.forEach(comment => {
// skip non-block comments
if (comment.value.slice(0, 4) !== '*\n *') return
try {
metadata.doc = doctrine.parse(comment.value, { unwrap: true })
} catch (err) {
/* don't care, for now? maybe add to `errors?` */
}
})
return true
})
return metadata
}
/**
* Traverse a pattern/identifier node, calling 'callback'
* for each leaf identifier.
* @param {node} pattern
* @param {Function} callback
* @return {void}
*/
export function recursivePatternCapture(pattern, callback) {
switch (pattern.type) {
case 'Identifier': // base case
callback(pattern)
break
case 'ObjectPattern':
pattern.properties.forEach(({ value }) => {
recursivePatternCapture(value, callback)
})
break
case 'ArrayPattern':
pattern.elements.forEach((element) => {
if (element == null) return
recursivePatternCapture(element, callback)
})
break
}
}