-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export-list.ts
58 lines (56 loc) · 1.73 KB
/
export-list.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
/**
* @file EXPORT_LIST_REGEX
* @module export-regex/list
*/
/**
* List `export` statement regex. Ignores matches in comments.
*
* @see https://regex101.com/r/KQEDdZ
* @see https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/export#syntax
*
* @example
* import { EXPORT_LIST_REGEX } from '@flex-development/export-regex'
* import { dedent } from 'ts-dedent'
*
* const code: string = dedent`
* export { defineBuildConfig, type BuildConfig }
* export type {
* JsonObject,
* LiteralUnion,
* Nullable
* }
* `
*
* const print = (matches: IterableIterator<RegExpMatchArray>): void => {
* console.debug([...matches].map(match => omit(match, ['input'])))
* }
*
* print(code.matchAll(EXPORT_LIST_REGEX))
* // [
* // {
* // '0': 'export { defineBuildConfig, type BuildConfig }',
* // '1': undefined,
* // '2': '{ defineBuildConfig, type BuildConfig }',
* // index: 0,
* // groups: [Object: null prototype] {
* // type: undefined,
* // exports: '{ defineBuildConfig, type BuildConfig }'
* // }
* // },
* // {
* // '0': 'export type {\n JsonObject,\n LiteralUnion,\n Nullable\n}',
* // '1': 'type',
* // '2': '{\n JsonObject,\n LiteralUnion,\n Nullable\n}',
* // index: 82,
* // groups: [Object: null prototype] {
* // type: 'type',
* // exports: '{\n JsonObject,\n LiteralUnion,\n Nullable\n}'
* // }
* // }
* // ]
*
* @const {RegExp} EXPORT_LIST_REGEX
*/
const EXPORT_LIST_REGEX: RegExp =
/(?<=^[\t ]*|[\n;](?:[\t ]*(?:\w+ )?)?)export(?:(?:\s+(?<type>type)\s*)|\s*)(?<exports>{[\w\t\n\r "$'*,./-]*?})(?=[\t\n ;](?!from)|$)/g
export default EXPORT_LIST_REGEX