swc_ecma_transforms_testing/
babel_like.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
use std::{fs::read_to_string, path::Path};

use ansi_term::Color;
use serde::Deserialize;
use serde_json::Value;
use swc_common::{comments::SingleThreadedComments, sync::Lrc, Mark, SourceMap};
use swc_ecma_ast::{EsVersion, Pass, Program};
use swc_ecma_codegen::Emitter;
use swc_ecma_parser::{parse_file_as_program, Syntax};
use swc_ecma_transforms_base::{
    assumptions::Assumptions,
    fixer::fixer,
    helpers::{inject_helpers, Helpers, HELPERS},
    hygiene::hygiene,
    resolver,
};
use testing::NormalizedOutput;

use crate::{exec_with_node_test_runner, parse_options, stdout_of};

pub type PassFactory<'a> =
    Box<dyn 'a + FnMut(&PassContext, &str, Option<Value>) -> Option<Box<dyn 'static + Pass>>>;

/// These tests use `options.json`.
///
///
/// Note: You should **not** use [resolver] by yourself.
pub struct BabelLikeFixtureTest<'a> {
    input: &'a Path,

    /// Default to [`Syntax::default`]
    syntax: Syntax,

    factories: Vec<Box<dyn 'a + FnOnce() -> PassFactory<'a>>>,

    source_map: bool,
    allow_error: bool,
}

impl<'a> BabelLikeFixtureTest<'a> {
    pub fn new(input: &'a Path) -> Self {
        Self {
            input,
            syntax: Default::default(),
            factories: Default::default(),
            source_map: false,
            allow_error: false,
        }
    }

    pub fn syntax(mut self, syntax: Syntax) -> Self {
        self.syntax = syntax;
        self
    }

    pub fn source_map(mut self) -> Self {
        self.source_map = true;
        self
    }

    pub fn allow_error(mut self) -> Self {
        self.source_map = true;
        self
    }

    /// This takes a closure which returns a [PassFactory]. This is because you
    /// may need to create [Mark], which requires [swc_common::GLOBALS] to be
    /// configured.
    pub fn add_factory(mut self, factory: impl 'a + FnOnce() -> PassFactory<'a>) -> Self {
        self.factories.push(Box::new(factory));
        self
    }

    fn run(self, output_path: Option<&Path>, compare_stdout: bool) {
        let err = testing::run_test(false, |cm, handler| {
            let mut factories = self.factories.into_iter().map(|f| f()).collect::<Vec<_>>();

            let options = parse_options::<BabelOptions>(self.input.parent().unwrap());

            let comments = SingleThreadedComments::default();
            let mut builder = PassContext {
                cm: cm.clone(),
                assumptions: options.assumptions,
                unresolved_mark: Mark::new(),
                top_level_mark: Mark::new(),
                comments: comments.clone(),
            };

            let mut pass: Box<dyn Pass> = Box::new(resolver(
                builder.unresolved_mark,
                builder.top_level_mark,
                self.syntax.typescript(),
            ));

            // Build pass using babel options

            //
            for plugin in options.plugins {
                let (name, options) = match plugin {
                    BabelPluginEntry::NameOnly(name) => (name, None),
                    BabelPluginEntry::WithConfig(name, options) => (name, Some(options)),
                };

                let mut done = false;
                for factory in &mut factories {
                    if let Some(built) = factory(&builder, &name, options.clone()) {
                        pass = Box::new((pass, built));
                        done = true;
                        break;
                    }
                }

                if !done {
                    panic!("Unknown plugin: {}", name);
                }
            }

            pass = Box::new((pass, hygiene(), fixer(Some(&comments))));

            // Run pass

            let src = read_to_string(self.input).expect("failed to read file");
            let src = if output_path.is_none() && !compare_stdout {
                format!(
                    "it('should work', async function () {{
                    {src}
                }})",
                )
            } else {
                src
            };
            let fm = cm.new_source_file(
                swc_common::FileName::Real(self.input.to_path_buf()).into(),
                src,
            );

            let mut errors = Vec::new();
            let input_program = parse_file_as_program(
                &fm,
                self.syntax,
                EsVersion::latest(),
                Some(&comments),
                &mut errors,
            );

            let errored = !errors.is_empty();

            for e in errors {
                e.into_diagnostic(handler).emit();
            }

            let input_program = match input_program {
                Ok(v) => v,
                Err(err) => {
                    err.into_diagnostic(handler).emit();
                    return Err(());
                }
            };

            if errored {
                return Err(());
            }

            let helpers = Helpers::new(output_path.is_some());
            let (code_without_helper, output_program) = HELPERS.set(&helpers, || {
                let mut p = input_program.apply(pass);

                let code_without_helper = builder.print(&p);

                if output_path.is_none() {
                    p.mutate(inject_helpers(builder.unresolved_mark))
                }

                (code_without_helper, p)
            });

            // Print output
            let code = builder.print(&output_program);

            println!(
                "\t>>>>> {} <<<<<\n{}\n\t>>>>> {} <<<<<\n{}",
                Color::Green.paint("Orig"),
                fm.src,
                Color::Green.paint("Code"),
                code_without_helper
            );

            if let Some(output_path) = output_path {
                // Fixture test

                if !self.allow_error && handler.has_errors() {
                    return Err(());
                }

                NormalizedOutput::from(code)
                    .compare_to_file(output_path)
                    .unwrap();
            } else if compare_stdout {
                // Execution test, but compare stdout

                let actual_stdout: String =
                    stdout_of(&code).expect("failed to execute transfomred code");
                let expected_stdout =
                    stdout_of(&fm.src).expect("failed to execute transfomred code");

                testing::assert_eq!(actual_stdout, expected_stdout);
            } else {
                // Execution test

                exec_with_node_test_runner(&format!("// {}\n{code}", self.input.display()))
                    .expect("failed to execute transfomred code");
            }

            Ok(())
        });

        if self.allow_error {
            match err {
                Ok(_) => {}
                Err(err) => {
                    err.compare_to_file(self.input.with_extension("stderr"))
                        .unwrap();
                }
            }
        }
    }

    /// Execute using node.js and mocha
    pub fn exec_with_test_runner(self) {
        self.run(None, false)
    }

    /// Execute using node.js
    pub fn compare_stdout(self) {
        self.run(None, true)
    }

    /// Run a fixture test
    pub fn fixture(self, output: &Path) {
        self.run(Some(output), false)
    }
}

#[derive(Debug, Deserialize)]
struct BabelOptions {
    #[serde(default)]
    assumptions: Assumptions,

    #[serde(default)]
    plugins: Vec<BabelPluginEntry>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase", untagged)]
enum BabelPluginEntry {
    NameOnly(String),
    WithConfig(String, Value),
}

#[derive(Clone)]
pub struct PassContext {
    pub cm: Lrc<SourceMap>,

    pub assumptions: Assumptions,
    pub unresolved_mark: Mark,
    pub top_level_mark: Mark,

    /// [SingleThreadedComments] is cheap to clone.
    pub comments: SingleThreadedComments,
}

impl PassContext {
    fn print(&mut self, program: &Program) -> String {
        let mut buf = Vec::new();
        {
            let mut emitter = Emitter {
                cfg: Default::default(),
                cm: self.cm.clone(),
                wr: Box::new(swc_ecma_codegen::text_writer::JsWriter::new(
                    self.cm.clone(),
                    "\n",
                    &mut buf,
                    None,
                )),
                comments: Some(&self.comments),
            };

            emitter.emit_program(program).unwrap();
        }

        let s = String::from_utf8_lossy(&buf);
        s.to_string()
    }
}