Skip to content
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

Use RBI::Formatter #808

Merged
merged 5 commits into from
Feb 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Options:
# Default: true
--dsl-dir, [--dsl-dir=directory] # The DSL directory used to correct gems strictnesses
# Default: sorbet/rbi/dsl
[--rbi-max-line-length=N] # Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped
# Default: 120
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
-V, [--verbose], [--no-verbose] # Verbose output for debugging purposes
Expand Down Expand Up @@ -181,6 +183,8 @@ Options:
-q, [--quiet], [--no-quiet] # Supresses file creation output
-w, [--workers=N] # EXPERIMENTAL: Number of parallel workers to use when generating RBIs
# Default: 1
[--rbi-max-line-length=N] # Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped
# Default: 120
-c, [--config=<config file path>] # Path to the Tapioca configuration file
# Default: sorbet/tapioca/config.yml
-V, [--verbose], [--no-verbose] # Verbose output for debugging purposes
Expand Down Expand Up @@ -232,6 +236,7 @@ dsl:
verify: false
quiet: false
workers: 1
rbi_max_line_length: 120
gem:
outdir: sorbet/rbi/gems
file_header: true
Expand All @@ -247,6 +252,7 @@ gem:
workers: 1
auto_strictness: true
dsl_dir: sorbet/rbi/dsl
rbi_max_line_length: 120
check_shims:
gem_rbi_dir: sorbet/rbi/gems
dsl_rbi_dir: sorbet/rbi/dsl
Expand Down
14 changes: 12 additions & 2 deletions lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def todo
type: :numeric,
desc: "EXPERIMENTAL: Number of parallel workers to use when generating RBIs",
default: 1
option :rbi_max_line_length,
type: :numeric,
desc: "Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped",
default: 120
def dsl(*constants)
generator = Generators::Dsl.new(
requested_constants: constants,
Expand All @@ -112,7 +116,8 @@ def dsl(*constants)
should_verify: options[:verify],
quiet: options[:quiet],
verbose: options[:verbose],
number_of_workers: options[:workers]
number_of_workers: options[:workers],
rbi_formatter: rbi_formatter(options)
)

if options[:workers] != 1
Expand Down Expand Up @@ -189,6 +194,10 @@ def dsl(*constants)
banner: "directory",
desc: "The DSL directory used to correct gems strictnesses",
default: DEFAULT_DSL_DIR
option :rbi_max_line_length,
type: :numeric,
desc: "Set the max line length of generated RBIs. Signatures longer than the max line length will be wrapped",
default: 120
def gem(*gems)
Tapioca.silence_warnings do
all = options[:all]
Expand All @@ -207,7 +216,8 @@ def gem(*gems)
include_exported_rbis: options[:exported_gem_rbis],
number_of_workers: options[:workers],
auto_strictness: options[:auto_strictness],
dsl_dir: options[:dsl_dir]
dsl_dir: options[:dsl_dir],
rbi_formatter: rbi_formatter(options)
)

raise MalformattedArgumentError, "Options '--all' and '--verify' are mutually exclusive" if all && verify
Expand Down
2 changes: 2 additions & 0 deletions lib/tapioca/dsl/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# typed: strict
# frozen_string_literal: true

require "rbi"
require "tapioca/rbi_ext/model"
require "tapioca/rbi_formatter"
require "tapioca/dsl/helpers/param_helper"
require "tapioca/dsl/pipeline"

