Skip to content

Commit

Permalink
Avoid panic on spelling inconsistencies errors
Browse files Browse the repository at this point in the history
close #5
  • Loading branch information
nihei9 committed Mar 21, 2022
1 parent 1adcb17 commit f6b90c9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
61 changes: 61 additions & 0 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,67 @@ func TestCompile(t *testing.T) {
}
]
}
`,
Err: true,
},
{
Caption: "don't allow kind names in the same mode to contain spelling inconsistencies",
Spec: `
{
"name": "test",
"entries": [
{
"kind": "foo_1",
"pattern": "foo_1"
},
{
"kind": "foo1",
"pattern": "foo1"
}
]
}
`,
Err: true,
},
{
Caption: "don't allow kind names across modes to contain spelling inconsistencies",
Spec: `
{
"name": "test",
"entries": [
{
"modes": ["default"],
"kind": "foo_1",
"pattern": "foo_1"
},
{
"modes": ["other_mode"],
"kind": "foo1",
"pattern": "foo1"
}
]
}
`,
Err: true,
},
{
Caption: "don't allow mode names to contain spelling inconsistencies",
Spec: `
{
"name": "test",
"entries": [
{
"modes": ["foo_1"],
"kind": "a",
"pattern": "a"
},
{
"modes": ["foo1"],
"kind": "b",
"pattern": "b"
}
]
}
`,
Err: true,
},
Expand Down
12 changes: 7 additions & 5 deletions spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,20 @@ func findSpellingInconsistenciesErrors(ids []string, hook func(ids []string) err

var errs []error
for _, dup := range duplicated {
err := hook(dup)
if err != nil {
errs = append(errs, err)
continue
if hook != nil {
err := hook(dup)
if err != nil {
errs = append(errs, err)
continue
}
}

var b strings.Builder
fmt.Fprintf(&b, "%+v", dup[0])
for _, id := range dup[1:] {
fmt.Fprintf(&b, ", %+v", id)
}
err = fmt.Errorf("these identifiers are treated as the same. please use the same spelling: %v", b.String())
err := fmt.Errorf("these identifiers are treated as the same. please use the same spelling: %v", b.String())
errs = append(errs, err)
}

Expand Down

0 comments on commit f6b90c9

Please sign in to comment.