-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport-static.ts
117 lines (115 loc) · 3.69 KB
/
import-static.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
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
/**
* @file STATIC_IMPORT_REGEX
* @module import-regex/static
*/
/**
* Static `import` statement regex. Ignores matches in comments.
*
* @see https://regex101.com/r/wlYQUN
* @see https://github.com/tc39/proposal-import-assertions#import-statements
*
* @example
* import { STATIC_IMPORT_REGEX } from '@flex-development/import-regex'
* import { dedent } from 'ts-dedent'
*
* const code: string = dedent`
* import { defineBuildConfig } from '@flex-development/mkbuild'
* import type {
* Join,
* Nullable,
* Opaque,
* Simplify
* } from '@flex-development/tutils'
* import * as color from 'colorette'
* import consola from 'consola'
* import tsconfig from './tsconfig.json' assert { type: 'json' }
* `
*
* const print = (matches: IterableIterator<RegExpMatchArray>): void => {
* console.debug([...matches].map(match => omit(match, ['input'])))
* }
*
* print(code.matchAll(STATIC_IMPORT_REGEX))
* // [
* // {
* // '0': "import { defineBuildConfig } from '@flex-development/mkbuild'",
* // '1': undefined,
* // '2': '{ defineBuildConfig }',
* // '3': '@flex-development/mkbuild',
* // '4': undefined,
* // index: 0,
* // groups: [Object: null prototype] {
* // type: undefined,
* // imports: '{ defineBuildConfig }',
* // specifier: '@flex-development/mkbuild',
* // assertion: undefined
* // }
* // },
* // {
* // '0': 'import type {\n' +
* // ' Join,\n' +
* // ' Nullable,\n' +
* // ' Opaque,\n' +
* // ' Simplify\n' +
* // "} from '@flex-development/tutils'",
* // '1': 'type',
* // '2': '{\n Join,\n Nullable,\n Opaque,\n Simplify\n}',
* // '3': '@flex-development/tutils',
* // '4': undefined,
* // index: 62,
* // groups: [Object: null prototype] {
* // type: 'type',
* // imports: '{\n Join,\n Nullable,\n Opaque,\n Simplify\n}',
* // specifier: '@flex-development/tutils',
* // assertion: undefined
* // }
* // },
* // {
* // '0': "import * as color from 'colorette'",
* // '1': undefined,
* // '2': '* as color',
* // '3': 'colorette',
* // '4': undefined,
* // index: 151,
* // groups: [Object: null prototype] {
* // type: undefined,
* // imports: '* as color',
* // specifier: 'colorette',
* // assertion: undefined
* // }
* // },
* // {
* // '0': "import consola from 'consola'",
* // '1': undefined,
* // '2': 'consola',
* // '3': 'consola',
* // '4': undefined,
* // index: 186,
* // groups: [Object: null prototype] {
* // type: undefined,
* // imports: 'consola',
* // specifier: 'consola',
* // assertion: undefined
* // }
* // },
* // {
* // '0': "import tsc from './tsconfig.json' assert { type: 'json' }",
* // '1': undefined,
* // '2': 'tsc',
* // '3': './tsconfig.json',
* // '4': "{ type: 'json' }",
* // index: 216,
* // groups: [Object: null prototype] {
* // type: undefined,
* // imports: 'tsc',
* // specifier: './tsconfig.json',
* // assertion: "{ type: 'json' }"
* // }
* // }
* // ]
*
* @const {RegExp} STATIC_IMPORT_REGEX
*/
const STATIC_IMPORT_REGEX: RegExp =
/(?<=^|[\n;](?:[\t ]*(?:\w+ )?)?)import\s*(?<type>type(?=\s+))?(?:[\s"']*(?<imports>[\w\t\n\r "$'*,/{}-]+?)\s+from\s*)?["']\s*(?<specifier>(?:(?<='\s*)[^']*[^\s'](?=\s*'))|(?:(?<="\s*)[^"]*[^\s"](?=\s*")))\s*["'](?:\s+assert\s+(?<assertion>{[\w\t\n\r "':]+?}))?(?=[\s;]*)/g
export default STATIC_IMPORT_REGEX