Skip to content

[compiler] Move capitalize to compiler #7199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 2, 2025
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
9 changes: 9 additions & 0 deletions .chronus/changes/capitalize-util-2025-4-1-15-35-12.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
changeKind: feature
packages:
- "@typespec/compiler"
- "@typespec/openapi3"
- "@typespec/protobuf"
---

Add "capitalize" string helper to compiler
3 changes: 3 additions & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
"./internals/prettier-formatter": {
"import": "./dist/src/internals/prettier-formatter.js"
},
"./casing": {
"import": "./dist/src/casing/index.js"
}
},
"browser": {
Expand Down
6 changes: 6 additions & 0 deletions packages/compiler/src/casing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Simple utility function to capitalize a string.
*/
export function capitalize<S extends string>(s: S): Capitalize<S> {
return (s.charAt(0).toUpperCase() + s.slice(1)) as Capitalize<S>;
}
5 changes: 3 additions & 2 deletions packages/compiler/src/typekit/utils/get-plausible-name.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { capitalize } from "../../casing/index.js";
import { isTemplateInstance } from "../../core/type-utils.js";
import { Enum, Interface, Model, Scalar, Union } from "../../core/types.js";

Expand All @@ -14,9 +15,9 @@ export function getPlausibleName(type: Model | Union | Enum | Scalar | Interface
if (a.entityKind === "Type") {
switch (a.kind) {
case "Scalar":
// Box<scalar> is not a scalar so capital case naming convention applies
const name = getPlausibleName(a);
return name.charAt(0).toUpperCase() + name.slice(1);
// Box<scalar> is not a scalar so capital case naming convention applies
return capitalize(name);
case "Model":
case "Interface":
case "Enum":
Expand Down
16 changes: 16 additions & 0 deletions packages/compiler/test/casing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { capitalize } from "../src/casing/index.js";

describe("capitalize", () => {
it("should capitalize the first letter of a string", () => {
expect(capitalize("hello world")).toEqual("Hello world");
});

it("should return an empty string when input is empty", () => {
expect(capitalize("")).toEqual("");
});

it("should handle single-character strings", () => {
expect(capitalize("a")).toEqual("A");
});
});
8 changes: 1 addition & 7 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
isSecret,
resolveEncodedName,
} from "@typespec/compiler";
import { capitalize } from "@typespec/compiler/casing";
import { $ } from "@typespec/compiler/typekit";
import { MetadataInfo, Visibility, getVisibilitySuffix } from "@typespec/http";
import {
Expand Down Expand Up @@ -860,10 +861,3 @@ export const Builders = {
return builder;
},
} as const;

/**
* Simple utility function to capitalize a string.
*/
function capitalize<S extends string>(s: S) {
return (s.slice(0, 1).toUpperCase() + s.slice(1)) as Capitalize<S>;
}
8 changes: 1 addition & 7 deletions packages/protobuf/src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
Union,
} from "@typespec/compiler";
import { SyntaxKind } from "@typespec/compiler/ast";
import { capitalize } from "@typespec/compiler/casing";
import {
map,
matchType,
Expand Down Expand Up @@ -1005,13 +1006,6 @@ function isArray(t: Type) {
return t.kind === "Model" && t.name === "Array" && t.namespace?.name === "TypeSpec";
}

/**
* Simple utility function to capitalize a string.
*/
function capitalize<S extends string>(s: S) {
return (s.slice(0, 1).toUpperCase() + s.slice(1)) as Capitalize<S>;
}

/**
* Gets the syntactic return type target for an operation.
*
Expand Down
Loading