Skip to content

Commit

Permalink
Add Tapioca::Dsl::Compilers namespace file
Browse files Browse the repository at this point in the history
Refactor some constants related to locating constants and finding
constant by name to be under that namespace.
  • Loading branch information
paracycle committed Feb 13, 2022
1 parent d4abcf2 commit 0a044d6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def dsl(*constants)
only: options[:only],
exclude: options[:exclude],
file_header: options[:file_header],
compiler_path: Tapioca::Dsl::DSL_COMPILERS_DIR,
compiler_path: Tapioca::Dsl::Compilers::DIRECTORY,
tapioca_path: TAPIOCA_DIR,
should_verify: options[:verify],
quiet: options[:quiet],
Expand Down
15 changes: 14 additions & 1 deletion lib/tapioca/commands/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def constantize(constant_names)
sig { params(compiler_names: T::Array[String]).returns(T::Array[T.class_of(Tapioca::Dsl::Compiler)]) }
def constantize_compilers(compiler_names)
compiler_map = compiler_names.to_h do |name|
[name, Tapioca::Dsl::Compiler.resolve(name)]
[name, resolve(name)]
end

unprocessable_compilers = compiler_map.select { |_, v| v.nil? }
Expand All @@ -203,6 +203,19 @@ def constantize_compilers(compiler_names)
T.cast(compiler_map.values, T::Array[T.class_of(Tapioca::Dsl::Compiler)])
end

sig { params(name: String).returns(T.nilable(T.class_of(Tapioca::Dsl::Compiler))) }
def resolve(name)
# Try to find built-in tapioca compiler first, then globally defined compiler.
potentials = Tapioca::Dsl::Compilers::NAMESPACES.map do |namespace|
Object.const_get(namespace + name)
rescue NameError
# Skip if we can't find compiler by the potential name
nil
end

potentials.compact.first
end

sig do
params(
constant_name: String,
Expand Down
15 changes: 0 additions & 15 deletions lib/tapioca/dsl/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

module Tapioca
module Dsl
DSL_COMPILERS_DIR = T.let(File.expand_path("../compilers", __FILE__).to_s, String)

class Compiler
extend T::Sig
extend T::Helpers
Expand All @@ -29,19 +27,6 @@ class Compiler
sig { returns(RBI::Tree) }
attr_reader :root

sig { params(name: String).returns(T.nilable(T.class_of(Compiler))) }
def self.resolve(name)
# Try to find built-in tapioca compiler first, then globally defined compiler.
potentials = ["Tapioca::Dsl::Compilers::#{name}", name].map do |potential_name|
Object.const_get(potential_name)
rescue NameError
# Skip if we can't find compiler by the potential name
nil
end

potentials.compact.first
end

sig { params(pipeline: Tapioca::Dsl::Pipeline, root: RBI::Tree, constant: Elem).void }
def initialize(pipeline, root, constant)
@pipeline = pipeline
Expand Down
31 changes: 31 additions & 0 deletions lib/tapioca/dsl/compilers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# typed: strict
# frozen_string_literal: true

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

module Tapioca
module Dsl
module Compilers
DIRECTORY = T.let(
File.expand_path("compilers", __dir__),
String
)

# DSL compilers are either built-in to Tapioca and live under the
# `Tapioca::Dsl::Compilers` namespace (i.e. this namespace), and
# can be referred to by just using the class name, or they live in
# a different namespace and can only be referred to using their fully
# qualified name. This constant encapsulates that dual lookup when
# a compiler needs to be resolved by name.
NAMESPACES = T.let(
[
"#{name}::", # compilers in this namespace
"::", # compilers that need to be fully namespaced
],
T::Array[String]
)
end
end
end
6 changes: 4 additions & 2 deletions lib/tapioca/dsl/pipeline.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# typed: strict
# frozen_string_literal: true

require "tapioca/dsl/compiler"
require "tapioca/dsl/compilers"

module Tapioca
module Dsl
Expand Down Expand Up @@ -87,8 +87,10 @@ def add_error(error)

sig { params(compiler_name: String).returns(T::Boolean) }
def compiler_enabled?(compiler_name)
potential_names = Compilers::NAMESPACES.map { |namespace| namespace + compiler_name }

@compilers.any? do |compiler|
["Tapioca::Compilers::Dsl::#{compiler_name}", compiler_name].any?(compiler.name)
potential_names.any?(compiler.name)
end
end

Expand Down

0 comments on commit 0a044d6

Please sign in to comment.