@davidwells/extract-deps 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/README.md +211 -0
  2. package/_usage.js +175 -0
  3. package/examples/get-exports.js +31 -0
  4. package/package.json +27 -0
  5. package/src/detect-syntax.js +70 -0
  6. package/src/get-exports.js +299 -0
  7. package/src/get-imports.js +165 -0
  8. package/src/get-requires.js +104 -0
  9. package/src/index.js +90 -0
  10. package/src/utils.js +64 -0
  11. package/tests/detect-syntax.test.js +280 -0
  12. package/tests/get-exports.test.js +346 -0
  13. package/tests/get-imports.test.js +395 -0
  14. package/tests/get-requires.test.js +201 -0
  15. package/tests/index.test.js +202 -0
  16. package/tests/test-utils.js +30 -0
  17. package/tsconfig.json +19 -0
  18. package/types/detect-syntax.d.ts +39 -0
  19. package/types/detect-syntax.d.ts.map +1 -0
  20. package/types/extract-deps/_usage.d.ts +20 -0
  21. package/types/extract-deps/_usage.d.ts.map +1 -0
  22. package/types/extract-deps/examples/get-exports.d.ts +2 -0
  23. package/types/extract-deps/examples/get-exports.d.ts.map +1 -0
  24. package/types/extract-deps/src/detect-syntax.d.ts +39 -0
  25. package/types/extract-deps/src/detect-syntax.d.ts.map +1 -0
  26. package/types/extract-deps/src/get-exports.d.ts +52 -0
  27. package/types/extract-deps/src/get-exports.d.ts.map +1 -0
  28. package/types/extract-deps/src/get-imports.d.ts +53 -0
  29. package/types/extract-deps/src/get-imports.d.ts.map +1 -0
  30. package/types/extract-deps/src/get-requires.d.ts +34 -0
  31. package/types/extract-deps/src/get-requires.d.ts.map +1 -0
  32. package/types/extract-deps/src/index.d.ts +39 -0
  33. package/types/extract-deps/src/index.d.ts.map +1 -0
  34. package/types/extract-deps/src/utils.d.ts +4 -0
  35. package/types/extract-deps/src/utils.d.ts.map +1 -0
  36. package/types/extract-deps/tests/detect-syntax.test.d.ts +2 -0
  37. package/types/extract-deps/tests/detect-syntax.test.d.ts.map +1 -0
  38. package/types/extract-deps/tests/get-exports.test.d.ts +2 -0
  39. package/types/extract-deps/tests/get-exports.test.d.ts.map +1 -0
  40. package/types/extract-deps/tests/get-imports.test.d.ts +2 -0
  41. package/types/extract-deps/tests/get-imports.test.d.ts.map +1 -0
  42. package/types/extract-deps/tests/get-requires.test.d.ts +2 -0
  43. package/types/extract-deps/tests/get-requires.test.d.ts.map +1 -0
  44. package/types/extract-deps/tests/index.test.d.ts +18 -0
  45. package/types/extract-deps/tests/index.test.d.ts.map +1 -0
  46. package/types/extract-deps/tests/test-utils.d.ts +9 -0
  47. package/types/extract-deps/tests/test-utils.d.ts.map +1 -0
  48. package/types/get-exports.d.ts +52 -0
  49. package/types/get-exports.d.ts.map +1 -0
  50. package/types/get-imports.d.ts +53 -0
  51. package/types/get-imports.d.ts.map +1 -0
  52. package/types/get-requires.d.ts +34 -0
  53. package/types/get-requires.d.ts.map +1 -0
  54. package/types/index.d.ts +39 -0
  55. package/types/index.d.ts.map +1 -0
  56. package/types/packages-list/index.d.ts +138 -0
  57. package/types/packages-list/index.d.ts.map +1 -0
  58. package/types/utils.d.ts +4 -0
  59. package/types/utils.d.ts.map +1 -0
