Skip to content

Rewrite some interpolations with `safe_format`/`tag_pair`

Mark Florian requested to merge safe-format-migration into master

What does this MR do and why?

This transforms a somewhat common pattern of html_safe/link interpolation with the safe_format/tag_pair helpers.

This was implemented using Comby. The configuration file used can be found at https://gitlab.com/gitlab-org/frontend/pajamas-adoption-scanner/-/blob/safe-format-migration/comby/html_safe.toml.

For transparency, here's the contents of the config file:

[500-simple]
match=''':[ indent]- :[[link]] = '<a href="%{url}" target="_blank" rel="noopener noreferrer">'.html_safe % { url: :[url] }
= :[[f]](:[text]).html_safe % { :[[start_key]]: :[start], :[[end_key]]: '</a>'.html_safe }'''
rewrite=''':[indent]- link = link_to('', :[url], target: '_blank', rel: 'noopener noreferrer')
:[indent]= safe_format(:[f](:[text]), tag_pair(link, ::[start_key], ::[end_key]))'''
rule='''
where
match :[link] {
| ":[_~_start$]" -> true
| ":[_]" -> false
},
'''

The tool was run with something like this command in the GitLab repository:

comby \
    -config ~/dev/pajamas-adoption-scanner/comby/html_safe.toml \
    -custom-matcher ~/dev/pajamas-adoption-scanner/comby/haml.json \
    -matcher .generic -in-place \
    -f .haml

It may be there are other common patterns of html_safe usage that can be transformed in this way in future iterations.

Screenshots or screen recordings

n/a

How to set up and validate locally

Because this is a batch/automated migration across the whole codebase, it's hard to write validation steps, since these strings are visible under different circumstances. I'd suggest choosing two or three to review visually, and relying on inspection of the code for the rest.

Note that because all but one of the changes (see first commit) were automated, typos are extremely unlikely. More useful things to look out for might be:

  • Shadowing of any existing link variable
  • Dropped link attributes
  • I'm not sure what else 🤷

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Mark Florian

Merge request reports