dmnd/dedent

๐Ÿš€ Feature: Add an option to disable special character escaping

JoshuaKGoldberg opened this issue ยท 2 comments

This pulls together #2, #26, #45, #46:

  • #2: Backticks were confirmed as expected to be (un-)escaped properly when dedent is used with template literal strings.
    • By extension, it makes sense to consider other native template literal string escapings as unwanted: #45 -> #46
  • #26: On the other hand, when dedent is used as a function, users aren't expecting that special character escaping

I think the "right" (or: least unexpected) way would be to add an option with a not-very-savory name like escapeSpecialCharacters. The option would default to:

  • true when called for a template literal string
  • false when called as a function

Proposal:

  • Let's add this option in separately from #46, to resolve this issue and 26
  • I can release a new minor version that includes this option and 26
    • I'm a little hesitant to merge that PR before this option is in, in case folks are relying on the existing behavior

Thoughts @G-Rath @sirian?

Personally I would be considered about having different behaviour depending on how its called given you can do:

dedent(`my string`)

which cannot be detected as a template literal

Actually having thought about it a bit more, I think that default actually makes sense in reflection of the use-case:

  • being called as a function, it's most likely that the input is completely programmatic i.e. dedent(cmsData.text)
    • meaning you would want to have the content treated equal because any escaped template literal characters couldn't ever cause interpolation since its a computed string at that point
  • being called with template literals, it's most likely that the input is "decorative" i.e. ESLint test cases, reporter formatters, etc
    • meaning you would want to have template literal characters escaped, as they could cause interpolation hence why they were probably escaped