diff --git a/gnovm/pkg/gnomod/file.go b/gnovm/pkg/gnomod/file.go index 564d4e29361..473e60850ca 100644 --- a/gnovm/pkg/gnomod/file.go +++ b/gnovm/pkg/gnomod/file.go @@ -15,6 +15,7 @@ import ( // Parsed gno.mod file. type File struct { + Draft bool Module *modfile.Module Go *modfile.Go Require []*modfile.Require diff --git a/gnovm/pkg/gnomod/parse.go b/gnovm/pkg/gnomod/parse.go index dfb5ac62057..a23a5a06b46 100644 --- a/gnovm/pkg/gnomod/parse.go +++ b/gnovm/pkg/gnomod/parse.go @@ -49,6 +49,10 @@ func Parse(file string, data []byte) (*File, error) { f.add(&errs, x, l, x.Token[0], l.Token) } } + case *modfile.CommentBlock: + if x.Start.Line == 1 { + f.Draft = parseDraft(x) + } } } diff --git a/gnovm/pkg/gnomod/parse_test.go b/gnovm/pkg/gnomod/parse_test.go index ce3a745147e..934531e69c7 100644 --- a/gnovm/pkg/gnomod/parse_test.go +++ b/gnovm/pkg/gnomod/parse_test.go @@ -116,3 +116,54 @@ func TestModuleDeprecated(t *testing.T) { }) } } + +func TestParseDraft(t *testing.T) { + for _, tc := range []struct { + desc, in string + expected bool + }{ + { + desc: "no_comment", + in: `module m`, + }, + { + desc: "other_comment", + in: `// yo`, + }, + { + desc: "draft_no_space", + in: `//Draft`, + expected: true, + }, + { + desc: "draft_simple", + in: `// Draft`, + expected: true, + }, + { + desc: "draft_lowercase", + in: `// draft`, + }, + { + desc: "draft_multiline", + in: `// Draft + // yo`, + }, + { + desc: "draft_mixed", + in: `// some other comment + // Draft`, + }, + { + desc: "draft_not_first_line", + in: ` + // Draft`, + }, + } { + t.Run(tc.desc, func(t *testing.T) { + f, err := Parse("in", []byte(tc.in)) + assert.Nil(t, err) + assert.Equal(t, tc.expected, f.Draft) + }) + } +} diff --git a/gnovm/pkg/gnomod/read.go b/gnovm/pkg/gnomod/read.go index a05efa19c9f..206c843f86a 100644 --- a/gnovm/pkg/gnomod/read.go +++ b/gnovm/pkg/gnomod/read.go @@ -833,3 +833,15 @@ func parseDirectiveComment(block *modfile.LineBlock, line *modfile.Line) string } return strings.Join(lines, "\n") } + +// parseDraft returns whether the module is marked as draft. +func parseDraft(block *modfile.CommentBlock) bool { + if len(block.Before) != 1 { + return false + } + comment := block.Before[0] + if strings.TrimSpace(strings.TrimPrefix(comment.Token, "//")) != "Draft" { + return false + } + return true +}