Skip to content

Commit 2ea2986

Browse files
authored
Merge pull request #35 from arduino/per1234/spell-check
Add checks for commonly misspelled words in library.properties
2 parents 7fa8e7c + 8b87bb2 commit 2ea2986

File tree

13 files changed

+116
-1
lines changed

13 files changed

+116
-1
lines changed

Diff for: Taskfile.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,5 @@ vars:
189189

190190
WORKFLOW_SCHEMA_PATH: "$(mktemp -t gha-workflow-schema-XXXXXXXXXX.json)"
191191

192-
CODESPELL_SKIP_OPTION: '--skip "./.git,./go.mod,./go.sum,./arduino-check,./arduino-check.exe"'
192+
CODESPELL_SKIP_OPTION: '--skip "./.git,./go.mod,./go.sum,./arduino-check,./arduino-check.exe,./check/checkfunctions/testdata/libraries/MisspelledSentenceParagraphValue/library.properties"'
193193
CODESPELL_IGNORE_WORDS_OPTION: "--ignore-words ./etc/codespell-ignore-words-list.txt"

Diff for: check/checkconfigurations/checkconfigurations.go

+30
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,36 @@ var configurations = []Type{
206206
ErrorModes: []checkmode.Type{checkmode.All},
207207
CheckFunction: checkfunctions.LibraryPropertiesAuthorFieldLTMinLength,
208208
},
209+
{
210+
ProjectType: projecttype.Library,
211+
Category: "library.properties",
212+
Subcategory: "sentence field",
213+
ID: "",
214+
Brief: "sentence spell check",
215+
Description: "",
216+
MessageTemplate: "A commonly misspelled word was found in the library.properties sentence field. Suggested correction: {{.}}",
217+
DisableModes: nil,
218+
EnableModes: []checkmode.Type{checkmode.All},
219+
InfoModes: nil,
220+
WarningModes: []checkmode.Type{checkmode.All},
221+
ErrorModes: nil,
222+
CheckFunction: checkfunctions.LibraryPropertiesSentenceFieldSpellCheck,
223+
},
224+
{
225+
ProjectType: projecttype.Library,
226+
Category: "library.properties",
227+
Subcategory: "paragraph field",
228+
ID: "",
229+
Brief: "paragraph spell check",
230+
Description: "",
231+
MessageTemplate: "A commonly misspelled word was found in the library.properties paragraph field. Suggested correction: {{.}}",
232+
DisableModes: nil,
233+
EnableModes: []checkmode.Type{checkmode.All},
234+
InfoModes: nil,
235+
WarningModes: []checkmode.Type{checkmode.All},
236+
ErrorModes: nil,
237+
CheckFunction: checkfunctions.LibraryPropertiesParagraphFieldSpellCheck,
238+
},
209239
{
210240
ProjectType: projecttype.Library,
211241
Category: "library.properties",

Diff for: check/checkdata/library.go

+13
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/arduino/arduino-check/result/feedback"
2828
"github.com/arduino/go-paths-helper"
2929
"github.com/arduino/go-properties-orderedmap"
30+
"github.com/client9/misspell"
3031
"github.com/ory/jsonschema/v3"
3132
"github.com/sirupsen/logrus"
3233
)
@@ -61,6 +62,11 @@ func InitializeForLibrary(project project.Type, schemasPath *paths.Path) {
6162
panic(err)
6263
}
6364
}
65+
66+
if misspelledWordsReplacer == nil { // The replacer only needs to be compiled once per run.
67+
misspelledWordsReplacer = misspell.New()
68+
misspelledWordsReplacer.Compile()
69+
}
6470
}
6571

6672
var libraryPropertiesLoadError error
@@ -90,3 +96,10 @@ var libraryManagerIndex map[string]interface{}
9096
func LibraryManagerIndex() map[string]interface{} {
9197
return libraryManagerIndex
9298
}
99+
100+
var misspelledWordsReplacer *misspell.Replacer
101+
102+
// MisspelledWordsReplacer returns the misspelled words replacer used for spell check.
103+
func MisspelledWordsReplacer() *misspell.Replacer {
104+
return misspelledWordsReplacer
105+
}

Diff for: check/checkfunctions/library.go

+29
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ func LibraryPropertiesAuthorFieldLTMinLength() (result checkresult.Type, output
175175
return checkresult.Pass, ""
176176
}
177177

