Skip to content

Commit

Permalink
Support verbatim module syntax option in typescript (#16)
Browse files Browse the repository at this point in the history
* support verbatim module syntax option in typescript

* Add specs

* Update CHANGELOG.md

---------

Co-authored-by: Svyatoslav Kryukov <[email protected]>
  • Loading branch information
patvice and skryukov authored Nov 7, 2024
1 parent 4c5ab1d commit ce0365f
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 4 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
5 changes: 3 additions & 2 deletions lib/typelizer/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Config < Struct.new(
:output_dir,
:types_import_path,
:types_global,
:verbatim_module_syntax,
keyword_init: true
) do
class << self
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/typelizer/templates/index.ts.erb
Original file line number Diff line number Diff line change
@@ -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 -%>
4 changes: 4 additions & 0 deletions lib/typelizer/templates/interface.ts.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ type <%= interface.name %> = {
}
<%- end -%>

<%-if interface.config.verbatim_module_syntax -%>
export type { <%= interface.name %> };
<%- else -%>
export default <%= interface.name %>;
<%- end -%>
10 changes: 10 additions & 0 deletions spec/__snapshots__/AlbaVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -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 };
10 changes: 10 additions & 0 deletions spec/__snapshots__/AmsVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -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 };
10 changes: 10 additions & 0 deletions spec/__snapshots__/OjSerializersVerbatimModuleSyntax.ts.snap
Original file line number Diff line number Diff line change
@@ -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 };
5 changes: 4 additions & 1 deletion spec/__snapshots__/index.ts.snap
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Alba
class VerbatimModuleSyntaxSerializer < BaseSerializer
typelize_from ::User

attributes :id, :username

typelizer_config.verbatim_module_syntax = true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Ams
class VerbatimModuleSyntaxSerializer < BaseSerializer
typelize_from ::User

attributes :id, :username

typelizer_config.verbatim_module_syntax = true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module OjSerializers
class VerbatimModuleSyntaxSerializer < BaseSerializer
typelize_from ::User

attributes :id, :username

typelizer_config.verbatim_module_syntax = true
end
end

0 comments on commit ce0365f

Please sign in to comment.