Skip to content

Commit

Permalink
fix(swc): Fix bugs (#2396)
Browse files Browse the repository at this point in the history
swc_ecma_codegen:
 - Check for comments deeply while emitting a return statement.

swc_ecma_transforms_react:
 - Fix handling of texts in attributes.
  • Loading branch information
kdy1 authored Oct 10, 2021
1 parent a330322 commit ff2baf7
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ecmascript/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_codegen"
repository = "https://github.com/swc-project/swc.git"
version = "0.74.2"
version = "0.74.3"

[dependencies]
bitflags = "1"
Expand Down
63 changes: 55 additions & 8 deletions ecmascript/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,57 @@ where
emit!(node.body);
}

fn has_leading_comment(&self, arg: &Expr) -> bool {
if let Some(cmt) = self.comments {
let lo = arg.span().lo;

// see #415
if cmt.has_leading(lo) {
return true;
}
}

match arg {
Expr::Call(c) => match &c.callee {
ExprOrSuper::Super(callee) => {
if let Some(cmt) = self.comments {
let lo = callee.span.lo;

if cmt.has_leading(lo) {
return true;
}
}
}
ExprOrSuper::Expr(callee) => {
if self.has_leading_comment(&callee) {
return true;
}
}
},

Expr::Member(m) => match &m.obj {
ExprOrSuper::Super(obj) => {
if let Some(cmt) = self.comments {
let lo = obj.span.lo;

if cmt.has_leading(lo) {
return true;
}
}
}
ExprOrSuper::Expr(obj) => {
if self.has_leading_comment(&obj) {
return true;
}
}
},

_ => {}
}

false
}

#[emitter]
fn emit_return_stmt(&mut self, n: &ReturnStmt) -> Result {
self.emit_leading_comments_of_span(n.span, false)?;
Expand All @@ -2276,14 +2327,10 @@ where

if let Some(ref arg) = n.arg {
let need_paren = !n.arg.span().is_dummy()
&& if let Some(cmt) = self.comments {
let lo = n.arg.span().lo();

// see #415
cmt.has_leading(lo)
} else {
false
};
&& n.arg
.as_deref()
.map(|expr| self.has_leading_comment(expr))
.unwrap_or(false);
if need_paren {
punct!("(");
} else {
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/react/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_transforms_react"
repository = "https://github.com/swc-project/swc.git"
version = "0.48.0"
version = "0.48.1"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Expand Down
9 changes: 6 additions & 3 deletions ecmascript/transforms/react/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,12 @@ fn transform_jsx_attr_str(v: &str) -> String {
match c {
'\u{0008}' => buf.push_str("\\b"),
'\u{000c}' => buf.push_str("\\f"),
'\n' => buf.push_str("\\n"),
'\r' => buf.push_str("\\r"),
'\t' => buf.push_str("\\t"),
' ' | '\n' | '\r' | '\t' => {
if buf.ends_with(' ') {
} else {
buf.push(' ')
}
}
'\u{000b}' => buf.push_str("\\v"),
'\0' => buf.push_str("\\x00"),

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default <A
className={b}
header="C"
subheader="D
E"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default React.createElement(A, {
className: b,
header: "C",
subheader: "D E"
});
10 changes: 10 additions & 0 deletions tests/fixture/codegen/comment-1/es5/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsc": {
"target": "es5",
"externalHelpers": true,
"parser": {
"syntax": "typescript",
"tsx": true
}
}
}
16 changes: 16 additions & 0 deletions tests/fixture/codegen/comment-1/es5/input/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function a({ b = [] }: { b: C[] }) {
const t = useMemo(() => {
return [
// Cmt1
...a.slice(0, 1),
// Cmt2
...b,
// Cmt3
...c.slice(1),
];
}, [frameworks]);

return 1;
}

export default a;
14 changes: 14 additions & 0 deletions tests/fixture/codegen/comment-1/es5/output/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as swcHelpers from "@swc/helpers";
function a(param) {
var _b = param.b, b = _b === void 0 ? [] : _b;
var t = useMemo(function() {
return(// Cmt1
swcHelpers.toConsumableArray(a.slice(0, 1)).concat(// Cmt2
swcHelpers.toConsumableArray(b), // Cmt3
swcHelpers.toConsumableArray(c.slice(1))));
}, [
frameworks
]);
return 1;
}
export default a;
8 changes: 8 additions & 0 deletions tests/fixture/codegen/jsx-1/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": true
}
}
}
6 changes: 6 additions & 0 deletions tests/fixture/codegen/jsx-1/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default <A
className={b}
header="C"
subheader="D
E"
/>
5 changes: 5 additions & 0 deletions tests/fixture/codegen/jsx-1/output/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default /*#__PURE__*/ React.createElement(A, {
className: b,
header: "C",
subheader: "D E"
});
2 changes: 1 addition & 1 deletion tests/fixture/issue-1233/case-1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function Component() {
return(/*#__PURE__*/ React.createElement("div", {
name: "A\\n\\n B"
name: "A B"
}));
}
2 changes: 1 addition & 1 deletion tests/fixture/issue-1661/case1/output/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
console.log(/*#__PURE__*/ React.createElement("h1", {
value: "abc\\nas"
value: "abc as"
}, "s"));

0 comments on commit ff2baf7

Please sign in to comment.