Refuse to write a document's includes into one file by accident

- Write and WriteTo return an error for a document with includes unless the
  includes are inlined, instead of dropping the files they hold. Only
  WriteFiles collects included files and checks them for conflicts.
- ExpandVariables leaves comments as they are.
- An inlined file no longer leaves a line of its own for the include's line
  break.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 18:30:19 +00:00
co-authored by Claude Sonnet 5.5
parent ec4b27c005
commit d15c8fd3e6
3 changed files with 131 additions and 29 deletions
+41 -24
View File
@@ -41,29 +41,37 @@ type WriteOptions struct {
// forward slashes, in the folder of the main file.
type FileWriter func(path string, data []byte) error
// Write writes the main file to w. With ExpandVariables and not
// InlineIncludes, the files it includes would need the variables it no longer
// defines, so it is an error unless no include was read; use [Document.WriteFiles]
// then.
// Write writes the document as one file to w. A document that has includes
// that were read can be written as one file only with InlineIncludes, or else
// the files they hold would be dropped; Write returns [ErrInvalid] then. With
// ExpandVariables, the included files would also need variables that the main
// file no longer defines, so it needs InlineIncludes as well. Use
// [Document.WriteFiles] to write the files separately.
func (d *Document) Write(w io.Writer, opts WriteOptions) error {
if d.hasIncludes() && !opts.InlineIncludes {
return fmt.Errorf("%w: the document has includes, which need InlineIncludes, or WriteFiles", ErrInvalid)
}
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}}
text := r.file(d.Root)
if r.err != nil {
return r.err
}
if opts.ExpandVariables && !opts.InlineIncludes && len(r.outputs) > 0 {
return fmt.Errorf("%w: ExpandVariables needs InlineIncludes, or WriteFiles, when includes were read", ErrInvalid)
}
_, err := io.WriteString(w, r.lineEndings(text))
_, err := io.WriteString(w, r.lineEndings(r.file(d.Root)))
return err
}
// hasIncludes reports whether the document read any include.
func (d *Document) hasIncludes() bool {
for _, n := range d.Root.Nodes {
if n.Kind == KindInclude && n.File != nil {
return true
}
}
return false
}
// WriteFiles writes the main file with the path root, and each file it
// includes at its own path, in the folder of root, using fw. It writes nothing
// and returns [ErrInvalid] if an include's path is absolute or leaves the
// folder.
func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) error {
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}}
r := &renderer{opts: opts, defines: defines{}, outputs: map[string]string{}, collect: true}
text := r.file(d.Root)
if r.err != nil {
return r.err
@@ -94,6 +102,9 @@ func (d *Document) WriteFiles(root string, fw FileWriter, opts WriteOptions) err
type renderer struct {
opts WriteOptions
defines defines
// collect is whether the included files that stand apart are kept in
// outputs, as WriteFiles writes them.
collect bool
// outputs are the included files that stand apart, by path.
outputs map[string]string
err error
@@ -113,8 +124,15 @@ func (r *renderer) file(f *File) string {
// dropBreak is whether the next line break is cut, as the directive it
// followed is.
dropBreak := false
// gap is whether an inlined file ended without whitespace, so that what
// follows needs some.
gap := false
for _, n := range f.Nodes {
raw := n.Raw
if gap && raw != "" && !isSpace(raw[0]) {
b.WriteByte(' ')
}
gap = false
switch n.Kind {
case KindDefine:
if r.opts.ExpandVariables {
@@ -126,13 +144,19 @@ func (r *renderer) file(f *File) string {
dropBreak = false
if r.opts.InlineIncludes && n.File != nil {
inner := r.file(n.File)
b.WriteString(spaced(b.String(), inner))
if s := b.String(); s != "" && !isSpace(s[len(s)-1]) && inner != "" && !isSpace(inner[0]) {
b.WriteByte(' ')
}
b.WriteString(inner)
// The include's own line break would leave an empty line.
dropBreak = strings.HasSuffix(inner, "\n")
gap = inner != "" && !isSpace(inner[len(inner)-1])
continue
}
if r.opts.ExpandVariables {
raw = `#include "` + n.Resolved + `"`
}
if n.File != nil {
if n.File != nil && r.collect {
r.output(n, r.file(n.File))
}
case KindSpace:
@@ -140,6 +164,8 @@ func (r *renderer) file(f *File) string {
raw = strings.TrimPrefix(strings.TrimPrefix(raw, "\r"), "\n")
}
dropBreak = false
case KindComment:
dropBreak = false
default:
dropBreak = false
if r.opts.ExpandVariables {
@@ -151,15 +177,6 @@ func (r *renderer) file(f *File) string {
return b.String()
}
// spaced returns the text of an included file, with a space on each side that
// would otherwise join it to the text around it.
func spaced(before, inner string) string {
if before != "" && !isSpace(before[len(before)-1]) && inner != "" && !isSpace(inner[0]) {
inner = " " + inner
}
return inner + " "
}
// output keeps the text of an included file, and reports a file that comes out
// differently each time it is included.
func (r *renderer) output(n *Node, text string) {