Expand Down
16 changes: 10 additions & 6 deletions lib/tapioca/generators/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Dsl < Base
quiet: T::Boolean,
verbose: T::Boolean,
number_of_workers: T.nilable(Integer),
rbi_formatter: RBIFormatter
).void
end
def initialize(
Expand All @@ -34,7 +35,8 @@ def initialize(
should_verify: false,
quiet: false,
verbose: false,
number_of_workers: nil
number_of_workers: nil,
rbi_formatter: DEFAULT_RBI_FORMATTER
)
@requested_constants = requested_constants
@outpath = outpath
Expand All @@ -47,6 +49,7 @@ def initialize(
@quiet = quiet
@verbose = verbose
@number_of_workers = number_of_workers
@rbi_formatter = rbi_formatter

super(default_command: default_command, file_writer: file_writer)

Expand Down Expand Up @@ -217,13 +220,14 @@ def compile_dsl_rbi(constant_name, rbi, outpath: @outpath, quiet: false)

filename = outpath / rbi_filename_for(constant_name)

rbi.set_file_header(
@rbi_formatter.write_header!(
rbi,
generate_command_for(constant_name),
reason: "dynamic methods in `#{constant_name}`",
display_heading: @file_header
)
reason: "dynamic methods in `#{constant_name}`"
) if @file_header

create_file(filename, rbi.transformed_string, verbose: !quiet)
rbi_string = @rbi_formatter.print_file(rbi)
create_file(filename, rbi_string, verbose: !quiet)

filename
end
Expand Down
19 changes: 11 additions & 8 deletions lib/tapioca/generators/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Gem < Base
file_writer: Thor::Actions,
number_of_workers: T.nilable(Integer),
auto_strictness: T::Boolean,
dsl_dir: String
dsl_dir: String,
rbi_formatter: RBIFormatter
).void
end
def initialize(
Expand All @@ -38,7 +39,8 @@ def initialize(
file_writer: FileWriter.new,
number_of_workers: nil,
auto_strictness: true,
dsl_dir: DEFAULT_DSL_DIR
dsl_dir: DEFAULT_DSL_DIR,
rbi_formatter: DEFAULT_RBI_FORMATTER
)
@gem_names = gem_names
@exclude = exclude
Expand All @@ -50,6 +52,7 @@ def initialize(
@number_of_workers = number_of_workers
@auto_strictness = auto_strictness
@dsl_dir = dsl_dir
@rbi_formatter = rbi_formatter

super(default_command: default_command, file_writer: file_writer)

Expand Down Expand Up @@ -165,24 +168,24 @@ def compile_gem_rbi(gem)
gem_name = set_color(gem.name, :yellow, :bold)

rbi = RBI::File.new(strictness: @typed_overrides[gem.name] || "true")
rbi.set_file_header(

@rbi_formatter.write_header!(rbi,
"#{@default_command} gem #{gem.name}",
reason: "types exported from the `#{gem.name}` gem",
display_heading: @file_header
)
reason: "types exported from the `#{gem.name}` gem",) if @file_header

rbi.root = Compilers::SymbolTableCompiler.new(gem, include_doc: @doc).compile

merge_with_exported_rbi(gem, rbi) if @include_exported_rbis

if rbi.empty?
rbi.set_empty_body_content
@rbi_formatter.write_empty_body_comment!(rbi)
say("Compiled #{gem_name} (empty output)", :yellow)
else
say("Compiled #{gem_name}", :green)
end

create_file(@outpath / gem.rbi_file_name, rbi.transformed_string)
rbi_string = @rbi_formatter.print_file(rbi)
create_file(@outpath / gem.rbi_file_name, rbi_string)

T.unsafe(Pathname).glob((@outpath / "#{gem.name}@*.rbi").to_s) do |file|
remove_file(file) unless file.basename.to_s == gem.rbi_file_name
Expand Down
7 changes: 7 additions & 0 deletions lib/tapioca/helpers/cli_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ def say_error(message = "", *color)
super(message, color)
end
end

sig { params(options: T::Hash[Symbol, T.untyped]).returns(RBIFormatter) }
def rbi_formatter(options)
rbi_formatter = DEFAULT_RBI_FORMATTER
rbi_formatter.max_line_length = options[:rbi_max_line_length]
rbi_formatter
end
end
end
2 changes: 1 addition & 1 deletion lib/tapioca/helpers/test/dsl_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def rbi_for(constant_name)

compiler.decorate(file.root, constant)

file.transformed_string
Tapioca::DEFAULT_RBI_FORMATTER.print_file(file)
end

sig { returns(T::Array[String]) }
Expand Down
41 changes: 0 additions & 41 deletions lib/tapioca/rbi_ext/model.rb
Original file line number Diff line number Diff line change
@@ -1,48 +1,7 @@
# typed: strict
# frozen_string_literal: true

require "rbi"

module RBI
class File
extend T::Sig

sig { returns(String) }
def transformed_string
transform_rbi!
string
end

sig { void }
def transform_rbi!
root.nest_singleton_methods!
root.nest_non_public_methods!
root.group_nodes!
root.sort_nodes!
end

sig do
params(
command: String,
reason: T.nilable(String),
display_heading: T::Boolean
).void
end
def set_file_header(command, reason: nil, display_heading: true)
return unless display_heading
comments << RBI::Comment.new("DO NOT EDIT MANUALLY")
comments << RBI::Comment.new("This is an autogenerated file for #{reason}.") unless reason.nil?
comments << RBI::Comment.new("Please instead update this file by running `#{command}`.")
end

sig { void }
def set_empty_body_content
comments << RBI::BlankLine.new unless comments.empty?
comments << RBI::Comment.new("THIS IS AN EMPTY RBI FILE.")
comments << RBI::Comment.new("see https://github.com/Shopify/tapioca/wiki/Manual-Gem-Requires")
end
end

class Tree
extend T::Sig

Expand Down
37 changes: 37 additions & 0 deletions lib/tapioca/rbi_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# typed: strict
# frozen_string_literal: true

module Tapioca
class RBIFormatter < RBI::Formatter
extend T::Sig

sig do
params(
file: RBI::File,
command: String,
reason: T.nilable(String)
).void
end
def write_header!(file, command, reason: nil)
file.comments << RBI::Comment.new("DO NOT EDIT MANUALLY")
file.comments << RBI::Comment.new("This is an autogenerated file for #{reason}.") unless reason.nil?
file.comments << RBI::Comment.new("Please instead update this file by running `#{command}`.")
end

sig { params(file: RBI::File).void }
def write_empty_body_comment!(file)
file.comments << RBI::BlankLine.new unless file.comments.empty?
file.comments << RBI::Comment.new("THIS IS AN EMPTY RBI FILE.")
file.comments << RBI::Comment.new("see https://github.com/Shopify/tapioca/wiki/Manual-Gem-Requires")
end
end

DEFAULT_RBI_FORMATTER = T.let(RBIFormatter.new(
add_sig_templates: false,
group_nodes: true,
max_line_length: nil,
nest_singleton_methods: true,
nest_non_public_methods: true,
sort_nodes: true
), RBIFormatter)
end
Loading