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

Nested macros, comments, and macro variable lookup #4380

Closed
bew opened this issue May 4, 2017 · 4 comments · Fixed by #12175
Closed

Nested macros, comments, and macro variable lookup #4380

bew opened this issue May 4, 2017 · 4 comments · Fixed by #12175
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:lang:macros

Comments

@bew
Copy link
Contributor

bew commented May 4, 2017

https://carc.in/#/r/1zai

{% for verb in {:get, :post, :put} %}
  macro {{verb.id}}(arg)
    puts "not commented" \{{arg}}
    # puts "commented" \{{arg}}          # => undefined macro variable 'arg'
  end
{% end %}

The escaped macro variable lookup isn't respected when the line is commented.

@bew
Copy link
Contributor Author

bew commented Jan 6, 2018

Now that I re-think about it, whether the line is commented or not doesn't make sense for macro code, but all I wanted was to comment a line in the macro, just to check something.

I think there is currently no way to comment stuff in a macro without breaking like in OP.

As we already have {% %} for macro statement, {{ }} for printed macro statement, we could have {# #} for commenting stuff in macros?

Maybe it's not a good idea to allow multiline commenting, as this could be abused to comment multiple lines in normal code, we could have {#} to comment to the end of the line?

@bew
Copy link
Contributor Author

bew commented Mar 4, 2018

I just realized I can comment macro stuff with:

{% # comment
 %}

# or (cleaner?)

{%
 # comment
 %}

# note: this doesn't work, as the %} is understood as part of the comment
{% # comment %}

I would still prefer a dedicated syntax like {#} as it would be easier to use, but it's good enough for now.

Or maybe we can change the behevior for {% # comment %} to ensure that the %} is not understood as part of the comment? (but there might be tricky edge cases with this method)

@jwoertink
Copy link
Contributor

I just ran in to this using 1.4.1

class Base
  macro inherited
    macro foo(name)
      # some comments \{{ name.id }}
      def foo
        puts "\{{ name.id }}"
      end
    end
  end
end
 
class Foo < Base
  foo bar
end

Foo.new.foo
Showing last frame. Use --error-trace for full trace.

error in line 4
Error: undefined macro variable 'name'

To get around this, it looks like you can use verbatim

class Base
  macro inherited
    {% verbatim do %}
      macro foo(name)
        # some comments {{name}}
        def foo
          puts "{{name.id}}"
        end
      end
    {% end %}
  end
end
 
class Foo < Base
  foo bar
end
 
Foo.new.foo

@asterite
Copy link
Member

It's actually simpler: the \{{...}} doesn't seem to be working inside comments. Here's a very small example:

macro foo(name)
  # some comments \{{name.id}}
  "\{{name.id}}"

  {% debug %}
end

foo(hello)

The output is:

# some comments \hello
"{{name.id}}"

but it should be:

# some comments {{name.id}}
"{{name.id}}"

@Blacksmoke16 Blacksmoke16 added kind:bug A bug in the code. Does not apply to documentation, specs, etc. and removed kind:feature labels Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:lang:macros
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants