Skip to content
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

Use multiline comments for better IDE support #22

Merged
merged 1 commit into from
Nov 15, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
```

Expand Down Expand Up @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion lib/typelizer/templates/interface.ts.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
}
Expand Down
6 changes: 4 additions & 2 deletions spec/__snapshots__/AlbaUserAuthor.ts.snap
Original file line number Diff line number Diff line change
@@ -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<AlbaPost>;
avatar: unknown;
/** Typed avatar URL
* Active user only */
typed_avatar: string | null;
}

Expand Down
6 changes: 4 additions & 2 deletions spec/__snapshots__/AlbaUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
@@ -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<AlbaPost>;
avatar: unknown;
/** Typed avatar URL
* Active user only */
typed_avatar: string | null;
}

Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AmsUserAuthor.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/AmsUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/OjSerializersUserAuthor.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {OjSerializersPost} from '@/types'

type OjSerializersUserAuthor = {
id: number;
// Author login handle
/** Author login handle */
username: string | null;
posts?: Array<OjSerializersPost>;
avatar: unknown;
Expand Down
2 changes: 1 addition & 1 deletion spec/__snapshots__/OjSerializersUserEmptyNested.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {OjSerializersPost} from '@/types'

type OjSerializersUserEmptyNested = {
id: number;
// Author login handle
/** Author login handle */
username: string | null;
posts?: Array<OjSerializersPost>;
avatar: unknown;
Expand Down
5 changes: 4 additions & 1 deletion spec/app/app/serializers/alba/user/author_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading