-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathmain.rs
758 lines (680 loc) · 29.3 KB
/
main.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
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
#![allow(clippy::print_stdout, clippy::print_stderr)]
use std::{
borrow::Cow,
collections::HashMap,
fmt::{self, Display, Formatter},
};
use convert_case::{Case, Casing};
use oxc_allocator::Allocator;
use oxc_ast::{
ast::{
Argument, ArrayExpressionElement, CallExpression, ExportDefaultDeclarationKind, Expression,
ExpressionStatement, ObjectExpression, ObjectProperty, ObjectPropertyKind, Program,
PropertyKey, Statement, StaticMemberExpression, StringLiteral, TaggedTemplateExpression,
TemplateLiteral,
},
Visit,
};
use oxc_parser::Parser;
use oxc_span::{GetSpan, SourceType, Span};
use serde::Serialize;
use ureq::Response;
mod json;
mod template;
const ESLINT_TEST_PATH: &str =
"https://raw.githubusercontent.com/eslint/eslint/main/tests/lib/rules";
const JEST_TEST_PATH: &str =
"https://raw.githubusercontent.com/jest-community/eslint-plugin-jest/main/src/rules/__tests__";
const TYPESCRIPT_ESLINT_TEST_PATH: &str = "https://raw.githubusercontent.com/typescript-eslint/typescript-eslint/main/packages/eslint-plugin/tests/rules";
const UNICORN_TEST_PATH: &str =
"https://raw.githubusercontent.com/sindresorhus/eslint-plugin-unicorn/main/test";
const REACT_TEST_PATH: &str =
"https://raw.githubusercontent.com/jsx-eslint/eslint-plugin-react/master/tests/lib/rules";
const JSX_A11Y_TEST_PATH: &str =
"https://raw.githubusercontent.com/jsx-eslint/eslint-plugin-jsx-a11y/main/__tests__/src/rules";
const NEXT_JS_TEST_PATH: &str =
"https://raw.githubusercontent.com/vercel/next.js/canary/test/unit/eslint-plugin-next";
const JSDOC_TEST_PATH: &str =
"https://raw.githubusercontent.com/gajus/eslint-plugin-jsdoc/main/test/rules/assertions";
const REACT_PERF_TEST_PATH: &str =
"https://raw.githubusercontent.com/cvazac/eslint-plugin-react-perf/main/tests/lib/rules";
const NODE_TEST_PATH: &str =
"https://raw.githubusercontent.com/eslint-community/eslint-plugin-n/master/tests/lib/rules";
const TREE_SHAKING_PATH: &str =
"https://raw.githubusercontent.com/lukastaegert/eslint-plugin-tree-shaking/master/src/rules";
const PROMISE_TEST_PATH: &str =
"https://raw.githubusercontent.com/eslint-community/eslint-plugin-promise/main/__tests__";
const VITEST_TEST_PATH: &str =
"https://raw.githubusercontent.com/veritem/eslint-plugin-vitest/main/tests";
struct TestCase {
source_text: String,
code: Option<String>,
output: Option<String>,
group_comment: Option<String>,
config: Option<String>,
settings: Option<String>,
filename: Option<String>,
language_options: Option<String>,
}
impl TestCase {
fn new(source_text: &str, arg: &Expression<'_>) -> Self {
let mut test_case = Self {
source_text: source_text.to_string(),
code: None,
output: None,
config: None,
settings: None,
group_comment: None,
filename: None,
language_options: None,
};
test_case.visit_expression(arg);
test_case
}
fn with_group_comment(mut self, comment: String) -> Self {
self.group_comment = Some(comment);
self
}
fn code(&self, need_config: bool, need_settings: bool, need_filename: bool) -> String {
self.code
.as_ref()
.map(|code| {
let code_str = format_code_snippet(code);
let config = self.config.as_ref().map_or_else(
|| "None".to_string(),
|config| format!("Some(serde_json::json!({config}))"),
);
let settings = self.settings.as_ref().map_or_else(
|| "None".to_string(),
|settings| format!(r#"Some(serde_json::json!({{ "settings": {settings} }}))"#),
);
let filename = self.filename.as_ref().map_or_else(
|| "None".to_string(),
|filename| format!(r#"Some(PathBuf::from("{filename}"))"#),
);
let code_str = if need_filename {
format!("({code_str}, {config}, {settings}, {filename})")
} else if need_settings {
format!("({code_str}, {config}, {settings})")
} else if need_config {
format!("({code_str}, {config})")
} else {
code_str
};
if let Some(language_options) = &self.language_options {
format!("{code_str}, // {language_options}")
} else {
code_str
}
})
.unwrap_or_default()
}
fn group_comment(&self) -> Option<&str> {
self.group_comment.as_deref()
}
fn output(&self) -> Option<String> {
let code = format_code_snippet(self.code.as_ref()?);
let output = format_code_snippet(self.output.as_ref()?);
let config = self.config.as_ref().map_or_else(
|| "None".to_string(),
|config| format!("Some(serde_json::json!({config}))"),
);
// ("null==null", "null === null", None),
Some(format!(r#"({code}, {output}, {config})"#))
}
}
fn format_code_snippet(code: &str) -> String {
let code = if code.contains('\n') {
code.replace('\n', "\n\t\t\t").replace('\\', "\\\\").replace('\"', "\\\"")
} else {
code.to_string()
};
// "debugger" => "debugger"
if !code.contains('"') {
return format!("\"{code}\"");
}
// "document.querySelector("#foo");" => r##"document.querySelector("#foo");"##
if code.contains("\"#") {
return format!("r##\"{code}\"##");
}
// 'import foo from "foo";' => r#"import foo from "foo";"#
format!("r#\"{}\"#", code.replace("\\\"", "\""))
}
impl<'a> Visit<'a> for TestCase {
fn visit_expression(&mut self, expr: &Expression<'a>) {
match expr {
Expression::StringLiteral(lit) => self.visit_string_literal(lit),
Expression::TemplateLiteral(lit) => self.visit_template_literal(lit),
Expression::ObjectExpression(obj_expr) => self.visit_object_expression(obj_expr),
Expression::CallExpression(call_expr) => self.visit_call_expression(call_expr),
Expression::TaggedTemplateExpression(tag_expr) => {
self.visit_tagged_template_expression(tag_expr);
}
_ => {}
}
}
fn visit_call_expression(&mut self, expr: &CallExpression<'a>) {
if let Some(member_expr) = expr.callee.as_member_expression() {
if let Expression::ArrayExpression(array_expr) = member_expr.object() {
// ['class A {', '}'].join('\n')
let mut code = String::new();
for arg in &array_expr.elements {
let ArrayExpressionElement::StringLiteral(lit) = arg else {
continue;
};
code.push_str(lit.value.as_str());
code.push('\n');
}
self.code = Some(code);
self.config = None;
}
}
}
fn visit_object_expression(&mut self, expr: &ObjectExpression<'a>) {
for obj_prop in &expr.properties {
match obj_prop {
ObjectPropertyKind::ObjectProperty(prop) => match &prop.key {
PropertyKey::StaticIdentifier(ident) if ident.name == "code" => {
self.code = match &prop.value {
Expression::StringLiteral(s) => Some(s.value.to_string()),
Expression::TaggedTemplateExpression(tag_expr) => {
// There are `dedent`(in eslint-plugin-jest), `outdent`(in eslint-plugin-unicorn) and `noFormat`(in typescript-eslint)
// are known to be used to format test cases for their own purposes.
// We read the quasi of tagged template directly also for the future usage.
tag_expr.quasi.quasi().map(|quasi| quasi.to_string())
}
Expression::TemplateLiteral(tag_expr) => {
tag_expr.quasi().map(|quasi| quasi.to_string())
}
// handle code like ["{", "a: 1", "}"].join("\n")
Expression::CallExpression(call_expr) => {
if !call_expr.arguments.first().is_some_and(|arg| matches!(arg, Argument::StringLiteral(string) if string.value == "\n")) {
continue;
}
let Expression::StaticMemberExpression(member) = &call_expr.callee
else {
continue;
};
if member.property.name != "join" {
continue;
}
let Expression::ArrayExpression(array_expr) = &member.object else {
continue;
};
Some(
array_expr
.elements
.iter()
.map(|arg| match arg {
ArrayExpressionElement::StringLiteral(string) => {
string.value.as_str()
}
_ => "",
})
.collect::<Vec<_>>()
.join("\n"),
)
}
_ => continue,
}
}
PropertyKey::StaticIdentifier(ident) if ident.name == "output" => {
self.output = match &prop.value {
Expression::StringLiteral(s) => Some(s.value.to_string()),
Expression::TaggedTemplateExpression(tag_expr) => {
tag_expr.quasi.quasi().map(|quasi| quasi.to_string())
}
Expression::TemplateLiteral(tag_expr) => {
tag_expr.quasi().map(|quasi| quasi.to_string())
}
_ => None,
}
}
PropertyKey::StaticIdentifier(ident) if ident.name == "options" => {
let span = prop.value.span();
let option_text = &self.source_text[span.start as usize..span.end as usize];
self.config = Some(json::convert_config_to_json_literal(option_text));
}
PropertyKey::StaticIdentifier(ident) if ident.name == "settings" => {
let span = prop.value.span();
let setting_text = span.source_text(&self.source_text);
self.settings = Some(json::convert_config_to_json_literal(setting_text));
}
PropertyKey::StaticIdentifier(ident) if ident.name == "filename" => {
let span = prop.value.span();
let filename = span.source_text(&self.source_text);
self.filename = Some(filename.to_string());
}
PropertyKey::StaticIdentifier(ident) if ident.name == "languageOptions" => {
let span = prop.value.span();
let language_options = span.source_text(&self.source_text);
let language_options =
json::convert_config_to_json_literal(language_options);
self.language_options = Some(language_options);
}
_ => continue,
},
ObjectPropertyKind::SpreadProperty(_) => continue,
}
}
}
fn visit_template_literal(&mut self, lit: &TemplateLiteral<'a>) {
self.code = Some(lit.quasi().unwrap().to_string());
self.config = None;
}
fn visit_string_literal(&mut self, lit: &StringLiteral) {
self.code = Some(lit.value.to_string());
self.config = None;
}
fn visit_tagged_template_expression(&mut self, expr: &TaggedTemplateExpression<'a>) {
let Expression::Identifier(ident) = &expr.tag else {
return;
};
if ident.name != "dedent" && ident.name != "outdent" {
return;
}
self.code = expr.quasi.quasi().map(|quasi| quasi.to_string());
self.config = None;
}
}
#[derive(Serialize)]
pub struct Context {
plugin_name: String,
kebab_rule_name: String,
pascal_rule_name: String,
snake_rule_name: String,
pass_cases: String,
fail_cases: String,
fix_cases: Option<String>,
has_filename: bool,
/// Language examples are written in.
///
/// Should be `"js"`, `"jsx"`, `"ts"`, `"tsx"`. Defaults to `"js"`.
language: Cow<'static, str>,
}
impl Context {
fn new(plugin_name: String, rule_name: &str, pass_cases: String, fail_cases: String) -> Self {
let pascal_rule_name = rule_name.to_case(Case::Pascal);
let kebab_rule_name = rule_name.to_case(Case::Kebab);
let underscore_rule_name = rule_name.to_case(Case::Snake);
Self {
plugin_name,
kebab_rule_name,
pascal_rule_name,
snake_rule_name: underscore_rule_name,
pass_cases,
fail_cases,
fix_cases: None,
has_filename: false,
language: Cow::Borrowed("js"),
}
}
fn with_filename(mut self, has_filename: bool) -> Self {
self.has_filename = has_filename;
self
}
fn with_fix_cases(mut self, fix_cases: String) -> Self {
self.fix_cases = Some(fix_cases);
self
}
fn with_language<S: Into<Cow<'static, str>>>(mut self, language: S) -> Self {
self.language = language.into();
self
}
}
struct State<'a> {
source_text: &'a str,
valid_tests: Vec<&'a Expression<'a>>,
invalid_tests: Vec<&'a Expression<'a>>,
expression_to_group_comment_map: HashMap<Span, String>,
group_comment_stack: Vec<String>,
}
impl<'a> State<'a> {
fn new(source_text: &'a str) -> Self {
Self {
source_text,
valid_tests: vec![],
invalid_tests: vec![],
expression_to_group_comment_map: HashMap::new(),
group_comment_stack: vec![],
}
}
fn pass_cases(&self) -> Vec<TestCase> {
self.get_test_cases(&self.valid_tests)
}
fn fail_cases(&self) -> Vec<TestCase> {
self.get_test_cases(&self.invalid_tests)
}
fn get_test_cases(&self, tests: &[&'a Expression<'a>]) -> Vec<TestCase> {
tests
.iter()
.map(|arg| {
let case = TestCase::new(self.source_text, arg);
if let Some(group_comment) = self.expression_to_group_comment_map.get(&arg.span()) {
case.with_group_comment(group_comment.to_string())
} else {
case
}
})
.collect::<Vec<_>>()
}
fn get_comment(&self) -> String {
self.group_comment_stack.join(" ")
}
fn add_valid_test(&mut self, expr: &'a Expression<'a>) {
self.valid_tests.push(expr);
self.expression_to_group_comment_map.insert(expr.span(), self.get_comment());
}
fn add_invalid_test(&mut self, expr: &'a Expression<'a>) {
self.invalid_tests.push(expr);
self.expression_to_group_comment_map.insert(expr.span(), self.get_comment());
}
}
impl<'a> Visit<'a> for State<'a> {
fn visit_program(&mut self, program: &Program<'a>) {
for stmt in &program.body {
self.visit_statement(stmt);
}
}
fn visit_statement(&mut self, stmt: &Statement<'a>) {
match stmt {
Statement::ExpressionStatement(expr_stmt) => self.visit_expression_statement(expr_stmt),
// for eslint-plugin-jsdoc
Statement::ExportDefaultDeclaration(export_decl) => {
if let ExportDefaultDeclarationKind::ObjectExpression(obj_expr) =
&export_decl.declaration
{
self.visit_object_expression(obj_expr);
}
}
_ => {}
}
}
fn visit_expression_statement(&mut self, stmt: &ExpressionStatement<'a>) {
self.visit_expression(&stmt.expression);
}
fn visit_call_expression(&mut self, expr: &CallExpression<'a>) {
let mut pushed = false;
if let Expression::Identifier(ident) = &expr.callee {
// Add describe's first parameter as part group comment
// e.g. for `describe('valid', () => { ... })`, the group comment will be "valid"
if ident.name == "describe" {
if let Some(Argument::StringLiteral(lit)) = expr.arguments.first() {
pushed = true;
self.group_comment_stack.push(lit.value.to_string());
}
}
}
for arg in &expr.arguments {
self.visit_argument(arg);
}
if pushed {
self.group_comment_stack.pop();
}
self.visit_expression(&expr.callee);
}
fn visit_object_property(&mut self, prop: &ObjectProperty<'a>) {
let PropertyKey::StaticIdentifier(ident) = &prop.key else { return };
match ident.name.as_str() {
"valid" => {
if let Expression::ArrayExpression(array_expr) = &prop.value {
let array_expr = self.alloc(array_expr);
for arg in &array_expr.elements {
if let Some(expr) = arg.as_expression() {
self.add_valid_test(expr);
}
}
}
// for eslint-plugin-jsx-a11y
if let Some(args) = find_parser_arguments(&prop.value).map(|args| self.alloc(args))
{
for arg in args {
if let Some(expr) = arg.as_expression() {
self.add_valid_test(expr);
}
}
}
if let Expression::CallExpression(call_expr) = &prop.value {
if call_expr.callee.is_member_expression() {
// for eslint-plugin-react
if let Some(Argument::ArrayExpression(array_expr)) =
call_expr.arguments.first()
{
let array_expr = self.alloc(array_expr);
for arg in &array_expr.elements {
if let Some(expr) = arg.as_expression() {
self.add_valid_test(expr);
}
}
}
}
}
}
"invalid" => {
if let Expression::ArrayExpression(array_expr) = &prop.value {
let array_expr = self.alloc(array_expr);
for arg in &array_expr.elements {
if let Some(expr) = arg.as_expression() {
self.add_invalid_test(expr);
}
}
}
// for eslint-plugin-jsx-a11y
if let Some(args) = find_parser_arguments(&prop.value).map(|args| self.alloc(args))
{
for arg in args {
if let Some(expr) = arg.as_expression() {
self.add_invalid_test(expr);
}
}
}
// for eslint-plugin-react
if let Expression::CallExpression(call_expr) = &prop.value {
if call_expr.callee.is_member_expression() {
if let Some(Argument::ArrayExpression(array_expr)) =
call_expr.arguments.first()
{
let array_expr = self.alloc(array_expr);
for arg in &array_expr.elements {
if let Some(expr) = arg.as_expression() {
self.add_invalid_test(expr);
}
}
}
}
}
}
_ => {}
}
}
}
fn find_parser_arguments<'a, 'b>(
mut expr: &'b Expression<'a>,
) -> Option<&'b oxc_allocator::Vec<'a, Argument<'a>>> {
loop {
let Expression::CallExpression(call_expr) = expr else { return None };
let Expression::StaticMemberExpression(static_member_expr) = &call_expr.callee else {
return None;
};
let StaticMemberExpression { object, property, .. } = &**static_member_expr;
if let Expression::Identifier(iden) = object {
if iden.name == "parsers" && property.name == "all" {
if let Some(arg) = call_expr.arguments.first() {
if let Argument::CallExpression(call_expr) = arg {
if call_expr.callee.is_member_expression() {
return Some(&call_expr.arguments);
}
return None;
}
if arg.is_expression() {
return None;
}
}
}
}
expr = object;
}
}
#[derive(Clone, Copy)]
pub enum RuleKind {
ESLint,
Jest,
Typescript,
Unicorn,
React,
ReactPerf,
JSXA11y,
Oxc,
NextJS,
JSDoc,
Node,
TreeShaking,
Promise,
Vitest,
}
impl RuleKind {
fn from(kind: &str) -> Self {
match kind {
"jest" => Self::Jest,
"typescript" => Self::Typescript,
"unicorn" => Self::Unicorn,
"react" => Self::React,
"react-perf" => Self::ReactPerf,
"jsx-a11y" => Self::JSXA11y,
"oxc" => Self::Oxc,
"nextjs" => Self::NextJS,
"jsdoc" => Self::JSDoc,
"n" => Self::Node,
"tree-shaking" => Self::TreeShaking,
"promise" => Self::Promise,
"vitest" => Self::Vitest,
_ => Self::ESLint,
}
}
}
impl Display for RuleKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::ESLint => write!(f, "eslint"),
Self::Typescript => write!(f, "typescript-eslint"),
Self::Jest => write!(f, "eslint-plugin-jest"),
Self::Unicorn => write!(f, "eslint-plugin-unicorn"),
Self::React => write!(f, "eslint-plugin-react"),
Self::ReactPerf => write!(f, "eslint-plugin-react-perf"),
Self::JSXA11y => write!(f, "eslint-plugin-jsx-a11y"),
Self::Oxc => write!(f, "oxc"),
Self::NextJS => write!(f, "eslint-plugin-next"),
Self::JSDoc => write!(f, "eslint-plugin-jsdoc"),
Self::Node => write!(f, "eslint-plugin-n"),
Self::TreeShaking => write!(f, "eslint-plugin-tree-shaking"),
Self::Promise => write!(f, "eslint-plugin-promise"),
Self::Vitest => write!(f, "eslint-plugin-vitest"),
}
}
}
fn main() {
let mut args = std::env::args();
args.next();
let rule_name = args.next().expect("expected rule name").to_case(Case::Snake);
let rule_kind = args.next().map_or(RuleKind::ESLint, |kind| RuleKind::from(&kind));
let kebab_rule_name = rule_name.to_case(Case::Kebab);
let camel_rule_name = rule_name.to_case(Case::Camel);
let plugin_name = rule_kind.to_string();
let rule_test_path = match rule_kind {
RuleKind::ESLint => format!("{ESLINT_TEST_PATH}/{kebab_rule_name}.js"),
RuleKind::Jest => format!("{JEST_TEST_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::Typescript => format!("{TYPESCRIPT_ESLINT_TEST_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::Unicorn => format!("{UNICORN_TEST_PATH}/{kebab_rule_name}.mjs"),
RuleKind::React => format!("{REACT_TEST_PATH}/{kebab_rule_name}.js"),
RuleKind::ReactPerf => format!("{REACT_PERF_TEST_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::JSXA11y => format!("{JSX_A11Y_TEST_PATH}/{kebab_rule_name}-test.js"),
RuleKind::NextJS => format!("{NEXT_JS_TEST_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::JSDoc => format!("{JSDOC_TEST_PATH}/{camel_rule_name}.js"),
RuleKind::Node => format!("{NODE_TEST_PATH}/{kebab_rule_name}.js"),
RuleKind::TreeShaking => format!("{TREE_SHAKING_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::Promise => format!("{PROMISE_TEST_PATH}/{kebab_rule_name}.js"),
RuleKind::Vitest => format!("{VITEST_TEST_PATH}/{kebab_rule_name}.test.ts"),
RuleKind::Oxc => String::new(),
};
let language = match rule_kind {
RuleKind::Typescript | RuleKind::Oxc => "ts",
RuleKind::NextJS => "tsx",
RuleKind::React | RuleKind::ReactPerf | RuleKind::JSXA11y | RuleKind::TreeShaking => "jsx",
_ => "js",
};
println!("Reading test file from {rule_test_path}");
let body = oxc_tasks_common::agent().get(&rule_test_path).call().map(Response::into_string);
let context = match body {
Ok(Ok(body)) => {
let allocator = Allocator::default();
let source_type = SourceType::from_path(rule_test_path).expect("incorrect {path:?}");
let ret = Parser::new(&allocator, &body, source_type).parse();
let program = allocator.alloc(ret.program);
let mut state = State::new(&body);
state.visit_program(program);
let pass_cases = state.pass_cases();
let fail_cases = state.fail_cases();
println!(
"File parsed and {} pass cases, {} fail cases are found",
pass_cases.len(),
fail_cases.len()
);
let pass_has_config = pass_cases.iter().any(|case| case.config.is_some());
let fail_has_config = fail_cases.iter().any(|case| case.config.is_some());
let has_config = pass_has_config || fail_has_config;
let pass_has_settings = pass_cases.iter().any(|case| case.settings.is_some());
let fail_has_settings = fail_cases.iter().any(|case| case.settings.is_some());
let has_settings = pass_has_settings || fail_has_settings;
let pass_has_filename = pass_cases.iter().any(|case| case.filename.is_some());
let fail_has_filename = fail_cases.iter().any(|case| case.filename.is_some());
let has_filename = pass_has_filename || fail_has_filename;
let gen_cases_string = |cases: Vec<TestCase>| {
let mut codes = vec![];
let mut fix_codes = vec![];
let mut last_comment = String::new();
for case in cases {
let current_comment = case.group_comment();
let mut code = case.code(has_config, has_settings, has_filename);
if code.is_empty() {
continue;
}
if let Some(current_comment) = current_comment {
if current_comment != last_comment {
last_comment = current_comment.to_string();
code = format!(
"// {}\n{}",
&last_comment,
case.code(has_config, has_settings, has_filename)
);
}
}
if let Some(output) = case.output() {
fix_codes.push(output);
}
codes.push(code);
}
(codes.join(",\n"), fix_codes.join(",\n"))
};
// pass cases don't need to be fixed
let (pass_cases, _) = gen_cases_string(pass_cases);
let (fail_cases, fix_cases) = gen_cases_string(fail_cases);
Context::new(plugin_name, &rule_name, pass_cases, fail_cases)
.with_language(language)
.with_filename(has_filename)
.with_fix_cases(fix_cases)
}
Err(_err) => {
println!("Rule {rule_name} cannot be found in {rule_kind}, use empty template.");
Context::new(plugin_name, &rule_name, String::new(), String::new())
}
Ok(Err(err)) => {
println!("Failed to convert rule source code to string: {err}, use empty template");
Context::new(plugin_name, &rule_name, String::new(), String::new())
}
};
let rule_name = &context.kebab_rule_name;
let template = template::Template::with_context(&context);
if let Err(err) = template.render(rule_kind) {
eprintln!("failed to render {rule_name} rule template: {err}");
}
}