Skip to content

Commit f535c90

Browse files
committed
fix(guards): quote kebab-case object property identifiers
Signed-off-by: Ryan Bower <rbower@qti.qualcomm.com>
1 parent 6b6f42f commit f535c90

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

packages/common/typedoc/src/guards.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export function isUndefinedType(
3838
return type?.type === "intrinsic" && type?.name === "undefined"
3939
}
4040

41+
const identifierName = /^[$A-Z_a-z][$\w]*$/
42+
43+
export function formatObjectPropertyName(name: string) {
44+
return identifierName.test(name) ? name : JSON.stringify(name)
45+
}
46+
4147
export function getTypeFromProperties(
4248
properties: SerializedType["properties"],
4349
) {
@@ -47,7 +53,9 @@ export function getTypeFromProperties(
4753
return properties
4854
.filter((property) => property.name && property.type)
4955
.map((property) => {
50-
return `${property.name}${property.required ? "" : "?"}: ${
56+
return `${formatObjectPropertyName(property.name)}${
57+
property.required ? "" : "?"
58+
}: ${
5159
property.inheritDoc
5260
? property.type
5361
: (property.referencedType ?? property.type)

packages/common/typedoc/tests/src/package-1/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,20 @@ export type TestTuple1 = [string, boolean, string, number]
1717
* Template literal
1818
*/
1919
export type TestTemplateLiteral1 = `${string}.${number}`
20+
21+
export interface DashCasedBindingProps {
22+
"data-disabled": ""
23+
"data-stepper-part": "prev-trigger"
24+
dir: "ltr" | "rtl"
25+
disabled: boolean
26+
}
27+
28+
export type DashCasedRenderProp<Props> = string | ((props: Props) => string)
29+
30+
/**
31+
* @public
32+
* @interface
33+
*/
34+
export interface DashCasedRenderPropHost {
35+
children: DashCasedRenderProp<DashCasedBindingProps>
36+
}

packages/common/typedoc/tests/types.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ function getTestCases(parseResult: ParseResult): TestCase[] {
6262
expected: "`${string}.${number}`",
6363
},
6464
],
65+
[
66+
"DashCasedBindingProps",
67+
{
68+
actual: interfaces.DashCasedBindingProps.resolvedType.type,
69+
expected:
70+
"{\"data-disabled\": ''; \"data-stepper-part\": 'prev-trigger'; dir: 'ltr' | 'rtl'; disabled: boolean;}",
71+
},
72+
],
73+
[
74+
"DashCasedRenderPropHost.children",
75+
{
76+
actual: findResolvedType(props.DashCasedRenderPropHost, "children")
77+
.type,
78+
expected:
79+
"string | ((props: {\"data-disabled\": ''; \"data-stepper-part\": 'prev-trigger'; dir: 'ltr' | 'rtl'; disabled: boolean;}) => string)",
80+
},
81+
],
6582
[
6683
"TestTuple1",
6784
{

0 commit comments

Comments
 (0)