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
7 changes: 4 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reflag/cli",
"version": "1.0.2",
"version": "1.0.3",
"packageManager": "yarn@4.1.1",
"description": "CLI for Reflag service",
"main": "./dist/index.js",
Expand Down Expand Up @@ -31,8 +31,9 @@
"scripts": {
"build": "tsc && shx chmod +x dist/index.js",
"reflag": "yarn build && ./dist/index.js",
"test": "vitest -c vite.config.js",
"test:ci": "vitest run -c vite.config.js --reporter=default --reporter=junit --outputFile=junit.xml",
"test": "vitest run",
"test:watch": "vitest",
"test:ci": "yarn test --reporter=default --reporter=junit --outputFile=junit.xml",
"coverage": "vitest run --coverage",
"lint": "eslint .",
"lint:ci": "eslint --output-file eslint-report.json --format json .",
Expand Down
42 changes: 42 additions & 0 deletions packages/cli/test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, it } from "vitest";
import {
JSONToType,
mergeTypeASTs,
quoteKey,
stringifyTypeAST,
toTypeAST,
TypeAST,
Expand Down Expand Up @@ -300,6 +301,37 @@ describe("JSON utilities", () => {
expect(stringifyTypeAST({ kind: "object", properties: [] })).toBe("{}");
});

it("should quote object keys with special characters", () => {
const ast: TypeAST = {
kind: "object",
properties: [
{
key: "my-key",
type: { kind: "primitive", type: "string" },
optional: false,
},
{
key: "my key",
type: { kind: "primitive", type: "string" },
optional: false,
},
{
key: "my.key",
type: { kind: "primitive", type: "string" },
optional: false,
},
{
key: "123key",
type: { kind: "primitive", type: "string" },
optional: false,
},
],
};

const expected = `{\n "my-key": string,\n "my key": string,\n "my.key": string,\n "123key": string\n}`;
expect(stringifyTypeAST(ast)).toBe(expected);
});

it("should stringify union types", () => {
const ast: TypeAST = {
kind: "union",
Expand Down Expand Up @@ -422,4 +454,14 @@ describe("JSON utilities", () => {
).toBe(expected);
});
});

describe("quoteKey", () => {
it("should quote keys with special characters", () => {
expect(quoteKey("my-key")).toBe('"my-key"');
expect(quoteKey("my key")).toBe('"my key"');
expect(quoteKey("my.key")).toBe('"my.key"');
expect(quoteKey("123key")).toBe('"123key"');
expect(quoteKey("key")).toBe("key");
});
});
});
4 changes: 2 additions & 2 deletions packages/cli/utils/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dirname, isAbsolute, join } from "node:path";

import { Flag, RemoteConfig } from "../services/flags.js";

import { JSONToType } from "./json.js";
import { JSONToType, quoteKey } from "./json.js";

export type GenFormat = "react" | "node";

Expand Down Expand Up @@ -118,7 +118,7 @@ ${flags
.map(({ key }) => {
const config = configDefs.get(key);
return indentLines(
`"${key}": ${config?.definition ? `{ config: { payload: ${config.name} } }` : "boolean"};`,
`${quoteKey(key)}: ${config?.definition ? `{ config: { payload: ${config.name} } }` : "boolean"};`,
4,
);
})
Expand Down
17 changes: 10 additions & 7 deletions packages/cli/utils/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,12 @@ export function stringifyTypeAST(ast: TypeAST, nestLevel = 0): string {
if (ast.properties.length === 0) return "{}";

return `{\n${ast.properties
.map(
({ key, optional, type }) =>
`${nextIndent}${key}${optional ? "?" : ""}: ${stringifyTypeAST(
type,
nestLevel + 1,
)}`,
)
.map(({ key, optional, type }) => {
return `${nextIndent}${quoteKey(key)}${optional ? "?" : ""}: ${stringifyTypeAST(
type,
nestLevel + 1,
)}`;
})
.join(",\n")}\n${indent}}`;

case "union":
Expand All @@ -198,6 +197,10 @@ export function stringifyTypeAST(ast: TypeAST, nestLevel = 0): string {
}
}

export function quoteKey(key: string): string {
return /[^a-zA-Z0-9_]/.test(key) || /^[0-9]/.test(key) ? `"${key}"` : key;
}

// Convert JSON array to TypeScript type
export function JSONToType(json: JSONPrimitive[]): string | null {
if (!json.length) return null;
Expand Down