Skip to content

Commit c9d4f1f

Browse files
committed
camelized props
1 parent cb2137c commit c9d4f1f

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

__fixtures__/output/chain.camel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ export interface CosmosChain {
150150
rest?: Endpoint[];
151151
grpc?: Endpoint[];
152152
wss?: Endpoint[];
153-
"grpc-web"?: Endpoint[];
154-
"evm-http-jsonrpc"?: Endpoint[];
153+
grpcWeb?: Endpoint[];
154+
evmHttpJsonrpc?: Endpoint[];
155155
};
156156
explorers?: Explorer[];
157157
keywords?: string[];

__tests__/__snapshots__/chain.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ export interface CosmosChain {
315315
rest?: Endpoint[];
316316
grpc?: Endpoint[];
317317
wss?: Endpoint[];
318-
"grpc-web"?: Endpoint[];
319-
"evm-http-jsonrpc"?: Endpoint[];
318+
grpcWeb?: Endpoint[];
319+
evmHttpJsonrpc?: Endpoint[];
320320
};
321321
explorers?: Explorer[];
322322
keywords?: string[];

src/schema.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as t from "@babel/types";
33

44
import { SchemaTSContext, type SchemaTSOptions } from "./context";
55
import type { JSONSchema } from "./types";
6-
import { isValidIdentifier, toCamelCase, toPascalCase } from "./utils";
6+
import { isValidIdentifier, isValidIdentifierCamelized, toCamelCase, toPascalCase } from "./utils";
77

88
const identifier = (name: string, typeAnnotation: t.TSTypeAnnotation) => {
99
const i = t.identifier(name);
@@ -126,13 +126,18 @@ function createPropertySignature(
126126
required: string[],
127127
schema: JSONSchema
128128
): t.TSPropertySignature {
129-
130-
const isIdent = isValidIdentifier(key);
131129
let camelCaseFn: (str: string) => string = toCamelCase;
132130
if (ctx.options.camelCaseFn) camelCaseFn = ctx.options.camelCaseFn;
133131
const name = ctx.options.camelCase ? camelCaseFn(key) : key;
134132
const propType = getTypeForProp(ctx, prop, required, schema);
135-
const identifier = isIdent ? t.identifier(name) : t.stringLiteral(key);
133+
let identifier: t.Identifier | t.StringLiteral;
134+
let isIdent: boolean;
135+
if (ctx.options.camelCase) {
136+
isIdent = isValidIdentifierCamelized(key);
137+
} else {
138+
isIdent = isValidIdentifier(key);
139+
}
140+
identifier = isIdent ? t.identifier(name) : t.stringLiteral(key);
136141
const propSig = t.tsPropertySignature(
137142
identifier,
138143
t.tsTypeAnnotation(propType)

src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ export function toCamelCase(key: string) {
1212
.replace(/^./, (c) => c.toLowerCase());
1313
}
1414

15-
// Determine if the key is a valid JavaScript identifier
15+
// // Determine if the key is a valid JavaScript identifier
1616
export function isValidIdentifier(key: string) {
1717
return /^[$A-Z_][0-9A-Z_$]*$/i.test(key) && !/^[0-9]+$/.test(key);
18-
}
18+
}
19+
20+
21+
// Determine if the key is a valid JavaScript-like identifier, allowing internal hyphens
22+
export function isValidIdentifierCamelized(key: string) {
23+
return /^[$A-Z_][0-9A-Z_$\-]*$/i.test(key) && !/^[0-9]+$/.test(key) && !/^-/.test(key);
24+
}

0 commit comments

Comments
 (0)