Ensure only constants that extend Module are parsed from requested paths #2084
+62
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
I ran into a subtle bug when running
tapioca dsl
on specific file path containing an arbitrary non-Module constant. e.g:Where
file.rb
contains:Would blow up like this:
This would happen if any loaded compiler used the
Tapioca::Dsl::Compiler#descendants_of
method in it's#gather_constants
implementation. (TheActiveResource
compiler from the example above is one such).It only occurs when requesting specific paths from the CLI interface; not when run on the whole project, or when requesting specific symbols.
The cause of the issue is that
Taioca::Dsl::Compiler#descendants_of
implementation assumes everything in@@requested_constants
class attribute implements#<
, which should be true because@@requested_constants
is typedArray[Module]
. However, when requesting paths from CLI,@@requested_constants
is populated (in part) byTapioca::Commands::AbstractDsl#constantize
usingObject.const_get
, which grabs all constants in the requested file, regardless of their type.Object.const_get
isT.untyped
so the type mismatch is never caught.Implementation
The solution I'm proposing here is to silently throw away all constants that don't implement
Module
.An alternative solution I considered was changing the logic in
Taioca::Dsl::Compiler#descendants_of
to be resilient to other types, but since the@@requested_constants
attribute is clearly typedArray[Module]
I figured we shouldn't have allowed other types in there in first place.Given we should only be producing
Module
instances, the choice is whether to throw them away or fail when they are encountered. Failing would make it so you could never mix constants with DSL, which is silly so I went with throwing them away.I also considered adding a warning message, but generally there's nothing wrong with using constants in DSLS, so there's
nothing to complain about. Hence, they a thrown away silently.
I'm not convinced this is the best solution. I'm not terribly experienced with Ruby so there's likely a better way. My priority was communicate the issue and make it concrete with a failing test. I'm very open to suggestion on a better fix.
Tests
Added a test that fails without these changes. It generates RBIs using the CLI by file reference, for an
ActiveResource
implementation that contains an arbitrary constant.