Skip to content

Commit

Permalink
feat: support property comments (#34) (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerAberbach authored Dec 25, 2024
1 parent ef11ab1 commit 8906560
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 34 deletions.
16 changes: 14 additions & 2 deletions src/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ export const recordArbitrary = (

export type RecordArbitrary = {
type: `record`
properties: Map<string, { arbitrary: Arbitrary; required: boolean }>
properties: Map<
string,
{
arbitrary: Arbitrary
comment?: string
required: boolean
}
>
}

export const intersectionArbitrary = (
Expand Down Expand Up @@ -231,7 +238,12 @@ const getArbitraryKey = (arbitrary: Arbitrary): ArbitraryKey => {
return keyalesce([
arbitrary.type,
...flatMap(
([name, { arbitrary, required }]) => [name, arbitrary, required],
([name, { arbitrary, comment, required }]) => [
name,
arbitrary,
comment,
required,
],
arbitrary.properties,
),
])
Expand Down
76 changes: 45 additions & 31 deletions src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
refkey,
stc,
} from '@alloy-js/core'
import type { Child, Children } from '@alloy-js/core'
import type { Child } from '@alloy-js/core'
import type {
Arbitrary,
ArbitraryNamespace,
Expand Down Expand Up @@ -572,13 +572,16 @@ const RecordArbitrary = ({
ObjectExpression({
properties: pipe(
arbitrary.properties,
map(([name, { arbitrary }]) => [
map(([name, { arbitrary, comment }]) => [
name,
Arbitrary({
arbitrary,
sharedArbitraries,
currentStronglyConnectedArbitraries,
}),
{
comment,
value: Arbitrary({
arbitrary,
sharedArbitraries,
currentStronglyConnectedArbitraries,
}),
},
]),
reduce(toObject()),
),
Expand Down Expand Up @@ -638,28 +641,15 @@ const RecursiveReferenceArbitrary = ({
const ArrayExpression = ({ values }: { values: Child[] }): Child =>
code`[${ayJoin(values, { joiner: `, ` })}]`

const Commented = stc(
({ comment, children }: { comment?: string; children?: Child }) =>
ayJoin([comment && Comment({ comment }), children].filter(Boolean), {
joiner: `\n`,
}),
)

const Comment = ({ comment }: { comment: string }): Child => {
const lines = comment.split(`\n`)
if (lines.length <= 1) {
return code`/** ${comment} */`
}

return [`/**`, ...lines.map(line => ` * ${line}`), ` */`].join(`\n`)
}

const ObjectExpression = ({
properties,
emitEmpty = false,
singlePropertyOneLine = false,
}: {
properties: Record<string, Children>
properties: Record<
string,
{ comment: string | undefined; value: Child } | Child
>
emitEmpty?: boolean
singlePropertyOneLine?: boolean
}): Child => {
Expand All @@ -674,13 +664,21 @@ const ObjectExpression = ({

const objectProperties = pipe(
filteredProperties,
map(([name, value]) =>
ts.ObjectProperty({
name,
// https://github.com/alloy-framework/alloy/issues/42
value: typeof value === `number` ? String(value) : value,
}),
),
map(([name, property]) => {
const { comment, value } =
property !== null &&
typeof property === `object` &&
`comment` in property
? property
: { value: property }
return Commented({ comment }).children(
ts.ObjectProperty({
name,
// https://github.com/alloy-framework/alloy/issues/42
value: typeof value === `number` ? String(value) : value,
}),
)
}),
reduce(toArray()),
)
return singlePropertyOneLine && filteredProperties.size === 1
Expand All @@ -692,6 +690,22 @@ const ObjectExpression = ({
`
}

const Commented = stc(
({ comment, children }: { comment?: string; children?: Child }) =>
ayJoin([comment && Comment({ comment }), children].filter(Boolean), {
joiner: `\n`,
}),
)

const Comment = ({ comment }: { comment: string }): Child => {
const lines = comment.split(`\n`)
if (lines.length <= 1) {
return code`/** ${comment} */`
}

return [`/**`, ...lines.map(line => ` * ${line}`), ` */`].join(`\n`)
}

const CallExpression = ({
name,
args,
Expand Down
4 changes: 3 additions & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ const convertRecord = (
property.type,
mergeConstraints(constraints, getConstraints(program, property)),
)
const comment = getDoc(program, property)
return [
name,
property.defaultValue
Expand All @@ -508,9 +509,10 @@ const convertRecord = (
arbitrary,
convertValue(property.defaultValue),
]),
comment,
required: false,
}
: { arbitrary, required: !property.optional },
: { arbitrary, comment, required: !property.optional },
]
}),
reduce(toMap()),
Expand Down
10 changes: 10 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ test.each([
/** Non-shared model comment. */
model SingleLineNonSharedModel {
/** Property comment. */
property: SingleLineSharedModel
}
Expand All @@ -762,11 +763,16 @@ test.each([
* model comment.
*/
model MultiLineNonSharedModel {
/**
* Property
* comment.
*/
property: MultiLineSharedModel
}
/** Recursive model comment. */
model SingleLineRecursiveModel {
/** Property comment. */
property?: SingleLineRecursiveModel
}
Expand All @@ -775,6 +781,10 @@ test.each([
* model comment.
*/
model MultiLineRecursiveModel {
/**
* Property
* comment.
*/
property?: MultiLineRecursiveModel
}
Expand Down
10 changes: 10 additions & 0 deletions test/snapshots/comments/arbitraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as fc from "fast-check";
const group = fc.letrec(tie => ({
MultiLineRecursiveModel: fc.record(
{
/**
* Property
* comment.
*/
property: tie("MultiLineRecursiveModel"),
},
{ withDeletedKeys: true },
Expand All @@ -17,6 +21,7 @@ export const MultiLineRecursiveModel = group.MultiLineRecursiveModel;
const group_2 = fc.letrec(tie => ({
SingleLineRecursiveModel: fc.record(
{
/** Property comment. */
property: tie("SingleLineRecursiveModel"),
},
{ withDeletedKeys: true },
Expand Down Expand Up @@ -63,6 +68,7 @@ export const MultiLineNamespace = {

/** Non-shared model comment. */
export const SingleLineNonSharedModel = fc.record({
/** Property comment. */
property: SingleLineSharedModel,
});

Expand All @@ -71,5 +77,9 @@ export const SingleLineNonSharedModel = fc.record({
* model comment.
*/
export const MultiLineNonSharedModel = fc.record({
/**
* Property
* comment.
*/
property: MultiLineSharedModel,
});

0 comments on commit 8906560

Please sign in to comment.