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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use super::{Extractor, Import, ImportBinding, TokenKind};
impl Extractor<'_, '_> {
/// The module forms these languages write: `use a::b;`, `mod x;`,
/// `import "path"`, grouped Go imports, `import a.b.C;`, `using X;`.
// One pass keeps the shared statement boundaries beside the language
// spellings; those fail-open limits are what prevent run-on imports.
pub(super) fn import(&mut self, start: usize) -> Option<usize> {
let (index, forwarding) = self.import_head(start)?;
let word = self.text(index).to_owned();
if self.punct(index + 1, "(") {
return Some(self.grouped_import(index));
}
if word == "mod" {
return self.module_import(index);
}
let mut bindings = Vec::new();
let (cursor, specifier) = self.scan_import_specifier(index, &word, &mut bindings);
let specifier = specifier.trim_end_matches([':', '.']).to_owned();
if specifier.is_empty() {
return None;
}
Self::add_default_use_binding(&word, &specifier, &mut bindings);
self.record_import(index, cursor, specifier, bindings, forwarding);
Some(cursor)
}
fn import_head(&self, start: usize) -> Option<(usize, bool)> {
// `pub mod x;` is still a module dependency, so modifiers are stepped
// over here exactly as a declaration would step over them.
let mut index = start;
// `pub use x::y;` forwards another module's surface to importers of
// this one, exactly as `export ... from` does in JavaScript, so an
// importer reaches through it transitively.
let forwarding = self.rules.exported_keyword == Some(self.text(start));
while self.rules.modifiers.contains(&self.text(index)) {
index += 1;
// `pub(crate) use x;` carries a parenthesised visibility scope.
if self.punct(index, "(") {
while index < self.tokens.len() && !self.punct(index, ")") {
index += 1;
}
index += 1;
}
}
let word = self.text(index);
if !self.rules.imports.contains(&word) {
return None;
}
Some((index, forwarding))
}
fn grouped_import(&mut self, index: usize) -> usize {
// A parenthesised block lists several paths, as Go writes them.
let mut cursor = index + 2;
let limit = (index + 512).min(self.tokens.len());
while cursor < limit && !self.punct(cursor, ")") {
if self.kind(cursor) == Some(TokenKind::String) {
let specifier = self.text(cursor).trim_matches(['"', '`']).to_owned();
let bindings = self.package_import_bindings(cursor, &specifier);
let names = bindings
.iter()
.map(|binding| binding.local.clone())
.collect();
self.facts.imports.push(Import {
specifier,
span: self.span(cursor, cursor),
type_only: false,
reexport: false,
names,
bindings,
});
}
cursor += 1;
}
cursor + 1
}
fn module_import(&mut self, index: usize) -> Option<usize> {
// A `mod x { ... }` with a body defines the module here; only a
// declaration without a body pulls in another file.
let name = self.text(index + 1);
if name.is_empty() || !self.punct(index + 2, ";") {
return None;
}
self.facts.imports.push(Import {
specifier: format!("self::{name}"),
span: self.span(index, index + 1),
type_only: false,
reexport: false,
names: Vec::new(),
bindings: Vec::new(),
});
Some(index + 2)
}
fn scan_import_specifier(
&mut self,
index: usize,
word: &str,
bindings: &mut Vec<ImportBinding>,
) -> (usize, String) {
// A quoted path is the whole specifier; otherwise the specifier runs
// to the statement end.
let line = self.tokens[index].line;
let mut cursor = index + 1;
let mut specifier = String::new();
let limit = (index + 128).min(self.tokens.len());
while cursor < limit {
if self.kind(cursor) == Some(TokenKind::String) {
// A quoted path is the whole specifier, so anything read
// before it was a list of imported names, not a path.
specifier.clear();
specifier.push_str(self.text(cursor).trim_matches(['"', '`', '\'']));
if word == "import"
&& bindings.is_empty()
&& self.text(cursor.wrapping_sub(1)) != "from"
{
*bindings = self.package_import_bindings(cursor, &specifier);
}
cursor += 1;
break;
}
if self.punct(cursor, ";") {
break;
}
if self.punct(cursor, "{") {
// A trailing group narrows a path already read, as in
// `use a::{b, c}`. A leading one lists names being imported
// from a path still to come, as in `import {A} from "./x"`.
let mut close = cursor + 1;
while close < limit && !self.punct(close, "}") {
close += 1;
}
bindings.extend(self.named_import_bindings(cursor + 1, close));
if !specifier.is_empty() {
cursor = close.saturating_add(1);
break;
}
cursor = close.saturating_add(1);
continue;
}
if word == "use"
&& self.text(cursor) == "as"
&& self.kind(cursor + 1) == Some(TokenKind::Identifier)
{
let imported = specifier
.trim_end_matches(':')
.rsplit("::")
.next()
.unwrap_or(specifier.as_str())
.to_owned();
bindings.push(ImportBinding {
imported,
local: self.text(cursor + 1).to_owned(),
});
cursor += 2;
continue;
}
// A specifier ends with its line. Reading on would swallow what
// follows, which is how `#include <stdio.h>` consumed the function
// defined beneath it. While nothing has been read yet the scan may
// still cross a line, because `import {A} from` puts its path on
// the next one.
if self.tokens[cursor].line != line && !specifier.is_empty() {
break;
}
if matches!(
self.kind(cursor),
Some(TokenKind::Identifier | TokenKind::Punctuation)
) {
specifier.push_str(self.text(cursor));
}
cursor += 1;
}
(cursor, specifier)
}
fn add_default_use_binding(word: &str, specifier: &str, bindings: &mut Vec<ImportBinding>) {
if word == "use" && bindings.is_empty() {
let imported = specifier
.rsplit("::")
.next()
.unwrap_or(specifier)
.to_owned();
if imported != "*" {
bindings.push(ImportBinding {
local: imported.clone(),
imported,
});
}
}
}
fn record_import(
&mut self,
index: usize,
cursor: usize,
specifier: String,
bindings: Vec<ImportBinding>,
forwarding: bool,
) {
let names = bindings
.iter()
.map(|binding| binding.local.clone())
.collect();
self.facts.imports.push(Import {
specifier,
span: self.span(index, cursor.saturating_sub(1)),
type_only: false,
reexport: forwarding,
names,
bindings,
});
}
pub(super) fn named_import_bindings(&self, start: usize, end: usize) -> Vec<ImportBinding> {
let mut bindings = Vec::new();
let mut cursor = start;
while cursor < end {
if self.kind(cursor) != Some(TokenKind::Identifier)
|| matches!(self.text(cursor), "as" | "type")
|| self.text(cursor.wrapping_sub(1)) == "as"
{
cursor += 1;
continue;
}
let imported = self.text(cursor).to_owned();
let local = if self.text(cursor + 1) == "as"
&& self.kind(cursor + 2) == Some(TokenKind::Identifier)
{
self.text(cursor + 2).to_owned()
} else {
imported.clone()
};
bindings.push(ImportBinding { imported, local });
cursor += 1;
}
bindings
}
pub(super) fn package_import_bindings(
&self,
path: usize,
specifier: &str,
) -> Vec<ImportBinding> {
let imported = specifier
.trim_end_matches('/')
.rsplit('/')
.next()
.unwrap_or(specifier)
.to_owned();
let previous = path.wrapping_sub(1);
let same_line = self
.tokens
.get(previous)
.is_some_and(|token| token.line == self.tokens[path].line);
let local = if same_line
&& self.kind(previous) == Some(TokenKind::Identifier)
&& !matches!(self.text(previous), "import" | "from")
{
self.text(previous).to_owned()
} else if same_line && self.punct(previous, ".") {
"*".to_owned()
} else {
imported.clone()
};
if local == "_" {
Vec::new()
} else {
vec![ImportBinding { imported, local }]
}
}
}