Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/generator/typescript.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <sourcemeta/codegen/generator.h>

#include <iomanip> // std::hex, std::setfill, std::setw
#include <sstream> // std::ostringstream

namespace {
Expand All @@ -15,6 +16,12 @@ auto escape_string(const std::string &input) -> std::string {
case '"':
result << "\\\"";
break;
case '\b':
result << "\\b";
break;
case '\f':
result << "\\f";
break;
case '\n':
result << "\\n";
break;
Expand All @@ -25,7 +32,13 @@ auto escape_string(const std::string &input) -> std::string {
result << "\\t";
break;
default:
result << character;
// Escape other control characters (< 0x20) using \uXXXX format
if (static_cast<unsigned char>(character) < 0x20) {
result << "\\u" << std::hex << std::setfill('0') << std::setw(4)
<< static_cast<int>(static_cast<unsigned char>(character));
} else {
result << character;
}
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type TestWithus = string;

export type TestWithformfeed = string;

export type TestWithbackspace = string;

export type TestWithsoh = string;

export type TestWithnull = string;

export type TestNormal = string;

export type TestAdditionalProperties = never;

export interface Test {
"normal"?: TestNormal;
"with\bbackspace"?: TestWithbackspace;
"with\fformfeed"?: TestWithformfeed;
"with\u0000null"?: TestWithnull;
"with\u0001soh"?: TestWithsoh;
"with\u001fus"?: TestWithus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"defaultPrefix": "Test"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"normal": { "type": "string" },
"with\bbackspace": { "type": "string" },
"with\fformfeed": { "type": "string" },
"with\u0000null": { "type": "string" },
"with\u0001soh": { "type": "string" },
"with\u001fus": { "type": "string" }
},
"additionalProperties": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Test } from "./expected";

// Valid: object with control characters in property names
const validObject: Test = {
"normal": "hello",
"with\bbackspace": "value1",
"with\fformfeed": "value2",
"with\u0000null": "value3",
"with\u0001soh": "value4",
"with\u001fus": "value5"
};

// Valid: empty object (all properties are optional)
const emptyObject: Test = {};

// Valid: partial object
const partialObject: Test = {
"normal": "just normal"
};

// Invalid: extra property not allowed
const extraProp: Test = {
"normal": "hello",
// @ts-expect-error - extra property not allowed
"extra": "not allowed"
};

// Invalid: wrong type for property
const wrongType: Test = {
// @ts-expect-error - must be string
"normal": 123
};