biome_js_parser 0.5.7

Biome's JavaScript parser
Documentation
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
use crate::test_utils::has_bogus_nodes_or_empty_slots;
use crate::{parse, parse_module, test_utils::assert_errors_are_absent, JsParserOptions, Parse};
use biome_console::fmt::{Formatter, Termcolor};
use biome_console::markup;
use biome_diagnostics::DiagnosticExt;
use biome_diagnostics::PrintDiagnostic;
use biome_js_syntax::{AnyJsRoot, JsFileSource, JsSyntaxKind};
use biome_js_syntax::{JsCallArguments, JsLogicalExpression, JsSyntaxToken};
use biome_rowan::{AstNode, Direction, TextSize};
use expect_test::expect_file;
use std::fmt::Write;
use std::panic::catch_unwind;
use std::path::{Path, PathBuf};

#[test]
fn parser_smoke_test() {
    let src = r#"
import "x" with { type: "json" }
"#;

    let module = parse(src, JsFileSource::tsx(), JsParserOptions::default());
    assert_errors_are_absent(&module, Path::new("parser_smoke_test"));
}

#[test]
fn parser_missing_smoke_test() {
    let src = r#"
        console.log("Hello world";
    "#;

    let module = parse_module(src, JsParserOptions::default());

    let arg_list = module
        .syntax()
        .descendants()
        .find_map(JsCallArguments::cast)
        .unwrap();

    let opening = arg_list.syntax().element_in_slot(0);
    let list = arg_list.syntax().element_in_slot(1);
    let closing = arg_list.syntax().element_in_slot(2);

    assert_eq!(opening.map(|o| o.to_string()), Some(String::from("(")));
    assert_eq!(
        list.map(|l| l.kind()),
        Some(JsSyntaxKind::JS_CALL_ARGUMENT_LIST)
    );
    assert_eq!(closing, None);
}

fn try_parse(path: &str, text: &str, options: JsParserOptions) -> Parse<AnyJsRoot> {
    let res = catch_unwind(|| {
        let path = Path::new(path);
        // Files containing a // SCRIPT comment are parsed as script and not as module
        // This is needed to test features that are restricted in strict mode.
        let source_type = if text.contains("// SCRIPT") {
            JsFileSource::js_script()
        } else {
            path.try_into().unwrap()
        };

        let parse = parse(text, source_type, options);

        assert_eq!(
            parse.syntax().to_string(),
            text,
            "Original source and re-printed tree differ\nParsed Tree: {:#?}",
            parse.syntax(),
        );

        parse
    });
    assert!(res.is_ok(), "Trying to parse `{}` panicked", path);
    res.unwrap()
}

fn try_parse_with_printed_ast(
    path: &str,
    text: &str,
    options: JsParserOptions,
) -> (Parse<AnyJsRoot>, String) {
    catch_unwind(|| {
        let parse = try_parse(path, text, options.clone());
        let formatted = format!("{:#?}", &parse.tree());
        (parse, formatted)
    })
    .unwrap_or_else(|err| {
        // Re-parsing the source here seems silly. But the problem is, that `SyntaxNode`s aren't
        // unwind safe. That's why the same `ParseResult` can't be reused here.
        // This should be fine because this code is only executed for local tests. No checked-in
        // test should ever hit this line.
        let re_parsed = try_parse(path, text, options);
        panic!(
            "Printing the AST for `{}` panicked. That means it is malformed. Err: {:?}\n{:#?}",
            path,
            err,
            re_parsed.syntax()
        );
    })
}

#[cfg(test)]
fn run_and_expect_no_errors(path: &str, _: &str, _: &str, _: &str) {
    let path = PathBuf::from(path);
    let text = std::fs::read_to_string(&path).unwrap();

    let options_path = path.with_extension("options.json");
    let options: JsParserOptions = std::fs::read_to_string(options_path)
        .ok()
        .and_then(|options| serde_json::from_str(&options).ok())
        .unwrap_or_default();

    let (parse, ast) = try_parse_with_printed_ast(path.to_str().unwrap(), &text, options);
    assert_errors_are_absent(&parse, &path);
    let actual = format!("{}\n\n{:#?}", ast, parse.syntax());

    let path = path.with_extension("rast");
    expect_file![path].assert_eq(&actual)
}

#[cfg(test)]
fn run_and_expect_errors(path: &str, _: &str, _: &str, _: &str) {
    let path = PathBuf::from(path);
    let text = std::fs::read_to_string(&path).unwrap();

    let options_path = path.with_extension("options.json");
    let options: JsParserOptions = std::fs::read_to_string(options_path)
        .ok()
        .and_then(|options| serde_json::from_str(&options).ok())
        .unwrap_or_default();

    let (parse, ast) = try_parse_with_printed_ast(path.to_str().unwrap(), &text, options);
    assert_errors_are_present(&parse, &path);
    let mut actual = format!("{}\n\n{:#?}", ast, parse.syntax());
    for diag in parse.diagnostics() {
        let mut write = biome_diagnostics::termcolor::Buffer::no_color();
        let error = diag
            .clone()
            .with_file_path(path.file_name().unwrap().to_string_lossy().to_string())
            .with_file_source_code(text.to_string());
        Formatter::new(&mut Termcolor(&mut write))
            .write_markup(markup! {
                {PrintDiagnostic::verbose(&error)}
            })
            .expect("failed to emit diagnostic");
        write!(
            actual,
            "--\n{}",
            std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
        )
        .unwrap();
    }
    write!(actual, "--\n{}", text).unwrap();

    let path = path.with_extension("rast");
    expect_file![path].assert_eq(&actual)
}

mod parser {
    mod ok {
        tests_macros::gen_tests! {"test_data/inline/ok/**/*.{js,ts,jsx,tsx}", crate::tests::run_and_expect_no_errors, ""}
    }
    mod err {
        tests_macros::gen_tests! {"test_data/inline/err/**/*.{js,ts,jsx,tsx}", crate::tests::run_and_expect_errors, ""}
    }
}

fn assert_errors_are_present(program: &Parse<AnyJsRoot>, path: &Path) {
    assert!(
        !program.diagnostics().is_empty(),
        "There should be errors in the file {:?}\nSyntax Tree: {:#?}",
        path.display(),
        program.syntax()
    );
}

#[test]
pub fn test_trivia_attached_to_tokens() {
    let text = "/**/let a = 1; // nice variable \n /*hey*/ let \t b = 2; // another nice variable";
    let m = parse_module(text, JsParserOptions::default());
    let mut tokens = m.syntax().descendants_tokens(Direction::Next);

    let is_let = |x: &JsSyntaxToken| x.text_trimmed() == "let";
    let first_let = tokens.find(is_let).unwrap();

    // first let leading trivia asserts
    let pieces: Vec<_> = first_let.leading_trivia().pieces().collect();
    assert!(matches!(pieces.first().map(|x| x.text()), Some("/**/")));
    assert!(pieces.get(1).is_none());

    // first let trailing trivia asserts
    let pieces: Vec<_> = first_let.trailing_trivia().pieces().collect();
    assert!(matches!(pieces.first().map(|x| x.text()), Some(" ")));
    assert!(pieces.get(1).is_none());

    // second let leading trivia asserts
    let second_let = tokens.find(is_let).unwrap();
    let pieces: Vec<_> = second_let.leading_trivia().pieces().collect();
    assert_eq!(4, pieces.len());
    assert!(matches!(pieces.first().map(|x| x.text()), Some("\n")));
    assert!(matches!(pieces.get(1).map(|x| x.text()), Some(" ")));
    assert!(matches!(pieces.get(2).map(|x| x.text()), Some("/*hey*/")));
    assert!(matches!(pieces.get(3).map(|x| x.text()), Some(" ")));

    // second let trailing trivia asserts
    let pieces: Vec<_> = second_let.trailing_trivia().pieces().collect();
    assert_eq!(1, pieces.len());
    assert!(matches!(pieces.first().map(|x| x.text()), Some(" \t ")));
}

#[test]
pub fn jsroot_display_text_and_trimmed() {
    let code = " let a = 1; \n ";
    let root = parse_module(code, JsParserOptions::default());
    let syntax = root.syntax();

    assert_eq!(format!("{}", syntax), code);

    let syntax_text = syntax.text();
    assert_eq!(format!("{}", syntax_text), code);

    let syntax_text = syntax.text_trimmed();
    assert_eq!(format!("{}", syntax_text), code.trim());
}

#[test]
pub fn jsroot_ranges() {
    //               0123456789A
    let code = " let a = 1;";
    let root = parse_module(code, JsParserOptions::default());
    let syntax = root.syntax();

    let first_let = syntax.first_token().unwrap();
    let range = first_let.text_range();
    assert_eq!(0usize, usize::from(range.start()));
    assert_eq!(5usize, usize::from(range.end()));

    let range = first_let.text_trimmed_range();
    assert_eq!(1usize, usize::from(range.start()));
    assert_eq!(4usize, usize::from(range.end()));

    let eq = syntax
        .descendants_tokens(Direction::Next)
        .find(|x| x.text_trimmed() == "=")
        .unwrap();
    let range = eq.text_range();
    assert_eq!(7usize, usize::from(range.start()));
    assert_eq!(9usize, usize::from(range.end()));

    let range = eq.text_trimmed_range();
    assert_eq!(7usize, usize::from(range.start()));
    assert_eq!(8usize, usize::from(range.end()));
}

#[test]
pub fn node_range_must_be_correct() {
    //               0123456789A123456789B123456789
    let text = " function foo() { let a = 1; }";
    let root = parse_module(text, JsParserOptions::default());

    let var_decl = root
        .syntax()
        .descendants()
        .find(|x| x.kind() == JsSyntaxKind::JS_VARIABLE_STATEMENT)
        .unwrap();

    let range = var_decl.text_range();
    assert_eq!(18usize, usize::from(range.start()));
    assert_eq!(29usize, usize::from(range.end()));

    let range = var_decl.text_trimmed_range();
    assert_eq!(18usize, usize::from(range.start()));
    assert_eq!(28usize, usize::from(range.end()));
}

#[test]
pub fn last_trivia_must_be_appended_to_eof() {
    //               0123456789A123456789B123456789CC
    let text = " function foo() { let a = 1; }\n";
    let root = parse_module(text, JsParserOptions::default());
    let syntax = root.syntax();

    let range = syntax.text_range();
    let start = range.start();
    let end = range.end();

    assert_eq!(TextSize::from(0), start);
    assert_eq!(TextSize::from(31), end);
}

#[test]
pub fn just_trivia_must_be_appended_to_eof() {
    //               0123456789A123456789B123456789C123
    let text = "// just trivia... nothing else....";
    let root = parse_module(text, JsParserOptions::default());
    let syntax = root.syntax();

    let range = syntax.text_range();
    let start = range.start();
    let end = range.end();

    assert_eq!(TextSize::from(0), start);
    assert_eq!(TextSize::from(34), end);
}

#[test]
pub fn node_contains_comments() {
    let text = "true && true // comment";
    let root = parse_module(text, JsParserOptions::default());
    let syntax = root.syntax();

    assert!(syntax.has_comments_descendants());
}

#[test]
fn parser_regexp_after_operator() {
    fn assert_no_errors(src: &str) {
        let module = parse(src, JsFileSource::js_script(), JsParserOptions::default());
        assert_errors_are_absent(&module, Path::new("parser_regexp_after_operator"));
    }
    assert_no_errors(r#"a=/a/"#);
    assert_no_errors(r#"a==/a/"#);
    assert_no_errors(r#"a===/a/"#);
    assert_no_errors(r#"a!=/a/"#);
    assert_no_errors(r#"a!==/a/"#);
}

#[test]
pub fn node_contains_trailing_comments() {
    let text = "true && (3 - 2 == 0) // comment";
    let root = parse_module(text, JsParserOptions::default());
    let syntax = root.syntax();
    let node = syntax
        .descendants()
        .find(|n| n.kind() == JsSyntaxKind::JS_LOGICAL_EXPRESSION)
        .unwrap();

    let logical_expression = JsLogicalExpression::cast(node).unwrap();
    let right = logical_expression.right().unwrap();

    assert!(right.syntax().has_trailing_comments());
    assert!(!right.syntax().has_leading_comments());
}

#[test]
pub fn node_contains_leading_comments() {
    let text = r"true &&
// comment
(3 - 2 == 0)";
    let root = parse_module(text, JsParserOptions::default());
    let syntax = root.syntax();
    let node = syntax
        .descendants()
        .find(|n| n.kind() == JsSyntaxKind::JS_LOGICAL_EXPRESSION)
        .unwrap();

    let logical_expression = JsLogicalExpression::cast(node).unwrap();
    let right = logical_expression.right().unwrap();

    assert!(right.syntax().has_leading_comments());
    assert!(!right.syntax().has_trailing_comments());
}

#[test]
pub fn node_has_comments() {
    let text = r"true &&
// comment
(3 - 2 == 0)";
    let root = parse_module(text, JsParserOptions::default());
    let syntax = root.syntax();
    let node = syntax
        .descendants()
        .find(|n| n.kind() == JsSyntaxKind::JS_LOGICAL_EXPRESSION)
        .unwrap();

    let logical_expression = JsLogicalExpression::cast(node).unwrap();
    let right = logical_expression.right().unwrap();

    assert!(right.syntax().has_comments_direct());
}

#[test]
fn diagnostics_print_correctly() {
    let text = r"const a";

    let root = parse_module(text, JsParserOptions::default());
    for diagnostic in root.diagnostics() {
        let mut write = biome_diagnostics::termcolor::Buffer::no_color();
        let error = diagnostic
            .clone()
            .with_file_path("example.js")
            .with_file_source_code(text.to_string());

        Formatter::new(&mut Termcolor(&mut write))
            .write_markup(markup! {
                {PrintDiagnostic::verbose(&error)}
            })
            .expect("failed to emit diagnostic");

        eprintln!(
            "{}",
            std::str::from_utf8(write.as_slice()).expect("non utf8 in error buffer")
        );
    }
}

#[ignore]
#[test]
pub fn quick_test() {
    let code = r#"
        type Equals = A extends (x: B extends C ? D : E) => 0 ? F : G;
    "#;
    let root = parse(
        code,
        JsFileSource::ts(),
        JsParserOptions::default().with_parse_class_parameter_decorators(),
    );
    let syntax = root.syntax();
    dbg!(&syntax, root.diagnostics(), root.has_errors());

    if has_bogus_nodes_or_empty_slots(&syntax) {
        panic!(
            "modified tree has bogus nodes or empty slots:\n{syntax:#?} \n\n {}",
            syntax
        )
    }
}