gscan icon indicating copy to clipboard operation
gscan copied to clipboard

Detect invalid nested helpers in theme

Open cobbspur opened this issue 7 years ago • 4 comments

Detect invalid nested helpers Example:

{{#prev_post}}
    <div class="{{^next_post}}full-width{{/next_post}}" >
        ...
    </div>
{{/prev_post}}

cobbspur avatar Jan 02 '18 10:01 cobbspur

If I'm understanding it correctly, this is a fairly general problem of accessing properties which don't exist inside the current context.

{{#someObject}}
  {{someProperty}} <!-- someObject.someProperty doesn't exist -->
{{/someObject}}

I wasn't able to produce any errors using the example in the issue. @gargol are you reading this the same way?

kevinansfield avatar Apr 16 '19 09:04 kevinansfield

@kevinansfield my understanding is it's more about detecting usage of nested tags. In this situation it would be de detection of the following situation:

{{#next_post}}
    {{^prev_post}}
        <p>no prev post</p>

    {{/prev_post}}
    <p>has next post</p>
{{/next_post}}

{{^prev_post}}
    <p>no prev post</p>
{{/prev_post}}

Where {{^prev_post}} works but only when it's not nested.

naz avatar Apr 16 '19 10:04 naz

So I think there are a couple of things here...

  1. {{#prev_post}}{{^next_post}}oops{{/next_post}}{{/prev_post}} will never render "oops" because the logic is incorrect. If "prev_post" is available for the outer context then "next_post" will always be available on the inner context meaning the {{^next_post}} inverse is not reachable.
  2. {{#next/prev_post}} are async helpers. Nested async helpers in the version of express-hbs we use are not strictly supported

Unfortunately the original issue is missing the context around what wasn't working or if any errors were encountered.

As far as the rule for this is concerned, I'm not sure where to go. There are two options:

  1. a rule that detects nested async helpers (are we sure nested async helpers will always fail?)
  2. a rule that detects usage of {{#next/prev_post}} in a non-post context

Detecting the logic error in the issue's original template would be a lot more difficult I think 🤔

kevinansfield avatar Apr 18 '19 16:04 kevinansfield

Think we can introduce both rules as they would be quite helpful.

  1. a rule that detects nested async helpers (are we sure nested async helpers will always fail?)

I think we could issue a warning for all of our async helpers when nested, similarly to what we already say in the docs already.

My suspicion now is that this is exactly the problem the issue was about. Maybe @cobbspur you magically remember about the contexts of this issue?

  1. a rule that detects usage of {{#next/prev_post}} in a non-post context

This one just makes sense, as it would be invalid to use in any other context.

naz avatar Apr 19 '19 08:04 naz