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

Add content_for tag parser support + ValidContentForArguments check #568

Merged
merged 1 commit into from
Nov 8, 2024

Conversation

charlespwd
Copy link
Contributor

@charlespwd charlespwd commented Nov 7, 2024

Fixes #466

What are you adding in this PR?

  • Add parser support for the content_for tag
  • Updated the old content_for checks to use the AST rather than regexes to perform their work
  • Add prettier plugin support for the content_for tag
  • Add the ValidContentForArguments check
    • For {% content_for "blocks" %}
      • Only context.* arguments are accepted
    • For {% content_for "block" %}
      • type and id are required
      • type and id must be strings
      • context.* arguments are optional
      • nothing else is accepted

What's next? Any followup issues?

What did you learn?

There were more things I needed to change than I remembered. TypeScript helps a ton here because it complains any time you add a new node type.

Before you deploy

  • This PR includes a new checks or changes the configuration of a check
    • I included a minor bump changeset
    • It's in the allChecks array in src/checks/index.ts
    • I ran yarn build and committed the updated configuration files
  • I included a minor bump changeset
  • My feature is backward compatible

@charlespwd
Copy link
Contributor Author

Tagging @albchu and @miazbikowski for context sharing.

@charlespwd charlespwd assigned aswamy and unassigned aswamy Nov 7, 2024
Comment on lines +381 to +396
case NodeTypes.ContentForMarkup: {
const contentForType = path.call((p: any) => print(p), 'contentForType');
const doc: Doc = [contentForType];
if (node.args.length > 0) {
doc.push(
',',
line,
join(
[',', line],
path.map((p) => print(p), 'args'),
),
);
}

return doc;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Prettier logic is dense.

The gist is that line is a space when the doc doesn't break on a new line, and a new line + indentation adjustment when the doc breaks.

What I'm writing here is that ContentForMarkup nodes should be printed as follows

// no arguments?
{% content_for 'contentForType' %}

// arguments & no break ?
{% content_for 'contentForType', arg1: value1, arg2: value2 %}

// arguments + break ?
{% content_for 'contentForType',
  arg1: value1,
  arg2: value2
%}

const markup = node.markup;
const trailingWhitespace = markup.args.length > 0 ? line : ' ';
return tag(trailingWhitespace);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tag(trailingWhitespace) helper is here to handle the logic about what "whitespace-logic-char" we want to use at the end of the tag.

// when it doesn't break
tag('') => {% content_for "blocks"%}
tag(' ') => {% content_for "blocks" %}
tag(line) => {% content_for "blocks" %}

// when it breaks
tag('') => {% content_for "blocks"%}
tag(' ') => {% content_for "blocks" %}
tag(line) => {% content_for "blocks"
%}

Copy link
Contributor

@aswamy aswamy left a comment

Choose a reason for hiding this comment

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

small stuff

@charlespwd charlespwd merged commit 568d53b into main Nov 8, 2024
6 checks passed
@charlespwd charlespwd deleted the feature/content_for_tag branch November 8, 2024 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The content_for tag should be supported by the parser and parse its arguments.
2 participants