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 quotes converting free-form syntax to yaml #4361

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .config/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ ruleset
runas
sarif
scalarint
scalarstring
scancode
schemafile
sdist
Expand Down
4 changes: 2 additions & 2 deletions examples/playbooks/transform-no-free-form.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

- name: Example task with usage for '=' as module params
ansible.builtin.debug:
msg: "'Hello there world'"
msg: "Hello there world"
changed_when: false

- name: Task that has a non-debug string with spaces
ansible.builtin.set_fact:
foo: '"String with spaces"'
foo: "String with spaces"
4 changes: 2 additions & 2 deletions examples/playbooks/vars/strings.transformed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# Make sure that the Transformer does not mangle strings
# TODO: there is a bug in ruamel.yaml that discards some EOL comments

single: single # this is a comment
single: "single" # this is a comment
single_with_double: '"single" quoted' # this is a comment

single_multiline_with_octothorpe: "single over 160 char line to force wrapping. over 160 char line to force wrapping. over 160 char line to force wrapping. over 160\n
# this is not a comment"

double: double # this is a comment
double: "double" # this is a comment
double_with_single: "'double' quoted" # this is a comment

double_multiline_with_octothorpe: "double over 160 char line to force wrapping. over 160 char line to force wrapping. over 160 char line to force wrapping. over 160\n
Expand Down
13 changes: 11 additions & 2 deletions src/ansiblelint/rules/no_free_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import sys
from typing import TYPE_CHECKING, Any

from ansiblelint.constants import INCLUSION_ACTION_NAMES, LINE_NUMBER_KEY
from ruamel.yaml.scalarstring import DoubleQuotedScalarString, SingleQuotedScalarString

from ansiblelint.constants import (
INCLUSION_ACTION_NAMES,
LINE_NUMBER_KEY,
)
from ansiblelint.rules import AnsibleLintRule, TransformMixin
from ansiblelint.rules.key_order import task_property_sorter

Expand Down Expand Up @@ -124,7 +129,11 @@ def filter_values(
# Keep quoted strings together
quote = v[0]
_, v, remainder = v.split(quote, 2)
v = f"{quote}{v}{quote}"
v = (
DoubleQuotedScalarString
if quote == '"'
else SingleQuotedScalarString
)(v)
else:
try:
v, remainder = v.split(" ", 1)
Expand Down
1 change: 1 addition & 0 deletions src/ansiblelint/yaml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ def __init__( # pylint: disable=too-many-arguments
# This will only preserve quotes for strings read from the file.
# anything modified by the transform will use no quotes, preferred_quote,
# or the quote that results in the least amount of escaping.
self.preserve_quotes = True
Copy link
Contributor Author

@cavcrosby cavcrosby Oct 10, 2024

Choose a reason for hiding this comment

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

This line has been placed under comments that seem to suggest that preserve_quotes should be set, but hasn't been, due to concerns about how this may affect transform functionality. Is this something we are fine with setting now? I set this to prevent quotes from being removed when linted.

# We should preserve_quotes loads all strings as a str subclass that carries
# a quote attribute. Will the str subclasses cause problems in transforms?
# Are there any other gotchas to this?
#
# This will only preserve quotes for strings read from the file.
# anything modified by the transform will use no quotes, preferred_quote,
# or the quote that results in the least amount of escaping.


# If needed, we can use this to change null representation to be explicit
# (see https://stackoverflow.com/a/44314840/1134951)
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/formatting-after/fmt-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ vegetables: # indented sequence:
- carrot

quoting:
- that should have double quotes
- that should remain in single quotes
- a string with " inside
- "that should have double quotes"
- "that should remain in single quotes"
- 'a string with " inside'
# next line has some undesired trailing spaces:
- a string with ' inside
- "a string with ' inside"
- can't be sure!
# next line should be converted to use double quotes:
- [foo, bar]
- ["foo", "bar"]

inline-dictionary:
- { foo: bar } # should add some spacing between curly braces and content
Expand Down
Loading