-
-
Notifications
You must be signed in to change notification settings - Fork 519
/
Copy pathlib.rs
648 lines (597 loc) · 27.1 KB
/
lib.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//! DTS Transformer / Transpiler
//!
//! References:
//! * <https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-rc/#isolated-declarations>
//! * <https://www.typescriptlang.org/tsconfig#isolatedDeclarations>
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformers/declarations.ts>
mod class;
mod declaration;
mod diagnostics;
mod r#enum;
mod formal_parameter_binding_pattern;
mod function;
mod inferrer;
mod literal;
mod module;
mod return_type;
mod scope;
mod signatures;
mod types;
use std::{cell::RefCell, mem};
use diagnostics::function_with_assigning_properties;
use oxc_allocator::{Allocator, CloneIn};
use oxc_ast::{ast::*, AstBuilder, Visit, NONE};
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::{Atom, GetSpan, SourceType, SPAN};
use rustc_hash::{FxHashMap, FxHashSet};
use crate::scope::ScopeTree;
#[derive(Debug, Default, Clone, Copy)]
pub struct IsolatedDeclarationsOptions {
/// Do not emit declarations for code that has an `@internal` annotation in its JSDoc comment.
/// This is an internal compiler option; use at your own risk, because the compiler does not
/// check that the result is valid.
///
/// Default: `false`
///
/// ## References
/// [TSConfig - `stripInternal`](https://www.typescriptlang.org/tsconfig/#stripInternal)
pub strip_internal: bool,
}
#[non_exhaustive]
pub struct IsolatedDeclarationsReturn<'a> {
pub program: Program<'a>,
pub errors: Vec<OxcDiagnostic>,
}
pub struct IsolatedDeclarations<'a> {
ast: AstBuilder<'a>,
// state
scope: ScopeTree<'a>,
errors: RefCell<Vec<OxcDiagnostic>>,
// options
strip_internal: bool,
/// Start position of `@internal` jsdoc annotations.
internal_annotations: FxHashSet<u32>,
}
impl<'a> IsolatedDeclarations<'a> {
pub fn new(allocator: &'a Allocator, options: IsolatedDeclarationsOptions) -> Self {
let strip_internal = options.strip_internal;
Self {
ast: AstBuilder::new(allocator),
strip_internal,
internal_annotations: FxHashSet::default(),
scope: ScopeTree::new(),
errors: RefCell::new(vec![]),
}
}
/// # Errors
///
/// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &Program<'a>) -> IsolatedDeclarationsReturn<'a> {
self.internal_annotations = self
.strip_internal
.then(|| Self::build_internal_annotations(program))
.unwrap_or_default();
let source_type = SourceType::d_ts();
let directives = self.ast.vec();
let stmts = self.transform_program(program);
let program = self.ast.program(
SPAN,
source_type,
program.source_text,
self.ast.vec_from_iter(program.comments.iter().copied()),
None,
directives,
stmts,
);
IsolatedDeclarationsReturn { program, errors: self.take_errors() }
}
fn take_errors(&self) -> Vec<OxcDiagnostic> {
mem::take(&mut self.errors.borrow_mut())
}
/// Add an Error
fn error(&self, error: OxcDiagnostic) {
self.errors.borrow_mut().push(error);
}
/// Build the lookup table for jsdoc `@internal`.
fn build_internal_annotations(program: &Program<'a>) -> FxHashSet<u32> {
let mut set = FxHashSet::default();
for comment in &program.comments {
let has_internal = comment.span.source_text(program.source_text).contains("@internal");
// Use the first jsdoc comment if there are multiple jsdoc comments for the same node.
if has_internal && !set.contains(&comment.attached_to) {
set.insert(comment.attached_to);
}
}
set
}
/// Check if the node has an `@internal` annotation.
fn has_internal_annotation(&self, span: Span) -> bool {
if !self.strip_internal {
return false;
}
self.internal_annotations.contains(&span.start)
}
}
impl<'a> IsolatedDeclarations<'a> {
fn transform_program(
&mut self,
program: &Program<'a>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
let has_import_or_export = program.body.iter().any(|stmt| {
matches!(
stmt,
Statement::ImportDeclaration(_)
| Statement::ExportAllDeclaration(_)
| Statement::ExportDefaultDeclaration(_)
| Statement::ExportNamedDeclaration(_)
)
});
if has_import_or_export {
self.transform_statements_on_demand(&program.body)
} else {
self.transform_program_without_module_declaration(&program.body)
}
}
fn transform_program_without_module_declaration(
&mut self,
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
self.report_error_for_expando_function(stmts);
let mut stmts =
self.ast.vec_from_iter(stmts.iter().filter(|stmt| {
stmt.is_declaration() && !self.has_internal_annotation(stmt.span())
}));
Self::remove_function_overloads_implementation(&mut stmts);
self.ast.vec_from_iter(stmts.iter().map(|stmt| {
if let Some(new_decl) = self.transform_declaration(stmt.to_declaration(), false) {
Statement::from(new_decl)
} else {
stmt.clone_in(self.ast.allocator)
}
}))
}
#[allow(clippy::missing_panics_doc)]
fn transform_statements_on_demand(
&mut self,
stmts: &oxc_allocator::Vec<'a, Statement<'a>>,
) -> oxc_allocator::Vec<'a, Statement<'a>> {
self.report_error_for_expando_function(stmts);
let mut stmts = self.ast.vec_from_iter(stmts.iter().filter(|stmt| {
(stmt.is_declaration() || stmt.is_module_declaration())
&& !self.has_internal_annotation(stmt.span())
}));
Self::remove_function_overloads_implementation(&mut stmts);
// https://github.com/microsoft/TypeScript/pull/58912
let mut need_empty_export_marker = true;
// Use span to identify transformed nodes
let mut transformed_spans: FxHashSet<Span> = FxHashSet::default();
let mut transformed_stmts: FxHashMap<Span, Statement<'a>> = FxHashMap::default();
let mut transformed_variable_declarator: FxHashMap<Span, VariableDeclarator<'a>> =
FxHashMap::default();
let mut export_default_var = None;
// 1. Collect all declarations, module declarations
// 2. Transform export declarations
// 3. Collect all bindings / reference from module declarations
// 4. Collect transformed indexes
for stmt in &stmts {
match stmt {
match_declaration!(Statement) => {
if let Statement::TSModuleDeclaration(decl) = stmt {
// `declare global { ... }` or `declare module "foo" { ... }`
// We need to emit it anyway
let is_global = decl.kind.is_global();
if is_global || decl.id.is_string_literal() {
transformed_spans.insert(decl.span);
let mut decl = decl.clone_in(self.ast.allocator);
// Remove export keyword from all statements in `declare module "xxx" { ... }`
if !is_global {
if let Some(body) =
decl.body.as_mut().and_then(|body| body.as_module_block_mut())
{
self.strip_export_keyword(&mut body.body);
}
}
// We need to visit the module declaration to collect all references
self.scope.visit_ts_module_declaration(decl.as_ref());
transformed_stmts.insert(
decl.span,
Statement::from(Declaration::TSModuleDeclaration(decl)),
);
}
}
}
match_module_declaration!(Statement) => {
match stmt.to_module_declaration() {
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
transformed_spans.insert(decl.span);
if let Some((var_decl, new_decl)) =
self.transform_export_default_declaration(decl)
{
if let Some(var_decl) = var_decl {
self.scope.visit_variable_declaration(&var_decl);
export_default_var = Some(Statement::VariableDeclaration(
self.ast.alloc(var_decl),
));
}
self.scope.visit_export_default_declaration(&new_decl);
transformed_stmts.insert(
decl.span,
Statement::from(ModuleDeclaration::ExportDefaultDeclaration(
self.ast.alloc(new_decl),
)),
);
} else {
self.scope.visit_export_default_declaration(decl);
}
need_empty_export_marker = false;
}
ModuleDeclaration::ExportNamedDeclaration(decl) => {
transformed_spans.insert(decl.span);
if let Some(new_decl) = self.transform_export_named_declaration(decl) {
self.scope.visit_export_named_declaration(&new_decl);
transformed_stmts.insert(
decl.span,
Statement::from(ModuleDeclaration::ExportNamedDeclaration(
self.ast.alloc(new_decl),
)),
);
} else if decl.declaration.is_none() {
need_empty_export_marker = false;
self.scope.visit_export_named_declaration(decl);
}
}
ModuleDeclaration::ImportDeclaration(_) => {
// We must transform this in the end, because we need to know all references
}
module_declaration => {
transformed_spans.insert(module_declaration.span());
self.scope.visit_module_declaration(module_declaration);
}
}
}
_ => {}
}
}
let last_transformed_len = transformed_spans.len() + transformed_variable_declarator.len();
// 5. Transform statements until no more transformation can be done
loop {
let cur_transformed_len =
transformed_spans.len() + transformed_variable_declarator.len();
for stmt in &stmts {
if transformed_spans.contains(&stmt.span()) {
continue;
}
let Some(decl) = stmt.as_declaration() else { continue };
// Skip transformed declarations
if transformed_spans.contains(&decl.span()) {
continue;
}
if let Declaration::VariableDeclaration(declaration) = decl {
let mut all_declarator_has_transformed = true;
for declarator in &declaration.declarations {
if transformed_spans.contains(&declarator.span) {
continue;
}
if let Some(new_declarator) =
self.transform_variable_declarator(declarator, true)
{
self.scope.visit_variable_declarator(&new_declarator);
transformed_variable_declarator.insert(declarator.span, new_declarator);
} else {
all_declarator_has_transformed = false;
}
}
if all_declarator_has_transformed {
let declarations = self.ast.vec_from_iter(
declaration.declarations.iter().map(|declarator| {
transformed_variable_declarator.remove(&declarator.span).unwrap()
}),
);
let decl = self.ast.variable_declaration(
declaration.span,
declaration.kind,
declarations,
self.is_declare(),
);
transformed_stmts.insert(
declaration.span,
Statement::VariableDeclaration(self.ast.alloc(decl)),
);
transformed_spans.insert(declaration.span);
}
} else if let Some(new_decl) = self.transform_declaration(decl, true) {
self.scope.visit_declaration(&new_decl);
transformed_spans.insert(new_decl.span());
transformed_stmts.insert(new_decl.span(), Statement::from(new_decl));
}
}
// Use transformed_spans to weather we need to continue
if cur_transformed_len
== transformed_spans.len() + transformed_variable_declarator.len()
{
break;
}
}
// If any declaration is transformed in previous step, we don't need to add empty export marker
if last_transformed_len != 0 && last_transformed_len == transformed_spans.len() {
need_empty_export_marker = false;
}
// 6. Transform variable/using declarations, import statements, remove unused imports
let mut new_stm =
self.ast.vec_with_capacity(stmts.len() + usize::from(export_default_var.is_some()));
stmts.iter().for_each(|stmt| {
if transformed_spans.contains(&stmt.span()) {
let new_stmt = transformed_stmts
.remove(&stmt.span())
.unwrap_or_else(|| stmt.clone_in(self.ast.allocator));
if matches!(new_stmt, Statement::ExportDefaultDeclaration(_)) {
if let Some(export_default_var) = export_default_var.take() {
new_stm.push(export_default_var);
}
}
new_stm.push(new_stmt);
return;
}
match stmt {
Statement::ImportDeclaration(decl) => {
// We must transform this in the end, because we need to know all references
if decl.specifiers.is_none() {
new_stm.push(stmt.clone_in(self.ast.allocator));
} else if let Some(new_decl) = self.transform_import_declaration(decl) {
new_stm.push(Statement::ImportDeclaration(new_decl));
}
}
Statement::VariableDeclaration(decl) => {
if decl.declarations.len() > 1 {
// Remove unreferenced declarations
let declarations = self.ast.vec_from_iter(
decl.declarations.iter().filter_map(|declarator| {
transformed_variable_declarator.remove(&declarator.span)
}),
);
new_stm.push(Statement::VariableDeclaration(
self.ast.alloc_variable_declaration(
decl.span,
decl.kind,
declarations,
self.is_declare(),
),
));
}
}
_ => {}
}
});
if need_empty_export_marker {
let specifiers = self.ast.vec();
let kind = ImportOrExportKind::Value;
let empty_export =
self.ast.alloc_export_named_declaration(SPAN, None, specifiers, None, kind, NONE);
new_stm.push(Statement::from(ModuleDeclaration::ExportNamedDeclaration(empty_export)));
} else if self.scope.is_ts_module_block() {
// If we are in a module block and we don't need to add `export {}`, in that case we need to remove `export` keyword from all ExportNamedDeclaration
// <https://github.com/microsoft/TypeScript/blob/a709f9899c2a544b6de65a0f2623ecbbe1394eab/src/compiler/transformers/declarations.ts#L1556-L1563>
self.strip_export_keyword(&mut new_stm);
}
new_stm
}
fn remove_function_overloads_implementation(
stmts: &mut oxc_allocator::Vec<'a, &Statement<'a>>,
) {
let mut last_function_name: Option<Atom<'a>> = None;
let mut is_export_default_function_overloads = false;
stmts.retain(move |stmt| match stmt {
Statement::FunctionDeclaration(ref func) => {
let name = &func
.id
.as_ref()
.unwrap_or_else(|| {
unreachable!(
"Only export default function declaration is allowed to have no name"
)
})
.name;
if func.body.is_some() {
if last_function_name.as_ref().is_some_and(|last_name| last_name == name) {
return false;
}
} else {
last_function_name = Some(name.clone());
}
true
}
Statement::ExportNamedDeclaration(ref decl) => {
if let Some(Declaration::FunctionDeclaration(ref func)) = decl.declaration {
let name = &func
.id
.as_ref()
.unwrap_or_else(|| {
unreachable!(
"Only export default function declaration is allowed to have no name"
)
})
.name;
if func.body.is_some() {
if last_function_name.as_ref().is_some_and(|last_name| last_name == name) {
return false;
}
} else {
last_function_name = Some(name.clone());
}
true
} else {
true
}
}
Statement::ExportDefaultDeclaration(ref decl) => {
if let ExportDefaultDeclarationKind::FunctionDeclaration(ref func) =
decl.declaration
{
if is_export_default_function_overloads && func.body.is_some() {
is_export_default_function_overloads = false;
return false;
}
is_export_default_function_overloads = true;
true
} else {
is_export_default_function_overloads = false;
true
}
}
_ => true,
});
}
fn get_assignable_properties_for_namespaces(
stmts: &'a oxc_allocator::Vec<'a, Statement<'a>>,
) -> FxHashMap<&'a str, FxHashSet<Atom>> {
let mut assignable_properties_for_namespace = FxHashMap::<&str, FxHashSet<Atom>>::default();
for stmt in stmts {
let decl = match stmt {
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::TSModuleDeclaration(decl)) = &decl.declaration {
decl
} else {
continue;
}
}
Statement::TSModuleDeclaration(decl) => decl,
_ => continue,
};
if decl.kind != TSModuleDeclarationKind::Namespace {
continue;
}
let TSModuleDeclarationName::Identifier(ident) = &decl.id else { continue };
let Some(TSModuleDeclarationBody::TSModuleBlock(block)) = &decl.body else {
continue;
};
for stmt in &block.body {
let Statement::ExportNamedDeclaration(decl) = stmt else { continue };
match &decl.declaration {
Some(Declaration::VariableDeclaration(var)) => {
for declarator in &var.declarations {
if let Some(name) = declarator.id.get_identifier() {
assignable_properties_for_namespace
.entry(&ident.name)
.or_default()
.insert(name);
}
}
}
Some(Declaration::FunctionDeclaration(func)) => {
if let Some(name) = func.name() {
assignable_properties_for_namespace
.entry(&ident.name)
.or_default()
.insert(name);
}
}
Some(Declaration::ClassDeclaration(cls)) => {
if let Some(id) = cls.id.as_ref() {
assignable_properties_for_namespace
.entry(&ident.name)
.or_default()
.insert(id.name.clone());
}
}
Some(Declaration::TSEnumDeclaration(decl)) => {
assignable_properties_for_namespace
.entry(&ident.name)
.or_default()
.insert(decl.id.name.clone());
}
_ => {}
}
}
}
assignable_properties_for_namespace
}
fn report_error_for_expando_function(&self, stmts: &oxc_allocator::Vec<'a, Statement<'a>>) {
let assignable_properties_for_namespace =
IsolatedDeclarations::get_assignable_properties_for_namespaces(stmts);
let mut can_expando_function_names = FxHashSet::default();
for stmt in stmts {
match stmt {
Statement::ExportNamedDeclaration(decl) => match decl.declaration.as_ref() {
Some(Declaration::FunctionDeclaration(func)) => {
if func.body.is_some() {
if let Some(id) = func.id.as_ref() {
can_expando_function_names.insert(id.name.clone());
}
}
}
Some(Declaration::VariableDeclaration(decl)) => {
for declarator in &decl.declarations {
if declarator.id.type_annotation.is_none()
&& declarator.init.as_ref().is_some_and(Expression::is_function)
{
if let Some(name) = declarator.id.get_identifier() {
can_expando_function_names.insert(name.clone());
}
}
}
}
_ => (),
},
Statement::ExportDefaultDeclaration(decl) => {
if let ExportDefaultDeclarationKind::FunctionDeclaration(func) =
&decl.declaration
{
if func.body.is_some() {
if let Some(name) = func.name() {
can_expando_function_names.insert(name);
}
}
}
}
Statement::FunctionDeclaration(func) => {
if func.body.is_some() {
if let Some(name) = func.name() {
if self.scope.has_reference(&name) {
can_expando_function_names.insert(name);
}
}
}
}
Statement::VariableDeclaration(decl) => {
for declarator in &decl.declarations {
if declarator.id.type_annotation.is_none()
&& declarator.init.as_ref().is_some_and(Expression::is_function)
{
if let Some(name) = declarator.id.get_identifier() {
if self.scope.has_reference(&name) {
can_expando_function_names.insert(name.clone());
}
}
}
}
}
Statement::ExpressionStatement(stmt) => {
if let Expression::AssignmentExpression(assignment) = &stmt.expression {
if let AssignmentTarget::StaticMemberExpression(static_member_expr) =
&assignment.left
{
if let Expression::Identifier(ident) = &static_member_expr.object {
if can_expando_function_names.contains(&ident.name)
&& !assignable_properties_for_namespace
.get(&ident.name.as_str())
.map_or(false, |properties| {
properties.contains(&static_member_expr.property.name)
})
{
self.error(function_with_assigning_properties(
static_member_expr.span,
));
}
}
}
}
}
_ => {}
}
}
}
fn is_declare(&self) -> bool {
// If we are in a module block, we don't need to add declare
!self.scope.is_ts_module_block()
}
}