Skip to content

format: normalize quote style across single, double, and backtick strings#164

Draft
claude[bot] wants to merge 4 commits into
mainfrom
format-single-to-double-quotes
Draft

format: normalize quote style across single, double, and backtick strings#164
claude[bot] wants to merge 4 commits into
mainfrom
format-single-to-double-quotes

Conversation

@claude

@claude claude Bot commented Jun 24, 2026

Copy link
Copy Markdown

Requested by Ole Herman · Slack thread

Before / After

cfengine format now normalizes string-literal quote style across double, single, and backtick quotes. It prefers double quotes; falls back to single quotes when the string contains a double-quote character; and uses backticks only when the string contains both a double and a single quote.

Examples:

  • 'hello' becomes "hello"
  • `hello` becomes "hello"
  • 'say "hi"' stays single-quoted (it contains a double quote but no single quote)
  • `say "hi"` becomes 'say "hi"'
  • a string with both quote characters (e.g. 'he said "hi" it\'s') becomes the backtick form `he said "hi" it's`

If a string somehow needs all three styles at once (it contains a double quote, a single quote, and a backtick), it is left unchanged, since no single style can hold it without escaping.

How

A _normalize_quotes / _decode_literal / _encode_literal trio in src/cfengine_cli/format.py decodes a literal to its logical content (honoring CFEngine's escaping rules — single/double quotes recognize only \\, \", \', and a backslash-newline line continuation; backticks process no escapes), chooses the preferred style from that content, and re-encodes. It is applied at the quoted_string choke point (stringify_single_line_node) and the promiser site (_promiser_text), covered by the tests/format/012_quotes golden fixture.

Note for review

Promisers are normalized too (e.g. reports: 'hello'; becomes reports: "hello";). Calling this out explicitly so it can be vetoed if promisers should be left alone.


Generated by Claude Code

@claude claude Bot changed the title format: convert single-quoted string literals to double quotes format: normalize quote style across single, double, and backtick strings Jun 24, 2026
Convert string literals to a preferred quote style: double quotes by
default, single quotes when the content contains a double quote, and
backticks when it contains both a double and a single quote. Applies to
rval strings (via the quoted_string choke point) and promisers.
@claude claude Bot force-pushed the format-single-to-double-quotes branch from c6a5d1b to e2d6aa2 Compare June 24, 2026 18:49
@olehermanse olehermanse removed the request for review from larsewi June 24, 2026 18:52
Comment thread src/cfengine_cli/format.py Outdated
Comment on lines +101 to +102
Backtick strings are taken literally - CFEngine processes no escapes
inside them. Single- and double-quoted strings recognize only the escapes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not true, backtick strings are treated the same, and escapes are processed.

Comment thread src/cfengine_cli/format.py Outdated
Comment on lines +119 to +121
if nxt == "\n": # line continuation: drop both characters
i += 2
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This behavior was not requested and is not covered by the test.

Comment thread src/cfengine_cli/format.py Outdated
Comment on lines +113 to +118
if c == "\\" and i + 1 < len(inner):
nxt = inner[i + 1]
if nxt in ("\\", '"', "'"):
out.append(nxt)
i += 2
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The formatting test does not have \\ (or \\\\), should cover that too.

Comment thread src/cfengine_cli/format.py Outdated
Comment on lines +143 to +148
if (
len(literal) < 2
or literal[0] != literal[-1]
or literal[0] not in ("'", '"', "`")
):
return literal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be an assert? I don't see when this would be the case?

Comment thread src/cfengine_cli/format.py Outdated
Comment on lines +159 to +162
if target == "`" and "`" in content:
# Needs all three quote styles at once; a backtick string cannot
# contain a backtick, so leave the literal as written.
return literal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is also not covered in the test.

Address review on PR #164:

- Backtick strings process the SAME escapes as single/double quotes
  (\\, \", \'); a backtick string is not literal. Its only special
  property is that the delimiter itself cannot be escaped, so a literal
  backtick can never appear inside one. Unified _decode_literal (drops
  its delim arg) and _encode_literal (now escapes backslashes for
  backticks too) accordingly.
- Removed the unrequested backslash-newline line-continuation handling
  from _decode_literal.
- Turned the literal-shape guard in _normalize_quotes into an assert,
  since callers always pass a real quoted_string literal.
- Expanded the 012_quotes golden fixture with backslash cases (l/m/n/p)
  and an all-three-quotes case (o) to cover the unified escape behavior
  and the backtick encode branch.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U4hEZuqiuEFH2zy8wWGwyD
Comment thread tests/format/012_quotes.input.cf
Comment thread tests/format/012_quotes.expected.cf
claude added 2 commits June 24, 2026 19:53
Replaced the _decode_literal/_encode_literal helpers with a single-pass,
escape-aware re-quote (_scan_quotes + _requote) so converting between quote
styles passes through escaped backslashes, line continuations, and other
backslash sequences unchanged, and only re-escapes the target delimiter quote
character. The old decode/encode round-trip would mangle a single-quoted
line-continuation string into a doubled backslash.

Fixed fixture case q to convert a single-quoted line-continuation string to
double quotes with the continuation preserved verbatim.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01U4hEZuqiuEFH2zy8wWGwyD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants