Skip to content

Commit

Permalink
fix(es/react): Don't panic on invalid react pragma (#5101)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Jul 4, 2022
1 parent b79593f commit cc555ef
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 13 deletions.
33 changes: 20 additions & 13 deletions crates/swc_ecma_transforms_react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,26 @@ impl JsxDirectives {
Some("@jsxRuntime") => match val {
Some("classic") => res.runtime = Some(Runtime::Classic),
Some("automatic") => res.runtime = Some(Runtime::Automatic),
_ => todo!("proper error reporting for wrong `@jsxRuntime`"),
None => {}
_ => {
HANDLER.with(|handler| {
handler
.struct_span_err(
cmt.span,
"Runtime must be either `classic` or `automatic`.",
)
.emit()
});
}
},
Some("@jsxImportSource") => match val {
Some(src) => {
Some("@jsxImportSource") => {
if let Some(src) = val {
res.runtime = Some(Runtime::Automatic);
res.import_source = Some(src.into());
}
_ => todo!("proper error reporting for wrong `@jsxImportSource`"),
},
Some("@jsxFrag") => match val {
Some(src) => {
}
Some("@jsxFrag") => {
if let Some(src) = val {
// TODO: Optimize
let mut e = (*parse_expr_for_jsx(
cm,
Expand All @@ -335,10 +344,9 @@ impl JsxDirectives {
respan(&mut e, cmt.span);
res.pragma_frag = Some(e.into())
}
_ => todo!("proper error reporting for wrong `@jsxFrag`"),
},
Some("@jsx") => match val {
Some(src) => {
}
Some("@jsx") => {
if let Some(src) = val {
// TODO: Optimize
let mut e = (*parse_expr_for_jsx(
cm,
Expand All @@ -350,8 +358,7 @@ impl JsxDirectives {
respan(&mut e, cmt.span);
res.pragma = Some(e.into());
}
_ => todo!("proper error reporting for wrong `@jsxFrag`"),
},
}
_ => {}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @jsx h */
/** @jsxFrag */
import { h } from "preact";

import { Marked } from "markdown";

export const handler = {
async GET(req, ctx) {
const markdown = await Deno.readTextFile(`posts/${ctx.params.id}.md`);
const markup = Marked.parse(markdown);
const resp = await ctx.render({ markup });
return resp;
},
};

export default function Greet(props) {
return (
<>
<div
dangerouslySetInnerHTML={{
__html: props.data.markup.content,
}}
/>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @jsx h */ /** @jsxFrag */ import { h } from "preact";
import { Marked } from "markdown";
export const handler = {
async GET (req, ctx) {
const markdown = await Deno.readTextFile(`posts/${ctx.params.id}.md`);
const markup = Marked.parse(markdown);
const resp = await ctx.render({
markup
});
return resp;
}
};
export default function Greet(props) {
return /*#__PURE__*/ h(React.Fragment, null, /*#__PURE__*/ h("div", {
dangerouslySetInnerHTML: {
__html: props.data.markup.content
}
}));
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @jsxRuntime typo */

<></>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** @jsxRuntime typo */ /*#__PURE__*/ React.createElement(React.Fragment, null);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

x Runtime must be either `classic` or `automatic`.
,-[input.js:1:1]
1 | /** @jsxRuntime typo */
: ^^^^^^^^^^^^^^^^^^^^^^^
`----
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @jsxRuntime */
/** @jsxImportSource */
/** @jsxFrag */
/** @jsx */

<></>;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** @jsxRuntime */ /** @jsxImportSource */ /** @jsxFrag */ /** @jsx */ /*#__PURE__*/ React.createElement(React.Fragment, null);

1 comment on commit cc555ef

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: cc555ef Previous: ddb31d1 Ratio
es/full/minify/libraries/antd 1771788978 ns/iter (± 16669574) 1599975030 ns/iter (± 28758983) 1.11
es/full/minify/libraries/d3 437184199 ns/iter (± 8211800) 396959922 ns/iter (± 15446481) 1.10
es/full/minify/libraries/echarts 1646626722 ns/iter (± 26247902) 1589097115 ns/iter (± 42324391) 1.04
es/full/minify/libraries/jquery 101124919 ns/iter (± 1505968) 84644941 ns/iter (± 2910349) 1.19
es/full/minify/libraries/lodash 123081517 ns/iter (± 1713130) 112608268 ns/iter (± 5492771) 1.09
es/full/minify/libraries/moment 56858034 ns/iter (± 1884735) 51338152 ns/iter (± 2305632) 1.11
es/full/minify/libraries/react 18318908 ns/iter (± 480075) 17026397 ns/iter (± 536243) 1.08
es/full/minify/libraries/terser 612749535 ns/iter (± 7881364) 587856641 ns/iter (± 18152813) 1.04
es/full/minify/libraries/three 566433380 ns/iter (± 10163256) 531676215 ns/iter (± 20852964) 1.07
es/full/minify/libraries/typescript 3622765861 ns/iter (± 51835452) 3379635233 ns/iter (± 75698354) 1.07
es/full/minify/libraries/victory 756431610 ns/iter (± 12645296) 708962964 ns/iter (± 13900330) 1.07
es/full/minify/libraries/vue 157329616 ns/iter (± 2630021) 130996250 ns/iter (± 5992301) 1.20
es/full/codegen/es3 31514 ns/iter (± 1149) 31644 ns/iter (± 1198) 1.00
es/full/codegen/es5 31595 ns/iter (± 1184) 31371 ns/iter (± 766) 1.01
es/full/codegen/es2015 31614 ns/iter (± 644) 31323 ns/iter (± 1012) 1.01
es/full/codegen/es2016 31474 ns/iter (± 948) 31478 ns/iter (± 1033) 1.00
es/full/codegen/es2017 31478 ns/iter (± 236) 31591 ns/iter (± 1577) 1.00
es/full/codegen/es2018 31496 ns/iter (± 411) 31549 ns/iter (± 4155) 1.00
es/full/codegen/es2019 31548 ns/iter (± 1006) 31465 ns/iter (± 851) 1.00
es/full/codegen/es2020 31505 ns/iter (± 803) 31636 ns/iter (± 880) 1.00
es/full/all/es3 194404854 ns/iter (± 7990189) 178687799 ns/iter (± 5083764) 1.09
es/full/all/es5 184087025 ns/iter (± 9525468) 167611732 ns/iter (± 5243538) 1.10
es/full/all/es2015 158410817 ns/iter (± 5015893) 137190996 ns/iter (± 4834286) 1.15
es/full/all/es2016 155362470 ns/iter (± 7982369) 135971499 ns/iter (± 4862728) 1.14
es/full/all/es2017 144914360 ns/iter (± 9326334) 135173979 ns/iter (± 8248585) 1.07
es/full/all/es2018 148640063 ns/iter (± 7121701) 133845158 ns/iter (± 3564844) 1.11
es/full/all/es2019 143510252 ns/iter (± 5524097) 134003135 ns/iter (± 4549299) 1.07
es/full/all/es2020 144708352 ns/iter (± 7610179) 128619488 ns/iter (± 3947352) 1.13
es/full/parser 720679 ns/iter (± 24655) 698411 ns/iter (± 40476) 1.03
es/full/base/fixer 29730 ns/iter (± 595) 28402 ns/iter (± 892) 1.05
es/full/base/resolver_and_hygiene 87439 ns/iter (± 1450) 85567 ns/iter (± 7764) 1.02
serialization of ast node 215 ns/iter (± 3) 209 ns/iter (± 6) 1.03
serialization of serde 229 ns/iter (± 4) 225 ns/iter (± 6) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.