Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Add an option to ignore objects with a given prefix in UndefinedObject#134

Closed
macournoyer wants to merge 1 commit into
masterfrom
optional-objects
Closed

Add an option to ignore objects with a given prefix in UndefinedObject#134
macournoyer wants to merge 1 commit into
masterfrom
optional-objects

Conversation

@macournoyer
Copy link
Copy Markdown
Contributor

Fixes #133

@macournoyer macournoyer requested a review from tskorupa January 27, 2021 21:06
Comment thread config/default.yml
UndefinedObject:
enabled: true
# Objects with this prefix will be ignored
optional_prefix: o_
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Is this an existing convention or a new feature?
  • Should this be convered by something like disable-theme-check UndefinedObject:foo-bar?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was suggested by @tyleralsbury in #133. It's the only way I could think of to support detecting undefined objects, or else we have to drop the whole check.

That sounds like a got idea for the comment. Do you know if eslint allows something similar?

Copy link
Copy Markdown

@tyleralsbury tyleralsbury Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for whether it's a convention or something new - in this case, it's something new, though it's more or less based on established patterns like dangling underscores for private methods in other languages.


The comment idea is an interesting approach as well. In ESLint you have two options - you can either disable it for a line or for the entire file, which might be good for cases like this, so maybe:

disable-next-line-theme-check UndefinedObject:foo_bar

and

disable-theme-check UndefinedObject:foo_bar (at the top of the file)

To allow for both options.

Being able to do this for all of the things theme-check looks for would be a nice way of allowing the developer to control which rules they want to care about for a particular file or line to account for all cases that we haven't thought of - if the developer is getting a report from theme-check that they think is wrong, they can just disable it.

Ideally, this would also come with a "quick fix" solution in the VS Code plugin. When you have an issue like that in ESLint, you can click "quick fix" and one of the options is "add disable-eslint ... ` for that particular line.

ESLint docs for disabling rules: https://eslint.org/docs/2.13.1/user-guide/configuring#disabling-rules-with-inline-comments

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyleralsbury good news 😄 => https://github.com/Shopify/theme-check#disable-checks-with-comments (not yet released on brew)

So the snippet template using optional variables would have this at the top:

{% comment %}
theme-check-disable UndefinedObject:optional_var1
theme-check-disable UndefinedObject:optional_var2
{% endcomment %}

Sounds good?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think this is a really good solution - it accounts for this case and any other possible case that we didn't think of specifically. Very scalable. 👍

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the optional variables in templates could be assigned at the top unless they are passed in. I'm not sure if this is possible in liquid;

assign optional_variable = optional_variable || nil

This would avoid introducing a linter language to the templates.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah that's really good @tskorupa !!

But can't use expression in assign, only filters I think. But since variables default to nil it could be:

{% assign optional = optional %}

It does look weird :/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would work too, more explicit:

{% assign optional = optional | default: nil %}

@macournoyer
Copy link
Copy Markdown
Contributor Author

I'm closing this in favor using {% assign optional = optional | default: nil %} to define optional vars.

I'll document in the ticket, but we might also need a way to educate users about this. Not sure the error message is the right place...

@tskorupa
Copy link
Copy Markdown
Contributor

tskorupa commented Feb 3, 2021

I'll document in the ticket, but we might also need a way to educate users about this. Not sure the error message is the right place...

Let me know if the error belongs to the snippet instead, I can modify it to say the likes of "undefined variable FOO, if it's an optional variable, you can add {% assign optional = optional | default: nil %} at the top of the snippet".

Even better, we can make that fix automatically if the developer wants it so.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Undefined object error when rendering snippet with optional parameters

4 participants