forked from oxc-project/oxc
/
ts_fixtures.rs
136 lines (113 loc) · 4.43 KB
/
ts_fixtures.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
use std::{
fs,
path::{Path, PathBuf},
};
use walkdir::WalkDir;
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_diagnostics::{miette::NamedSource, GraphicalReportHandler, GraphicalTheme};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_tasks_common::{normalize_path, project_root};
use oxc_transform_conformance::TestRunnerOptions;
use oxc_transformer::{TransformOptions, Transformer};
fn root() -> PathBuf {
project_root().join("tasks/coverage")
}
fn snap_root() -> PathBuf {
project_root().join("tasks/transform_conformance")
}
const CASES: &[&str] = &[
"typescript/tests/cases/conformance/enums",
"babel/packages/babel-plugin-transform-typescript/test/fixtures/enum",
];
const CONFORMANCE_SNAPSHOT: &str = "typescript.snap.md";
fn filter_ext(p: &Path) -> bool {
p.to_string_lossy().ends_with(".ts")
}
pub struct TypeScriptFixtures {
options: TestRunnerOptions,
}
impl TypeScriptFixtures {
pub fn new(options: TestRunnerOptions) -> Self {
Self { options }
}
pub fn run(self) {
let mut snapshot = String::new();
for case in CASES {
for path in Self::glob_files(&root().join(case), self.options.filter.as_ref()) {
snapshot.push_str("# ");
snapshot.push_str(&normalize_path(path.strip_prefix(&root()).unwrap()));
snapshot.push('\n');
snapshot.push_str("```");
let (content, lang) = match Self::transform(&path) {
Ok(content) => (content, "typescript"),
Err(err) => (err, "error"),
};
snapshot.push_str(lang);
snapshot.push('\n');
snapshot.push_str(&content);
snapshot.push_str("\n```\n\n");
}
}
if self.options.filter.is_none() {
fs::write(snap_root().join(CONFORMANCE_SNAPSHOT), snapshot).unwrap();
}
}
}
impl TypeScriptFixtures {
fn transform_options() -> TransformOptions {
// TODO: read options from slash directives
TransformOptions::default()
}
fn glob_files(root: &Path, filter: Option<&String>) -> Vec<PathBuf> {
let mut list: Vec<PathBuf> = WalkDir::new(root)
.into_iter()
.filter_map(Result::ok)
.map(walkdir::DirEntry::into_path)
.filter(|p| p.is_file())
.filter(|p| filter_ext(p.as_path()))
.filter(|p| filter.map_or(true, |f| p.to_string_lossy().contains(f)))
.collect();
list.sort_unstable();
list
}
fn transform(path: &Path) -> Result<String, String> {
let allocator = Allocator::default();
let source_text = fs::read_to_string(path).unwrap();
let source_type = SourceType::from_path(path).unwrap();
let parser_ret = Parser::new(&allocator, &source_text, source_type).parse();
let semantic_ret = SemanticBuilder::new(&source_text, source_type)
.with_trivias(parser_ret.trivias)
.with_check_syntax_error(true)
.build_module_record(PathBuf::new(), &parser_ret.program)
.build(&parser_ret.program);
let errors = parser_ret.errors.into_iter().chain(semantic_ret.errors).collect::<Vec<_>>();
if !errors.is_empty() {
let handler =
GraphicalReportHandler::new().with_theme(GraphicalTheme::unicode_nocolor());
let mut output = String::new();
for error in errors {
let error = error.with_source_code(NamedSource::new(
&normalize_path(path.strip_prefix(&root()).unwrap()),
source_text.to_string(),
));
handler.render_report(&mut output, error.as_ref()).unwrap();
output.push('\n');
}
return Err(output);
}
let semantic = semantic_ret.semantic;
let transformed_program = allocator.alloc(parser_ret.program);
let result = Transformer::new(&allocator, source_type, semantic, Self::transform_options())
.build(transformed_program);
result
.map(|()| {
Codegen::<false>::new("", &source_text, CodegenOptions::default())
.build(transformed_program)
.source_text
})
.map_err(|e| e.iter().map(ToString::to_string).collect())
}
}