Skip to content

Commit 8a6d167

Browse files
authored
Merge pull request #435 from code-hike/jsx-dev
Fix `jsxDEV` warning
2 parents af896e8 + ead378e commit 8a6d167

22 files changed

+5500
-8
lines changed

.changeset/lovely-houses-rule.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"codehike": patch
3+
---
4+
5+
Fix `jsxDEV` warning

apps/web/content/blog/from-remark-to-rsc.mdx

+2
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ authors: [pomber]
66
draft: true
77
---
88

9+
Content usually needs some kind of transformation before being rendered.
10+
911
A remark plugin is a function that transforms pieces of a markdown file.

apps/web/next.config.mjs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import createNextDocsMDX from "next-docs-mdx/config"
22
import { remarkCodeHike, recmaCodeHike } from "codehike/mdx"
3+
// import fs from "node:fs"
4+
// import { jsx, toJs } from "estree-util-to-js"
5+
// function recmaPlugin() {
6+
// return (tree) => {
7+
// const result = toJs(tree, { handlers: jsx })
8+
// // console.log("```js")
9+
// // console.log(result.value)
10+
// // console.log("```")
11+
// fs.writeFileSync("recma.js", result.value)
12+
// }
13+
// }
314

415
/** @type {import('codehike/mdx').CodeHikeConfig} */
516
const chConfig = {
@@ -17,7 +28,7 @@ const withMDX = createNextDocsMDX({
1728
mdxOptions: {
1829
remarkPlugins: [[remarkCodeHike, chConfig]],
1930
recmaPlugins: [[recmaCodeHike, chConfig]],
20-
jsx: true,
31+
// jsx: true,
2132
},
2233
})
2334

apps/web/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"codehike": "workspace:*",
2525
"diff": "^5.1.0",
2626
"dotenv": "^16.4.1",
27+
"estree-util-to-js": "^2.0.0",
2728
"lucide-react": "^0.303.0",
2829
"next": "14.1.0",
2930
"next-docs-mdx": "7.1.2",

packages/codehike/src/mdx/3.transform-hike-props.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function forEachHikeElementMoveChildrenToHikeProp(root: any, jsxOn: boolean) {
137137
visit(root, (node: any) => {
138138
if (isElementWithHikeAttribute(node, jsxOn)) {
139139
if (jsxOn) {
140-
moveChildrenToHikePropJSX(node, jsxOn)
140+
moveChildrenToHikePropJSX(node)
141141
} else {
142142
moveChildrenToHikeProp(node)
143143
}
@@ -157,7 +157,7 @@ function isElementWithHikeAttribute(node: any, jsxOn: boolean) {
157157
)
158158
}
159159

160-
function moveChildrenToHikePropJSX(node: any, jsxOn: boolean) {
160+
function moveChildrenToHikePropJSX(node: any) {
161161
// dictionary of children by path
162162
const childrenByPath: any = {}
163163
node.children.forEach((slot: any) => {
@@ -252,8 +252,8 @@ function moveChildrenToHikeProp(node: any) {
252252
(p: any) => p.key.name !== "children",
253253
)
254254

255-
// jsxs calls can only have arrays as children, so we turn any jsxs without array into jsx call
256255
visit(node, function (node: any) {
256+
// jsxs calls can only have arrays as children, so we turn any jsxs without array into jsx call
257257
if (node.type === "CallExpression" && node.callee.name === "_jsxs") {
258258
const childrenProp = node.arguments[1].properties.find(
259259
(p: any) => p.key.value === "children",
@@ -262,6 +262,16 @@ function moveChildrenToHikeProp(node: any) {
262262
node.callee.name = "_jsx"
263263
}
264264
}
265+
266+
// same, but in dev mode the name is _jsxDEV, and we should change the `isStaticChildren` argument
267+
if (node.type === "CallExpression" && node.callee.name === "_jsxDEV") {
268+
const childrenProp = node.arguments[1].properties.find(
269+
(p: any) => p.key.value === "children",
270+
)
271+
if (childrenProp && childrenProp.value.type !== "ArrayExpression") {
272+
node.arguments[3] = { type: "Literal", value: false }
273+
}
274+
}
265275
})
266276
}
267277

packages/codehike/tests/md-suite/basic.0.mdx

+4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ snapshots:
77
- before-recma-compiled-jsx
88
- before-recma-compiled-function
99
- before-recma-js
10+
- before-recma-js-dev
1011
- before-recma-jsx
1112
- after-recma-js
13+
- after-recma-js-dev
1214
- after-recma-jsx
1315
- compiled-js
16+
- compiled-js-dev
1417
- compiled-jsx
1518
- compiled-function
1619
- parsed-jsx
1720
- rendered
21+
- rendered-dev
1822
---
1923

2024
# Lorem

0 commit comments

Comments
 (0)