Skip to content

Commit

Permalink
chore: add location helper for imports (#914)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Tschumak <[email protected]>
  • Loading branch information
maxim-tschumak authored Jan 4, 2022
1 parent 3c6137e commit 2c5a599
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
6 changes: 6 additions & 0 deletions locations/file_locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ func FileResourceDefinition(f *desc.FileDescriptor, index int) *dpb.SourceCodeIn
// 8 == options
return pathLocation(f, 8, int(apb.E_ResourceDefinition.TypeDescriptor().Number()), index)
}

// FileImport returns the location of the import on the given `index`, or `nil`
// if no import with such `index` is found.
func FileImport(f *desc.FileDescriptor, index int) *dpb.SourceCodeInfo_Location {
return pathLocation(f, 3, index) // 3 == dependency
}
52 changes: 45 additions & 7 deletions locations/file_locations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestLocations(t *testing.T) {
// proto3 rules!
syntax = "proto3";
import "google/api/resource.proto";
package google.api.linter;
option csharp_namespace = "Google.Api.Linter";
Expand All @@ -45,18 +47,54 @@ func TestLocations(t *testing.T) {
tests := []struct {
testName string
fx func(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location
idxFx func(f *desc.FileDescriptor, i int) *dpb.SourceCodeInfo_Location
idx int
wantSpan []int32
}{
{"Syntax", FileSyntax, []int32{1, 0, int32(len("syntax = \"proto3\";"))}},
{"Package", FilePackage, []int32{3, 0, int32(len("package google.api.linter;"))}},
{"CsharpNamespace", FileCsharpNamespace, []int32{5, 0, int32(len(`option csharp_namespace = "Google.Api.Linter";`))}},
{"JavaPackage", FileJavaPackage, []int32{6, 0, int32(len(`option java_package = "com.google.api.linter";`))}},
{"PhpNamespace", FilePhpNamespace, []int32{7, 0, int32(len(`option php_namespace = "Google\\Api\\Linter";`))}},
{"RubyPackage", FileRubyPackage, []int32{8, 0, int32(len(`option ruby_package = "Google::Api::Linter";`))}},
{
testName: "Syntax",
fx: FileSyntax,
wantSpan: []int32{1, 0, int32(len("syntax = \"proto3\";"))},
}, {
testName: "Package",
fx: FilePackage,
wantSpan: []int32{5, 0, int32(len("package google.api.linter;"))},
}, {
testName: "CsharpNamespace",
fx: FileCsharpNamespace,
wantSpan: []int32{7, 0, int32(len(`option csharp_namespace = "Google.Api.Linter";`))},
},
{
testName: "JavaPackage",
fx: FileJavaPackage,
wantSpan: []int32{8, 0, int32(len(`option java_package = "com.google.api.linter";`))},
},
{
testName: "PhpNamespace",
fx: FilePhpNamespace,
wantSpan: []int32{9, 0, int32(len(`option php_namespace = "Google\\Api\\Linter";`))},
},
{
testName: "RubyPackage",
fx: FileRubyPackage,
wantSpan: []int32{10, 0, int32(len(`option ruby_package = "Google::Api::Linter";`))},
},
{
testName: "Import",
idxFx: FileImport,
idx: 0,
wantSpan: []int32{3, 0, int32(len(`import "google/api/resource.proto";`))},
},
}
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
if diff := cmp.Diff(test.fx(f).Span, test.wantSpan); diff != "" {
var l *dpb.SourceCodeInfo_Location
if test.fx != nil {
l = test.fx(f)
} else {
l = test.idxFx(f, test.idx)
}
if diff := cmp.Diff(l.Span, test.wantSpan); diff != "" {
t.Errorf(diff)
}
})
Expand Down

0 comments on commit 2c5a599

Please sign in to comment.