Skip to content

[yaml_edit] Support comment-aware flow delimiter scanning and empty flow map values - #2592

Open
sigurdm wants to merge 2 commits into
mainfrom
fix-flow-delimiter-comments
Open

sigurdm wants to merge 2 commits into
mainfrom
fix-flow-delimiter-comments

Conversation

@sigurdm

@sigurdm sigurdm commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

This PR adds comment-aware flow delimiter scanning and fixes updating empty values in flow maps in package:yaml_edit.

Changes

  • Add YamlChar constants and helper predicates in lib/src/char_codes.dart.
  • Add findNextFlowDelimiter and findPreviousFlowDelimiter in lib/src/utils.dart that skip # ... comments when searching for flow delimiters (,, ], }).
  • Use comment-aware delimiter searching in flow list insertions (_insertInFlowList), flow list removals (_removeFromFlowList), and flow map removals (_removeFromFlowMap).
  • In _replaceInFlowMap, handle empty flow map values ({a:}) without leaving syntax errors when replacing with a value.
  • Add unit tests in test/insert_test.dart, test/remove_test.dart, test/update_test.dart, and test/utils_test.dart.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Package publishing

If you have publishing permissions, you can use the links below to publish the changes after merging this PR.

Package Version Status Publish tag (post-merge)
package:test_reflective_loader 0.6.0 ready to publish test_reflective_loader-v0.6.0
package:yaml_edit 2.2.5-wip WIP (no publish necessary)
  • 24 already published.
  • 16 WIP (no publish necessary).

Documentation at https://github.com/dart-lang/ecosystem/wiki/Publishing-automation.

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

PR Health

License Headers ✔️
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

Files
no missing headers

All source files should start with a license header.

Unrelated files missing license headers
Files
pkgs/html/test/differential_test.dart
pkgs/html/test/dom_parsing_test.dart
pkgs/html/test/mxss_test.dart
pkgs/html/test/noah_ark_clause_limit_test.dart

This check can be disabled by tagging the PR with skip-license-check.

Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

This check can be disabled by tagging the PR with skip-changelog-check.

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
yaml_edit None 2.2.4 2.2.5-wip 2.2.5-wip ✔️

This check can be disabled by tagging the PR with skip-breaking-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbol Leaking sources

This check can be disabled by tagging the PR with skip-leaking-check.

Unused Dependencies ✔️
Package Status
yaml_edit ✔️ All dependencies utilized correctly.

For details on how to fix these, see dependency_validator.

This check can be disabled by tagging the PR with skip-unused-dependencies-check.

Coverage ⚠️
File Coverage
pkgs/yaml_edit/lib/src/char_codes.dart 💚 100 %
pkgs/yaml_edit/lib/src/list_mutations.dart 💚 99 % ⬆️ 0 %
pkgs/yaml_edit/lib/src/map_mutations.dart 💔 99 % ⬇️ 1 %
pkgs/yaml_edit/lib/src/utils.dart 💔 91 % ⬇️ 3 %

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request fixes flow list and flow map mutations when comments contain delimiter characters, and adds support for updating empty values in flow maps without whitespace after the colon. It introduces a new YamlChar utility class and robust delimiter-finding helper functions (findNextFlowDelimiter and findPreviousFlowDelimiter) that correctly skip comments and quoted strings. The reviewer suggested replacing remaining magic numbers (like 0x27, 0x22, and 0x5C) in the delimiter-finding logic with new constants in YamlChar to improve consistency and readability.

Comment on lines +591 to +632
int _findCommentStartOnLine(String yaml, int lineStart, int lineEnd) {
var inSingleQuote = false;
var inDoubleQuote = false;
for (var i = lineStart; i < lineEnd; i++) {
final c = yaml.codeUnitAt(i);
final prev = i > lineStart ? yaml.codeUnitAt(i - 1) : YamlChar.space;

if (inSingleQuote) {
if (c == 0x27 /* ' */) {
if (i + 1 < lineEnd && yaml.codeUnitAt(i + 1) == 0x27) {
i++;
} else {
inSingleQuote = false;
}
}
} else if (inDoubleQuote) {
if (c == 0x5C /* \ */) {
i++;
} else if (c == 0x22 /* " */) {
inDoubleQuote = false;
}
} else {
final isQuoteStart = i == lineStart ||
YamlChar.isWhitespace(prev) ||
YamlChar.isFlowIndicator(prev) ||
prev == YamlChar.colon;

if (c == 0x27 /* ' */ && isQuoteStart) {
inSingleQuote = true;
} else if (c == 0x22 /* " */ && isQuoteStart) {
inDoubleQuote = true;
} else if (c == YamlChar.hash) {
if (i == lineStart ||
YamlChar.isWhitespace(prev) ||
YamlChar.isLineBreak(prev)) {
return i;
}
}
}
}
return -1;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

You've introduced a very helpful YamlChar class to manage character codes, which is great for readability and maintainability. However, there are still some magic numbers used for quote and backslash characters in this function and in findNextFlowDelimiter.

To improve consistency, consider adding constants for single quote (0x27), double quote (0x22), and backslash (0x5C) to the YamlChar class. Then, you can replace the magic numbers in this file with these new constants.

For example, you could add the following to pkgs/yaml_edit/lib/src/char_codes.dart:

// ...
  /// Character code for double quote (`"`).
  static const int doubleQuote = 0x22;

  /// Character code for single quote (`'`).
  static const int singleQuote = 0x27;

  /// Character code for backslash (`\`).
  static const int backslash = 0x5C;
// ...

And then use them here and in findNextFlowDelimiter:

// ...
if (c == YamlChar.singleQuote) {
// ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Resolved in commit fc17034. Added doubleQuote, singleQuote, and backslash to YamlChar and replaced the magic numbers in utils.dart.

@sigurdm
sigurdm marked this pull request as ready for review September 9, 2026 14:56
@sigurdm
sigurdm requested a review from a team as a code owner September 9, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant