From ce0365fdf395e0186837a04161a1bbd43a256c3c Mon Sep 17 00:00:00 2001 From: Patrick Vice Date: Thu, 7 Nov 2024 09:40:42 -0500 Subject: [PATCH] Support verbatim module syntax option in typescript (#16) * support verbatim module syntax option in typescript * Add specs * Update CHANGELOG.md --------- Co-authored-by: Svyatoslav Kryukov --- CHANGELOG.md | 7 ++++++- README.md | 4 ++++ lib/typelizer/config.rb | 5 +++-- lib/typelizer/templates/index.ts.erb | 4 ++++ lib/typelizer/templates/interface.ts.erb | 4 ++++ spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap | 10 ++++++++++ spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap | 10 ++++++++++ .../OjSerializersVerbatimModuleSyntax.ts.snap | 10 ++++++++++ spec/__snapshots__/index.ts.snap | 5 ++++- .../alba/verbatim_module_syntax_serializer.rb | 9 +++++++++ .../ams/verbatim_module_syntax_serializer.rb | 9 +++++++++ .../verbatim_module_syntax_serializer.rb | 9 +++++++++ 12 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap create mode 100644 spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap create mode 100644 spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap create mode 100644 spec/app/app/serializers/alba/verbatim_module_syntax_serializer.rb create mode 100644 spec/app/app/serializers/ams/verbatim_module_syntax_serializer.rb create mode 100644 spec/app/app/serializers/oj_serializers/verbatim_module_syntax_serializer.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 66b3138..0ffa524 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning]. ## [Unreleased] +## Added + +- Add TypeScript verbatim module syntax support through `verbatim_module_syntax` config option ([@patvice]) + ## [0.1.5] - 2024-10-07 ## Fixed @@ -58,7 +62,8 @@ and this project adheres to [Semantic Versioning]. - Initial release ([@skryukov]) [@davidrunger]: https://github.com/davidrunger -[@okuramasafumi]: https://github.com/@okuramasafumi +[@okuramasafumi]: https://github.com/okuramasafumi +[@patvice]: https://github.com/patvice [@skryukov]: https://github.com/skryukov [Unreleased]: https://github.com/skryukov/typelizer/compare/v0.1.5...HEAD diff --git a/README.md b/README.md index 78d6eb3..51b4594 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,10 @@ Typelizer.configure do |config| # List of type names that should be considered global in TypeScript # (i.e. not prefixed with the import path) config.types_global << %w[Array Date Record File FileList] + + # 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 end ``` diff --git a/lib/typelizer/config.rb b/lib/typelizer/config.rb index e7d0fff..2e028f5 100644 --- a/lib/typelizer/config.rb +++ b/lib/typelizer/config.rb @@ -24,6 +24,7 @@ class Config < Struct.new( :output_dir, :types_import_path, :types_global, + :verbatim_module_syntax, keyword_init: true ) do class << self @@ -51,7 +52,8 @@ def instance types_import_path: "@/types", types_global: %w[Array Date Record File FileList], - properties_transformer: nil + properties_transformer: nil, + verbatim_module_syntax: false ) end @@ -70,7 +72,6 @@ def respond_to_missing?(name, include_private = false) def method_missing(method, *args, &block) return Typelizer.send(method, *args, &block) if Typelizer.respond_to?(method) - instance.send(method, *args, &block) end end diff --git a/lib/typelizer/templates/index.ts.erb b/lib/typelizer/templates/index.ts.erb index 6e368c2..5f58654 100644 --- a/lib/typelizer/templates/index.ts.erb +++ b/lib/typelizer/templates/index.ts.erb @@ -1,3 +1,7 @@ <%- interfaces.each do |interface| -%> +<%- if interface.config.verbatim_module_syntax -%> +export type { <%= interface.name %> } from './<%= interface.filename %>' +<%- else -%> export type { default as <%= interface.name %> } from './<%= interface.filename %>' <%- end -%> +<%- end -%> diff --git a/lib/typelizer/templates/interface.ts.erb b/lib/typelizer/templates/interface.ts.erb index 1daee04..9e766a1 100644 --- a/lib/typelizer/templates/interface.ts.erb +++ b/lib/typelizer/templates/interface.ts.erb @@ -23,4 +23,8 @@ type <%= interface.name %> = { } <%- end -%> +<%-if interface.config.verbatim_module_syntax -%> +export type { <%= interface.name %> }; +<%- else -%> export default <%= interface.name %>; +<%- end -%> diff --git a/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap new file mode 100644 index 0000000..01d2a85 --- /dev/null +++ b/spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap @@ -0,0 +1,10 @@ +// Typelizer digest a6d580654a084cc58071460e42093126 +// +// DO NOT MODIFY: This file was automatically generated by Typelizer. + +type AlbaVerbatimModuleSyntax = { + id: number; + username: string; +} + +export type { AlbaVerbatimModuleSyntax }; diff --git a/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap new file mode 100644 index 0000000..88f4f3c --- /dev/null +++ b/spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap @@ -0,0 +1,10 @@ +// Typelizer digest 4f6b6fed16d511dbfe9a02a0b7aa9c41 +// +// DO NOT MODIFY: This file was automatically generated by Typelizer. + +type AmsVerbatimModuleSyntax = { + id: number; + username: string; +} + +export type { AmsVerbatimModuleSyntax }; diff --git a/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap b/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap new file mode 100644 index 0000000..a828e0b --- /dev/null +++ b/spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap @@ -0,0 +1,10 @@ +// Typelizer digest c0f49e6c7ece40d4d23624555e33da67 +// +// DO NOT MODIFY: This file was automatically generated by Typelizer. + +type OjSerializersVerbatimModuleSyntax = { + id: number; + username: string; +} + +export type { OjSerializersVerbatimModuleSyntax }; diff --git a/spec/__snapshots__/index.ts.snap b/spec/__snapshots__/index.ts.snap index 4d3903c..98c2387 100644 --- a/spec/__snapshots__/index.ts.snap +++ b/spec/__snapshots__/index.ts.snap @@ -1,4 +1,4 @@ -// Typelizer digest b11065c38d475f7104b4d09c6ee44a31 +// Typelizer digest b30b67423d9475d4fb19db422c52a55f // // DO NOT MODIFY: This file was automatically generated by Typelizer. export type { default as AlbaInline } from './AlbaInline' @@ -9,12 +9,15 @@ export type { default as AlbaPost } from './AlbaPost' export type { default as AlbaUserAuthor } from './AlbaUserAuthor' export type { default as AlbaUser } from './AlbaUser' export type { default as AlbaUserSerializerFoo } from './AlbaUserSerializerFoo' +export type { AlbaVerbatimModuleSyntax } from './AlbaVerbatimModuleSyntax' export type { default as AmsPost } from './AmsPost' export type { default as AmsUserAuthor } from './AmsUserAuthor' export type { default as AmsUser } from './AmsUser' export type { default as AmsUserSerializerFoo } from './AmsUserSerializerFoo' +export type { AmsVerbatimModuleSyntax } from './AmsVerbatimModuleSyntax' export type { default as OjSerializersFlatUser } from './OjSerializersFlatUser' export type { default as OjSerializersPost } from './OjSerializersPost' export type { default as OjSerializersUserAuthor } from './OjSerializersUserAuthor' export type { default as OjSerializersUser } from './OjSerializersUser' export type { default as OjSerializersUserSerializerFoo } from './OjSerializersUserSerializerFoo' +export type { OjSerializersVerbatimModuleSyntax } from './OjSerializersVerbatimModuleSyntax' diff --git a/spec/app/app/serializers/alba/verbatim_module_syntax_serializer.rb b/spec/app/app/serializers/alba/verbatim_module_syntax_serializer.rb new file mode 100644 index 0000000..33b6dab --- /dev/null +++ b/spec/app/app/serializers/alba/verbatim_module_syntax_serializer.rb @@ -0,0 +1,9 @@ +module Alba + class VerbatimModuleSyntaxSerializer < BaseSerializer + typelize_from ::User + + attributes :id, :username + + typelizer_config.verbatim_module_syntax = true + end +end diff --git a/spec/app/app/serializers/ams/verbatim_module_syntax_serializer.rb b/spec/app/app/serializers/ams/verbatim_module_syntax_serializer.rb new file mode 100644 index 0000000..86d0969 --- /dev/null +++ b/spec/app/app/serializers/ams/verbatim_module_syntax_serializer.rb @@ -0,0 +1,9 @@ +module Ams + class VerbatimModuleSyntaxSerializer < BaseSerializer + typelize_from ::User + + attributes :id, :username + + typelizer_config.verbatim_module_syntax = true + end +end diff --git a/spec/app/app/serializers/oj_serializers/verbatim_module_syntax_serializer.rb b/spec/app/app/serializers/oj_serializers/verbatim_module_syntax_serializer.rb new file mode 100644 index 0000000..eca12ef --- /dev/null +++ b/spec/app/app/serializers/oj_serializers/verbatim_module_syntax_serializer.rb @@ -0,0 +1,9 @@ +module OjSerializers + class VerbatimModuleSyntaxSerializer < BaseSerializer + typelize_from ::User + + attributes :id, :username + + typelizer_config.verbatim_module_syntax = true + end +end