forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getExports.js
415 lines (338 loc) · 13.1 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
405
406
407
408
409
410
411
412
413
414
415
import { expect } from 'chai'
import semver from 'semver'
import eslintPkg from 'eslint/package.json'
import ExportMap from '../../../src/ExportMap'
import * as fs from 'fs'
import { getFilename } from '../utils'
import * as unambiguous from 'eslint-module-utils/unambiguous'
describe('ExportMap', function () {
const fakeContext = {
getFilename: getFilename,
settings: {},
parserPath: 'babel-eslint',
}
it('handles ExportAllDeclaration', function () {
var imports
expect(function () {
imports = ExportMap.get('./export-all', fakeContext)
}).not.to.throw(Error)
expect(imports).to.exist
expect(imports.has('foo')).to.be.true
})
it('returns a cached copy on subsequent requests', function () {
expect(ExportMap.get('./named-exports', fakeContext))
.to.exist.and.equal(ExportMap.get('./named-exports', fakeContext))
})
it('does not return a cached copy after modification', (done) => {
const firstAccess = ExportMap.get('./mutator', fakeContext)
expect(firstAccess).to.exist
// mutate (update modified time)
const newDate = new Date()
fs.utimes(getFilename('mutator.js'), newDate, newDate, (error) => {
expect(error).not.to.exist
expect(ExportMap.get('./mutator', fakeContext)).not.to.equal(firstAccess)
done()
})
})
it('does not return a cached copy with different settings', () => {
const firstAccess = ExportMap.get('./named-exports', fakeContext)
expect(firstAccess).to.exist
const differentSettings = Object.assign(
{},
fakeContext,
{ parserPath: 'espree' })
expect(ExportMap.get('./named-exports', differentSettings))
.to.exist.and
.not.to.equal(firstAccess)
})
it('does not throw for a missing file', function () {
var imports
expect(function () {
imports = ExportMap.get('./does-not-exist', fakeContext)
}).not.to.throw(Error)
expect(imports).not.to.exist
})
it('exports explicit names for a missing file in exports', function () {
var imports
expect(function () {
imports = ExportMap.get('./exports-missing', fakeContext)
}).not.to.throw(Error)
expect(imports).to.exist
expect(imports.has('bar')).to.be.true
})
it('finds exports for an ES7 module with babel-eslint', function () {
const path = getFilename('jsx/FooES7.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
var imports = ExportMap.parse(
path,
contents,
{ parserPath: 'babel-eslint', settings: {} },
)
expect(imports, 'imports').to.exist
expect(imports.errors).to.be.empty
expect(imports.get('default'), 'default export').to.exist
expect(imports.has('Bar')).to.be.true
})
context('deprecation metadata', function () {
function jsdocTests(parseContext, lineEnding) {
context('deprecated imports', function () {
let imports
before('parse file', function () {
const path = getFilename('deprecated.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' }).replace(/[\r]\n/g, lineEnding)
imports = ExportMap.parse(path, contents, parseContext)
// sanity checks
expect(imports.errors).to.be.empty
})
it('works with named imports.', function () {
expect(imports.has('fn')).to.be.true
expect(imports.get('fn'))
.to.have.nested.property('doc.tags[0].title', 'deprecated')
expect(imports.get('fn'))
.to.have.nested.property('doc.tags[0].description', 'please use \'x\' instead.')
})
it('works with default imports.', function () {
expect(imports.has('default')).to.be.true
const importMeta = imports.get('default')
expect(importMeta).to.have.nested.property('doc.tags[0].title', 'deprecated')
expect(importMeta).to.have.nested.property('doc.tags[0].description', 'this is awful, use NotAsBadClass.')
})
it('works with variables.', function () {
expect(imports.has('MY_TERRIBLE_ACTION')).to.be.true
const importMeta = imports.get('MY_TERRIBLE_ACTION')
expect(importMeta).to.have.nested.property(
'doc.tags[0].title', 'deprecated')
expect(importMeta).to.have.nested.property(
'doc.tags[0].description', 'please stop sending/handling this action type.')
})
context('multi-line variables', function () {
it('works for the first one', function () {
expect(imports.has('CHAIN_A')).to.be.true
const importMeta = imports.get('CHAIN_A')
expect(importMeta).to.have.nested.property(
'doc.tags[0].title', 'deprecated')
expect(importMeta).to.have.nested.property(
'doc.tags[0].description', 'this chain is awful')
})
it('works for the second one', function () {
expect(imports.has('CHAIN_B')).to.be.true
const importMeta = imports.get('CHAIN_B')
expect(importMeta).to.have.nested.property(
'doc.tags[0].title', 'deprecated')
expect(importMeta).to.have.nested.property(
'doc.tags[0].description', 'so awful')
})
it('works for the third one, etc.', function () {
expect(imports.has('CHAIN_C')).to.be.true
const importMeta = imports.get('CHAIN_C')
expect(importMeta).to.have.nested.property(
'doc.tags[0].title', 'deprecated')
expect(importMeta).to.have.nested.property(
'doc.tags[0].description', 'still terrible')
})
})
})
context('full module', function () {
let imports
before('parse file', function () {
const path = getFilename('deprecated-file.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
imports = ExportMap.parse(path, contents, parseContext)
// sanity checks
expect(imports.errors).to.be.empty
})
it('has JSDoc metadata', function () {
expect(imports.doc).to.exist
})
})
}
context('default parser', function () {
jsdocTests({
parserPath: 'espree',
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
attachComment: true,
},
settings: {},
}, '\n')
jsdocTests({
parserPath: 'espree',
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
attachComment: true,
},
settings: {},
}, '\r\n')
})
context('babel-eslint', function () {
jsdocTests({
parserPath: 'babel-eslint',
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
attachComment: true,
},
settings: {},
}, '\n')
jsdocTests({
parserPath: 'babel-eslint',
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module',
attachComment: true,
},
settings: {},
}, '\r\n')
})
})
context('exported static namespaces', function () {
const espreeContext = { parserPath: 'espree', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, settings: {} }
const babelContext = { parserPath: 'babel-eslint', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, settings: {} }
it('works with espree & traditional namespace exports', function () {
const path = getFilename('deep/a.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
const a = ExportMap.parse(path, contents, espreeContext)
expect(a.errors).to.be.empty
expect(a.get('b').namespace).to.exist
expect(a.get('b').namespace.has('c')).to.be.true
})
it('captures namespace exported as default', function () {
const path = getFilename('deep/default.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
const def = ExportMap.parse(path, contents, espreeContext)
expect(def.errors).to.be.empty
expect(def.get('default').namespace).to.exist
expect(def.get('default').namespace.has('c')).to.be.true
})
it('works with babel-eslint & ES7 namespace exports', function () {
const path = getFilename('deep-es7/a.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
const a = ExportMap.parse(path, contents, babelContext)
expect(a.errors).to.be.empty
expect(a.get('b').namespace).to.exist
expect(a.get('b').namespace.has('c')).to.be.true
})
})
context('deep namespace caching', function () {
const espreeContext = { parserPath: 'espree', parserOptions: { ecmaVersion: 2015, sourceType: 'module' }, settings: {} }
let a
before('sanity check and prime cache', function (done) {
// first version
fs.writeFileSync(getFilename('deep/cache-2.js'),
fs.readFileSync(getFilename('deep/cache-2a.js')))
const path = getFilename('deep/cache-1.js')
, contents = fs.readFileSync(path, { encoding: 'utf8' })
a = ExportMap.parse(path, contents, espreeContext)
expect(a.errors).to.be.empty
expect(a.get('b').namespace).to.exist
expect(a.get('b').namespace.has('c')).to.be.true
// wait ~1s, cache check is 1s resolution
setTimeout(function reup() {
fs.unlinkSync(getFilename('deep/cache-2.js'))
// swap in a new file and touch it
fs.writeFileSync(getFilename('deep/cache-2.js'),
fs.readFileSync(getFilename('deep/cache-2b.js')))
done()
}, 1100)
})
it('works', function () {
expect(a.get('b').namespace.has('c')).to.be.false
})
after('remove test file', (done) => fs.unlink(getFilename('deep/cache-2.js'), done))
})
context('Map API', function () {
context('#size', function () {
it('counts the names', () => expect(ExportMap.get('./named-exports', fakeContext))
.to.have.property('size', 10))
it('includes exported namespace size', () => expect(ExportMap.get('./export-all', fakeContext))
.to.have.property('size', 1))
})
})
context('issue #210: self-reference', function () {
it(`doesn't crash`, function () {
expect(() => ExportMap.get('./narcissist', fakeContext)).not.to.throw(Error)
})
it(`'has' circular reference`, function () {
expect(ExportMap.get('./narcissist', fakeContext))
.to.exist.and.satisfy(m => m.has('soGreat'))
})
it(`can 'get' circular reference`, function () {
expect(ExportMap.get('./narcissist', fakeContext))
.to.exist.and.satisfy(m => m.get('soGreat') != null)
})
})
context('issue #478: never parse non-whitelist extensions', function () {
const context = Object.assign({}, fakeContext,
{ settings: { 'import/extensions': ['.js'] } })
let imports
before('load imports', function () {
imports = ExportMap.get('./typescript.ts', context)
})
it('returns nothing for a TypeScript file', function () {
expect(imports).not.to.exist
})
})
context('alternate parsers', function () {
const configs = [
// ['string form', { 'typescript-eslint-parser': '.ts' }],
]
if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
configs.push(['array form', { '@typescript-eslint/parser': ['.ts', '.tsx'] }])
}
if (semver.satisfies(eslintPkg.version, '<6.0.0')) {
configs.push(['array form', { 'typescript-eslint-parser': ['.ts', '.tsx'] }])
}
configs.forEach(([description, parserConfig]) => {
describe(description, function () {
const context = Object.assign({}, fakeContext,
{ settings: {
'import/extensions': ['.js'],
'import/parsers': parserConfig,
} })
let imports
before('load imports', function () {
this.timeout(20000) // takes a long time :shrug:
imports = ExportMap.get('./typescript.ts', context)
})
it('returns something for a TypeScript file', function () {
expect(imports).to.exist
})
it('has no parse errors', function () {
expect(imports).property('errors').to.be.empty
})
it('has exported function', function () {
expect(imports.has('getFoo')).to.be.true
})
it('has exported typedef', function () {
expect(imports.has('MyType')).to.be.true
})
it('has exported enum', function () {
expect(imports.has('MyEnum')).to.be.true
})
it('has exported interface', function () {
expect(imports.has('Foo')).to.be.true
})
it('has exported abstract class', function () {
expect(imports.has('Bar')).to.be.true
})
})
})
})
// todo: move to utils
describe('unambiguous regex', function () {
const testFiles = [
['deep/b.js', true],
['bar.js', true],
['deep-es7/b.js', true],
['common.js', false],
]
for (let [testFile, expectedRegexResult] of testFiles) {
it(`works for ${testFile} (${expectedRegexResult})`, function () {
const content = fs.readFileSync('./tests/files/' + testFile, 'utf8')
expect(unambiguous.test(content)).to.equal(expectedRegexResult)
})
}
})
})