178+
// LibraryPropertiesSentenceFieldSpellCheck checks for commonly misspelled words in the library.properties `sentence` field value.
179+
func LibraryPropertiesSentenceFieldSpellCheck() (result checkresult.Type, output string) {
180+
return spellCheckLibraryPropertiesFieldValue("sentence")
181+
}
182+
183+
// LibraryPropertiesParagraphFieldSpellCheck checks for commonly misspelled words in the library.properties `paragraph` field value.
184+
func LibraryPropertiesParagraphFieldSpellCheck() (result checkresult.Type, output string) {
185+
return spellCheckLibraryPropertiesFieldValue("paragraph")
186+
}
187+
178188
// LibraryPropertiesDependsFieldNotInIndex checks whether the libraries listed in the library.properties `depends` field are in the Library Manager index.
179189
func LibraryPropertiesDependsFieldNotInIndex() (result checkresult.Type, output string) {
180190
if checkdata.LibraryPropertiesLoadError() != nil {
@@ -205,6 +215,25 @@ func LibraryPropertiesDependsFieldNotInIndex() (result checkresult.Type, output
205215
return checkresult.Pass, ""
206216
}
207217

218+
// spellCheckLibraryPropertiesFieldValue returns the value of the provided library.properties field with commonly misspelled words corrected.
219+
func spellCheckLibraryPropertiesFieldValue(fieldName string) (result checkresult.Type, output string) {
220+
if checkdata.LibraryPropertiesLoadError() != nil {
221+
return checkresult.NotRun, ""
222+
}
223+
224+
fieldValue, ok := checkdata.LibraryProperties().GetOk(fieldName)
225+
if !ok {
226+
return checkresult.NotRun, ""
227+
}
228+
229+
replaced, diff := checkdata.MisspelledWordsReplacer().Replace(fieldValue)
230+
if diff != nil {
231+
return checkresult.Fail, replaced
232+
}
233+
234+
return checkresult.Pass, ""
235+
}
236+
208237
// nameInLibraryManagerIndex returns whether there is a library in Library Manager index using the given name.
209238
func nameInLibraryManagerIndex(name string) bool {
210239
libraries := checkdata.LibraryManagerIndex()["libraries"].([]interface{})

Diff for: check/checkfunctions/library_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,28 @@ func TestLibraryPropertiesNameFieldNotInIndex(t *testing.T) {
8282
checkCheckFunction(LibraryPropertiesNameFieldNotInIndex, testTables, t)
8383
}
8484

85+
func TestLibraryPropertiesSentenceFieldSpellCheck(t *testing.T) {
86+
testTables := []checkFunctionTestTable{
87+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
88+
{"Not defined", "MissingFields", checkresult.NotRun, ""},
89+
{"Misspelled word", "MisspelledSentenceParagraphValue", checkresult.Fail, "^grill broccoli now$"},
90+
{"Correct spelling", "Recursive", checkresult.Pass, ""},
91+
}
92+
93+
checkCheckFunction(LibraryPropertiesSentenceFieldSpellCheck, testTables, t)
94+
}
95+
96+
func TestLibraryPropertiesParagraphFieldSpellCheck(t *testing.T) {
97+
testTables := []checkFunctionTestTable{
98+
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},
99+
{"Not defined", "MissingFields", checkresult.NotRun, ""},
100+
{"Misspelled word", "MisspelledSentenceParagraphValue", checkresult.Fail, "^There is a zebra$"},
101+
{"Correct spelling", "Recursive", checkresult.Pass, ""},
102+
}
103+
104+
checkCheckFunction(LibraryPropertiesParagraphFieldSpellCheck, testTables, t)
105+
}
106+
85107
func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) {
86108
testTables := []checkFunctionTestTable{
87109
{"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""},

Diff for: check/checkfunctions/testdata/libraries/MissingFields/library.properties

Whitespace-only changes.

Diff for: check/checkfunctions/testdata/libraries/MissingFields/src/MissingFields.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=MisspelledSentenceParagraphValue
2+
version=1.0.0
3+
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
4+
maintainer=Cristian Maglie <[email protected]>
5+
sentence=grill brocoli now
6+
paragraph=There is a zeebra
7+
category=Communication
8+
url=http://example.com/
9+
architectures=avr

Diff for: check/checkfunctions/testdata/libraries/MisspelledSentenceParagraphValue/src/MisspelledSentenceParagraphValue.h

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Recursive
2+
version=1.0.0
3+
author=Cristian Maglie <[email protected]>, Pippo Pluto <[email protected]>
4+
maintainer=Cristian Maglie <[email protected]>
5+
sentence=A library that makes coding a web server a breeze.
6+
paragraph=Supports HTTP1.1 and you can do GET and POST.
7+
category=Communication
8+
url=http://example.com/
9+
architectures=avr
10+
includes=Recursive.h

Diff for: check/checkfunctions/testdata/libraries/Recursive/src/Recursive.h

Whitespace-only changes.

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/arduino/arduino-cli v0.0.0-20200922073731-53e3230c4f71
77
github.com/arduino/go-paths-helper v1.3.2
88
github.com/arduino/go-properties-orderedmap v1.4.0
9+
github.com/client9/misspell v0.3.4
910
github.com/ory/jsonschema/v3 v3.0.1
1011
github.com/sirupsen/logrus v1.6.0
1112
github.com/spf13/cobra v1.0.1-0.20200710201246-675ae5f5a98c

Diff for: go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4r
4747
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
4848
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
4949
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575/go.mod h1:9d6lWj8KzO/fd/NrVaLscBKmPigpZpn5YawRPw+e3Yo=
50+
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
5051
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
5152
github.com/cmaglie/go.rice v1.0.3/go.mod h1:AF3bOWkvdOpp8/S3UL8qbQ4N7DiISIbJtj54GWFPAsc=
5253
github.com/cmaglie/pb v1.0.27/go.mod h1:GilkKZMXYjBA4NxItWFfO+lwkp59PLHQ+IOW/b/kmZI=

0 commit comments

Comments
 (0)