diff options
author | 2021-10-06 19:21:26 -0700 | |
---|---|---|
committer | 2021-10-06 19:21:26 -0700 | |
commit | a493c18193c4259efe272e4896187c562fd414b1 (patch) | |
tree | 9e0b1e73872559ee9ae7a26dffc823cca5a2c3f4 | |
parent | 5370ea71c0b3a6759c481f96608ce855bd043bc8 (diff) | |
download | bun-a493c18193c4259efe272e4896187c562fd414b1.tar.gz bun-a493c18193c4259efe272e4896187c562fd414b1.tar.zst bun-a493c18193c4259efe272e4896187c562fd414b1.zip |
Fix JSX transform edgecase with static children
-rw-r--r-- | integration/snippets/react-context-value-func.tsx | 34 | ||||
-rw-r--r-- | packages/bun-cli-darwin-x64/package.json | 2 | ||||
-rw-r--r-- | packages/bun-cli/package.json | 2 | ||||
-rw-r--r-- | src/js_parser/js_parser.zig | 45 | ||||
-rw-r--r-- | src/linker.zig | 5 |
5 files changed, 57 insertions, 31 deletions
diff --git a/integration/snippets/react-context-value-func.tsx b/integration/snippets/react-context-value-func.tsx new file mode 100644 index 000000000..5f38a5d1c --- /dev/null +++ b/integration/snippets/react-context-value-func.tsx @@ -0,0 +1,34 @@ +import React from "react"; + +const Context = React.createContext({}); + +const ContextProvider = ({ children }) => { + const [cb, setCB] = React.useState(function () {}); + const foo = true; + + return <Context.Provider value={cb}>{children(foo)}</Context.Provider>; +}; + +const ContextValue = ({}) => ( + <Context.Consumer> + {(foo) => { + if (foo) { + return <div>Worked!</div>; + } + + throw `Value "${foo}"" should be true`; + }} + </Context.Consumer> +); + +const TestComponent = () => ( + <ContextProvider> + <ContextValue /> + </ContextProvider> +); + +export function test() { + const foo = <TestComponent />; + + return testDone(import.meta.url); +} diff --git a/packages/bun-cli-darwin-x64/package.json b/packages/bun-cli-darwin-x64/package.json index 9a492fa7a..73ccbdde8 100644 --- a/packages/bun-cli-darwin-x64/package.json +++ b/packages/bun-cli-darwin-x64/package.json @@ -4,5 +4,5 @@ }, "name": "bun-cli-darwin-x64", "repository": "https://github.com/jarred-sumner/bun", - "version": "0.0.31" + "version": "0.0.32" } diff --git a/packages/bun-cli/package.json b/packages/bun-cli/package.json index 052d60e29..2e2682e24 100644 --- a/packages/bun-cli/package.json +++ b/packages/bun-cli/package.json @@ -9,5 +9,5 @@ "postinstall": "node postinstall.js", "prepublishOnly": "rm -rf ./bin/bun; chmod +x ./reset-bin.js; cp ./reset-bin.js ./bin/bun" }, - "version": "0.0.31" + "version": "0.0.32" } diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index 6acf11e6c..3530f84a9 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -11201,40 +11201,35 @@ pub fn NewParser( // children: [el1, el2] // } - const is_static_jsx = e_.children.len == 0 or e_.children.len > 1 or e_.children[0].data != .e_array; + { + var last_child: usize = 0; + for (e_.children[0..children_count]) |child| { + e_.children[last_child] = p.visitExpr(child); + // if tree-shaking removes the element, we must also remove it here. + last_child += @intCast(usize, @boolToInt(e_.children[last_child].data != .e_missing)); + } + e_.children = e_.children[0..last_child]; + } + + const children_key = Expr{ .data = jsxChildrenKeyData, .loc = expr.loc }; + + // Babel defines static jsx as children.len > 1 + const is_static_jsx = last_child > 1; // if (p.options.jsx.development) { - switch (children_count) { + switch (last_child) { 0 => {}, 1 => { - // static jsx must always be an array - if (is_static_jsx) { - const children_key = Expr{ .data = jsxChildrenKeyData, .loc = expr.loc }; - e_.children[0] = p.visitExpr(e_.children[0]); - props.append(G.Property{ - .key = children_key, - .value = p.e(E.Array{ - .items = e_.children[0..children_count], - .is_single_line = e_.children.len < 2, - }, expr.loc), - }) catch unreachable; - } else { - const children_key = Expr{ .data = jsxChildrenKeyData, .loc = expr.loc }; - props.append(G.Property{ - .key = children_key, - .value = p.visitExpr(e_.children[0]), - }) catch unreachable; - } + props.append(G.Property{ + .key = children_key, + .value = e_.children[0], + }) catch unreachable; }, else => { - for (e_.children[0..children_count]) |child, i| { - e_.children[i] = p.visitExpr(child); - } - const children_key = Expr{ .data = jsxChildrenKeyData, .loc = expr.loc }; props.append(G.Property{ .key = children_key, .value = p.e(E.Array{ - .items = e_.children[0..children_count], + .items = e_.children, .is_single_line = e_.children.len < 2, }, expr.loc), }) catch unreachable; diff --git a/src/linker.zig b/src/linker.zig index 16d6d8342..ecc6951d8 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -197,10 +197,7 @@ pub const Linker = struct { comptime ignore_runtime: bool, ) !void { var needs_runtime = result.ast.uses_exports_ref or result.ast.uses_module_ref or result.ast.runtime_imports.hasAny(); - const source_dir = if (file_path.is_symlink and file_path.pretty.len > 0 and import_path_format == .absolute_url and linker.options.platform.isNotBun()) - Fs.PathName.init(file_path.pretty).dirWithTrailingSlash() - else - file_path.sourceDir(); + const source_dir = file_path.sourceDir(); var externals = std.ArrayList(u32).init(linker.allocator); var needs_bundle = false; var first_bundled_index: ?u32 = null; |