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

fix: _comment_action rule #27

Merged
merged 2 commits into from
Jan 14, 2025
Merged

Conversation

guilhas07
Copy link
Contributor

Problem:

Comments are defined in https://pkg.go.dev/text/template like so:

{{/* a comment */}}
{{- /* a comment with white space trimmed from preceding and following text */ -}}
	A comment; discarded. May contain newlines.
	Comments do not nest and must start and end at the
	delimiters, as shown here.

Solution

Change the _comment_action rule so a comment always has either the delimiter {{ or {{- (note the space at the end). Also, every element in the rule should be immediate to each other. Meaning that delimiters can't be alone on a line.

make_grammar.js Outdated
@@ -107,7 +107,16 @@ module.exports = function make_grammar(dialect) {
),

_comment_action: ($) =>
seq($._left_delimiter, $.comment, $._right_delimiter),
choice(
seq(token('{{'), $.comment, token.immediate('}}')),
Copy link
Collaborator

Choose a reason for hiding this comment

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

The mixed case is missing:

{{- /* comment */}}
{{/* comment */ -}}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ups 😅 Pushed fix.

@guilhas07
Copy link
Contributor Author

I noticed this repo has a .prettierrc file but the make_grammar.js isn't formatted with it. Is it okay with you if after everything looks good I push changes that apply prettier formatting?

@qvalentin
Copy link
Collaborator

I noticed this repo has a .prettierrc file but the make_grammar.js isn't formatted with it. Is it okay with you if after everything looks good I push changes that apply prettier formatting?

Sure, you will have to rebase the generated files anyway.

comment: ($) =>
token(
choice(
seq('//', /.*/),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder why comments like

{{// a comment }}

where supported when gotemplate clearly does not support them.
But since there is no test for them I doubt it was intentional.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@guilhas07 guilhas07 force-pushed the fix-comment branch 2 times, most recently from 218fff6 to 0b0ae62 Compare January 14, 2025 14:42
@guilhas07
Copy link
Contributor Author

Applied prettier formatting and removed unused functions/variables.
Additionaly, the new tree-sitter version gives a warning to:

yaml_no_injection_text: ($) => choice('-'),

due to being a single token in a choice. Is this intentional?

@qvalentin
Copy link
Collaborator

Applied prettier formatting and removed unused functions/variables. Additionaly, the new tree-sitter version gives a warning to:

yaml_no_injection_text: ($) => choice('-'),

due to being a single token in a choice. Is this intentional?

I think you can just remove the choice.

@guilhas07
Copy link
Contributor Author

guilhas07 commented Jan 14, 2025

Applied prettier formatting and removed unused functions/variables. Additionaly, the new tree-sitter version gives a warning to:

yaml_no_injection_text: ($) => choice('-'),

due to being a single token in a choice. Is this intentional?

I think you can just remove the choice.

This triggered some changes in the test case, so I updated the tests with npx tree-sitter test -u, which raised a lot of cosmetic changes in test/corpus/actions.txt.

@qvalentin
Copy link
Collaborator

Applied prettier formatting and removed unused functions/variables. Additionaly, the new tree-sitter version gives a warning to:

yaml_no_injection_text: ($) => choice('-'),

due to being a single token in a choice. Is this intentional?

I think you can just remove the choice.

This triggered some changes in the test case, so I updated the tests with npx tree-sitter test -u, which raised a lot of cosmetic changes in test/corpus/actions.txt.

The diff is getting very difficult to read, could you remove the cosmetic changes (maybe we can later open a dedicated PR for them)?

- Remove unused variables/functions
- Remove single token choice node
- Apply prettier formatting
@guilhas07
Copy link
Contributor Author

Applied prettier formatting and removed unused functions/variables. Additionaly, the new tree-sitter version gives a warning to:

yaml_no_injection_text: ($) => choice('-'),

due to being a single token in a choice. Is this intentional?

I think you can just remove the choice.

This triggered some changes in the test case, so I updated the tests with npx tree-sitter test -u, which raised a lot of cosmetic changes in test/corpus/actions.txt.

The diff is getting very difficult to read, could you remove the cosmetic changes

Sorry about that. It should be better now.

maybe we can later open a dedicated PR for them

Maybe that could be done on your tree-sitter bump PR? Running npx tree-sitter test -u and it should be done.

@qvalentin qvalentin merged commit 161e38c into ngalaiko:master Jan 14, 2025
2 checks passed
@qvalentin
Copy link
Collaborator

Thanks for the contributions 😄

Also feel free to review my open PRs.

@guilhas07
Copy link
Contributor Author

Thanks for the contributions 😄

Also feel free to review my open PRs.

Thank you. Will do!

@guilhas07 guilhas07 deleted the fix-comment branch January 14, 2025 16:32
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.

2 participants