Skip to content

Commit

Permalink
Fix syntax error when using quotes or escapes in target name
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell committed Oct 25, 2024
1 parent 419888f commit c1f7cb5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -870,10 +870,10 @@ export function clientCode(
return (
versionedIdentifier(outputPath.targetName, webSocketPort) +
ClientCode.client.replace(
new RegExp(`%(${join(Object.keys(replacements), "|")})%`, "g"),
(match: string, name: string) =>
new RegExp(`"%(${join(Object.keys(replacements), "|")})%"`, "g"),
(_, name: string) =>
/* v8 ignore next */
replacements[name] ?? match,
Codec.JSON.stringify(Codec.string, replacements[name] ?? name),
)
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Hot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ describe("hot", () => {
expect(div.outerHTML).toMatchInlineSnapshot(`<div>main</div>`);
});

test("successful connect (target name with escapes)", async () => {
const { div } = await run({
fixture: "basic",
args: ['"Esc\\apes"'],
scripts: ["Escapes.js"],
init: (node) => {
window.Elm?.["HtmlMain"]?.init({ node });
},
onIdle: () => "Stop",
});

expect(div.outerHTML).toMatchInlineSnapshot(`<div>Hello, World!</div>`);
});

test("connect with elm.json error", async () => {
const fixture = "connect-with-elm-json-error";
const dir = path.join(FIXTURES_DIR, fixture);
Expand Down Expand Up @@ -979,6 +993,7 @@ describe("hot", () => {
SendBadJson
Reconnect
HttpCaching
"Esc\\apes"
If you want to have this target compiled, restart elm-watch either with more CLI arguments or no CLI arguments at all!
▲ ❌ 13:10:05 TargetDisabled
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/hot/basic/elm-watch.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@
"src/HtmlMain.elm"
],
"output": "build/HttpCaching.js"
},
"\"Esc\\apes\"": {
"inputs": [
"src/HtmlMain.elm"
],
"output": "build/Escapes.js"
}
}
}

0 comments on commit c1f7cb5

Please sign in to comment.