Skip to content

Commit

Permalink
Define Crystal::Macros::Path#global? properly (#10812)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil committed Jun 12, 2021
1 parent 21b96da commit 479b77d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Expand Up @@ -2513,6 +2513,22 @@ module Crystal
end

describe "path methods" do
it "executes names" do
assert_macro "x", %({{x.names}}), [Path.new("String")] of ASTNode, %([String])
assert_macro "x", %({{x.names}}), [Path.new(["Foo", "Bar"])] of ASTNode, %([Foo, Bar])
end

it "executes global?" do
assert_macro "x", %({{x.global?}}), [Path.new("Foo")] of ASTNode, %(false)
assert_macro "x", %({{x.global?}}), [Path.new("Foo", global: true)] of ASTNode, %(true)
end

# TODO: remove deprecated tests
it "executes global" do
assert_macro "x", %({{x.global}}), [Path.new("Foo")] of ASTNode, %(false)
assert_macro "x", %({{x.global}}), [Path.new("Foo", global: true)] of ASTNode, %(true)
end

it "executes resolve" do
assert_macro "x", %({{x.resolve}}), [Path.new("String")] of ASTNode, %(String)

Expand Down
5 changes: 5 additions & 0 deletions src/compiler/crystal/macros.cr
Expand Up @@ -1350,6 +1350,11 @@ module Crystal::Macros
def global? : BoolLiteral
end

# Returns `true` if this is a global path (starts with `::`)
@[Deprecated("Use `#global?` instead")]
def global : BoolLiteral
end

# Resolves this path to a `TypeNode` if it denotes a type, to
# the value of a constant if it denotes a constant, or otherwise
# gives a compile-time error.
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Expand Up @@ -2108,6 +2108,9 @@ module Crystal
ArrayLiteral.map(@names) { |name| MacroId.new(name) }
end
when "global"
interpreter.report_warning_at(name_loc, "Deprecated Path#global. Use `#global?` instead")
interpret_argless_method(method, args) { BoolLiteral.new(@global) }
when "global?"
interpret_argless_method(method, args) { BoolLiteral.new(@global) }
when "resolve"
interpret_argless_method(method, args) { interpreter.resolve(self) }
Expand Down

0 comments on commit 479b77d

Please sign in to comment.