denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.93k stars 5.39k forks source link

Panic parsing JSX opening element with exclamation point before name (ex. `<!doctype html>`) #13751

Closed ericdmoore closed 2 years ago

ericdmoore commented 2 years ago

Noob Alert This my first deno project, I started using deno and deno deploy maybe about 3 weeks ago (and I'm loving it for the most part) - I am a bit in over my head submitting this issue, but wanted to give back.

Developer's Intention:

Complication:

Something like

/** @jsx h */
/** @jsxFrag Fragment */

import { h, jsx, Fragment} from 'https://deno.land/x/sift@0.4.3/mod.ts';

export const pageLayout = ()=>jsx(
        <>
            < !doctype html >
            <html lang='en-US' charSet='UTF-8'>
                <Head/>
                <Body/>
            </html>,
        </>
    );

Project Pinpoint for Context

Estimated Solution

Full Backtrace Provided Below is the backtrace as requested from std out.

Platform: macos x86_64
Version: 1.19.0
Args: ["deno", "run", "--allow-net", "--allow-env", "./src/bin/handler.tsx"]

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /Users/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/swc_ecma_parser-0.87.1/src/lexer/state.rs:421:45
stack backtrace:
   0:        0x10a00886f - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h90c059b532d34bdd
   1:        0x109799d5b - core::fmt::write::hefb311138778afb5
   2:        0x10a006d7a - std::io::Write::write_fmt::h0e0ed87b18ae870c
   3:        0x10a0079c5 - std::panicking::default_hook::{{closure}}::h43f0e51e5825bd5b
   4:        0x10a00712f - std::panicking::default_hook::ha1c3473cbcf391f7
   5:        0x10973bf75 - deno::setup_panic_hook::{{closure}}::hc98f2f74696ef484
   6:        0x10a006c29 - std::panicking::rust_panic_with_hook::h0db3c4ba5fb4dc12
   7:        0x10a03b9ea - std::panicking::begin_panic_handler::{{closure}}::hc6d8c92efeef5a19
   8:        0x10a03b989 - std::sys_common::backtrace::__rust_end_short_backtrace::h7ffb59e898e02a83
   9:        0x10a03b945 - _rust_begin_unwind
  10:        0x10b276caf - core::panicking::panic_fmt::h7d73f67464d78916
  11:        0x10b276dd7 - core::panicking::panic::h9721d5a8c5d33ad7
  12:        0x10a0ede39 - swc_ecma_parser::lexer::state::<impl core::iter::traits::iterator::Iterator for swc_ecma_parser::lexer::Lexer<I>>::next::hdd0aa6a36a23aa49
  13:        0x10a0b9191 - swc_ecma_parser::parser::input::Buffer<I>::cur::hdb2b8626160a3267
  14:        0x10a0faa71 - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_var_stmt::h00d414eff63a720f
  15:        0x10a0bb658 - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_stmt_internal::hf447157fa05196ed
  16:        0x10a0b94c7 - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_stmt_like::h54f0951841e42b11
  17:        0x10a0b8c6b - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_block_body::hab1ae615f0f864c0
  18:        0x10a0d6756 - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_block::h0cedb992f91cdde2
  19:        0x10a0d6569 - swc_ecma_parser::parser::class_and_fn::<impl swc_ecma_parser::parser::Parser<I>>::parse_fn_body::h753516fc100259f2
  20:        0x10a0d4c49 - swc_ecma_parser::parser::expr::<impl swc_ecma_parser::parser::Parser<I>>::parse_paren_expr_or_arrow_fn::h4da622110ea9e691
  21:        0x10a0d0ece - swc_ecma_parser::parser::expr::<impl swc_ecma_parser::parser::Parser<I>>::parse_primary_expr::h3befaad93f899043
  22:        0x10a0cede3 - swc_ecma_parser::parser::expr::<impl swc_ecma_parser::parser::Parser<I>>::parse_member_expr_or_new_expr::ha539fa2593575688
  23:        0x10a0d793a - swc_ecma_parser::parser::expr::<impl swc_ecma_parser::parser::Parser<I>>::parse_lhs_expr::he461dadcc991998b
  24:        0x10a0c1240 - swc_ecma_parser::parser::expr::ops::<impl swc_ecma_parser::parser::Parser<I>>::parse_unary_expr::h7bee626fae54f838
  25:        0x10a0c0d62 - swc_ecma_parser::parser::expr::ops::<impl swc_ecma_parser::parser::Parser<I>>::parse_bin_expr::hf2f7ab003ea5daa2
  26:        0x10a0bf070 - swc_ecma_parser::parser::expr::<impl swc_ecma_parser::parser::Parser<I>>::parse_assignment_expr_base::h34f5899b9560477d
  27:        0x10a0bea09 - swc_ecma_parser::parser::expr::<impl swc_ecma_parser::parser::Parser<I>>::parse_assignment_expr::hd2d6abf15925655b
  28:        0x10a0fa70e - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_var_stmt::h00d414eff63a720f
  29:        0x10a103cfa - swc_ecma_parser::parser::stmt::module_item::<impl swc_ecma_parser::parser::Parser<I>>::parse_export::h9c0f34b4b1c908bf
  30:        0x10a0fe8dc - swc_ecma_parser::parser::stmt::<impl swc_ecma_parser::parser::Parser<I>>::parse_block_body::hb08a0354d3b5bb2d
  31:        0x10a05937c - swc_ecma_parser::parser::Parser<I>::parse_module::hbb5ba7ef8ef2640f
  32:        0x10985f2e9 - deno_ast::parsing::parse_string_input::h736173f9eba06da8
  33:        0x109860302 - deno_ast::parsing::parse_module::hcdbf29471f16fe58
  34:        0x1099534f9 - <deno_graph::ast::DefaultSourceParser as deno_graph::ast::SourceParser>::parse_module::h0bee09afa92ad943
  35:        0x109956cfd - deno_graph::graph::parse_module::h0e87e7e339ce9bcf
  36:        0x109528d3c - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h980520bd4cb4fdd8
  37:        0x109649990 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::ha6dc1b1764e81bf6
  38:        0x109649116 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h2bf4412b7649fa2e
  39:        0x109647315 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h16b244136e0f3bb7
  40:        0x1096464db - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h1d78007f8eb76fb1
  41:        0x109645cca - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hc0212ebe344b184c
  42:        0x10940ca28 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hacaa8791b0f23ad7
  43:        0x109675f91 - deno::run_command::{{closure}}::h75daa478631e1e58
  44:        0x109402dc4 - deno::main::{{closure}}::hea1cb5cbedd7389b
  45:        0x1093f8842 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h22d11cd99eed43f0
  46:        0x1093f7d0d - deno_runtime::tokio_util::run_basic::h2db761e992b65b50
  47:        0x1093f72f1 - deno::main::ha6aad75c386d134c
  48:        0x1093f6cce - std::sys_common::backtrace::__rust_begin_short_backtrace::h3a59aaefd2f0a777
  49:        0x1093f6f5a - _main
kitsonk commented 2 years ago

Looks like an swc parsing error. Will open the upstream issue later.

kitsonk commented 2 years ago

To be clear, fragments generally work. It just appears that swc doesn't parse <!doctype html> without panicking.

dsherret commented 2 years ago

Yeah, this shouldn't panic, but <!doctype html> is not valid JSX from my understanding (ex. tsc does not parse this) and a JSX fragment looks like:

JSXFragment :

< > JSXChildren < / >

https://facebook.github.io/jsx/

@ericdmoore I think you can just remove < !doctype html > (see https://doc.deno.land/https://deno.land/x/sift@0.4.3/mod.ts/~/jsx)

kitsonk commented 2 years ago

Yes, I wanted to double check as I was on my mobile, but it isn't valid JSX (and it might not be related to JSX at all). It shouldn't panic, but cause a syntax parse error.

kitsonk commented 2 years ago

Fixed upstream, should be fixed next swc update.

ericdmoore commented 2 years ago

@kitsonk @dsherret thanks. After further investigation on my end I have not found a JSX way to add the doctype tag. So I resorted to prepending that string in the response stream.