Skip to content

Commit 9af04c5

Browse files
committed
fix: Add support for trailing comma's in in sveltify() & improved detection of intrinsic elements.
Fixes #51
1 parent 18811ba commit 9af04c5

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/bfanger/svelte-preprocess-react.git"
1212
},
13-
"version": "2.1.0",
13+
"version": "2.1.1",
1414
"license": "MIT",
1515
"type": "module",
1616
"scripts": {

src/lib/preprocessReact.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,28 @@ function transform(content, options) {
150150
}
151151
}
152152
}
153+
153154
if ("end" in componentsArg && typeof componentsArg.end === "number") {
155+
let tailingComma = false;
156+
const lastProp = componentsArg.properties.at(-1);
157+
if (
158+
lastProp &&
159+
"end" in lastProp &&
160+
typeof lastProp.end === "number"
161+
) {
162+
tailingComma = s
163+
.slice(lastProp.end, componentsArg.end - 1)
164+
.includes(",");
165+
}
154166
for (const [alias, { expression }] of aliases) {
155167
if (!aliased.has(alias)) {
168+
if (!tailingComma) {
169+
s.appendRight(componentsArg.end - 1, ", ");
170+
tailingComma = true;
171+
}
156172
s.appendRight(
157173
componentsArg.end - 1,
158-
`, ${alias}: ${expression === expression.toLowerCase() ? JSON.stringify(expression) : expression} `,
174+
`${alias}: ${expression.substring(0, 1) === expression.substring(0, 1).toLowerCase() ? JSON.stringify(expression) : expression}, `,
159175
);
160176
}
161177
}

src/tests/__snapshots__/preprocess.spec.ts.snap

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ exports[`svelte-preprocess-react > should convert text content to react children
1717
"
1818
`;
1919

20+
exports[`svelte-preprocess-react > should detect tailing comma when adding aliases 1`] = `
21+
"<script lang="ts">import inject$$ReactDOM from "react-dom/client"; import { renderToString as inject$$renderToString } from "react-dom/server"; import { sveltify } from "svelte-preprocess-react"; import { createPortal as inject$$createPortal } from "react-dom";
22+
const react = sveltify({
23+
section: "section",
24+
h1: "h1", p: "p", }, { ReactDOM: inject$$ReactDOM, createPortal: inject$$createPortal, renderToString: inject$$renderToString });
25+
;</script>
26+
27+
<react.section>
28+
<react.h1 react$children="Title" />
29+
<react.p react$children="Description" />
30+
</react.section>
31+
"
32+
`;
33+
2034
exports[`svelte-preprocess-react > should import 'react-dom/server' when ssr is enabled 1`] = `
2135
"<script lang="ts">import inject$$ReactDOM from "react-dom/client"; import { renderToString as inject$$renderToString } from "react-dom/server"; import { sveltify } from "svelte-preprocess-react"; import { createPortal as inject$$createPortal } from "react-dom";
2236
import Clicker from "./Clicker";
@@ -80,7 +94,7 @@ exports[`svelte-preprocess-react > should process <react.Component.Item> tags 1`
8094
"<script lang="ts">import inject$$ReactDOM from "react-dom/client"; import { renderToString as inject$$renderToString } from "react-dom/server"; import { sveltify } from "svelte-preprocess-react"; import { createPortal as inject$$createPortal } from "react-dom";
8195
import List from "./List";
8296
83-
const react = sveltify({ List , inject$$List$Item: List.Item , inject$$List$Item$Icon: List.Item.Icon }, { ReactDOM: inject$$ReactDOM, createPortal: inject$$createPortal, renderToString: inject$$renderToString });
97+
const react = sveltify({ List , inject$$List$Item: List.Item, inject$$List$Item$Icon: List.Item.Icon, }, { ReactDOM: inject$$ReactDOM, createPortal: inject$$createPortal, renderToString: inject$$renderToString });
8498
;</script>
8599
86100
<react.List>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script lang="ts">
2+
const react = sveltify({
3+
section: "section",
4+
});
5+
</script>
6+
7+
<react.section>
8+
<react.h1>Title</react.h1>
9+
<react.p>Description</react.p>
10+
</react.section>

src/tests/preprocess.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ describe("svelte-preprocess-react", () => {
139139
const output = await preprocess(src, preprocessReact(), { filename });
140140
expect(output.code).toMatchSnapshot();
141141
});
142+
143+
it("should detect tailing comma when adding aliases", async () => {
144+
const filename = resolveFilename("./fixtures/TrailingComma.svelte");
145+
const src = await readFile(filename, "utf8");
146+
const output = await preprocess(src, preprocessReact(), { filename });
147+
expect(output.code).toMatchSnapshot();
148+
});
142149
});
143150

144151
const base = dirname(fileURLToPath(import.meta.url));

0 commit comments

Comments
 (0)