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

Add macro methods for Alias #14261

Merged
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
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 @@ -2611,6 +2611,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 @@ -2213,8 +2213,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 @@ -1549,6 +1549,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
Loading