@@ -0,0 +1,395 @@
1
+ const { test } = require('uvu')
2
+ const assert = require('uvu/assert')
3
+ const { normalizeTestResult } = require('./test-utils')
4
+ const {
5
+ getImports,
6
+ getStaticImports,
7
+ getTypeImports,
8
+ getDynamicImports,
9
+ getAssertionImports
10
+ } = require('../src/get-imports')
11
+
12
+ test('Exports api', async () => {
13
+ assert.is(typeof getStaticImports, 'function', 'Exports api')
14
+ assert.is(typeof getTypeImports, 'function', 'Exports api')
15
+ })
16
+
17
+ test('getStaticImports', async () => {
18
+ const code = `
19
+ import {fileURLToPath, URL, whatever as funky} from 'url'
20
+ import foo from 'fs'
21
+ import * as Fs from 'fs'
22
+ import assert from 'uvu/assert'
23
+ const { test } = require('uvu')
24
+ const { getRequires } = require('./get-imports')
25
+ `
26
+
27
+ const importsFound = getStaticImports(code)
28
+ /*
29
+ console.log('importsFound')
30
+ deepLog(importsFound)
31
+ /** */
32
+ assert.equal(normalizeTestResult(importsFound), [
33
+ {
34
+ line: "import {fileURLToPath, URL, whatever as funky} from 'url'",
35
+ varType: 'const',
36
+ varName: '{fileURLToPath, URL, whatever as funky}',
37
+ definitions: {
38
+ expectsDestructuring: true,
39
+ declaration: '{fileURLToPath, URL, whatever as funky}',
40
+ destructuredParameters: [
41
+ { declaration: 'fileURLToPath', name: 'fileURLToPath' },
42
+ { declaration: 'URL', name: 'URL' },
43
+ {
44
+ declaration: 'whatever as funky',
45
+ name: 'whatever',
46
+ alias: 'funky'
47
+ }
48
+ ],
49
+ name: '{fileURLToPath, URL, whatever as funky}'
50
+ },
51
+ from: 'url',
52
+ isImport: true,
53
+ isRelative: false
54
+ },
55
+ {
56
+ line: "import foo from 'fs'",
57
+ varType: 'const',
58
+ varName: 'foo',
59
+ definitions: { declaration: 'foo', name: 'foo' },
60
+ from: 'fs',
61
+ isImport: true,
62
+ isRelative: false
63
+ },
64
+ {
65
+ line: "import * as Fs from 'fs'",
66
+ varType: 'const',
67
+ varName: '* as Fs',
68
+ definitions: { declaration: '* as Fs', name: '*', alias: 'Fs' },
69
+ from: 'fs',
70
+ isImport: true,
71
+ isRelative: false
72
+ },
73
+ {
74
+ line: "import assert from 'uvu/assert'",
75
+ varType: 'const',
76
+ varName: 'assert',
77
+ definitions: { declaration: 'assert', name: 'assert' },
78
+ from: 'uvu/assert',
79
+ isImport: true,
80
+ isRelative: false
81
+ }
82
+ ])
83
+ })
84
+
85
+
86
+ test('get jsDoc Imports', async () => {
87
+ const code = `
88
+ /**
89
+ * @param {string} [fin="cool"]
90
+ * @param {import("./foo").hdhdhdhdhddh} [api] - lol
91
+ * @param {import("lodash").omit} Cool - lodash omit
92
+ * @param {import("uvu/assert")} Rad - other thing
93
+ */
94
+ `
95
+
96
+ const importsFound = getTypeImports(code)
97
+ /*
98
+ console.log('importsFound')
99
+ deepLog(importsFound)
100
+ /** */
101
+ assert.equal(normalizeTestResult(importsFound), [
102
+ {
103
+ importStatement: 'import("./foo").hdhdhdhdhddh',
104
+ importPath: './foo',
105
+ importName: 'hdhdhdhdhddh',
106
+ isImport: true,
107
+ isRelative: true
108
+ },
109
+ {
110
+ importStatement: 'import("lodash").omit',
111
+ importPath: 'lodash',
112
+ importName: 'omit',
113
+ isImport: true,
114
+ isRelative: false
115
+ },
116
+ {
117
+ importStatement: 'import("uvu/assert")',
118
+ importPath: 'uvu/assert',
119
+ importName: '',
120
+ isImport: true,
121
+ isRelative: false
122
+ }
123
+ ])
124
+ })
125
+
126
+ test('getDynamicImports', async () => {
127
+ const code = `
128
+ // Basic dynamic import with string literal
129
+ const module = await import('./module.js')
130
+
131
+ // Dynamic import with variable
132
+ const moduleName = './utils.js'
133
+ const utils = await import(moduleName)
134
+
135
+ // Dynamic import with template literal
136
+ const folder = 'components'
137
+ const component = await import(\`./src/\${folder}/Button.js\`)
138
+
139
+ // Dynamic import with path building
140
+ const modulePath = path.join('./src', 'helpers', 'api.js')
141
+ const api = await import(modulePath)
142
+
143
+ // Dynamic import with destructuring
144
+ const { default: Component, helper } = await import('./Component.js')
145
+
146
+ // Dynamic import with function call
147
+ const dynamicModule = await import(getModulePath())
148
+ `
149
+
150
+ const importsFound = getDynamicImports(code)
151
+ /*
152
+ console.log('importsFound')
153
+ deepLog(importsFound)
154
+ /** */
155
+ assert.equal(importsFound.length, 6, 'Should find 6 dynamic imports')
156
+
157
+ assert.equal(normalizeTestResult(importsFound), [
158
+ {
159
+ line: "const module = await import('./module.js')",
160
+ varType: 'const',
161
+ varName: 'module',
162
+ definitions: { declaration: 'module', name: 'module' },
163
+ from: "'./module.js'",
164
+ isImport: true,
165
+ isRelative: true,
166
+ isDynamic: true
167
+ },
168
+ {
169
+ line: 'const utils = await import(moduleName)',
170
+ varType: 'const',
171
+ varName: 'utils',
172
+ definitions: { declaration: 'utils', name: 'utils' },
173
+ from: 'moduleName',
174
+ isImport: true,
175
+ isRelative: false,
176
+ isDynamic: true
177
+ },
178
+ {
179
+ line: 'const component = await import(`./src/${folder}/Button.js`)',
180
+ varType: 'const',
181
+ varName: 'component',
182
+ definitions: { declaration: 'component', name: 'component' },
183
+ from: '`./src/${folder}/Button.js`',
184
+ isImport: true,
185
+ isRelative: false,
186
+ isDynamic: true
187
+ },
188
+ {
189
+ line: 'const api = await import(modulePath)',
190
+ varType: 'const',
191
+ varName: 'api',
192
+ definitions: { declaration: 'api', name: 'api' },
193
+ from: 'modulePath',
194
+ isImport: true,
195
+ isRelative: false,
196
+ isDynamic: true
197
+ },
198
+ {
199
+ line: "const { default: Component, helper } = await import('./Component.js')",
200
+ varType: 'const',
201
+ varName: '{ default: Component, helper }',
202
+ definitions: {
203
+ expectsDestructuring: true,
204
+ declaration: '{ default: Component, helper }',
205
+ destructuredParameters: [
206
+ {
207
+ declaration: 'default: Component',
208
+ name: 'default',
209
+ alias: 'Component'
210
+ },
211
+ { declaration: 'helper', name: 'helper' }
212
+ ],
213
+ name: '{ default: Component, helper }'
214
+ },
215
+ from: "'./Component.js'",
216
+ isImport: true,
217
+ isRelative: true,
218
+ isDynamic: true
219
+ },
220
+ {
221
+ line: 'const dynamicModule = await import(getModulePath())',
222
+ varType: 'const',
223
+ varName: 'dynamicModule',
224
+ definitions: { declaration: 'dynamicModule', name: 'dynamicModule' },
225
+ from: 'getModulePath()',
226
+ isImport: true,
227
+ isRelative: false,
228
+ isDynamic: true
229
+ }
230
+ ])
231
+ })
232
+
233
+ test('Imports kitchen sink', () =>{
234
+ const code = `
235
+ // Multi-line destructuring import
236
+ import {
237
+ Component
238
+ } from '@angular2/core';
239
+
240
+ // Default import
241
+ import defaultMember from "module-name";
242
+
243
+ // Namespace import with extra spaces
244
+ import * as name from "module-name ";
245
+
246
+ // Default import with missing quote
247
+ import React from react"
248
+
249
+ // Destructuring import with extra spaces
250
+ import { member } from " module-name";
251
+
252
+ // Destructuring import with alias
253
+ import { member as alias } from "module-name";
254
+
255
+ // Multi-line destructuring import
256
+ import { member1 ,
257
+ member2 } from "module-name";
258
+
259
+ // Multiple destructuring imports with aliases
260
+ import { member1 , member2 as alias2 , member3 as alias3 } from "module-name";
261
+
262
+ // Default import with destructuring
263
+ import defaultMember, { member, member } from "module-name";
264
+
265
+ // Default import with namespace
266
+ import defaultMember, * as name from "module-name";
267
+
268
+ // Side-effect import
269
+ import "module-name";
270
+
271
+ // Wildcard import
272
+ import * from './smdn';
273
+ `
274
+
275
+ const importsFound = normalizeTestResult(getImports(code))
276
+
277
+ /*
278
+ console.log('importsFound')
279
+ deepLog(importsFound)
280
+
281
+ importsFound.forEach(item => {
282
+ console.log(item.line)
283
+ })
284
+ /** */
285
+
286
+ assert.equal(importsFound.length, 12, 'Imports found')
287
+ })
288
+
289
+
290
+ test('Assert imports', async () => {
291
+ const code = `
292
+ // JSON assertion import
293
+ import data from './data.json' assert { type: 'json' }
294
+
295
+ // CSS assertion import
296
+ import styles from './styles.css' assert { type: 'css' }
297
+
298
+ // JSON assertion import with with keyword
299
+ import dataTwo from './data-two.json' with { type: 'json' }
300
+
301
+ // CSS assertion import with with keyword
302
+ import styles from './styles.css' with { type: 'css' }
303
+ `
304
+
305
+ const importsFound = normalizeTestResult(getAssertionImports(code))
306
+ /*
307
+ console.log('assert importsFound')
308
+ deepLog(importsFound)
309
+ /** */
310
+
311
+ assert.equal(importsFound.length, 4, 'Should find 4 assertion imports')
312
+
313
+ assert.equal(importsFound, [
314
+ {
315
+ line: "import data from './data.json' assert { type: 'json' }",
316
+ varType: 'const',
317
+ varName: 'data',
318
+ definitions: { declaration: 'data', name: 'data' },
319
+ from: './data.json',
320
+ assertion: "type: 'json'",
321
+ assertionType: 'assert',
322
+ isImport: true,
323
+ isRelative: true,
324
+ isAssertion: true
325
+ },
326
+ {
327
+ line: "import styles from './styles.css' assert { type: 'css' }",
328
+ varType: 'const',
329
+ varName: 'styles',
330
+ definitions: { declaration: 'styles', name: 'styles' },
331
+ from: './styles.css',
332
+ assertion: "type: 'css'",
333
+ assertionType: 'assert',
334
+ isImport: true,
335
+ isRelative: true,
336
+ isAssertion: true
337
+ },
338
+ {
339
+ line: "import dataTwo from './data-two.json' with { type: 'json' }",
340
+ varType: 'const',
341
+ varName: 'dataTwo',
342
+ definitions: { declaration: 'dataTwo', name: 'dataTwo' },
343
+ from: './data-two.json',
344
+ assertion: "type: 'json'",
345
+ assertionType: 'with',
346
+ isImport: true,
347
+ isRelative: true,
348
+ isAssertion: true
349
+ },
350
+ {
351
+ line: "import styles from './styles.css' with { type: 'css' }",
352
+ varType: 'const',
353
+ varName: 'styles',
354
+ definitions: { declaration: 'styles', name: 'styles' },
355
+ from: './styles.css',
356
+ assertion: "type: 'css'",
357
+ assertionType: 'with',
358
+ isImport: true,
359
+ isRelative: true,
360
+ isAssertion: true
361
+ }
362
+ ])
363
+ })
364
+
365
+ test('getImports with assertion imports', async () => {
366
+ const code = `
367
+ // Regular import
368
+ import foo from 'fs'
369
+
370
+ // Assertion import
371
+ import data from './data.json' assert { type: 'json' }
372
+
373
+ // With import
374
+ import styles from './styles.css' with { type: 'css' }
375
+ `
376
+
377
+ const importsFound = normalizeTestResult(getImports(code, { includeAssertionImports: true }))
378
+ /*
379
+ console.log('importsFound')
380
+ deepLog(importsFound)
381
+ /** */
382
+
383
+ assert.equal(importsFound.length, 3, 'Should find 3 imports including assertion imports')
384
+
385
+ // Check that we have all types of imports
386
+ const hasRegularImport = importsFound.some(imp => !imp.isAssertion && !imp.isDynamic)
387
+ const hasAssertImport = importsFound.some(imp => imp.isAssertion && imp.assertionType === 'assert')
388
+ const hasWithImport = importsFound.some(imp => imp.isAssertion && imp.assertionType === 'with')
389
+
390
+ assert.is(hasRegularImport, true, 'Should have regular import')
391
+ assert.is(hasAssertImport, true, 'Should have assert import')
392
+ assert.is(hasWithImport, true, 'Should have with import')
393
+ })
394
+
395
+ test.run()
@@ -0,0 +1,201 @@
1
+ const { test } = require('uvu')
2
+ const assert = require('uvu/assert')
3
+ const { getRequires, getDynamicRequires } = require('../src/get-requires')
4
+ const { normalizeTestResult } = require('./test-utils')
5
+
6
+ test('Exports api', async () => {
7
+ assert.is(typeof getRequires, 'function', 'Exports getDocs')
8
+ })
9
+
10
+ test('getRequires', async () => {
11
+ const code = `
12
+ const { test } = require('uvu')
13
+ const assert = require('uvu/assert')
14
+ const { getRequires } = require('./get-imports')
15
+ `
16
+
17
+ const requiresFound = getRequires(code)
18
+ /*
19
+ console.log('requiresFound')
20
+ deepLog(requiresFound)
21
+ /** */
22
+ assert.equal(normalizeTestResult(requiresFound), [
23
+ {
24
+ line: "const { test } = require('uvu')",
25
+ varType: 'const',
26
+ varName: '{ test }',
27
+ definitions: {
28
+ expectsDestructuring: true,
29
+ declaration: '{ test }',
30
+ destructuredParameters: [ { declaration: 'test', name: 'test' } ],
31
+ name: '{ test }'
32
+ },
33
+ from: 'uvu',
34
+ isRequire: true,
35
+ isRelative: false,
36
+ isDynamic: false
37
+ },
38
+ {
39
+ line: "const assert = require('uvu/assert')",
40
+ varType: 'const',
41
+ varName: 'assert',
42
+ definitions: { declaration: 'assert', name: 'assert' },
43
+ from: 'uvu/assert',
44
+ isRequire: true,
45
+ isRelative: false,
46
+ isDynamic: false
47
+ },
48
+ {
49
+ line: "const { getRequires } = require('./get-imports')",
50
+ varType: 'const',
51
+ varName: '{ getRequires }',
52
+ definitions: {
53
+ expectsDestructuring: true,
54
+ declaration: '{ getRequires }',
55
+ destructuredParameters: [ { declaration: 'getRequires', name: 'getRequires' } ],
56
+ name: '{ getRequires }'
57
+ },
58
+ from: './get-imports',
59
+ isRequire: true,
60
+ isRelative: true,
61
+ isDynamic: false
62
+ }
63
+ ])
64
+ })
65
+
66
+ test('getRequires 2', async () => {
67
+ const code = `
68
+ const { test : funky, foo = false } = require('uvu')
69
+ const assert = require('uvu/assert')
70
+ const { getRequires } = require('./get-imports')
71
+ `
72
+ const requiresFound = getRequires(code)
73
+ /*
74
+ console.log('requiresFound')
75
+ deepLog(requiresFound)
76
+ /** */
77
+
78
+ assert.equal(normalizeTestResult(requiresFound), [
79
+ {
80
+ line: "const { test : funky, foo = false } = require('uvu')",
81
+ varType: 'const',
82
+ varName: '{ test : funky, foo = false }',
83
+ definitions: {
84
+ expectsDestructuring: true,
85
+ declaration: '{ test : funky, foo = false }',
86
+ destructuredParameters: [
87
+ { declaration: 'test : funky', name: 'test', alias: 'funky' },
88
+ {
89
+ defaultValue: 'false',
90
+ declaration: 'foo = false',
91
+ name: 'foo'
92
+ }
93
+ ],
94
+ name: '{ test : funky, foo = false }'
95
+ },
96
+ from: 'uvu',
97
+ isRequire: true,
98
+ isRelative: false,
99
+ isDynamic: false
100
+ },
101
+ {
102
+ line: "const assert = require('uvu/assert')",
103
+ varType: 'const',
104
+ varName: 'assert',
105
+ definitions: { declaration: 'assert', name: 'assert' },
106
+ from: 'uvu/assert',
107
+ isRequire: true,
108
+ isRelative: false,
109
+ isDynamic: false
110
+ },
111
+ {
112
+ line: "const { getRequires } = require('./get-imports')",
113
+ varType: 'const',
114
+ varName: '{ getRequires }',
115
+ definitions: {
116
+ expectsDestructuring: true,
117
+ declaration: '{ getRequires }',
118
+ destructuredParameters: [ { declaration: 'getRequires', name: 'getRequires' } ],
119
+ name: '{ getRequires }'
120
+ },
121
+ from: './get-imports',
122
+ isRequire: true,
123
+ isRelative: true,
124
+ isDynamic: false
125
+ }
126
+ ])
127
+ })
128
+
129
+ test('getRequires with dynamic requires', async () => {
130
+ const code = `
131
+ const moduleName = 'fs'
132
+ const dynamicRequire = require(moduleName)
133
+ const path = require('path')
134
+ const dynamicPath = require(path.join(__dirname, 'utils'))
135
+ const foo = require('foo-pkg')
136
+ require(\`./$\{folder}/file\`)
137
+ const { test : funky, foo = false } = require(moduleTwo)
138
+ `
139
+
140
+ const requiresFound = getDynamicRequires(code)
141
+ /*
142
+ console.log('requiresFound')
143
+ deepLog(requiresFound)
144
+ /** */
145
+ assert.equal(normalizeTestResult(requiresFound), [
146
+ {
147
+ line: 'const dynamicRequire = require(moduleName)',
148
+ varType: 'const',
149
+ varName: 'dynamicRequire',
150
+ definitions: { declaration: 'dynamicRequire', name: 'dynamicRequire' },
151
+ from: 'moduleName',
152
+ isRequire: true,
153
+ isRelative: false,
154
+ isDynamic: true
155
+ },
156
+ {
157
+ line: "const dynamicPath = require(path.join(__dirname, 'utils'))",
158
+ varType: 'const',
159
+ varName: 'dynamicPath',
160
+ definitions: { declaration: 'dynamicPath', name: 'dynamicPath' },
161
+ from: "path.join(__dirname, 'utils')",
162
+ isRequire: true,
163
+ isRelative: false,
164
+ isDynamic: true
165
+ },
166
+ {
167
+ line: 'require(`./${folder}/file`)',
168
+ varType: '',
169
+ varName: '',
170
+ definitions: null,
171
+ from: '`./${folder}/file`',
172
+ isRequire: true,
173
+ isRelative: false,
174
+ isDynamic: true
175
+ },
176
+ {
177
+ line: 'const { test : funky, foo = false } = require(moduleTwo)',
178
+ varType: 'const',
179
+ varName: '{ test : funky, foo = false }',
180
+ definitions: {
181
+ expectsDestructuring: true,
182
+ declaration: '{ test : funky, foo = false }',
183
+ destructuredParameters: [
184
+ { declaration: 'test : funky', name: 'test', alias: 'funky' },
185
+ {
186
+ defaultValue: 'false',
187
+ declaration: 'foo = false',
188
+ name: 'foo'
189
+ }
190
+ ],
191
+ name: '{ test : funky, foo = false }'
192
+ },
193
+ from: 'moduleTwo',
194
+ isRequire: true,
195
+ isRelative: false,
196
+ isDynamic: true
197
+ }
198
+ ])
199
+ })
200
+
201
+ test.run()