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

mixin - using #[block] fails #3004

Open
BananaAcid opened this issue Apr 21, 2018 · 7 comments
Open

mixin - using #[block] fails #3004

BananaAcid opened this issue Apr 21, 2018 · 7 comments

Comments

@BananaAcid
Copy link

The following does not work. The block does not expand. (Interestingly, it does with pug-php.

mixin a()
    span somehting #[block]

+a()
    b text

exptected:

<span>something <b>text</b></span>
@SerayaEryn
Copy link
Contributor

SerayaEryn commented Apr 26, 2018

It is not possible to use a block like that (see Mixin Documentation).

This works:

mixin a()
    span
        | somehting 
        block

+a()
    b text

@droooney
Copy link
Member

I think what you need is

mixin a()
  span
    something
    block

+a()
  b text

@ForbesLindesay
Copy link
Member

This is a bug, that should work. To fix this, someone needs to:

  1. add a test case to https://github.com/pugjs/pug/tree/master/packages/pug-lexer/test/cases which will show what the result of the lexer is. This will initially be wrong.
  2. Add a test similar to https://github.com/pugjs/pug/tree/master/packages/pug/test/regression-2436 to show that the html generated is incorrect.
  3. Update the scanEndOfLine method:
    scanEndOfLine: function (regexp, type) {
    var captures;
    if (captures = regexp.exec(this.input)) {
    var whitespaceLength = 0;
    var whitespace;
    var tok;
    if (whitespace = /^([ ]+)([^ ]*)/.exec(captures[0])) {
    whitespaceLength = whitespace[1].length;
    this.incrementColumn(whitespaceLength);
    }
    var newInput = this.input.substr(captures[0].length);
    if (newInput[0] === ':') {
    this.input = newInput;
    tok = this.tok(type, captures[1]);
    this.incrementColumn(captures[0].length - whitespaceLength);
    return tok;
    }
    if (/^[ \t]*(\n|$)/.test(newInput)) {
    this.input = newInput.substr(/^[ \t]*/.exec(newInput)[0].length);
    tok = this.tok(type, captures[1]);
    this.incrementColumn(captures[0].length - whitespaceLength);
    return tok;
    }
    }
    },
    so that it also accepts ] as a "line ending" when this.interpolated is true.
  4. run yarn test -u to update snapshots

@karansapolia
Copy link

karansapolia commented Feb 10, 2019

Hey @ForbesLindesay I was working on this issue and would appreciate some further help in understanding how a ] could be registered as a line ending.

mixin a()
    span somehting #[block]

+a()
    b text

For this particular example, the addText() function runs this.addText(type, child.input); after input: "block]" has been identified and categorized. Next, it always scan the remaining empty string "" and adds a token of type text and value "" on it. In the method I am trying, block is identified as interpolated-code and its token is returned, but an extra token of type: "text" and value "" is returned at the end of the line. Could this be causing problems with ] being identified as line ending?

Also, the same occurs in tag interpolation, a token of type: text and value "" always follows the interpolation token. But despite the same lexer output, tag interpolation with [] works.
Can you guide me here, i understand this might be an easy one, but i feel stuck!

Also, the "indent" type is also a way of identifying line end, should that be used here? Even then, how do I deal with the trailing "" token after the block? Should i leave it alone?

@ForbesLindesay
Copy link
Member

The empty string token is fine and correct. The issue is just that:

  1. the scanEndOfLine function only treats \n or : or the end of the file as a line ending
  2. the lexer for block asserts that the block keyword is immediately followed by a line ending
  3. in tag interpolation, e.g. #[block], the contents of #[.........] is effectively its own fragment of pug, meaning that the ] is effectively a line ending character just like \n.

If you would like to take this issue, please comment to let me know you're working on it, and then submit a pull request that gets as far as possible through the steps I've outlined, even if it's just adding the failing test case. It's harder to have this discussion when we're just working in abstract words.

@karansapolia
Copy link

Thank you! This makes things a lot more clearer to me. Yes, I would like to take this issue up. I am working on it. I'll add the failing test first and then, with your guidance, hopefully submit a useful PR resolving this issue. I'll try and be more succinct too.

@BananaAcid
Copy link
Author

Hi, how is the process on the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants