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

[crystal tool expand] Can not expand macro in class body of included files. #6098

Open
LeonLiuY opened this issue May 15, 2018 · 1 comment · May be fixed by #8987
Open

[crystal tool expand] Can not expand macro in class body of included files. #6098

LeonLiuY opened this issue May 15, 2018 · 1 comment · May be fixed by #8987

Comments

@LeonLiuY
Copy link

Crystal version: 0.24.1

test.cr

macro test
  def to_json
    "hey"
  end
end

class Test
  test
end

main.cr

require "./test"
p Test.new.to_json

Facts

The program has no problem:

$ crystal run main.cr
"hey"

Can expand the macro when the program is set to test.cr

$ crystal tool expand -c test.cr:8:3 test.cr          
1 expansion found
expansion 1:
   test

# expand macro 'test' (/home/liuyang/work/ac-expense-api/test.cr:1:1)
~> def to_json
     "hey"
   end

Can not expand the macro when the program is set to main.cr

$ crystal tool expand -c test.cr:8:3 main.cr          
no expansion found
@LeonLiuY LeonLiuY changed the title [crystal tool expand] Can not expand macro in class body. [crystal tool expand] Can not expand macro in class body other than the main file. May 15, 2018
@LeonLiuY LeonLiuY changed the title [crystal tool expand] Can not expand macro in class body other than the main file. [crystal tool expand] Can not expand macro in class body of included files. May 15, 2018
@LeonLiuY
Copy link
Author

LeonLiuY commented May 15, 2018

Use case

I want to expand the schema macro in Crecto model, but it fails.

class User < Crecto::Model

  schema "users" do
    field :age, Int32 # or use `PkeyValue` alias: `field :age, PkeyValue`
    field :name, String
    field :is_admin, Bool, default: false
    field :temporary_info, Float64, virtual: true
    field :email, String
    has_many :posts, Post, dependent: :destroy
  end

  validate_required [:name, :age]
  validate_format :name, /^[a-zA-Z]*$/
  unique_constraint :email
end

I have my file structure:

models
  user.cr
  ...
app.cr

where app.cr requries libraries needed for the application as the main file. So It's not convenient to use user.cr as the main file of crystal tool expand (compile error)

makenowjust added a commit to makenowjust/crystal that referenced this issue Apr 1, 2020
…ire` (fixed crystal-lang#6098)

As a side effect, `crystal tool expand` can expand `require` statement now.
I think it may help us.

Note that spec is missing for now.
Because there are no foundation to write `tool expand` spec over multiple files currently.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants