Skip to content

Commit

Permalink
Use DTS by default only if module_type is set, prevents breaking proj…
Browse files Browse the repository at this point in the history
…ects that can't handle "export" keyword.
  • Loading branch information
rapito committed Sep 8, 2024
1 parent fa54201 commit 41cf966
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/js_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,26 @@ def generate!(file_name = configuration.file, **opts)

sig { params(opts: T.untyped).returns(String) }
def definitions(**opts)
generate(**opts, module_type: 'DTS',)
generate(**opts, module_type: default_module_type,)
end

sig { params(file_name: FileName, opts: T.untyped).void }
def definitions!(file_name = nil, **opts)
file_name ||= configuration.file&.sub(%r{(\.d)?\.(j|t)s\Z}, ".d.ts")
generate!(file_name, **opts, module_type: 'DTS')
generate!(file_name, **opts, module_type: default_module_type)
end

sig { params(value: T.untyped).returns(String) }
def json(value)
ActiveSupport::JSON.encode(value)
end

sig { returns(T.nilable(String)) }
def default_module_type
'DTS' if configuration.module_type
end


end
module Generators
end
Expand Down
10 changes: 10 additions & 0 deletions spec/js_routes/module_types/dts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,15 @@
generated_js = JsRoutes.definitions(**options)
expect(generated_js).to include('export {};')
end

it 'does not use DTS module if module_type is not set' do
previous_module_type = JsRoutes.configuration.module_type
JsRoutes.configuration.module_type = nil

generated_js = JsRoutes.definitions(**options.merge(module_type: nil))
expect(generated_js).not_to include('export {};')

JsRoutes.configuration.module_type = previous_module_type
end
end
end

0 comments on commit 41cf966

Please sign in to comment.