Skip to content

Commit 0c198cc

Browse files
authored
Improve mapping for "with" hint (#133)
1 parent 9e5f4be commit 0c198cc

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

.changeset/loud-pets-wait.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@knuckles/fabricator": patch
3+
---
4+
5+
Add "if" method to chunk.

.changeset/spotty-olives-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@knuckles/typescript": patch
3+
---
4+
5+
Fix mapping with "with" hint.

packages/fabricator/src/chunk.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ export class Chunk {
7979
return this;
8080
}
8181

82+
if(
83+
condition: unknown,
84+
positive: (chunk: this) => void,
85+
negative?: (chunk: this) => void,
86+
) {
87+
if (condition) {
88+
positive(this);
89+
} else {
90+
negative?.(this);
91+
}
92+
}
93+
8294
marker(id: string): this {
8395
this.#markers.add(new DynamicMarker(id, this.#text.length));
8496
return this;

packages/typescript/src/transpiler/transpiler.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,26 @@ class Renderer {
281281

282282
#renderWithVirtualElement(node: ko.WithVirtualElement): Chunk {
283283
const childContext = new Chunk()
284-
.append(`${ns}.hints.with(${ns}.type<typeof import(`)
285-
.append(quote(node.import.module.value))
286-
.append(")")
287-
.append(
288-
node.import.identifier.value === "*"
289-
? ""
290-
: "[" + quote(node.import.identifier.value) + "]",
284+
.append(`${ns}.hints.with(${ns}.type<`)
285+
.while(
286+
(chunk) =>
287+
chunk
288+
.append("typeof import(")
289+
.append(node.import.module.text, { mirror: node.import.module })
290+
.append(")")
291+
.if(node.import.identifier.value !== "*", (chunk) =>
292+
chunk.while(
293+
(chunk) =>
294+
chunk
295+
.append('["')
296+
.append(quote(node.import.identifier.value).slice(1, -1), {
297+
mirror: node.import.identifier,
298+
})
299+
.append('"]'),
300+
{ blame: node.import.identifier },
301+
),
302+
),
303+
{ blame: node.import },
291304
)
292305
.append(">(), $context)");
293306

0 commit comments

Comments
 (0)