Skip to content

Commit

Permalink
Add macro methods for Alias (#14261)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Jan 29, 2024
1 parent 57d67c2 commit 0f7c4e0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
12 changes: 12 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,18 @@ module Crystal
end
end

describe Alias do
node = Alias.new("Foo".path, Generic.new(Path.new(["Bar", "Baz"], global: true), ["T".path] of ASTNode))

it "executes name" do
assert_macro %({{x.name}}), %(Foo), {x: node}
end

it "executes type" do
assert_macro %({{x.type}}), %(::Bar::Baz(T)), {x: node}
end
end

describe "visibility modifier methods" do
node = VisibilityModifier.new(Visibility::Protected, Def.new("some_def"))

Expand Down
18 changes: 16 additions & 2 deletions src/compiler/crystal/macros.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2234,8 +2234,22 @@ module Crystal::Macros
end
end

# class Alias < ASTNode
# end
# An `alias` statement.
#
# Every statement `node` is equivalent to:
#
# ```
# alias {{ node.name }} = {{ node.type }}
# ```
class Alias < ASTNode
# Returns the name of the alias.
def name : Path
end

# Returns the name of the type this alias is equivalent to.
def type : ASTNode
end
end

# A metaclass in a type expression: `T.class`
class Metaclass < ASTNode
Expand Down
13 changes: 13 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,19 @@ module Crystal
end
end

class Alias
def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode)?, block : Crystal::Block?, interpreter : Crystal::MacroInterpreter, name_loc : Location?)
case method
when "name"
interpret_check_args { @name }
when "type"
interpret_check_args { @value }
else
super
end
end
end

class OffsetOf
def interpret(method : String, args : Array(ASTNode), named_args : Hash(String, ASTNode)?, block : Crystal::Block?, interpreter : Crystal::MacroInterpreter, name_loc : Location?)
case method
Expand Down

0 comments on commit 0f7c4e0

Please sign in to comment.