Skip to content

Commit

Permalink
fix: implement preprocessing for formatting anomalies
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorBrennan committed Dec 10, 2024
1 parent 4508b57 commit 1c96201
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ func loadYAMLFiles(fsys fs.FS, paths []string) (loadResults, error) {
return nil, fmt.Errorf("failed to read file %s: %w", pth, err)
}

// Preprocess the YAML content
cleanedContents, err := preprocessYAML(contents)
if err != nil {
return nil, fmt.Errorf("failed to preprocess YAML content for %s: %w", pth, err)
}

var node yaml.Node
dec := yaml.NewDecoder(bytes.NewReader(contents))
dec := yaml.NewDecoder(bytes.NewReader(cleanedContents))
dec.SetScanBlockScalarAsLiteral(true)
if err := dec.Decode(&node); err != nil {
return nil, fmt.Errorf("failed to parse yaml for %s: %w", pth, err)
Expand Down Expand Up @@ -233,6 +239,18 @@ func computeNewlineTargets(before, after string) []int {
return result
}

func preprocessYAML(content []byte) ([]byte, error) {
var cleaned []string
lines := strings.Split(string(content), "\n")

for _, line := range lines {
cleanedLine := strings.TrimRight(line, " \t")
cleaned = append(cleaned, cleanedLine)
}

return []byte(strings.Join(cleaned, "\n")), nil
}

func processMultilineNodes(node *yaml.Node) {
if node == nil {
return
Expand Down

0 comments on commit 1c96201

Please sign in to comment.