-
-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathmodule.rs
464 lines (418 loc) · 17.9 KB
/
module.rs
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use oxc_allocator::{Box, Vec};
use oxc_ast::ast::*;
use oxc_diagnostics::Result;
use oxc_span::Span;
use super::{
function::FunctionKind,
list::{AssertEntries, ExportNamedSpecifiers, ImportSpecifierList},
};
use crate::{diagnostics, lexer::Kind, list::SeparatedList, Context, Parser};
impl<'a> Parser<'a> {
/// [Import Call](https://tc39.es/ecma262/#sec-import-calls)
/// `ImportCall` : import ( `AssignmentExpression` )
pub(crate) fn parse_import_expression(&mut self, span: Span) -> Result<Expression<'a>> {
self.bump_any(); // advance '('
let has_in = self.ctx.has_in();
self.ctx = self.ctx.and_in(true);
let expression = self.parse_assignment_expression_base()?;
let mut arguments = self.ast.new_vec();
if self.eat(Kind::Comma) && !self.at(Kind::RParen) {
arguments.push(self.parse_assignment_expression_base()?);
}
self.ctx = self.ctx.and_in(has_in);
self.ctx = self.ctx.and_in(has_in);
self.bump(Kind::Comma);
self.expect(Kind::RParen)?;
Ok(self.ast.import_expression(self.end_span(span), expression, arguments))
}
/// Section 16.2.2 Import Declaration
pub(crate) fn parse_import_declaration(&mut self) -> Result<Statement<'a>> {
let span = self.start_span();
self.bump_any(); // advance `import`
if self.ts_enabled()
&& ((self.cur_kind().is_binding_identifier() && self.peek_at(Kind::Eq))
|| (self.at(Kind::Type)
&& self.peek_kind().is_binding_identifier()
&& self.nth_at(2, Kind::Eq)))
{
let decl = self.parse_ts_import_equals_declaration(span, false)?;
return Ok(Statement::Declaration(decl));
}
// `import type ...`
let import_kind = self.parse_import_or_export_kind();
let specifiers = if self.at(Kind::Str) {
// import "source"
None
} else {
Some(self.parse_import_declaration_specifiers()?)
};
let source = self.parse_literal_string()?;
let assertions = self.parse_import_attributes()?;
self.asi()?;
let span = self.end_span(span);
let decl = ModuleDeclaration::ImportDeclaration(self.ast.import_declaration(
span,
specifiers,
source,
assertions,
import_kind,
));
Ok(self.ast.module_declaration(decl))
}
// Full Syntax: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#syntax>
fn parse_import_declaration_specifiers(
&mut self,
) -> Result<Vec<'a, ImportDeclarationSpecifier>> {
let mut specifiers = self.ast.new_vec();
// import defaultExport from "module-name";
if self.cur_kind().is_binding_identifier() {
specifiers.push(self.parse_import_default_specifier()?);
if self.eat(Kind::Comma) {
match self.cur_kind() {
// import defaultExport, * as name from "module-name";
Kind::Star => specifiers.push(self.parse_import_namespace_specifier()?),
// import defaultExport, { export1 [ , [...] ] } from "module-name";
Kind::LCurly => {
let mut import_specifiers = self.parse_import_specifiers()?;
specifiers.append(&mut import_specifiers);
}
_ => return Err(self.unexpected()),
}
}
// import * as name from "module-name";
} else if self.at(Kind::Star) {
specifiers.push(self.parse_import_namespace_specifier()?);
// import { export1 , export2 as alias2 , [...] } from "module-name";
} else if self.at(Kind::LCurly) {
let mut import_specifiers = self.parse_import_specifiers()?;
specifiers.append(&mut import_specifiers);
};
self.expect(Kind::From)?;
Ok(specifiers)
}
// import default from "module-name"
fn parse_import_default_specifier(&mut self) -> Result<ImportDeclarationSpecifier> {
let span = self.start_span();
let local = self.parse_binding_identifier()?;
Ok(ImportDeclarationSpecifier::ImportDefaultSpecifier(ImportDefaultSpecifier {
span: self.end_span(span),
local,
}))
}
// import * as name from "module-name"
fn parse_import_namespace_specifier(&mut self) -> Result<ImportDeclarationSpecifier> {
let span = self.start_span();
self.bump_any(); // advance `*`
self.expect(Kind::As)?;
let local = self.parse_binding_identifier()?;
Ok(ImportDeclarationSpecifier::ImportNamespaceSpecifier(ImportNamespaceSpecifier {
span: self.end_span(span),
local,
}))
}
// import { export1 , export2 as alias2 , [...] } from "module-name";
fn parse_import_specifiers(&mut self) -> Result<Vec<'a, ImportDeclarationSpecifier>> {
let ctx = self.ctx;
self.ctx = Context::default();
let specifiers = ImportSpecifierList::parse(self)?.import_specifiers;
self.ctx = ctx;
Ok(specifiers)
}
/// [Import assertion](https://tc39.es/proposal-import-assertions)
fn parse_import_attributes(&mut self) -> Result<Option<Vec<'a, ImportAttribute>>> {
if !self.at(Kind::Assert) || self.cur_token().is_on_new_line {
return Ok(None);
}
self.bump_any();
let ctx = self.ctx;
self.ctx = Context::default();
let entries = AssertEntries::parse(self)?.elements;
self.ctx = ctx;
Ok(Some(entries))
}
pub(crate) fn parse_ts_export_assignment_declaration(
&mut self,
) -> Result<Box<'a, TSExportAssignment<'a>>> {
let span = self.start_span();
self.expect(Kind::Eq)?;
let expression = self.parse_assignment_expression_base()?;
self.asi()?;
Ok(self.ast.alloc(TSExportAssignment { span: self.end_span(span), expression }))
}
pub(crate) fn parse_ts_export_namespace(
&mut self,
) -> Result<Box<'a, TSNamespaceExportDeclaration>> {
let span = self.start_span();
self.expect(Kind::As)?;
self.expect(Kind::Namespace)?;
let id = self.parse_identifier_name()?;
self.asi()?;
Ok(self.ast.alloc(TSNamespaceExportDeclaration { span: self.end_span(span), id }))
}
/// [Exports](https://tc39.es/ecma262/#sec-exports)
pub(crate) fn parse_export_declaration(&mut self) -> Result<Statement<'a>> {
let span = self.start_span();
self.bump_any(); // advance `export`
let decl = match self.cur_kind() {
Kind::Eq if self.ts_enabled() => self
.parse_ts_export_assignment_declaration()
.map(ModuleDeclaration::TSExportAssignment),
Kind::As if self.peek_at(Kind::Namespace) && self.ts_enabled() => self
.parse_ts_export_namespace()
.map(ModuleDeclaration::TSNamespaceExportDeclaration),
Kind::Default => self
.parse_export_default_declaration(span)
.map(ModuleDeclaration::ExportDefaultDeclaration),
Kind::Star => {
self.parse_export_all_declaration(span).map(ModuleDeclaration::ExportAllDeclaration)
}
Kind::LCurly => self
.parse_export_named_specifiers(span)
.map(ModuleDeclaration::ExportNamedDeclaration),
Kind::Type if self.peek_at(Kind::LCurly) && self.ts_enabled() => self
.parse_export_named_specifiers(span)
.map(ModuleDeclaration::ExportNamedDeclaration),
Kind::Type if self.peek_at(Kind::Star) => {
self.parse_export_all_declaration(span).map(ModuleDeclaration::ExportAllDeclaration)
}
_ => self
.parse_export_named_declaration(span)
.map(ModuleDeclaration::ExportNamedDeclaration),
}?;
Ok(self.ast.module_declaration(decl))
}
// export NamedExports ;
// NamedExports :
// { }
// { ExportsList }
// { ExportsList , }
// ExportsList :
// ExportSpecifier
// ExportsList , ExportSpecifier
// ExportSpecifier :
// ModuleExportName
// ModuleExportName as ModuleExportName
fn parse_export_named_specifiers(
&mut self,
span: Span,
) -> Result<Box<'a, ExportNamedDeclaration<'a>>> {
let export_kind = self.parse_import_or_export_kind();
let ctx = self.ctx;
self.ctx = Context::default();
let specifiers = ExportNamedSpecifiers::parse(self)?.elements;
self.ctx = ctx;
let source = if self.eat(Kind::From) && self.cur_kind().is_literal() {
let source = self.parse_literal_string()?;
Some(source)
} else {
None
};
// ExportDeclaration : export NamedExports ;
if source.is_none() {
for specifier in &specifiers {
match &specifier.local {
// It is a Syntax Error if ReferencedBindings of NamedExports contains any StringLiterals.
ModuleExportName::StringLiteral(literal) => {
self.error(diagnostics::ExportNamedString(
specifier.local.to_string(),
specifier.exported.to_string(),
literal.span,
));
}
// For each IdentifierName n in ReferencedBindings of NamedExports:
// It is a Syntax Error if StringValue of n is a ReservedWord or the StringValue of n
// is one of "implements", "interface", "let", "package", "private", "protected", "public", or "static".
ModuleExportName::Identifier(id) => {
let match_result = Kind::match_keyword(&id.name);
if match_result.is_reserved_keyword()
|| match_result.is_future_reserved_keyword()
{
self.error(diagnostics::ExportReservedWord(
specifier.local.to_string(),
specifier.exported.to_string(),
id.span,
));
}
}
}
}
}
self.asi()?;
let span = self.end_span(span);
Ok(self.ast.export_named_declaration(span, None, specifiers, source, export_kind))
}
// export Declaration
fn parse_export_named_declaration(
&mut self,
span: Span,
) -> Result<Box<'a, ExportNamedDeclaration<'a>>> {
let decl_span = self.start_span();
// For tc39/proposal-decorators
// For more information, please refer to <https://babeljs.io/docs/babel-plugin-proposal-decorators#decoratorsbeforeexport>
self.eat_decorators()?;
let modifiers = if self.ts_enabled() {
self.eat_modifiers_before_declaration().1
} else {
Modifiers::empty()
};
let declaration = self.parse_declaration(decl_span, modifiers)?;
let span = self.end_span(span);
Ok(self.ast.export_named_declaration(
span,
Some(declaration),
self.ast.new_vec(),
None,
ImportOrExportKind::Value,
))
}
// export default HoistableDeclaration[~Yield, +Await, +Default]
// export default ClassDeclaration[~Yield, +Await, +Default]
// export default AssignmentExpression[+In, ~Yield, +Await] ;
fn parse_export_default_declaration(
&mut self,
span: Span,
) -> Result<Box<'a, ExportDefaultDeclaration<'a>>> {
let exported = self.parse_keyword_identifier(Kind::Default);
let decl_span = self.start_span();
// For tc39/proposal-decorators
// For more information, please refer to <https://babeljs.io/docs/babel-plugin-proposal-decorators#decoratorsbeforeexport>
self.eat_decorators()?;
let declaration = match self.cur_kind() {
Kind::Class => self
.parse_class_declaration(decl_span, /* modifiers */ Modifiers::empty())
.map(ExportDefaultDeclarationKind::ClassDeclaration)?,
_ if self.at(Kind::Abstract) && self.peek_at(Kind::Class) && self.ts_enabled() => {
// eat the abstract modifier
let (_, modifiers) = self.eat_modifiers_before_declaration();
self.parse_class_declaration(decl_span, modifiers)
.map(ExportDefaultDeclarationKind::ClassDeclaration)?
}
_ if self.at(Kind::Interface)
&& !self.peek_token().is_on_new_line
&& self.ts_enabled() =>
{
self.parse_ts_interface_declaration(decl_span, Modifiers::empty()).map(|decl| {
match decl {
Declaration::TSInterfaceDeclaration(decl) => {
ExportDefaultDeclarationKind::TSInterfaceDeclaration(decl)
}
_ => unreachable!(),
}
})?
}
_ if self.at_function_with_async() => self
.parse_function_impl(FunctionKind::DefaultExport)
.map(ExportDefaultDeclarationKind::FunctionDeclaration)?,
_ => {
let decl = self
.parse_assignment_expression_base()
.map(ExportDefaultDeclarationKind::Expression)?;
self.asi()?;
decl
}
};
let exported = ModuleExportName::Identifier(exported);
let span = self.end_span(span);
Ok(self.ast.export_default_declaration(span, declaration, exported))
}
// export ExportFromClause FromClause ;
// ExportFromClause :
// *
// * as ModuleExportName
// NamedExports
fn parse_export_all_declaration(
&mut self,
span: Span,
) -> Result<Box<'a, ExportAllDeclaration<'a>>> {
let export_kind = self.parse_import_or_export_kind();
self.bump_any(); // bump `star`
let exported = self.eat(Kind::As).then(|| self.parse_module_export_name()).transpose()?;
self.expect(Kind::From)?;
let source = self.parse_literal_string()?;
let assertions = self.parse_import_attributes()?;
self.asi()?;
let span = self.end_span(span);
Ok(self.ast.export_all_declaration(span, exported, source, assertions, export_kind))
}
// ImportSpecifier :
// ImportedBinding
// ModuleExportName as ImportedBinding
pub(crate) fn parse_import_specifier(&mut self) -> Result<ImportSpecifier> {
let specifier_span = self.start_span();
let peek_kind = self.peek_kind();
let mut import_kind = ImportOrExportKind::Value;
if self.ts_enabled() && self.at(Kind::Type) {
if self.peek_at(Kind::As) {
if self.nth_at(2, Kind::As) {
if self.nth_kind(3).is_identifier_name() {
import_kind = ImportOrExportKind::Type;
}
} else if !self.nth_kind(2).is_identifier_name() {
import_kind = ImportOrExportKind::Type;
}
} else if peek_kind.is_identifier_name() {
import_kind = ImportOrExportKind::Type;
}
}
if import_kind == ImportOrExportKind::Type {
self.bump_any();
}
let (imported, local) = if self.peek_at(Kind::As) {
let imported = self.parse_module_export_name()?;
self.bump(Kind::As);
let local = self.parse_binding_identifier()?;
(imported, local)
} else {
let local = self.parse_binding_identifier()?;
let imported = IdentifierName { span: local.span, name: local.name.clone() };
(ModuleExportName::Identifier(imported), local)
};
Ok(ImportSpecifier { span: self.end_span(specifier_span), imported, local, import_kind })
}
// ModuleExportName :
// IdentifierName
// StringLiteral
pub(crate) fn parse_module_export_name(&mut self) -> Result<ModuleExportName> {
match self.cur_kind() {
Kind::Str => {
let literal = self.parse_literal_string()?;
// ModuleExportName : StringLiteral
// It is a Syntax Error if IsStringWellFormedUnicode(the SV of StringLiteral) is false.
if !literal.is_string_well_formed_unicode() {
self.error(diagnostics::ExportLoneSurrogate(literal.span));
};
Ok(ModuleExportName::StringLiteral(literal))
}
_ => Ok(ModuleExportName::Identifier(self.parse_identifier_name()?)),
}
}
fn parse_import_or_export_kind(&mut self) -> ImportOrExportKind {
if !self.ts_enabled() {
return ImportOrExportKind::Value;
}
// OK
// import type { bar } from 'foo';
// import type * as React from 'react';
// import type ident from 'foo';
// export type { bar } from 'foo';
// NO
// import type from 'foo';
// OK
// import type from from 'foo';
if !self.at(Kind::Type) {
return ImportOrExportKind::Value;
}
if matches!(self.peek_kind(), Kind::LCurly | Kind::Star) {
self.bump_any();
return ImportOrExportKind::Type;
}
if !self.peek_at(Kind::Ident) && !self.peek_kind().is_contextual_keyword() {
return ImportOrExportKind::Value;
}
if !self.peek_at(Kind::From) || self.nth_at(2, Kind::From) {
self.bump_any();
return ImportOrExportKind::Type;
}
ImportOrExportKind::Value
}
}