-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-aggregate.ts
76 lines (74 loc) · 2.39 KB
/
export-aggregate.ts
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
/**
* @file EXPORT_AGGREGATE_REGEX
* @module export-regex/aggregate
*/
/**
* Aggregate `export` statement regex. Ignores matches in comments.
*
* @see https://regex101.com/r/JtvRUt
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#re-exporting_aggregating
*
* @example
* import { EXPORT_AGGREGATE_REGEX } from '@flex-development/export-regex'
* import { dedent } from 'ts-dedent'
*
* const code: string = dedent`
* export { defineBuildConfig, type BuildConfig } from "#src"
* export type {
* JsonObject,
* LiteralUnion,
* Nullable
* } from '@flex-development/tutils'
* export * as constants from "./constants"
* export * from './interfaces'
* `
*
* const print = (matches: IterableIterator<RegExpMatchArray>): void => {
* console.debug([...matches].map(match => omit(match, ['input'])))
* }
*
* print(code.matchAll(EXPORT_AGGREGATE_REGEX))
* // [
* // {
* // '0': 'export { defineBuildConfig, type BuildConfig } from "#src"',
* // '1': undefined,
* // '2': '{ defineBuildConfig, type BuildConfig }',
* // '3': '#src',
* // index: 0,
* // groups: [Object: null prototype] {
* // type: undefined,
* // exports: '{ defineBuildConfig, type BuildConfig }',
* // specifier: '#src'
* // }
* // },
* // {
* // '0': 'export * as constants from "./constants"',
* // '1': undefined,
* // '2': '* as constants',
* // '3': './constants',
* // index: 149,
* // groups: [Object: null prototype] {
* // type: undefined,
* // exports: '* as constants',
* // specifier: './constants'
* // }
* // },
* // {
* // '0': "export * from './interfaces'",
* // '1': undefined,
* // '2': '*',
* // '3': './interfaces',
* // index: 190,
* // groups: [Object: null prototype] {
* // type: undefined,
* // exports: '*',
* // specifier: './interfaces'
* // }
* // }
* // ]
*
* @const {RegExp} EXPORT_AGGREGATE_REGEX
*/
const EXPORT_AGGREGATE_REGEX: RegExp =
/(?<=^[\t ]*|[\n;](?:[\t ]*(?:\w+ )?)?)export(?:(?: *(?<type>type) *)|[\t\n ]*)(?<exports>(?:\*(?: +as +\S+)?)|\S+|(?:{[\w\t\n\r "$'*,./-]+?}))[\t ]*from[\t ]*["']\s*(?<specifier>(?:(?<=' *)[^']*[^\s'](?= *'))|(?:(?<=" *)[^"]*[^\s"](?= *"))) *["']/g
export default EXPORT_AGGREGATE_REGEX