Skip to content

Commit eed2760

Browse files
committed
Fix consistency check when using line compiler directive.
1 parent aeefda5 commit eed2760

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

getters.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {
5858
return nil, errUnsuitableInput
5959
}
6060

61-
// Check consistency to avoid checking slice indexes in each function
61+
// Check consistency to avoid checking slice indexes in each function.
62+
// Note that `PositionFor` is used with `adjusted=false` to skip `//line`
63+
// directives that can set references to other files (e.g. templates)
64+
// instead of the real ones, and break consistency here.
65+
// Issue: https://github.com/tetafro/godot/issues/32
6266
lastComment := pf.file.Comments[len(pf.file.Comments)-1]
63-
if p := pf.fset.Position(lastComment.End()); len(pf.lines) < p.Line {
67+
if p := pf.fset.PositionFor(lastComment.End(), false); len(pf.lines) < p.Line {
6468
return nil, fmt.Errorf("inconsistency between file and AST: %s", p.Filename)
6569
}
6670

godot_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ func TestRun(t *testing.T) {
4040
}
4141
})
4242

43+
t.Run("line directive", func(t *testing.T) {
44+
testFile := filepath.Join("testdata", "line", "main.go")
45+
fset := token.NewFileSet()
46+
f, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)
47+
if err != nil {
48+
t.Fatalf("Failed to parse input file: %v", err)
49+
}
50+
51+
issues, err := Run(f, fset, Settings{})
52+
if err != nil {
53+
t.Fatalf("Unexpected error: %v", err)
54+
}
55+
if len(issues) > 0 {
56+
t.Fatal("Unexpected issues")
57+
}
58+
})
59+
4360
testFile := filepath.Join("testdata", "check", "main.go")
4461
fset := token.NewFileSet()
4562
file, err := parser.ParseFile(fset, testFile, nil, parser.ParseComments)

testdata/line/main.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This is a test for `//line` compiler directive. It can add references
2+
// to other files, for parsed code. If it happens, it breaks consistency
3+
// between the source code and the parsed files, which godot relies on.
4+
package main
5+
6+
import "fmt"
7+
8+
func main() {
9+
//line main.tpl:100
10+
fmt.Println("Template")
11+
// Bye!
12+
}

testdata/line/main.tpl

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Template file

0 commit comments

Comments
 (0)