From b4e8d31da783ba69313614c29887f696af5ef918 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Fri, 15 Nov 2024 11:15:48 +0300 Subject: [PATCH] Use multiline comments for better IDE support --- CHANGELOG.md | 2 ++ README.md | 10 ++++++++++ lib/typelizer/templates/interface.ts.erb | 2 +- spec/__snapshots__/AlbaUserAuthor.ts.snap | 6 ++++-- spec/__snapshots__/AlbaUserEmptyNested.ts.snap | 6 ++++-- spec/__snapshots__/AmsUserAuthor.ts.snap | 2 +- spec/__snapshots__/AmsUserEmptyNested.ts.snap | 2 +- spec/__snapshots__/OjSerializersUserAuthor.ts.snap | 2 +- .../__snapshots__/OjSerializersUserEmptyNested.ts.snap | 2 +- .../app/app/serializers/alba/user/author_serializer.rb | 5 ++++- 10 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2da5bd..639b1ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning]. ## Added +- Add support for comments in generated TypeScript interfaces ([@envek]) - Add TypeScript verbatim module syntax support through `verbatim_module_syntax` config option ([@patvice]) - Add `typelizer:generate:refresh` command to clean output directory and regenerate all interfaces ([@patvice]) - Allow disabling Typelizer in Rails development with `DISABLE_TYPELIZER` environment variable to `true` ([@okuramasafumi]) @@ -69,6 +70,7 @@ and this project adheres to [Semantic Versioning]. - Initial release ([@skryukov]) [@davidrunger]: https://github.com/davidrunger +[@envek]: https://github.com/envek [@okuramasafumi]: https://github.com/okuramasafumi [@patvice]: https://github.com/patvice [@skryukov]: https://github.com/skryukov diff --git a/README.md b/README.md index d7795d4..3856054 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Typelizer is a Ruby gem that automatically generates TypeScript interfaces from - [TypeScript Integration](#typescript-integration) - [Manual Generation](#manual-generation) - [Automatic Generation in Development](#automatic-generation-in-development) + - [Disabling Typelizer](#disabling-typelizer) - [Configuration](#configuration) - [Global Configuration](#global-configuration) - [Config Options](#config-options) @@ -79,6 +80,11 @@ class PostResource < ApplicationResource attribute :author_name do |post| post.author.name end + + typelize :string, nullable: true, comment: "Author's avatar URL" + attribute :avatar do + "https://example.com/avatar.png" if active? + end end ``` @@ -259,6 +265,10 @@ Typelizer.configure do |config| # Support TypeScript's Verbatim module syntax option (default: false) # Will change imports and exports of types from default to support this syntax option config.verbatim_module_syntax = false + + # Support comments in generated TypeScript interfaces (default: false) + # Will add comments to the generated interfaces + config.comments = false end ``` diff --git a/lib/typelizer/templates/interface.ts.erb b/lib/typelizer/templates/interface.ts.erb index 57f8795..a27a681 100644 --- a/lib/typelizer/templates/interface.ts.erb +++ b/lib/typelizer/templates/interface.ts.erb @@ -18,7 +18,7 @@ type <%= interface.name %> = { <%- else -%> type <%= interface.name %> = { <%- interface.properties.each do |property| -%> -<%= indent("// #{property.comment.tr("\n", '\n')}\n") if interface.config.comments && property.comment -%> +<%= indent("/** #{property.comment.split("\n").map(&:strip).join("\n * ")} */\n") if interface.config.comments && property.comment -%> <%= indent(property) %>; <%- end -%> } diff --git a/spec/__snapshots__/AlbaUserAuthor.ts.snap b/spec/__snapshots__/AlbaUserAuthor.ts.snap index 80b84af..4b3eaa5 100644 --- a/spec/__snapshots__/AlbaUserAuthor.ts.snap +++ b/spec/__snapshots__/AlbaUserAuthor.ts.snap @@ -1,14 +1,16 @@ -// Typelizer digest 5853f063c6c19b98aeeee99e9c29980a +// Typelizer digest 6a70418b072c53d25848cea380599d44 // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AlbaPost} from '@/types' type AlbaUserAuthor = { id: number; - // Author login handle + /** Author login handle */ username: string | null; posts?: Array; avatar: unknown; + /** Typed avatar URL + * Active user only */ typed_avatar: string | null; } diff --git a/spec/__snapshots__/AlbaUserEmptyNested.ts.snap b/spec/__snapshots__/AlbaUserEmptyNested.ts.snap index 50c4119..8c2ade6 100644 --- a/spec/__snapshots__/AlbaUserEmptyNested.ts.snap +++ b/spec/__snapshots__/AlbaUserEmptyNested.ts.snap @@ -1,14 +1,16 @@ -// Typelizer digest 3bf7801f630edffea7eefdcb86e3fb74 +// Typelizer digest 149809de5e88dc7e93c61dd1c8df854d // // DO NOT MODIFY: This file was automatically generated by Typelizer. import type {AlbaPost} from '@/types' type AlbaUserEmptyNested = { id: number; - // Author login handle + /** Author login handle */ username: string | null; posts?: Array; avatar: unknown; + /** Typed avatar URL + * Active user only */ typed_avatar: string | null; } diff --git a/spec/__snapshots__/AmsUserAuthor.ts.snap b/spec/__snapshots__/AmsUserAuthor.ts.snap index 4f18894..78e3595 100644 --- a/spec/__snapshots__/AmsUserAuthor.ts.snap +++ b/spec/__snapshots__/AmsUserAuthor.ts.snap @@ -5,7 +5,7 @@ import type {AmsPost} from '@/types' type AmsUserAuthor = { id: number; - // Author login handle + /** Author login handle */ username: string | null; avatar: unknown; typed_avatar: string | null; diff --git a/spec/__snapshots__/AmsUserEmptyNested.ts.snap b/spec/__snapshots__/AmsUserEmptyNested.ts.snap index 2e32e44..47b49a3 100644 --- a/spec/__snapshots__/AmsUserEmptyNested.ts.snap +++ b/spec/__snapshots__/AmsUserEmptyNested.ts.snap @@ -5,7 +5,7 @@ import type {AmsPost} from '@/types' type AmsUserEmptyNested = { id: number; - // Author login handle + /** Author login handle */ username: string | null; avatar: unknown; typed_avatar: string | null; diff --git a/spec/__snapshots__/OjSerializersUserAuthor.ts.snap b/spec/__snapshots__/OjSerializersUserAuthor.ts.snap index ea4a8d7..6c0e556 100644 --- a/spec/__snapshots__/OjSerializersUserAuthor.ts.snap +++ b/spec/__snapshots__/OjSerializersUserAuthor.ts.snap @@ -5,7 +5,7 @@ import type {OjSerializersPost} from '@/types' type OjSerializersUserAuthor = { id: number; - // Author login handle + /** Author login handle */ username: string | null; posts?: Array; avatar: unknown; diff --git a/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap b/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap index ddd6009..e499d62 100644 --- a/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap +++ b/spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap @@ -5,7 +5,7 @@ import type {OjSerializersPost} from '@/types' type OjSerializersUserEmptyNested = { id: number; - // Author login handle + /** Author login handle */ username: string | null; posts?: Array; avatar: unknown; diff --git a/spec/app/app/serializers/alba/user/author_serializer.rb b/spec/app/app/serializers/alba/user/author_serializer.rb index 0bb4adc..2492dc1 100644 --- a/spec/app/app/serializers/alba/user/author_serializer.rb +++ b/spec/app/app/serializers/alba/user/author_serializer.rb @@ -15,7 +15,10 @@ class AuthorSerializer < BaseSerializer # typelize typed_avatar: [:string, nullable: true] # typelize ["string", "null"] # typelize "string | null" - typelize :string, nullable: true + typelize :string, nullable: true, comment: <<~TXT + Typed avatar URL + Active user only + TXT attribute :typed_avatar do "https://example.com/avatar.png" if active? end