-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ExportMap.js
480 lines (388 loc) · 12.3 KB
/
ExportMap.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
import fs from 'fs'
import doctrine from 'doctrine'
import debug from 'debug'
import parse from 'eslint-module-utils/parse'
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'
const log = debug('eslint-plugin-import:ExportMap')
const exportCache = new Map()
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
}
/**
* 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 reexports = this.reexports.get(name)
, imported = reexports.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 && reexports.local === name) {
return { found: false, path: [this] }
}
const deep = imported.hasDeep(reexports.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 reexports = this.reexports.get(name)
, imported = reexports.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 && reexports.local === name) return undefined
return imported.get(reexports.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((reexports, name) => {
const reexported = reexports.getImport()
// can't look up meta for ignored re-exports (#348)
callback.call(thisArg, reexported && reexported.get(reexports.local), name, this)
})
this.dependencies.forEach(dep => {
const d = dep()
// CJS / ignored dependencies won't exist (#717)
if (d == null) return
d.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 docs from the first node that has leading comments
* @param {...[type]} nodes [description]
* @return {{doc: object}}
*/
function captureDoc(docStyleParsers) {
const metadata = {}
, nodes = Array.prototype.slice.call(arguments, 1)
// 'some' short-circuits on first 'true'
nodes.some(n => {
if (!n.leadingComments) return false
for (let name in docStyleParsers) {
const doc = docStyleParsers[name](n.leadingComments)
if (doc) {
metadata.doc = doc
}
}
return true
})
return metadata
}
const availableDocStyleParsers = {
jsdoc: captureJsDoc,
tomdoc: captureTomDoc,
}
/**
* parse JSDoc from leading comments
* @param {...[type]} comments [description]
* @return {{doc: object}}
*/
function captureJsDoc(comments) {
let doc
// capture XSDoc
comments.forEach(comment => {
// skip non-block comments
if (comment.value.slice(0, 4) !== '*\n *') return
try {
doc = doctrine.parse(comment.value, { unwrap: true })
} catch (err) {
/* don't care, for now? maybe add to `errors?` */
}
})
return doc
}
/**
* parse TomDoc section from comments
*/
function captureTomDoc(comments) {
// collect lines up to first paragraph break
const lines = []
for (let i = 0; i < comments.length; i++) {
const comment = comments[i]
if (comment.value.match(/^\s*$/)) break
lines.push(comment.value.trim())
}
// return doctrine-like object
const statusMatch = lines.join(' ').match(/^(Public|Internal|Deprecated):\s*(.+)/)
if (statusMatch) {
return {
description: statusMatch[2],
tags: [{
title: statusMatch[1].toLowerCase(),
description: statusMatch[2],
}],
}
}
}
ExportMap.get = function (source, context) {
const path = resolve(source, context)
if (path == null) return null
return ExportMap.for(path, context)
}
ExportMap.for = function (path, context) {
let exportMap
const cacheKey = hashObject({
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?
}
// check valid extensions first
if (!hasValidExtension(path, context)) {
exportCache.set(cacheKey, null)
return null
}
const content = fs.readFileSync(path, { encoding: 'utf8' })
// check for and cache ignore
if (isIgnored(path, context) || !unambiguous.test(content)) {
log('ignored path due to unambiguous regex or ignore settings:', path)
exportCache.set(cacheKey, null)
return null
}
exportMap = ExportMap.parse(path, content, context)
// ambiguous modules return null
if (exportMap == null) return null
exportMap.mtime = stats.mtime
exportCache.set(cacheKey, exportMap)
return exportMap
}
ExportMap.parse = function (path, content, context) {
var m = new ExportMap(path)
try {
var ast = parse(path, content, context)
} catch (err) {
log('parse error:', path, err)
m.errors.push(err)
return m // can't continue
}
if (!unambiguous.isModule(ast)) return null
const docstyle = (context.settings && context.settings['import/docstyle']) || ['jsdoc']
const docStyleParsers = {}
docstyle.forEach(style => {
docStyleParsers[style] = availableDocStyleParsers[style]
})
// 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(docStyleParsers, 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(docStyleParsers, n))
break
case 'VariableDeclaration':
n.declaration.declarations.forEach((d) =>
recursivePatternCapture(d.id,
id => m.namespace.set(id.name, captureDoc(docStyleParsers, 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
}
/**
* 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(p => {
recursivePatternCapture(p.value, callback)
})
break
case 'ArrayPattern':
pattern.elements.forEach((element) => {
if (element == null) return
recursivePatternCapture(element, callback)
})
break
}
}