-
Notifications
You must be signed in to change notification settings - Fork 156
Use Prism in Sorbet calls (if enabled) #2567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
amomchilov
wants to merge
2
commits into
main
Choose a base branch
from
Alex/use-prism-in-sorbet-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−9
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,24 +13,81 @@ module SorbetHelper | |
|
|
||
| SPOOM_CONTEXT = Spoom::Context.new(".") #: Spoom::Context | ||
|
|
||
| # Represents the `sorbet/config` file, and provides access to its options. https://sorbet.org/docs/cli-ref | ||
| # If the file doesn't exist, this object will still exist, but will return default values for all options. | ||
| class SorbetConfig | ||
| class << self | ||
| #: (Spoom::Context spoom_context) -> SorbetConfig | ||
| def parse_from(spoom_context) | ||
| config_path = File.join(spoom_context.absolute_path, "sorbet", "config") | ||
| content = File.exist?(config_path) ? File.read(config_path) : "" | ||
| parse(content) | ||
| end | ||
|
|
||
| #: (String content) -> SorbetConfig | ||
| def parse(content) | ||
| lines = content.lines.map(&:strip).reject(&:empty?) | ||
|
|
||
| options = lines.filter_map do |line| | ||
| next if line.start_with?("#") # Skip comments | ||
| next unless line.start_with?("--") | ||
|
|
||
| key, value = line.split("=", 2) | ||
| key = key #: as !nil | ||
|
|
||
| [key, value] | ||
| end.to_h #: Hash[String, String | bool | nil] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we get
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah that's up-stack in https://app.graphite.com/github/pr/Shopify/tapioca/2569 I moved it. |
||
|
|
||
| new( | ||
| parser: options["--parser"] == "prism" ? :prism : :original, | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| #: (parser: Symbol) -> void | ||
| def initialize(parser:) | ||
| @parser = parser #: Symbol | ||
| end | ||
|
|
||
| #: Symbol | ||
| attr_reader :parser | ||
|
|
||
| #: -> bool | ||
| def parse_with_prism? = @parser == :prism | ||
| end | ||
|
|
||
| FEATURE_REQUIREMENTS = { | ||
| # feature_name: ::Gem::Requirement.new(">= ___"), # https://github.com/sorbet/sorbet/pull/___ | ||
|
|
||
| prism_syntax_check_with_stop_after_parser: ::Gem::Requirement.new("> 0.6.13073"), # https://github.com/sorbet/sorbet/pull/10076 | ||
| }.freeze #: Hash[Symbol, ::Gem::Requirement] | ||
|
|
||
| #: (*String sorbet_args) -> Spoom::ExecResult | ||
| def sorbet(*sorbet_args) | ||
| if sorbet_config.parse_with_prism? | ||
| sorbet_args << "--parser=prism" | ||
| end | ||
|
|
||
| SPOOM_CONTEXT.srb(sorbet_args.join(" "), sorbet_bin: sorbet_path) | ||
| end | ||
|
|
||
| #: (String, rbi_mode: bool) { (String stderr) -> void } -> void | ||
| def sorbet_syntax_check!(source, rbi_mode:, &on_failure) | ||
| quoted_source = "\"#{source}\"" | ||
|
|
||
| stop_after = "--stop-after=parser" | ||
|
|
||
| # This version of Sorbet doesn't report parse errors until the desugarer, so we need to modify the | ||
| # stop-after argument to get far enough to get those errors (and a non-zero exit code). | ||
| if sorbet_config.parse_with_prism? && !sorbet_supports?(:prism_syntax_check_with_stop_after_parser) | ||
| stop_after = "--stop-after=desugarer" | ||
| end | ||
|
|
||
| result = if rbi_mode | ||
| # --e-rbi cannot be used on its own, so we pass a dummy value like `-e ""` | ||
| sorbet("--no-config", "--stop-after=parser", "-e", '""', "--e-rbi", quoted_source) | ||
| sorbet("--no-config", stop_after, "-e", '""', "--e-rbi", quoted_source) | ||
| else | ||
| sorbet("--no-config", "--stop-after=parser", "-e", quoted_source) | ||
| sorbet("--no-config", stop_after, "-e", quoted_source) | ||
| end | ||
|
|
||
| unless result.status | ||
|
|
@@ -41,6 +98,11 @@ def sorbet_syntax_check!(source, rbi_mode:, &on_failure) | |
| nil | ||
| end | ||
|
|
||
| #: -> SorbetConfig | ||
| def sorbet_config | ||
| @sorbet_config ||= SorbetConfig.parse_from(SPOOM_CONTEXT) #: SorbetConfig? | ||
| end | ||
|
|
||
| #: -> String | ||
| def sorbet_path | ||
| sorbet_path = ENV.fetch(SORBET_EXE_PATH_ENV_VAR, SORBET_BIN) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wow, Claude just spit this out so easily, didn't even realize this already exists: https://github.com/Shopify/spoom/blob/main/lib/spoom/sorbet/config.rb