-
-
Notifications
You must be signed in to change notification settings - Fork 897
Expand file tree
/
Copy pathdiagnostics.rs
More file actions
1351 lines (1155 loc) · 44.7 KB
/
diagnostics.rs
File metadata and controls
1351 lines (1155 loc) · 44.7 KB
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::borrow::Cow;
use oxc_ast::ast::REGEXP_FLAGS_LIST;
use oxc_diagnostics::OxcDiagnostic;
use oxc_span::Span;
use crate::modifiers::{Modifier, ModifierFlags, ModifierKind};
trait DiagnosticExt {
fn with_allowed_modifier_help(self, allowed: Option<ModifierFlags>) -> Self;
}
impl DiagnosticExt for OxcDiagnostic {
fn with_allowed_modifier_help(self, allowed: Option<ModifierFlags>) -> Self {
if let Some(allowed) = allowed {
if allowed.is_empty() {
self.with_help("No modifiers are allowed here.")
} else if allowed.iter().count() == 1 {
self.with_help(format!("Only '{allowed}' modifier is allowed here."))
} else {
self.with_help(format!("Allowed modifiers are: {allowed}"))
}
} else {
self
}
}
}
#[inline]
fn ts_error<C, M>(code: C, message: M) -> OxcDiagnostic
where
C: Into<Cow<'static, str>>,
M: Into<Cow<'static, str>>,
{
OxcDiagnostic::error(message).with_error_code("TS", code)
}
#[cold]
pub fn redeclaration(x0: &str, declare_span: Span, redeclare_span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Identifier `{x0}` has already been declared")).with_labels([
declare_span.label(format!("`{x0}` has already been declared here")),
redeclare_span.label("It can not be redeclared here"),
])
}
#[cold]
pub fn overlong_source() -> OxcDiagnostic {
OxcDiagnostic::error("Source length exceeds 4 GiB limit")
}
#[cold]
pub fn file_appears_to_be_binary() -> OxcDiagnostic {
ts_error("1490", "File appears to be binary.")
}
#[cold]
pub fn flow(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Flow is not supported").with_label(span)
}
#[cold]
pub fn unexpected_token(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected token").with_label(span)
}
#[cold]
pub fn private_identifier_in_property_name(name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Private identifier '#{name}' is not allowed in property names"))
.with_label(span)
}
#[cold]
pub fn html_comment_in_module(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("HTML comments are not allowed in modules").with_label(span)
}
#[cold]
pub fn merge_conflict_marker(
start_span: Span,
middle_span: Option<Span>,
end_span: Option<Span>,
) -> OxcDiagnostic {
let mut diagnostic = OxcDiagnostic::error("Encountered diff marker")
.and_label(
start_span.primary_label(
"between this marker and `=======` is the code that we're merging into",
),
)
.with_help(
"Conflict markers indicate that a merge was started but could not be completed due to \
merge conflicts.\n\
To resolve a conflict, keep only the code you want and then delete the lines containing \
conflict markers.\n\
If you're having merge conflicts after pulling new code, the top section is the code you \
already had and the bottom section is the remote code.\n\
If you're in the middle of a rebase, the top section is the code being rebased onto and \
the bottom section is the code coming from the current commit being rebased.\n\
If you have nested conflicts, resolve the outermost conflict first.",
);
if let Some(middle) = middle_span {
diagnostic = diagnostic
.and_label(middle.label("between this marker and `>>>>>>>` is the incoming code"));
} else {
// Incomplete conflict - missing middle or end markers
diagnostic = diagnostic.with_help(
"This conflict marker appears to be incomplete (missing `=======` or `>>>>>>>`).\n\
Check if the conflict markers were accidentally modified or partially deleted.",
);
}
if let Some(end) = end_span {
diagnostic = diagnostic.and_label(end.label("this marker concludes the conflict region"));
}
diagnostic
}
#[cold]
pub fn jsx_in_non_jsx(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected JSX expression")
.with_label(span)
.with_help("JSX syntax is disabled and should be enabled via the parser options")
}
#[cold]
pub fn expect_token(x0: &str, x1: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Expected `{x0}` but found `{x1}`"))
.with_label(span.label(format!("`{x0}` expected")))
}
#[cold]
pub fn expect_closing(
expected_closing: &str,
actual: &str,
span: Span,
opening_span: Span,
) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Expected `{expected_closing}` but found `{actual}`")).with_labels(
[
span.primary_label(format!("`{expected_closing}` expected")),
opening_span.label("Opened here"),
],
)
}
#[cold]
pub fn expect_closing_or_separator(
expected_closing: &str,
expected_separator: &str,
actual: &str,
span: Span,
opening_span: Span,
) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"Expected `{expected_separator}` or `{expected_closing}` but found `{actual}`"
))
.with_labels([
span.primary_label(format!("`{expected_separator}` or `{expected_closing}` expected")),
opening_span.label("Opened here"),
])
}
#[cold]
pub fn expect_conditional_alternative(x: &str, span: Span, question_span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Expected `:` but found `{x}`")).with_labels([
span.primary_label("`:` expected"),
question_span.label("Conditional starts here"),
])
}
#[cold]
pub fn unexpected_trailing_comma(name: &'static str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("{name} may not have a trailing comma."))
.with_label(span)
.with_help("Remove the trailing comma here")
}
#[cold]
pub fn invalid_escape_sequence(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid escape sequence").with_label(span)
}
#[cold]
pub fn unicode_escape_sequence(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid Unicode escape sequence").with_label(span)
}
#[cold]
pub fn invalid_character(x0: char, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Invalid Character `{x0}`")).with_label(span1)
}
#[cold]
pub fn invalid_number_end(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid characters after number").with_label(span)
}
#[cold]
pub fn unterminated_multi_line_comment(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unterminated multiline comment").with_label(span)
}
#[cold]
pub fn unterminated_string(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unterminated string").with_label(span)
}
#[cold]
pub fn reg_exp_flag(x0: char, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Unexpected flag {x0} in regular expression literal"))
.with_label(span1)
.with_help(format!("The allowed flags are `{REGEXP_FLAGS_LIST}`"))
}
#[cold]
pub fn reg_exp_flag_twice(x0: char, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Flag {x0} is mentioned twice in regular expression literal"))
.with_label(span1)
.with_help("Remove the duplicated flag here")
}
#[cold]
pub fn unexpected_end(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected end of file").with_label(span)
}
#[cold]
pub fn unexpected_jsx_end(span: Span, a: char, b: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Unexpected token. Did you mean `{{'{a}'}}` or `&{b};`?"))
.with_label(span)
}
#[cold]
pub fn unterminated_reg_exp(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unterminated regular expression").with_label(span)
}
#[cold]
pub fn invalid_number(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Invalid Number {x0}")).with_label(span1)
}
#[cold]
pub fn escaped_keyword(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Keywords cannot contain escape characters").with_label(span)
}
#[cold]
pub fn auto_semicolon_insertion(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(
"Expected a semicolon or an implicit semicolon after a statement, but found none",
)
.with_help("Try inserting a semicolon here")
.with_label(span)
}
#[cold]
pub fn lineterminator_before_arrow(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Line terminator not permitted before arrow")
.with_label(span)
.with_help("Remove the line break before here")
}
#[cold]
pub fn invalid_destructuring_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Missing initializer in destructuring declaration")
.with_label(span)
.with_help("Add an initializer (e.g. ` = undefined`) here")
}
#[cold]
pub fn missing_initializer_in_const(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Missing initializer in const declaration")
.with_label(span)
.with_help("Add an initializer (e.g. ` = undefined`) here")
}
#[cold]
pub fn lexical_declaration_single_statement(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Lexical declaration cannot appear in a single-statement context")
.with_help("Wrap this declaration in a block statement")
.with_label(span)
}
#[cold]
pub fn async_function_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Async functions can only be declared at the top level or inside a block")
.with_label(span)
}
#[cold]
pub fn generator_function_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Generators can only be declared at the top level or inside a block")
.with_label(span)
}
#[cold]
pub fn await_expression(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(
"`await` is only allowed within async functions and at the top levels of modules",
)
.with_label(span)
.with_help("Either remove this `await` or add the `async` keyword to the enclosing function")
}
#[cold]
pub fn yield_expression(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A 'yield' expression is only allowed in a generator body.")
.with_label(span)
.with_help("Either remove this `yield` or change the enclosing function to a generator function (`function*`)")
}
#[cold]
pub fn class_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid class declaration")
.with_help("Classes can only be declared at top level or inside a block")
.with_label(span)
}
// 'extends' clause already seen. ts(1172)
#[cold]
pub fn extends_clause_already_seen(span: Span) -> OxcDiagnostic {
ts_error("1172", "'extends' clause already seen").with_label(span)
}
// 'extends' clause must precede 'implements' clause. ts(1173)
#[cold]
pub fn extends_clause_must_precede_implements(span: Span, implements_span: Span) -> OxcDiagnostic {
ts_error("1173", "'extends' clause must precede 'implements' clause")
.with_labels([
implements_span.label("'implements' clause found here"),
span.primary_label("'extends' clause found here"),
])
.with_help("Move the 'extends' clause before the 'implements' clause")
}
// Classes can only extend a single class. ts(1174)
#[cold]
pub fn classes_can_only_extend_single_class(span: Span) -> OxcDiagnostic {
ts_error("1174", "Classes can only extend a single class.")
.with_label(span)
.with_help("Remove the extra base class or use interfaces for multiple inheritance")
}
// 'implements' clause already seen. ts(1175)
#[cold]
pub fn implements_clause_already_seen(span: Span, seen_span: Span) -> OxcDiagnostic {
ts_error("1175", "'implements' clause already seen")
.with_labels([seen_span, span])
.with_help("Merge the two 'implements' clauses into one by a ','")
}
/// A class member cannot have the 'const' keyword. ts(1248)
#[cold]
pub fn const_class_member(span: Span) -> OxcDiagnostic {
ts_error("1248", "A class member cannot have the 'const' keyword.")
.with_help("Did you mean `readonly`?")
.with_label(span)
}
// A required element cannot follow an optional element. ts(1257)
#[cold]
pub fn required_element_cannot_follow_optional_element(
span: Span,
optional_span: Span,
) -> OxcDiagnostic {
ts_error("1257", "A required element cannot follow an optional element.").with_labels([
span.label("Required element here"),
optional_span.label("Optional element seen here"),
])
}
/// A rest element cannot follow another rest element. ts(1265)
#[cold]
pub fn rest_element_cannot_follow_another_rest_element(
seen_span: Span,
span: Span,
) -> OxcDiagnostic {
ts_error("1265", "A rest element cannot follow another rest element.")
.with_labels([span.label("Second rest element here"), seen_span.label("First seen here")])
}
/// An optional element cannot follow a rest element. ts(1266)
#[cold]
pub fn optional_element_cannot_follow_rest_element(span: Span, rest_span: Span) -> OxcDiagnostic {
ts_error("1266", "An optional element cannot follow a rest element.").with_labels([
span.label("Optional element here"),
rest_span.label("Rest element seen here"),
])
}
// A type-only import can specify a default import or named bindings, but not both. ts(1363)
#[cold]
pub fn type_only_import_default_and_named(specifier_span: Span) -> OxcDiagnostic {
ts_error(
"1363",
"A type-only import can specify a default import or named bindings, but not both.",
)
.with_label(specifier_span)
}
#[cold]
pub fn binding_rest_element_last(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A rest element must be last in a destructuring pattern").with_label(span)
}
#[cold]
pub fn rest_parameter_last(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A rest parameter must be last in a parameter list").with_label(span)
}
#[cold]
pub fn spread_last_element(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Spread must be last element").with_label(span)
}
#[cold]
pub fn rest_element_trailing_comma(span: Span) -> OxcDiagnostic {
unexpected_trailing_comma("A rest parameter or binding pattern", span)
}
#[cold]
pub fn invalid_binding_rest_element(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid rest element")
.with_help("Expected identifier in rest element")
.with_label(span)
}
#[cold]
pub fn a_rest_parameter_cannot_be_optional(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A rest parameter cannot be optional")
.with_label(span)
.with_help("Remove this `?`. The default value is an empty array")
}
#[cold]
pub fn invalid_assignment(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Cannot assign to this expression").with_label(span)
}
#[cold]
pub fn invalid_lhs_assignment(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(
"The left-hand side of an assignment expression must be a variable or a property access.",
)
.with_label(span)
}
#[cold]
pub fn new_optional_chain(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Optional chaining cannot appear in the callee of new expressions")
.with_label(span)
}
#[cold]
pub fn invalid_new_optional_chain(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid optional chain from new expression.").with_label(span)
}
#[cold]
pub fn decorator_optional(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Expression must be enclosed in parentheses to be used as a decorator.")
.with_label(span)
}
#[cold]
pub fn for_loop_async_of(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("The left-hand side of a `for...of` statement may not be `async`")
.with_label(span)
.with_help("Did you mean to use a for await...of statement?")
}
pub fn for_loop_let_reserved_word(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("The left-hand side of a `for...of` statement may not start with `let`")
.with_label(span)
}
#[cold]
pub fn for_await(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("await can only be used in conjunction with `for...of` statements")
.with_label(span)
.with_help("Did you mean to use a for...of statement?")
}
#[cold]
pub fn new_dynamic_import(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Cannot use new with dynamic import")
.with_label(span)
.with_help("Wrap this with parenthesis")
}
#[cold]
pub fn new_super(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("'new super()' is not allowed").with_label(span)
}
#[cold]
pub fn private_name_constructor(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Classes can't have an element named '#constructor'").with_label(span)
}
#[cold]
pub fn static_prototype(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Classes may not have a static property named 'prototype'")
.with_label(span)
}
#[cold]
pub fn constructor_getter_setter(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Constructor can't have get/set modifier").with_label(span)
}
#[cold]
pub fn constructor_async(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Constructor can't be an async method").with_label(span)
}
#[cold]
pub fn optional_accessor_property(span: Span) -> OxcDiagnostic {
ts_error("1276", "An 'accessor' property cannot be declared optional.")
.with_label(span)
.with_help("Remove this `?`")
}
#[cold]
pub fn constructor_accessor(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Classes may not have a field named 'constructor'").with_label(span)
}
#[cold]
pub fn optional_definite_property(span: Span) -> OxcDiagnostic {
// NOTE: could not find an error code when tsc parses this; its parser panics.
OxcDiagnostic::error("A property cannot be both optional and definite.")
.with_label(span)
.with_help("Remove either the `?` or the `!`")
}
#[cold]
pub fn identifier_async(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Cannot use `{x0}` as an identifier in an async context"))
.with_label(span1)
}
#[cold]
pub fn identifier_generator(x0: &str, span1: Span, looks_like_expression: bool) -> OxcDiagnostic {
let diagnostic =
OxcDiagnostic::error(format!("Cannot use `{x0}` as an identifier in a generator context"))
.with_label(span1);
if looks_like_expression {
diagnostic.with_help(format!(
"Wrap this in parentheses if you want to use a `{x0}` expression here"
))
} else {
diagnostic
}
}
#[cold]
pub fn identifier_expected(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Identifier expected.").with_label(span)
}
#[cold]
pub fn identifier_reserved_word(span: Span, reserved: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!(
"Identifier expected. '{reserved}' is a reserved word that cannot be used here."
))
.with_label(span)
}
#[cold]
pub fn constructor_generator(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Constructor can't be a generator").with_label(span)
}
#[cold]
pub fn declare_constructor(span: Span) -> OxcDiagnostic {
ts_error("1031", "'declare' modifier cannot appear on a constructor declaration.")
.with_label(span)
}
#[cold]
pub fn constructor_return_type(span: Span) -> OxcDiagnostic {
ts_error("1093", "Type annotation cannot appear on a constructor declaration.").with_label(span)
}
#[cold]
pub fn field_constructor(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Classes can't have a field named 'constructor'").with_label(span)
}
#[cold]
pub fn export_lone_surrogate(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("An export name cannot include a unicode lone surrogate").with_label(span)
}
#[cold]
pub fn export_named_string(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A string literal cannot be used as an exported binding without `from`")
.with_help(format!("Did you mean `export {{ {x0} as {x1} }} from 'some-module'`?"))
.with_label(span2)
}
#[cold]
pub fn export_reserved_word(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A reserved word cannot be used as an exported binding without `from`")
.with_help(format!("Did you mean `export {{ {x0} as {x1} }} from 'some-module'`?"))
.with_label(span2)
}
#[cold]
pub fn template_literal(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Bad escape sequence in untagged template literal").with_label(span)
}
#[cold]
pub fn empty_parenthesized_expression(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Empty parenthesized expression").with_label(span)
}
#[cold]
pub fn illegal_newline(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Illegal newline after {x0}")).with_labels([
span1.label(format!("{x0} starts here")),
span2.label("A newline is not expected here"),
])
}
#[cold]
pub fn optional_chain_tagged_template(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Tagged template expressions are not permitted in an optional chain")
.with_label(span)
}
#[cold]
pub fn ts_constructor_this_parameter(span: Span) -> OxcDiagnostic {
ts_error("2681", "A constructor cannot have a `this` parameter.").with_label(span)
}
#[cold]
pub fn ts_constructor_type_parameter(span: Span) -> OxcDiagnostic {
ts_error("1092", "Type parameters cannot appear on a constructor declaration")
.with_label(span)
.with_help("Instead, add type parameters to the class itself")
}
#[cold]
pub fn ts_arrow_function_this_parameter(span: Span) -> OxcDiagnostic {
ts_error("2730", "An arrow function cannot have a `this` parameter.")
.with_label(span)
.with_help("Arrow function does not bind `this` and inherits `this` from the outer scope")
}
#[cold]
pub fn ts_empty_type_parameter_list(span: Span) -> OxcDiagnostic {
ts_error("1098", "Type parameter list cannot be empty.").with_label(span)
}
#[cold]
pub fn ts_empty_type_argument_list(span: Span) -> OxcDiagnostic {
ts_error("1099", "Type argument list cannot be empty.").with_label(span)
}
#[cold]
pub fn ts_instantiation_expression_cannot_be_followed_by_property_access(
span: Span,
) -> OxcDiagnostic {
ts_error("1477", "An instantiation expression cannot be followed by a property access.")
.with_label(span)
}
#[cold]
pub fn ts_string_literal_expected(span: Span) -> OxcDiagnostic {
ts_error("1141", "String literal expected.").with_label(span)
}
#[cold]
pub fn unexpected_super(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("'super' can only be used with function calls or in property accesses")
.with_help("Replace with `super()` or `super.prop` or `super[prop]`")
.with_label(span)
}
#[cold]
pub fn expect_function_name(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Expected function name")
.with_help("Function name is required in function declaration or named export")
.with_label(span)
}
#[cold]
pub fn expect_catch_finally(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Missing catch or finally clause")
.with_label(span)
.with_help("Either unwrap this try block or add catch / finally clause")
}
#[cold]
pub fn v8_intrinsic_spread_elem(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("V8 runtime calls cannot have spread elements as arguments")
.with_label(span)
}
#[cold]
pub fn a_set_accessor_cannot_have_a_return_type_annotation(span: Span) -> OxcDiagnostic {
ts_error("1095", "A 'set' accessor cannot have a return type annotation.").with_label(span)
}
#[cold]
pub fn return_statement_only_in_function_body(span: Span) -> OxcDiagnostic {
ts_error("1108", "A 'return' statement can only be used within a function body.")
.with_label(span)
}
#[cold]
pub fn invalid_identifier_in_using_declaration(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Using declarations may not have binding patterns.").with_label(span)
}
#[cold]
pub fn await_using_declaration_not_allowed_in_for_in_statement(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(
"The left-hand side of a for...in statement cannot be an await using declaration.",
)
.with_label(span)
.with_help("Did you mean to use a for...of statement?")
}
#[cold]
pub fn using_declaration_not_allowed_in_for_in_statement(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error(
"The left-hand side of a for...in statement cannot be an using declaration.",
)
.with_label(span)
.with_help("Did you mean to use a for...of statement?")
}
#[cold]
pub fn using_declarations_must_be_initialized(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Using declarations must have an initializer.")
.with_label(span)
.with_help("Add an initializer (e.g. ` = undefined`) here")
}
#[cold]
pub fn using_declaration_cannot_be_exported(identifier: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Using declarations cannot be exported directly.")
.with_label(span)
.with_help(format!("Remove the `export` here and add `export {{ {identifier} }}` as a separate statement to export the declaration"))
}
#[cold]
pub fn using_declaration_not_allowed_in_switch_bare_case(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Using declaration cannot appear in the bare case statement.")
.with_label(span)
.with_help("Wrap this declaration in a block statement")
}
#[cold]
pub fn using_declarations_not_allowed_in_ambient_contexts(span: Span) -> OxcDiagnostic {
ts_error("1545", "'using' declarations are not allowed in ambient contexts.").with_label(span)
}
#[cold]
pub fn await_using_declarations_not_allowed_in_ambient_contexts(span: Span) -> OxcDiagnostic {
ts_error("1546", "'await using' declarations are not allowed in ambient contexts.")
.with_label(span)
}
#[cold]
pub fn jsx_element_no_match(span: Span, span1: Span, name: &str) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Expected corresponding JSX closing tag for '{name}'."))
.with_labels([
span1.primary_label(format!("Expected `</{name}>`")),
span.label("Opened here"),
])
}
#[cold]
pub fn jsx_fragment_no_match(opening_span: Span, closing_span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Expected corresponding closing tag for JSX fragment.").with_labels([
closing_span.primary_label("Expected `</>`"),
opening_span.label("Opened here"),
])
}
#[cold]
pub fn cover_initialized_name(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Invalid assignment in object literal")
.with_help("Did you mean to use a ':'? An '=' can only follow a property name when the containing object literal is part of a destructuring pattern.")
.with_label(span)
}
#[cold]
pub fn duplicate_export(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::error(format!("Duplicated export '{x0}'")).with_labels([
span1.label("Export has already been declared here"),
span2.label("It cannot be redeclared here"),
])
}
#[cold]
pub fn duplicate_default_export(spans: impl IntoIterator<Item = Span>) -> OxcDiagnostic {
ts_error("2528", "A module cannot have multiple default exports.").with_labels(spans)
}
#[cold]
pub fn import_meta(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("The only valid meta property for import is import.meta").with_label(span)
}
#[cold]
pub fn new_target(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("The only valid meta property for new is new.target").with_label(span)
}
#[cold]
pub fn private_in_private(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected right-hand side of private-in expression").with_label(span)
}
#[cold]
pub fn unexpected_private_identifier(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Unexpected private identifier").with_label(span)
}
#[cold]
pub fn import_arguments(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("Dynamic imports can only accept a module specifier and an optional set of attributes as arguments").with_label(span)
}
#[cold]
pub fn rest_element_property_name(span: Span) -> OxcDiagnostic {
ts_error("2566", "A rest element cannot have a property name.").with_label(span)
}
#[cold]
pub fn a_rest_element_cannot_have_an_initializer(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("A rest element cannot have an initializer.").with_label(span)
}
#[cold]
pub fn import_requires_a_specifier(span: Span) -> OxcDiagnostic {
OxcDiagnostic::error("import() requires a specifier.").with_label(span)
}
#[cold]
pub fn modifier_cannot_be_used_here(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
OxcDiagnostic::error(format!("'{}' modifier cannot be used here.", modifier.kind))
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
#[cold]
pub fn modifier_only_on_property_declaration_or_index_signature(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error(
"1024",
format!(
"'{}' modifier can only appear on a property declaration or index signature.",
modifier.kind
),
)
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
#[cold]
pub fn accessibility_modifier_already_seen(modifier: &Modifier) -> OxcDiagnostic {
ts_error("1028", "Accessibility modifier already seen.")
.with_label(modifier.span)
.with_help("Remove the duplicate modifier.")
}
#[cold]
pub fn modifier_must_precede_other_modifier(
modifier: &Modifier,
other_modifier: ModifierKind,
) -> OxcDiagnostic {
ts_error(
"1029",
format!("'{}' modifier must precede '{}' modifier.", modifier.kind, other_modifier),
)
.with_label(modifier.span)
}
#[cold]
pub fn modifier_already_seen(modifier: &Modifier) -> OxcDiagnostic {
ts_error("1030", format!("'{}' modifier already seen.", modifier.kind))
.with_label(modifier.span)
.with_help("Remove the duplicate modifier.")
}
pub fn cannot_appear_on_class_elements(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error(
"1031",
format!("'{}' modifier cannot appear on class elements of this kind.", modifier.kind),
)
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
pub fn cannot_appear_on_a_type_member(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error("1070", format!("'{}' modifier cannot appear on a type member.", modifier.kind))
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
#[cold]
pub fn cannot_appear_on_a_type_parameter(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error("1273", format!("'{}' modifier cannot be used on a type parameter.", modifier.kind))
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
#[cold]
pub fn a_parameter_cannot_have_question_mark_and_initializer(span: Span) -> OxcDiagnostic {
ts_error("1015", "A parameter cannot have a question mark and an initializer.").with_label(span)
}
#[cold]
pub fn can_only_appear_on_a_type_parameter_of_a_class_interface_or_type_alias(
modifier: ModifierKind,
span: Span,
) -> OxcDiagnostic {
ts_error("1274", format!("'{modifier}' modifier can only appear on a type parameter of a class, interface or type alias."))
.with_label(span)
}
pub fn cannot_appear_on_a_parameter(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error("1090", format!("'{}' modifier cannot appear on a parameter.", modifier.kind))
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
#[cold]
pub fn parameter_property_cannot_be_binding_pattern(span: Span) -> OxcDiagnostic {
ts_error("1187", "A parameter property may not be declared using a binding pattern.")
.with_label(span)
}
pub fn cannot_appear_on_an_index_signature(
modifier: &Modifier,
allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error("1071", format!("'{}' modifier cannot appear on an index signature.", modifier.kind))
.with_label(modifier.span)
.with_allowed_modifier_help(allowed)
}
pub fn accessor_modifier(modifier: &Modifier, allowed: Option<ModifierFlags>) -> OxcDiagnostic {
ts_error(
"1243",
format!("'accessor' modifier cannot be used with '{}' modifier.", modifier.kind),
)
.with_label(modifier.span)
.with_allowed_modifier_help(allowed.map(|a| a - ModifierFlags::ACCESSOR))
}
#[cold]
pub fn readonly_in_array_or_tuple_type(span: Span) -> OxcDiagnostic {
ts_error("1354", "'readonly' type modifier is only permitted on array and tuple literal types.")
.with_label(span)
}
#[cold]
pub fn accessibility_modifier_on_private_property(
modifier: &Modifier,
_allowed: Option<ModifierFlags>,
) -> OxcDiagnostic {
ts_error("18010", "An accessibility modifier cannot be used with a private identifier.")
.with_label(modifier.span)
.with_help("Private identifiers are enforced at runtime, while accessibility modifiers only affect type checking, so using both is redundant.")
}
#[cold]
pub fn type_modifier_on_named_type_import(span: Span) -> OxcDiagnostic {
ts_error("2206", "The 'type' modifier cannot be used on a named import when 'import type' is used on its import statement.")
.with_label(span)
.with_help("Remove this 'type' modifier")
}