mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 17:01:37 +03:00
fix(codemode): preserve null in text encoding (#57639)
This commit is contained in:
@@ -220,7 +220,11 @@ export const textEncoderGlobal = <R>(ctx: Interpreter<R>) => {
|
||||
const encoder = new TextEncoder()
|
||||
defineAccessor(proto, "encoding", () => "utf-8")
|
||||
methods(builtins, proto, [
|
||||
["encode", 0, (_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] ?? "")))],
|
||||
[
|
||||
"encode",
|
||||
0,
|
||||
(_, args) => new Bytes(builtins.Uint8Array, encoder.encode(coerceToString(args[0] === undefined ? "" : args[0]))),
|
||||
],
|
||||
])
|
||||
return constructor(builtins, proto, {
|
||||
name: "TextEncoder",
|
||||
|
||||
@@ -1176,6 +1176,15 @@ describe("Uint8Array", () => {
|
||||
})
|
||||
|
||||
describe("TextEncoder and TextDecoder", () => {
|
||||
test("encodes null as text while omitted and undefined inputs stay empty", async () => {
|
||||
expect(
|
||||
await value(`
|
||||
const encoder = new TextEncoder()
|
||||
return [[...encoder.encode(null)], [...encoder.encode()], [...encoder.encode(undefined)]]
|
||||
`),
|
||||
).toEqual([[110, 117, 108, 108], [], []])
|
||||
})
|
||||
|
||||
test("round-trips UTF-8 and exposes the standard fields", async () => {
|
||||
expect(
|
||||
await value(`
|
||||
|
||||
Reference in New Issue
Block a user