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(yaml): less aggressive jinja2 stripping #33190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rarkins
Copy link
Collaborator

@rarkins rarkins commented Dec 18, 2024

Changes

Context

#32967
https://regexr.com/89otc

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • No documentation update is required

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

@rarkins rarkins requested a review from viceice December 18, 2024 13:28
@@ -143,7 +143,7 @@ export function dump(obj: any, opts?: DumpOptions): string {
function massageContent(content: string, options?: YamlOptions): string {
if (options?.removeTemplates) {
return content
.replace(regEx(/\s+{{.+?}}:.+/gs), '')
.replace(regEx(/\n\s*{{.+?}}:\s*\n/gs), '')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.replace(regEx(/\n\s*{{.+?}}:\s*\n/gs), '')
.replace(regEx(/^\s*{{.+?}}:\s*\n/gs), '')

otherwise you'll remove one new line to much

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will that ^ match "start of line" or "start of file"?

Copy link

@Sculas Sculas Dec 18, 2024

Choose a reason for hiding this comment

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

If I open the regexr link and use that regex, it no longer matches the template. The documentation for ^ mentions:

Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled.

So, to answer the question above, it matches the "start of file" when the multiline flag is disabled (by default).
Enabling the multiline flag makes the regex work as expected (regexr link):

Suggested change
.replace(regEx(/\n\s*{{.+?}}:\s*\n/gs), '')
.replace(regEx(/^\s*{{.+?}}:\s*\n/gms), '')

Copy link
Member

Choose a reason for hiding this comment

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

single line flag is also required, so . doesn't match newline

Copy link

Choose a reason for hiding this comment

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

If you don't want . to match newlines, you should remove the dotall flag:

Suggested change
.replace(regEx(/\n\s*{{.+?}}:\s*\n/gs), '')
.replace(regEx(/^\s*{{.+?}}:\s*\n/gm), '')

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we're stuck again? @viceice please propose a fully working change

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.

3 participants