Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/rules/yaml-key-sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export default class YamlKeySort extends RuleBuilder<YamlKeySortOptions> {
const doc = parseYAML(yamlText);
const startingPriorityKeys = getEmptyDocument(doc);

if (doc.contents == null) {
return text;
}

Comment on lines +66 to +69
Copy link
Collaborator

@pjkaufman pjkaufman Sep 24, 2025

Choose a reason for hiding this comment

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

Should this not be before line 64 since that is where the error is happening since that allows us to exit immediately after we know there is no content in the YAML? At least, that is where the error happens for me locally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes - you're right. Or should the code of

if (!yaml) {
be changed to check for an empty match by changing it to if (!yaml || yaml[1] == "")

Copy link
Collaborator

Choose a reason for hiding this comment

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

Returning an empty YAML string is fine from my understanding since it is possible to have YAML that is just empty and I would expect the content to be an empty string in that case. I am not sure if things would break if we also returned null for empty strings for the YAML.

I think that the proposed solution just before getting an empty document makes the most sense to me at this time.

let remainingKeys = this.getYAMLKeysSorted(yamlKeys, doc, startingPriorityKeys);

const sortOrder = options.yamlSortOrderForOtherKeys;
Expand Down