-
Notifications
You must be signed in to change notification settings - Fork 31
/
analyze.ts
132 lines (110 loc) · 4.28 KB
/
analyze.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { matchAll } from './_utils'
export interface ESMImport {
type: 'static' | 'dynamic'
code: string
start: number
end: number
}
export interface StaticImport extends ESMImport {
type: 'static'
imports: string
specifier: string
}
export interface ParsedStaticImport extends StaticImport {
defaultImport?: string
namespacedImport?: string
namedImports?: { [name: string]: string }
}
export interface DynamicImport extends ESMImport {
type: 'dynamic'
expression: string
}
export interface ESMExport {
_type?: 'declaration' | 'named' | 'default' | 'star',
type: 'declaration' | 'named' | 'default' | 'star',
code: string
start: number
end: number
name?: string
names: string[]
specifier?: string
}
export interface DeclarationExport extends ESMExport {
type: 'declaration'
declaration: string
name: string
}
export interface NamedExport extends ESMExport {
type: 'named'
exports: string
names: string[]
specifier?: string
}
export interface DefaultExport extends ESMExport {
type: 'default'
}
export const ESM_STATIC_IMPORT_RE = /(?<=\s*|^|;)import\s*(["'\s]*(?<imports>[\w*${}\n\r\t, /]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][\s;]*/gm
export const DYNAMIC_IMPORT_RE = /import\s*\((?<expression>(?:[^)(]+|\((?:[^)(]+|\([^)(]*\))*\))*)\)/gm
export const EXPORT_DECAL_RE = /\bexport\s+(?<declaration>(async function|function|let|const|var|class))\s+(?<name>[\w$_]+)/g
const EXPORT_NAMED_RE = /\bexport\s+{(?<exports>[^}]+)}(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_STAR_RE = /\bexport\s*(\*)\s*(\s*from\s*["']\s*(?<specifier>(?<="\s*)[^"]*[^"\s](?=\s*")|(?<='\s*)[^']*[^'\s](?=\s*'))\s*["'][^\n]*)?/g
const EXPORT_DEFAULT_RE = /\bexport\s+default\s+/g
export function findStaticImports (code: string): StaticImport[] {
return matchAll(ESM_STATIC_IMPORT_RE, code, { type: 'static' })
}
export function findDynamicImports (code: string): DynamicImport[] {
return matchAll(DYNAMIC_IMPORT_RE, code, { type: 'dynamic' })
}
export function parseStaticImport (matched: StaticImport): ParsedStaticImport {
const cleanedImports = (matched.imports || '')
.replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, '')
.replace(/\s+/g, ' ')
const namedImports = {}
for (const namedImport of cleanedImports.match(/\{([^}]*)\}/)?.[1]?.split(',') || []) {
const [, source = namedImport.trim(), importName = source] = namedImport.match(/^\s*([^\s]*) as ([^\s]*)\s*$/) || []
if (source) {
namedImports[source] = importName
}
}
const topLevelImports = cleanedImports.replace(/\{([^}]*)\}/, '')
const namespacedImport = topLevelImports.match(/\* as \s*([^\s]*)/)?.[1]
const defaultImport = topLevelImports.split(',').find(i => !i.match(/[*{}]/))?.trim() || undefined
return {
...matched,
defaultImport,
namespacedImport,
namedImports
} as ParsedStaticImport
}
export function findExports (code: string): ESMExport[] {
// Find declarations like export const foo = 'bar'
const declaredExports = matchAll(EXPORT_DECAL_RE, code, { type: 'declaration' })
// Find named exports
const namedExports = matchAll(EXPORT_NAMED_RE, code, { type: 'named' })
for (const namedExport of namedExports) {
namedExport.names = namedExport.exports.split(/\s*,\s*/g).map(name => name.replace(/^.*?\sas\s/, '').trim())
}
// Find export default
const defaultExport = matchAll(EXPORT_DEFAULT_RE, code, { type: 'default', name: 'default' })
// Find export star
const starExports = matchAll(EXPORT_STAR_RE, code, { type: 'star' })
// Merge and normalize exports
const exports = [].concat(declaredExports, namedExports, defaultExport, starExports)
for (const exp of exports) {
if (!exp.name && exp.names && exp.names.length === 1) {
exp.name = exp.names[0]
}
if (exp.name === 'default' && exp.type !== 'default') {
exp._type = exp.type
exp.type = 'default'
}
if (!exp.names && exp.name) {
exp.names = [exp.name]
}
}
return exports.filter((exp, index, exports) => {
// Prevent multiple exports of same function, only keep latest iteration of signatures
const nextExport = exports[index + 1]
return !nextExport || exp.type !== nextExport.type || exp.name !== nextExport.name
})
}