sorbet-typescript 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +10 -0
- data/lib/sorbet/typescript/exporter.rb +35 -0
- data/lib/sorbet/typescript/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1cf81a39a35ccac245e89223b1058cdac4fd86ffce3b41170ee4bea5a322d54
|
4
|
+
data.tar.gz: 026eccb4bfe1701578c88ed1ecbcc54d79a11e32071bba40f64746fa834443cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afcc21912da9832d4c91cd0662418ad08dfb983eea44c617b4b34dc0051e00b73968e052da4be3d0576761953d095d4aa68b678ea0ab590c53b61a19e4be94d7
|
7
|
+
data.tar.gz: 5a874c2be4a84432ab1718b2eef3405f2dc26af1a4517c30aca61acbb7b8d96e9532a5b6294dd47f794b5bf9c8a111dbef0c8c8bef79ebe0ad4afa2b646ee796
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -52,6 +52,16 @@ outputs:
|
|
52
52
|
|
53
53
|
Run it with `bundle exec sorbet-typescript export --config sorbet_typescript.yml`. Any CLI options override the config file.
|
54
54
|
|
55
|
+
#### What gets generated?
|
56
|
+
|
57
|
+
Each output captures the same Sorbet definitions in a format that is convenient for different consumers:
|
58
|
+
|
59
|
+
- **Enums JSON (`enums_json`)** – map of fully qualified enum names to metadata with the original name, the simple (namespace-free) name, and an ordered list of values (`name`, `serialized`).
|
60
|
+
- **Structs JSON (`structs_json`)** – map of fully qualified struct names to field metadata. Every field entry describes its Sorbet type (`type`, `enum_name`, `struct_name`, etc.), optionality, and—when the Sorbet definition provides defaults—the serialized enum default (`default_enum_*` keys) or literal default (`default`).
|
61
|
+
- **TypeScript file (`typescript_file`)** – ambient definitions that mirror the Sorbet types: literal unions for enums and interfaces for structs. Enum-backed fields become unions of the serialized values so the TS compiler enforces the same constraints as Sorbet.
|
62
|
+
|
63
|
+
These files share the same structure whether you call the CLI or the Ruby API, making it easy to feed the JSON into documentation generators, schemas, or client SDKs.
|
64
|
+
|
55
65
|
### Ruby API
|
56
66
|
|
57
67
|
If you want to script the export yourself, use the `Exporter` directly:
|
@@ -263,6 +263,7 @@ module Sorbet::Typescript
|
|
263
263
|
type_object = prop_info[:type_object] || prop_info[:type]
|
264
264
|
info = analyse_type_object(type_object)
|
265
265
|
info = augment_enum_metadata(info)
|
266
|
+
info = augment_default_metadata(info, prop_info)
|
266
267
|
info[:optional] = prop_info[:_tnilable] || prop_info[:fully_optional] || false
|
267
268
|
info
|
268
269
|
end
|
@@ -483,6 +484,40 @@ module Sorbet::Typescript
|
|
483
484
|
info
|
484
485
|
end
|
485
486
|
|
487
|
+
sig do
|
488
|
+
params(info: T::Hash[Symbol, T.untyped], prop_info: T::Hash[Symbol, T.untyped])
|
489
|
+
.returns(T::Hash[Symbol, T.untyped])
|
490
|
+
end
|
491
|
+
def augment_default_metadata(info, prop_info)
|
492
|
+
default = default_from_prop(prop_info)
|
493
|
+
return info unless default
|
494
|
+
|
495
|
+
if default.is_a?(T::Enum)
|
496
|
+
info[:default_enum_name] = default.class.name
|
497
|
+
info[:default_enum_value] = enum_constant_name(default)
|
498
|
+
info[:default_enum_serialized] = default.serialize
|
499
|
+
elsif default.is_a?(String) || default.is_a?(Symbol) || default.is_a?(Numeric) || default == true || default == false
|
500
|
+
info[:default] = default
|
501
|
+
end
|
502
|
+
|
503
|
+
info
|
504
|
+
end
|
505
|
+
|
506
|
+
sig { params(prop_info: T::Hash[Symbol, T.untyped]).returns(T.untyped) }
|
507
|
+
def default_from_prop(prop_info)
|
508
|
+
return nil if prop_info[:factory]
|
509
|
+
|
510
|
+
default = prop_info[:default]
|
511
|
+
default = nil if default.is_a?(Proc)
|
512
|
+
|
513
|
+
if default.nil?
|
514
|
+
applier = prop_info[:_t_props_private_apply_default]
|
515
|
+
default = applier.default if applier&.respond_to?(:default)
|
516
|
+
end
|
517
|
+
|
518
|
+
default
|
519
|
+
end
|
520
|
+
|
486
521
|
sig { params(info: T::Hash[Symbol, T.untyped]).returns(T::Array[T::Hash[Symbol, T.untyped]]) }
|
487
522
|
def enum_value_details_for(info)
|
488
523
|
details = info[:enum_value_details]
|