-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.d.ts
148 lines (131 loc) · 4.33 KB
/
index.d.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/** Options for parsing imports. */
export type Options = {
/**
* If set to a file path, then {@link Import.moduleSpecifier.resolved} of
* returned instances will be set to the result of calling
* `require.resolve(moduleSpecifier.value)` from the given file path.
* Otherwise, will be undefined.
*/
readonly resolveFrom?: string
}
/**
* A type representing what kind of module a specifier refers to.
*
* - 'unknown' if the module specifier is not a simple constant string literal
* - 'invalid' if the module specifier is the empty string
* - 'absolute' if the module specifier is an absolute file path
* - 'relative' if the module specifier is a relative file path
* - 'builtin' if the module specifier is the name of a builtin Node.js package
* - 'package' otherwise
*/
export type ModuleSpecifierType =
| 'invalid'
| 'absolute'
| 'relative'
| 'builtin'
| 'package'
| 'unknown'
/**
* A type representing an import in JavaScript code.
*
* `code.substring(startIndex, endIndex)` returns the full import statement or
* expression.
*/
export type Import = {
/** The start index of the import in the JavaScript (inclusive). */
startIndex: number
/** The end index of the import in the JavaScript (exclusive). */
endIndex: number
/** Whether the import is a dynamic import (e.g. `import('module')`). */
isDynamicImport: boolean
/**
* A type representing the code specifiying the module being imported.
*
* `code.substring(moduleSpecifier.startIndex, moduleSpecifier.endIndex)`
* returns the module specifier including quotes.
*/
moduleSpecifier: {
/**
* What kind of module the specifier refers to.
*
* 'unknown' when `moduleSpecifier.isConstant` is false.
*/
type: ModuleSpecifierType
/** The start index of the specifier in the JavaScript (inclusive). */
startIndex: number
/** The end index of the specifier in the JavaScript (exclusive). */
endIndex: number
/**
* True when the import is not a dynamic import (`isDynamicImport` is
* false), or when the import is a dynamic import where the specifier is a
* simple string literal (e.g. import('fs'), import("fs"), import(`fs`)).
*/
isConstant: boolean
/**
* The module specifier as it was written in the code. For non-constant
* dynamic imports it could be a complex expression.
*/
code: string
/**
* `code` without string literal quotes and unescaped if `isConstant` is
* true. Otherwise, it is undefined.
*/
value?: string
/** Set if the `resolveFrom` option is set and `value` is not undefined. */
resolved?: string
}
/**
* A type representing what is being imported from the module.
*
* Undefined if `isDynamicImport` is true.
*/
importClause?: {
/**
* The default import identifier or undefined if the import statement does
* not have a default import.
*/
default?: string
/**
* An array of objects representing the named imports of the import
* statement. It is empty if the import statement does not have any named
* imports. Each object in the array has a specifier field set to the
* imported identifier and a binding field set to the identifier for
* accessing the imported value.
* For example, `import { a, x as y } from 'something'` would have the
* following array:
* ```
* [{ specifier: 'a', binding: 'a' }, { specifier: 'x', binding: 'y' }]
* ```
*/
named: { specifier: string; binding: string }[]
/**
* The namespace import identifier or undefined if the import statement does
* not have a namespace import.
*/
namespace?: string
}
}
/**
* A promise that resolves once WASM has finished loading.
*
* Await this promise to be certain calling `parseImportsSync` is safe.
*/
export const wasmLoadPromise: Promise<void>
/**
* Returns a promise resolving to a lazy iterable/iterator that iterates over
* the imports in `code`.
*/
export const parseImports: (
code: string,
options?: Options,
) => Promise<Iterable<Import>>
/**
* Returns a lazy iterable/iterator that iterates over the imports in `code`.
*
* @throws if called before WASM has finished loading. Await `wasmLoadPromise`
* to be sure it has finished.
*/
export const parseImportsSync: (
code: string,
options?: Options,
) => Iterable<Import>