diff options
| author | Fuwn <[email protected]> | 2026-02-27 07:13:17 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-27 07:13:17 +0000 |
| commit | 856e2994722e2e7f67b47d55b8e673ddabcebe83 (patch) | |
| tree | 5a4e108384038eaa072d8e6c5f71ab68901fb431 | |
| download | kivia-main.tar.xz kivia-main.zip | |
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | LICENSE-APACHE | 202 | ||||
| -rw-r--r-- | LICENSE-MIT | 5 | ||||
| -rw-r--r-- | README.md | 164 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | internal/analyze/analyze.go | 208 | ||||
| -rw-r--r-- | internal/analyze/analyze_test.go | 174 | ||||
| -rw-r--r-- | internal/analyze/resources.go | 26 | ||||
| -rw-r--r-- | internal/collect/collect.go | 331 | ||||
| -rw-r--r-- | internal/nlp/dictionary.go | 614 | ||||
| -rw-r--r-- | internal/nlp/dictionary_test.go | 165 | ||||
| -rw-r--r-- | internal/report/report.go | 80 | ||||
| -rw-r--r-- | justfile | 20 | ||||
| -rw-r--r-- | main.go | 196 | ||||
| -rw-r--r-- | main_test.go | 103 | ||||
| -rw-r--r-- | testdata/dictionary/words.txt | 9 | ||||
| -rw-r--r-- | testdata/samplepkg/sample.go | 20 |
18 files changed, 2325 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..0e77b64 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,5 @@ +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2cc58af --- /dev/null +++ b/README.md @@ -0,0 +1,164 @@ +# 🏰 Kivia + +> Self-documenting Name Analyser for Go + +Name things so your code explains itself. + +Kivia is a fast, local-only analyser that flags identifiers whose terms are absent from dictionary sources or resemble abbreviations of dictionary words. It is built for teams that want explicit, readable naming conventions without external AI calls. + +## Philosophy + +Identifier names should be fully self-documenting. + +Kivia enforces a strict readability standard: + +- Prefer full words over shorthand +- Avoid ambiguous abbreviations +- Keep the naming intent clear from the identifier itself + +Examples: + +- `userNum` → invalid (`num` is an abbreviation) +- `ctx` → invalid (`ctx` is an abbreviation) +- `userCount` → valid +- `requestContext` → valid + +## Rules + +1. **Dictionary words pass**: If a token is present in the loaded dictionary sources, it passes. +2. **Abbreviations are violations**: If a token expands to a dictionary word (for example, `ctx` -> `context`), it is flagged. +3. **Unknown terms are violations**: If a token is not in the dictionary and does not map to a known expansion, it is flagged. +4. **Minimum length is explicit**: `--min-eval-length` determines whether short identifiers are evaluated. + +Kivia also applies dictionary-backed spelling-variant matching for common British/American pairs (for example `normalise`/`normalize`, `colour`/`color`, `centre`/`center`). + +## How It Works + +Kivia parses Go source using the standard library's AST, extracts identifiers, tokenises names (camel, snake, or kebab), and evaluates each token against a local NLP dictionary pipeline. + +- No network requests +- No LLM/API dependency +- Deterministic local analysis + +## Installation + +```bash +go install github.com/Fuwn/kivia@latest +``` + +Or build locally: + +```bash +go build -o ./bin/kivia . +``` + +## Usage + +```bash +# Analyse a package tree +kivia --path ./... + +# Ignore single-letter names during evaluation +kivia --path ./... --min-eval-length 2 + +# Ignore specific violations +kivia --path ./... --ignore name=ctx --ignore file=testdata + +# JSON output without context payload +kivia --path ./... --format json --omit-context +``` + +### Flags + +| Flag | Description | +|------|-------------| +| `--path` | Path to analyse (`directory`, `file`, or `./...`). | +| `--omit-context` | Hide usage context in output. | +| `--min-eval-length` | Minimum identifier length in runes to evaluate (must be `>= 1`). | +| `--format` | Output format: `text` or `json`. | +| `--fail-on-violation` | Exit with code `1` when violations are found. | +| `--ignore` | Ignore violations by matcher. Repeatable. Prefixes: `name=`, `kind=`, `file=`, `reason=`, `func=`. | + +## Ignore Matchers + +`--ignore` supports targeted filtering: + +- `name=<substring>` +- `kind=<substring>` +- `file=<substring>` +- `reason=<substring>` +- `func=<substring>` + +Without a prefix, the matcher is applied as a substring across all violation fields. + +Example: + +```bash +kivia --path ./... \ + --ignore name=ctx \ + --ignore reason=abbreviation \ + --ignore file=_test.go +``` + +## Output + +### Text (default) + +```text +internal/example/sample.go:12:9 parameter "ctx": Contains abbreviation: ctx. + context: type=context.Context, function=Handle +``` + +### JSON + +```json +{ + "violations": [ + { + "identifier": { + "name": "ctx", + "kind": "parameter", + "file": "internal/example/sample.go", + "line": 12, + "column": 9, + "context": { + "enclosingFunction": "Handle", + "type": "context.Context" + } + }, + "reason": "Contains abbreviation: ctx." + } + ] +} +``` + +## Identifier Scope (Go) + +Kivia currently extracts and evaluates: + +- Types +- Functions and methods +- Receivers +- Parameters +- Named results +- Variables (`var`/`const` and `:=`) +- Range keys and values +- Struct fields +- Interface methods + +## Dictionary and NLP Source + +Kivia loads dictionary data only from configured/system dictionary files. + +1. `KIVIA_DICTIONARY_PATH` (optional): one path or multiple paths separated by your OS path separator (`:` on macOS/Linux, `;` on Windows). Commas are also accepted. +2. If `KIVIA_DICTIONARY_PATH` is not set, Kivia uses a default set of dictionary files (for example, `/usr/share/dict/words`, `/usr/share/dict/web2`, and Hunspell dictionaries when present). +3. If no usable words are found, the analysis fails with an error. + +## License + +Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or +[MIT license](LICENSE-MIT) at your option. + +Unless you explicitly state otherwise, any contribution intentionally submitted +for inclusion in this crate by you, as defined in the Apache-2.0 license, shall +be dual licensed as above, without any additional terms or conditions. @@ -0,0 +1,5 @@ +module github.com/Fuwn/kivia + +go 1.26 + +require github.com/sajari/fuzzy v1.0.0 @@ -0,0 +1,2 @@ +github.com/sajari/fuzzy v1.0.0 h1:+FmwVvJErsd0d0hAPlj4CxqxUtQY/fOoY0DwX4ykpRY= +github.com/sajari/fuzzy v1.0.0/go.mod h1:OjYR6KxoWOe9+dOlXeiCJd4dIbED4Oo8wpS89o0pwOo= diff --git a/internal/analyze/analyze.go b/internal/analyze/analyze.go new file mode 100644 index 0000000..315f086 --- /dev/null +++ b/internal/analyze/analyze.go @@ -0,0 +1,208 @@ +package analyze + +import ( + "github.com/Fuwn/kivia/internal/collect" + "strings" + "unicode" + "unicode/utf8" +) + +type Options struct { + MinEvaluationLength int +} + +type Result struct { + Violations []Violation `json:"violations"` +} + +type Violation struct { + Identifier collect.Identifier `json:"identifier"` + Reason string `json:"reason"` +} + +func Run(identifiers []collect.Identifier, options Options) (Result, error) { + minimumEvaluationLength := options.MinEvaluationLength + + if minimumEvaluationLength <= 0 { + minimumEvaluationLength = 1 + } + + resources, err := getResources() + + if err != nil { + return Result{}, err + } + + violations := make([]Violation, 0) + + for _, identifier := range identifiers { + if utf8.RuneCountInString(strings.TrimSpace(identifier.Name)) < minimumEvaluationLength { + continue + } + + evaluation := evaluateIdentifier(identifier, resources, minimumEvaluationLength) + + if !evaluation.isViolation { + continue + } + + violation := Violation{ + Identifier: identifier, + Reason: evaluation.reason, + } + violations = append(violations, violation) + } + + return Result{Violations: violations}, nil +} + +type evaluationResult struct { + isViolation bool + reason string +} + +func evaluateIdentifier(identifier collect.Identifier, resources resources, minimumTokenLength int) evaluationResult { + name := strings.TrimSpace(identifier.Name) + + if name == "" { + return evaluationResult{} + } + + tokens := tokenize(name) + + if len(tokens) == 0 { + return evaluationResult{} + } + + for _, token := range tokens { + if utf8.RuneCountInString(token) < minimumTokenLength { + continue + } + + if !isAlphabeticToken(token) { + continue + } + + if resources.dictionary.IsWord(token) { + continue + } + + if isUpperCaseToken(name, token) { + continue + } + + if isDisallowedAbbreviation(token, resources) { + return evaluationResult{isViolation: true, reason: "Contains abbreviation: " + token + "."} + } + + return evaluationResult{isViolation: true, reason: "Term not found in dictionary: " + token + "."} + } + + return evaluationResult{} +} + +func isUpperCaseToken(identifierName string, token string) bool { + tokenLength := utf8.RuneCountInString(token) + + if tokenLength < 2 || tokenLength > 8 { + return false + } + + return strings.Contains(identifierName, strings.ToUpper(token)) +} + +func tokenize(name string) []string { + name = strings.TrimSpace(name) + + if name == "" { + return nil + } + + parts := strings.FieldsFunc(name, func(r rune) bool { + return r == '_' || r == '-' || r == ' ' + }) + + if len(parts) == 0 { + return nil + } + + result := make([]string, 0, len(parts)*2) + + for _, part := range parts { + if part == "" { + continue + } + + result = append(result, splitCamel(part)...) + } + + return result +} + +func splitCamel(input string) []string { + if input == "" { + return nil + } + + runes := []rune(input) + + if len(runes) == 0 { + return nil + } + + tokens := make([]string, 0, 2) + start := 0 + + for index := 1; index < len(runes); index++ { + current := runes[index] + previous := runes[index-1] + next := rune(0) + + if index+1 < len(runes) { + next = runes[index+1] + } + + isBoundary := false + + if unicode.IsLower(previous) && unicode.IsUpper(current) { + isBoundary = true + } + + if unicode.IsDigit(previous) != unicode.IsDigit(current) { + isBoundary = true + } + + if unicode.IsUpper(previous) && unicode.IsUpper(current) && next != 0 && unicode.IsLower(next) { + isBoundary = true + } + + if isBoundary { + tokens = append(tokens, strings.ToLower(string(runes[start:index]))) + start = index + } + } + + tokens = append(tokens, strings.ToLower(string(runes[start:]))) + + return tokens +} + +func isDisallowedAbbreviation(token string, resources resources) bool { + _, hasExpansion := resources.dictionary.AbbreviationExpansion(token) + + return hasExpansion +} + +func isAlphabeticToken(token string) bool { + if token == "" { + return false + } + + for _, character := range token { + if !unicode.IsLetter(character) { + return false + } + } + + return true +} diff --git a/internal/analyze/analyze_test.go b/internal/analyze/analyze_test.go new file mode 100644 index 0000000..8aebf8d --- /dev/null +++ b/internal/analyze/analyze_test.go @@ -0,0 +1,174 @@ +package analyze_test + +import ( + "github.com/Fuwn/kivia/internal/analyze" + "github.com/Fuwn/kivia/internal/collect" + "os" + "path/filepath" + "testing" +) + +func dictionaryPathForTests(testingContext *testing.T) string { + testingContext.Helper() + + return filepath.Join("..", "..", "testdata", "dictionary", "words.txt") +} + +func TestAnalyzeFlagsAbbreviations(testingContext *testing.T) { + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext)) + + root := filepath.Join("..", "..", "testdata", "samplepkg") + identifiers, err := collect.FromPath(root) + + if err != nil { + testingContext.Fatalf("collect.FromPath returned an error: %v", err) + } + + result, err := analyze.Run(identifiers, analyze.Options{}) + + if err != nil { + testingContext.Fatalf("analyze.Run returned an error: %v", err) + } + + if len(result.Violations) == 0 { + testingContext.Fatalf("Expected at least one violation, got none.") + } + + mustContainViolation(testingContext, result, "ctx") + mustContainViolation(testingContext, result, "userNum") + mustContainViolation(testingContext, result, "usr") +} + +func TestAnalyzeFlagsTechnicalTermsNotInDictionary(testingContext *testing.T) { + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext)) + + identifiers := []collect.Identifier{ + {Name: "userID", Kind: "variable"}, + {Name: "httpClient", Kind: "variable"}, + } + result, err := analyze.Run(identifiers, analyze.Options{}) + + if err != nil { + testingContext.Fatalf("analyze.Run returned an error: %v", err) + } + + if len(result.Violations) == 0 { + testingContext.Fatalf("Expected violations, got none.") + } + + mustContainViolation(testingContext, result, "userID") + mustContainViolation(testingContext, result, "httpClient") +} + +func TestAnalyzeDoesNotFlagNormalDictionaryWords(testingContext *testing.T) { + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext)) + + identifiers := []collect.Identifier{ + {Name: "options", Kind: "variable"}, + {Name: "parsedResource", Kind: "variable"}, + {Name: "hasResources", Kind: "variable"}, + {Name: "allowlist", Kind: "variable"}, + } + result, err := analyze.Run(identifiers, analyze.Options{}) + + if err != nil { + testingContext.Fatalf("analyze.Run returned an error: %v", err) + } + + if len(result.Violations) != 0 { + testingContext.Fatalf("Expected no violations, got %d.", len(result.Violations)) + } +} + +func TestAnalyzeMinEvaluationLengthSkipsSingleLetterIdentifiers(testingContext *testing.T) { + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext)) + + identifiers := []collect.Identifier{ + {Name: "t", Kind: "parameter"}, + {Name: "v", Kind: "receiver"}, + {Name: "ctx", Kind: "parameter"}, + } + result, err := analyze.Run(identifiers, analyze.Options{ + MinEvaluationLength: 2, + }) + + if err != nil { + testingContext.Fatalf("analyze.Run returned an error: %v", err) + } + + if len(result.Violations) != 1 { + testingContext.Fatalf("Expected one violation, got %d.", len(result.Violations)) + } + + if result.Violations[0].Identifier.Name != "ctx" { + testingContext.Fatalf("Expected only ctx to be evaluated, got %q.", result.Violations[0].Identifier.Name) + } +} + +func TestAnalyzeFlagsExpressionAbbreviation(testingContext *testing.T) { + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext)) + + identifiers := []collect.Identifier{ + {Name: "expr", Kind: "variable"}, + } + result, err := analyze.Run(identifiers, analyze.Options{ + MinEvaluationLength: 1, + }) + + if err != nil { + testingContext.Fatalf("analyze.Run returned an error: %v", err) + } + + if len(result.Violations) != 1 { + testingContext.Fatalf("Expected one violation, got %d.", len(result.Violations)) + } + + if result.Violations[0].Identifier.Name != "expr" { + testingContext.Fatalf("Expected expr to be flagged, got %q.", result.Violations[0].Identifier.Name) + } +} + +func TestAnalyzeAllowsUpperCaseTokens(testingContext *testing.T) { + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext)) + + identifiers := []collect.Identifier{ + {Name: "JSON", Kind: "variable"}, + } + result, err := analyze.Run(identifiers, analyze.Options{}) + + if err != nil { + testingContext.Fatalf("analyze.Run returned an error: %v", err) + } + + if len(result.Violations) != 0 { + testingContext.Fatalf("Expected no violations, got %d.", len(result.Violations)) + } +} + +func TestAnalyzeFailsWhenDictionaryIsUnavailable(testingContext *testing.T) { + emptyDictionaryPath := filepath.Join(testingContext.TempDir(), "empty.txt") + + if err := os.WriteFile(emptyDictionaryPath, []byte("\n"), 0o644); err != nil { + testingContext.Fatalf("os.WriteFile returned an error: %v", err) + } + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", emptyDictionaryPath) + + _, err := analyze.Run([]collect.Identifier{{Name: "ctx", Kind: "parameter"}}, analyze.Options{}) + + if err == nil { + testingContext.Fatalf("Expected analyze.Run to fail when dictionary data is unavailable.") + } +} + +func mustContainViolation(testingContext *testing.T, result analyze.Result, name string) { + testingContext.Helper() + + for _, violation := range result.Violations { + if violation.Identifier.Name == name { + return + } + } + + testingContext.Fatalf("Expected a violation for %q.", name) +} diff --git a/internal/analyze/resources.go b/internal/analyze/resources.go new file mode 100644 index 0000000..f42c757 --- /dev/null +++ b/internal/analyze/resources.go @@ -0,0 +1,26 @@ +package analyze + +import ( + "fmt" + "github.com/Fuwn/kivia/internal/nlp" +) + +type resources struct { + dictionary *nlp.Dictionary +} + +func getResources() (resources, error) { + return loadResources() +} + +func loadResources() (resources, error) { + dictionary, err := nlp.NewDictionary() + + if err != nil { + return resources{}, fmt.Errorf("Failed to load dictionary: %w", err) + } + + return resources{ + dictionary: dictionary, + }, nil +} diff --git a/internal/collect/collect.go b/internal/collect/collect.go new file mode 100644 index 0000000..ccb3b46 --- /dev/null +++ b/internal/collect/collect.go @@ -0,0 +1,331 @@ +package collect + +import ( + "bytes" + "fmt" + "go/ast" + "go/parser" + "go/printer" + "go/token" + "io/fs" + "os" + "path/filepath" + "sort" + "strings" +) + +type Context struct { + EnclosingFunction string `json:"enclosingFunction,omitempty"` + Type string `json:"type,omitempty"` + ValueExpression string `json:"valueExpression,omitempty"` + ParentType string `json:"parentType,omitempty"` +} + +type Identifier struct { + Name string `json:"name"` + Kind string `json:"kind"` + File string `json:"file"` + Line int `json:"line"` + Column int `json:"column"` + Context Context `json:"context"` +} + +func FromPath(path string) ([]Identifier, error) { + files, err := discoverFiles(path) + + if err != nil { + return nil, err + } + + fileSet := token.NewFileSet() + identifiers := make([]Identifier, 0, 128) + + for _, filePath := range files { + fileNode, parseErr := parser.ParseFile(fileSet, filePath, nil, parser.SkipObjectResolution) + + if parseErr != nil { + return nil, fmt.Errorf("Failed to parse %s: %w", filePath, parseErr) + } + + collector := visitor{ + fileSet: fileSet, + file: filePath, + } + + ast.Walk(&collector, fileNode) + + identifiers = append(identifiers, collector.identifiers...) + } + + return identifiers, nil +} + +type visitor struct { + fileSet *token.FileSet + file string + identifiers []Identifier + functionStack []string + typeStack []string +} + +func (identifierVisitor *visitor) Visit(node ast.Node) ast.Visitor { + switch typedNode := node.(type) { + case *ast.FuncDecl: + identifierVisitor.addIdentifier(typedNode.Name, "function", Context{}) + + identifierVisitor.functionStack = append(identifierVisitor.functionStack, typedNode.Name.Name) + + identifierVisitor.captureFieldList(typedNode.Recv, "receiver") + identifierVisitor.captureFieldList(typedNode.Type.Params, "parameter") + identifierVisitor.captureFieldList(typedNode.Type.Results, "result") + + return leaveScope(identifierVisitor, func() { + identifierVisitor.functionStack = identifierVisitor.functionStack[:len(identifierVisitor.functionStack)-1] + }) + case *ast.TypeSpec: + identifierVisitor.addIdentifier(typedNode.Name, "type", Context{}) + + identifierVisitor.typeStack = append(identifierVisitor.typeStack, typedNode.Name.Name) + + identifierVisitor.captureTypeMembers(typedNode.Name.Name, typedNode.Type) + + return leaveScope(identifierVisitor, func() { identifierVisitor.typeStack = identifierVisitor.typeStack[:len(identifierVisitor.typeStack)-1] }) + case *ast.ValueSpec: + declaredType := renderExpression(identifierVisitor.fileSet, typedNode.Type) + rightHandValue := renderExpressionList(identifierVisitor.fileSet, typedNode.Values) + + for _, name := range typedNode.Names { + identifierVisitor.addIdentifier(name, "variable", Context{Type: declaredType, ValueExpression: rightHandValue}) + } + case *ast.AssignStmt: + if typedNode.Tok != token.DEFINE { + break + } + + rightHandValue := renderExpressionList(identifierVisitor.fileSet, typedNode.Rhs) + + for index, left := range typedNode.Lhs { + identifierNode, ok := left.(*ast.Ident) + + if !ok { + continue + } + + assignmentContext := Context{ValueExpression: rightHandValue} + + if index < len(typedNode.Rhs) { + assignmentContext.Type = inferTypeFromExpression(typedNode.Rhs[index]) + } + + identifierVisitor.addIdentifier(identifierNode, "variable", assignmentContext) + } + case *ast.RangeStmt: + if typedNode.Tok != token.DEFINE { + break + } + + if keyIdentifier, ok := typedNode.Key.(*ast.Ident); ok { + identifierVisitor.addIdentifier(keyIdentifier, "rangeKey", Context{ValueExpression: renderExpression(identifierVisitor.fileSet, typedNode.X)}) + } + + if valueIdentifier, ok := typedNode.Value.(*ast.Ident); ok { + identifierVisitor.addIdentifier(valueIdentifier, "rangeValue", Context{ValueExpression: renderExpression(identifierVisitor.fileSet, typedNode.X)}) + } + } + + return identifierVisitor +} + +type scopeExit struct { + parent *visitor + onLeave func() +} + +func leaveScope(parent *visitor, onLeave func()) ast.Visitor { + return &scopeExit{parent: parent, onLeave: onLeave} +} + +func (scopeExitVisitor *scopeExit) Visit(node ast.Node) ast.Visitor { + if node == nil { + scopeExitVisitor.onLeave() + + return nil + } + + return scopeExitVisitor.parent +} + +func (identifierVisitor *visitor) captureFieldList(fields *ast.FieldList, kind string) { + if fields == nil { + return + } + + for _, field := range fields.List { + declaredType := renderExpression(identifierVisitor.fileSet, field.Type) + + for _, name := range field.Names { + identifierVisitor.addIdentifier(name, kind, Context{Type: declaredType}) + } + } +} + +func (identifierVisitor *visitor) captureTypeMembers(typeName string, typeExpression ast.Expr) { + switch typedType := typeExpression.(type) { + case *ast.StructType: + if typedType.Fields == nil { + return + } + + for _, field := range typedType.Fields.List { + memberType := renderExpression(identifierVisitor.fileSet, field.Type) + + for _, fieldName := range field.Names { + identifierVisitor.addIdentifier(fieldName, "field", Context{Type: memberType, ParentType: typeName}) + } + } + case *ast.InterfaceType: + if typedType.Methods == nil { + return + } + + for _, method := range typedType.Methods.List { + memberType := renderExpression(identifierVisitor.fileSet, method.Type) + + for _, methodName := range method.Names { + identifierVisitor.addIdentifier(methodName, "interfaceMethod", Context{Type: memberType, ParentType: typeName}) + } + } + } +} + +func (identifierVisitor *visitor) addIdentifier(identifier *ast.Ident, kind string, context Context) { + if identifier == nil || identifier.Name == "_" { + return + } + + position := identifierVisitor.fileSet.Position(identifier.NamePos) + context.EnclosingFunction = currentFunction(identifierVisitor.functionStack) + identifierVisitor.identifiers = append(identifierVisitor.identifiers, Identifier{ + Name: identifier.Name, + Kind: kind, + File: identifierVisitor.file, + Line: position.Line, + Column: position.Column, + Context: context, + }) +} + +func currentFunction(stack []string) string { + if len(stack) == 0 { + return "" + } + + return stack[len(stack)-1] +} + +func discoverFiles(path string) ([]string, error) { + searchRoot := path + recursive := false + + if strings.HasSuffix(path, "/...") { + searchRoot = strings.TrimSuffix(path, "/...") + recursive = true + } + + if searchRoot == "" { + searchRoot = "." + } + + pathFileDetails, err := os.Stat(searchRoot) + + if err != nil { + return nil, err + } + + if !pathFileDetails.IsDir() { + if strings.HasSuffix(searchRoot, ".go") { + return []string{searchRoot}, nil + } + + return nil, fmt.Errorf("Path %q is not a Go file.", searchRoot) + } + + files := make([]string, 0, 64) + walkErr := filepath.WalkDir(searchRoot, func(candidate string, entry fs.DirEntry, walkError error) error { + if walkError != nil { + return walkError + } + + if entry.IsDir() { + name := entry.Name() + + if name == ".git" || name == "vendor" || name == "node_modules" { + return filepath.SkipDir + } + + if !recursive && candidate != searchRoot { + return filepath.SkipDir + } + + return nil + } + + if strings.HasSuffix(candidate, ".go") { + files = append(files, candidate) + } + + return nil + }) + + if walkErr != nil { + return nil, walkErr + } + + sort.Strings(files) + + return files, nil +} + +func renderExpression(fileSet *token.FileSet, expression ast.Expr) string { + if expression == nil { + return "" + } + + var buffer bytes.Buffer + + if err := printer.Fprint(&buffer, fileSet, expression); err != nil { + return "" + } + + return buffer.String() +} + +func renderExpressionList(fileSet *token.FileSet, expressions []ast.Expr) string { + if len(expressions) == 0 { + return "" + } + + parts := make([]string, 0, len(expressions)) + + for _, expression := range expressions { + parts = append(parts, renderExpression(fileSet, expression)) + } + + return strings.Join(parts, ", ") +} + +func inferTypeFromExpression(expression ast.Expr) string { + switch typedExpression := expression.(type) { + case *ast.CallExpr: + switch functionExpression := typedExpression.Fun.(type) { + case *ast.Ident: + return functionExpression.Name + case *ast.SelectorExpr: + return functionExpression.Sel.Name + } + + return "" + default: + return "" + } +} diff --git a/internal/nlp/dictionary.go b/internal/nlp/dictionary.go new file mode 100644 index 0000000..e7db37e --- /dev/null +++ b/internal/nlp/dictionary.go @@ -0,0 +1,614 @@ +package nlp + +import ( + "bufio" + "errors" + "fmt" + "github.com/sajari/fuzzy" + "os" + "path" + "path/filepath" + "regexp" + "sort" + "strconv" + "strings" + "unicode/utf8" +) + +var wordPattern = regexp.MustCompile(`[A-Za-z]+`) + +type Dictionary struct { + model *fuzzy.Model + words map[string]struct{} + wordsByFirstCharacter map[rune][]string +} + +func NewDictionary() (*Dictionary, error) { + words, err := loadWords() + + if err != nil { + return nil, err + } + + wordSet := makeWordSet(words) + wordsByFirstCharacter := makeWordsByFirstCharacter(words) + model, loadErr := loadCachedModel() + + if loadErr == nil { + return &Dictionary{model: model, words: wordSet, wordsByFirstCharacter: wordsByFirstCharacter}, nil + } + + model = fuzzy.NewModel() + + model.SetThreshold(1) + model.SetDepth(1) + model.SetUseAutocomplete(false) + model.Train(words) + + _ = saveCachedModel(model) + + return &Dictionary{model: model, words: wordSet, wordsByFirstCharacter: wordsByFirstCharacter}, nil +} + +func (dictionary *Dictionary) IsWord(token string) bool { + token = normalizeToken(token) + + if token == "" || dictionary == nil { + return false + } + + return dictionary.isLexiconWord(token) +} + +func (dictionary *Dictionary) Suggest(token string) string { + token = normalizeToken(token) + + if token == "" || dictionary == nil || dictionary.model == nil { + return "" + } + + if dictionary.isLexiconWord(token) { + return "" + } + + suggestions := dictionary.model.SpellCheckSuggestions(token, 1) + + if len(suggestions) == 0 { + return "" + } + + if suggestions[0] == token { + return "" + } + + return suggestions[0] +} + +func (dictionary *Dictionary) isLexiconWord(token string) bool { + if dictionary == nil { + return false + } + + if _, ok := dictionary.words[token]; ok { + return true + } + + candidates := make([]string, 0, 16) + candidates = append(candidates, inflectionCandidates(token)...) + candidates = append(candidates, spellingVariantCandidates(token)...) + + for _, candidate := range inflectionCandidates(token) { + candidates = append(candidates, spellingVariantCandidates(candidate)...) + } + + uniqueCandidates := make(map[string]struct{}, len(candidates)) + + for _, candidate := range candidates { + if candidate == "" || candidate == token { + continue + } + + if _, seen := uniqueCandidates[candidate]; seen { + continue + } + + uniqueCandidates[candidate] = struct{}{} + + if _, ok := dictionary.words[candidate]; ok { + return true + } + } + + return false +} + +func (dictionary *Dictionary) AbbreviationExpansion(token string) (string, bool) { + token = normalizeToken(token) + + if token == "" || dictionary == nil { + return "", false + } + + tokenLength := utf8.RuneCountInString(token) + + if tokenLength <= 1 || tokenLength > 4 { + return "", false + } + + firstCharacter, _ := utf8.DecodeRuneInString(token) + candidates := dictionary.wordsByFirstCharacter[firstCharacter] + + if len(candidates) == 0 { + return "", false + } + + bestCandidate := "" + bestScore := 1 << 30 + + for _, candidate := range candidates { + if !isLikelyAbbreviationForToken(token, candidate) { + continue + } + + score := abbreviationScore(token, candidate) + + if score < bestScore { + bestScore = score + bestCandidate = candidate + } + } + + if bestCandidate == "" { + return "", false + } + + return bestCandidate, true +} + +func isLikelyAbbreviationForToken(token string, candidate string) bool { + if candidate == "" || token == "" || token == candidate { + return false + } + + tokenLength := utf8.RuneCountInString(token) + candidateLength := utf8.RuneCountInString(candidate) + + if candidateLength <= tokenLength { + return false + } + + if !isSubsequence(token, candidate) { + return false + } + + if strings.HasPrefix(candidate, token) && tokenLength <= 4 { + return true + } + + tokenConsonants := consonantSkeleton(token) + candidateConsonants := consonantSkeleton(candidate) + + if tokenConsonants == "" || candidateConsonants == "" { + return false + } + + if isSubsequence(tokenConsonants, candidateConsonants) && tokenLength <= 5 { + return true + } + + return false +} + +func abbreviationScore(token string, candidate string) int { + tokenLength := utf8.RuneCountInString(token) + candidateLength := utf8.RuneCountInString(candidate) + lengthGap := max(candidateLength-tokenLength, 0) + score := lengthGap * 10 + + if strings.HasPrefix(candidate, token) { + score -= 3 + } + + return score +} + +func isSubsequence(shorter string, longer string) bool { + shorterRunes := []rune(shorter) + longerRunes := []rune(longer) + shorterIndex := 0 + + for _, character := range longerRunes { + if shorterIndex >= len(shorterRunes) { + break + } + + if shorterRunes[shorterIndex] == character { + shorterIndex++ + } + } + + return shorterIndex == len(shorterRunes) +} + +func consonantSkeleton(word string) string { + var builder strings.Builder + + for _, character := range word { + switch character { + case 'a', 'e', 'i', 'o', 'u': + continue + default: + builder.WriteRune(character) + } + } + + return builder.String() +} + +func inflectionCandidates(token string) []string { + candidates := make([]string, 0, 8) + + if strings.HasSuffix(token, "ies") && len(token) > 3 { + candidates = append(candidates, token[:len(token)-3]+"y") + } + + if strings.HasSuffix(token, "es") && len(token) > 2 { + candidates = append(candidates, token[:len(token)-2]) + } + + if strings.HasSuffix(token, "s") && len(token) > 1 { + candidates = append(candidates, token[:len(token)-1]) + } + + if strings.HasSuffix(token, "ed") && len(token) > 2 { + candidateWithoutSuffix := token[:len(token)-2] + candidates = append(candidates, candidateWithoutSuffix) + candidates = append(candidates, candidateWithoutSuffix+"e") + + if len(candidateWithoutSuffix) >= 2 { + lastCharacter := candidateWithoutSuffix[len(candidateWithoutSuffix)-1] + secondToLastCharacter := candidateWithoutSuffix[len(candidateWithoutSuffix)-2] + + if lastCharacter == secondToLastCharacter { + candidates = append(candidates, candidateWithoutSuffix[:len(candidateWithoutSuffix)-1]) + } + } + } + + if strings.HasSuffix(token, "ing") && len(token) > 3 { + candidateWithoutSuffix := token[:len(token)-3] + candidates = append(candidates, candidateWithoutSuffix) + candidates = append(candidates, candidateWithoutSuffix+"e") + } + + if strings.HasSuffix(token, "er") && len(token) > 2 { + candidateWithoutSuffix := token[:len(token)-2] + candidates = append(candidates, candidateWithoutSuffix) + candidates = append(candidates, candidateWithoutSuffix+"e") + + if len(candidateWithoutSuffix) >= 2 { + lastCharacter := candidateWithoutSuffix[len(candidateWithoutSuffix)-1] + secondToLastCharacter := candidateWithoutSuffix[len(candidateWithoutSuffix)-2] + + if lastCharacter == secondToLastCharacter { + candidates = append(candidates, candidateWithoutSuffix[:len(candidateWithoutSuffix)-1]) + } + } + } + + if strings.HasSuffix(token, "ize") && len(token) > 3 { + candidates = append(candidates, token[:len(token)-3]) + } + + if strings.HasSuffix(token, "ized") && len(token) > 4 { + candidates = append(candidates, token[:len(token)-4]) + } + + if strings.HasSuffix(token, "izing") && len(token) > 5 { + candidates = append(candidates, token[:len(token)-5]) + } + + if strings.HasSuffix(token, "izer") && len(token) > 4 { + candidates = append(candidates, token[:len(token)-4]) + } + + if strings.HasSuffix(token, "ization") && len(token) > 7 { + candidates = append(candidates, token[:len(token)-7]) + } + + return candidates +} + +func spellingVariantCandidates(token string) []string { + candidates := make([]string, 0, 8) + + appendSuffixVariant(&candidates, token, "isation", "ization") + appendSuffixVariant(&candidates, token, "ization", "isation") + appendSuffixVariant(&candidates, token, "ising", "izing") + appendSuffixVariant(&candidates, token, "izing", "ising") + appendSuffixVariant(&candidates, token, "ised", "ized") + appendSuffixVariant(&candidates, token, "ized", "ised") + appendSuffixVariant(&candidates, token, "iser", "izer") + appendSuffixVariant(&candidates, token, "izer", "iser") + appendSuffixVariant(&candidates, token, "ise", "ize") + appendSuffixVariant(&candidates, token, "ize", "ise") + appendSuffixVariant(&candidates, token, "our", "or") + appendSuffixVariant(&candidates, token, "or", "our") + appendSuffixVariant(&candidates, token, "tre", "ter") + appendSuffixVariant(&candidates, token, "ter", "tre") + + return candidates +} + +func appendSuffixVariant(candidates *[]string, token string, fromSuffix string, toSuffix string) { + if !strings.HasSuffix(token, fromSuffix) || len(token) <= len(fromSuffix) { + return + } + + root := token[:len(token)-len(fromSuffix)] + *candidates = append(*candidates, root+toSuffix) +} + +func makeWordSet(words []string) map[string]struct{} { + set := make(map[string]struct{}, len(words)) + + for _, word := range words { + set[word] = struct{}{} + } + + return set +} + +func makeWordsByFirstCharacter(words []string) map[rune][]string { + grouped := make(map[rune][]string) + + for _, word := range words { + firstCharacter, size := utf8.DecodeRuneInString(word) + + if firstCharacter == utf8.RuneError && size == 0 { + continue + } + + grouped[firstCharacter] = append(grouped[firstCharacter], word) + } + + for firstCharacter := range grouped { + sort.Strings(grouped[firstCharacter]) + } + + return grouped +} + +func loadWords() ([]string, error) { + configuredDictionaryPaths := parseDictionaryPaths(os.Getenv("KIVIA_DICTIONARY_PATH")) + + if len(configuredDictionaryPaths) > 0 { + words, err := loadWordsFromPaths(configuredDictionaryPaths, true) + + if err != nil { + return nil, err + } + + if len(words) == 0 { + return nil, errors.New("configured dictionary sources contain no usable words") + } + + return words, nil + } + + words, err := loadWordsFromPaths(defaultDictionaryPaths, false) + + if err != nil { + return nil, err + } + + if len(words) == 0 { + return nil, errors.New("no usable dictionary words found; set KIVIA_DICTIONARY_PATH") + } + + return words, nil +} + +func readWordsFromFile(filePath string) ([]string, error) { + file, err := os.Open(filePath) + + if err != nil { + return nil, err + } + + defer file.Close() + + words := make([]string, 0, 1024) + scanner := bufio.NewScanner(file) + isSpellDictionaryFile := strings.EqualFold(path.Ext(filePath), ".dic") + lineNumber := 0 + + for scanner.Scan() { + lineNumber++ + + line := normalizeDictionaryLine(scanner.Text(), lineNumber, isSpellDictionaryFile) + + if line == "" { + continue + } + + words = append(words, line) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return normalizeWords(words), nil +} + +func parseDictionaryPaths(value string) []string { + trimmedValue := strings.TrimSpace(value) + + if trimmedValue == "" { + return nil + } + + expandedValue := strings.ReplaceAll(trimmedValue, ",", string(os.PathListSeparator)) + parts := strings.Split(expandedValue, string(os.PathListSeparator)) + paths := make([]string, 0, len(parts)) + + for _, entry := range parts { + candidate := strings.TrimSpace(entry) + + if candidate == "" { + continue + } + + paths = append(paths, candidate) + } + + return paths +} + +func loadWordsFromPaths(paths []string, strict bool) ([]string, error) { + combinedWords := make([]string, 0, 4096) + + for _, dictionaryPath := range paths { + words, err := readWordsFromFile(dictionaryPath) + + if err != nil { + if strict { + return nil, fmt.Errorf("failed to read dictionary %q: %w", dictionaryPath, err) + } + + continue + } + + combinedWords = append(combinedWords, words...) + } + + return normalizeWords(combinedWords), nil +} + +func normalizeDictionaryLine(line string, lineNumber int, isSpellDictionaryFile bool) string { + trimmedLine := strings.TrimSpace(line) + + if trimmedLine == "" || strings.HasPrefix(trimmedLine, "#") { + return "" + } + + if isSpellDictionaryFile && lineNumber == 1 { + if _, err := strconv.Atoi(trimmedLine); err == nil { + return "" + } + } + + if slashIndex := strings.Index(trimmedLine, "/"); slashIndex >= 0 { + trimmedLine = trimmedLine[:slashIndex] + } + + return trimmedLine +} + +func normalizeWords(words []string) []string { + unique := make(map[string]struct{}, len(words)) + + for _, word := range words { + normalized := normalizeToken(word) + + if normalized == "" { + continue + } + + if len(normalized) <= 1 { + continue + } + + unique[normalized] = struct{}{} + } + + output := make([]string, 0, len(unique)) + + for word := range unique { + output = append(output, word) + } + + sort.Strings(output) + + return output +} + +func normalizeToken(token string) string { + token = strings.ToLower(strings.TrimSpace(token)) + + if token == "" { + return "" + } + + match := wordPattern.FindString(token) + + if match == "" { + return "" + } + + return match +} + +func cachePath() (string, error) { + base, err := os.UserCacheDir() + + if err != nil { + return "", err + } + + return filepath.Join(base, "kivia", "fuzzy_model_v1.json"), nil +} + +func loadCachedModel() (*fuzzy.Model, error) { + path, err := cachePath() + + if err != nil { + return nil, err + } + + model, err := fuzzy.Load(path) + + if err != nil { + return nil, err + } + + return model, nil +} + +func saveCachedModel(model *fuzzy.Model) error { + if model == nil { + return errors.New("Model cannot be nil.") + } + + path, err := cachePath() + + if err != nil { + return err + } + + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { + return err + } + + return model.Save(path) +} + +var defaultDictionaryPaths = []string{ + "/usr/share/dict/words", + "/usr/dict/words", + "/usr/share/dict/web2", + "/usr/share/dict/web2a", + "/usr/share/dict/propernames", + "/usr/share/dict/connectives", + "/usr/share/hunspell/en_US.dic", + "/usr/share/hunspell/en_GB.dic", + "/usr/share/hunspell/en_CA.dic", + "/usr/share/hunspell/en_AU.dic", + "/usr/share/myspell/en_US.dic", + "/usr/share/myspell/en_GB.dic", + "/opt/homebrew/share/hunspell/en_US.dic", + "/opt/homebrew/share/hunspell/en_GB.dic", + "/usr/local/share/hunspell/en_US.dic", + "/usr/local/share/hunspell/en_GB.dic", +} diff --git a/internal/nlp/dictionary_test.go b/internal/nlp/dictionary_test.go new file mode 100644 index 0000000..c24e332 --- /dev/null +++ b/internal/nlp/dictionary_test.go @@ -0,0 +1,165 @@ +package nlp_test + +import ( + "github.com/Fuwn/kivia/internal/nlp" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestDictionaryRecognizesLexiconWords(testingContext *testing.T) { + dictionaryFile := filepath.Join("..", "..", "testdata", "dictionary", "words.txt") + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryFile) + + dictionary, err := nlp.NewDictionary() + + if err != nil { + testingContext.Fatalf("NewDictionary returned an error: %v", err) + } + + if !dictionary.IsWord("options") { + testingContext.Fatalf("Expected options to be recognized.") + } + + if !dictionary.IsWord("has") { + testingContext.Fatalf("Expected has to be recognized.") + } + + if !dictionary.IsWord("resources") { + testingContext.Fatalf("Expected resources to be recognized through plural inflection.") + } +} + +func TestDictionaryFindsAbbreviationExpansions(testingContext *testing.T) { + dictionaryFile := filepath.Join("..", "..", "testdata", "dictionary", "words.txt") + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryFile) + + dictionary, err := nlp.NewDictionary() + + if err != nil { + testingContext.Fatalf("NewDictionary returned an error: %v", err) + } + + cases := map[string]string{ + "expr": "expression", + "ctx": "context", + "err": "error", + } + + for token, expectedExpansion := range cases { + expansion, ok := dictionary.AbbreviationExpansion(token) + + if !ok { + testingContext.Fatalf("Expected an abbreviation expansion for %q.", token) + } + + if expansion != expectedExpansion { + testingContext.Fatalf("Expected %q to expand to %q, got %q.", token, expectedExpansion, expansion) + } + } +} + +func TestDictionaryLoadsFromMultipleDictionaryFiles(testingContext *testing.T) { + tempDirectory := testingContext.TempDir() + firstDictionaryPath := filepath.Join(tempDirectory, "first.txt") + secondDictionaryPath := filepath.Join(tempDirectory, "second.txt") + combinedPathList := strings.Join([]string{firstDictionaryPath, secondDictionaryPath}, string(os.PathListSeparator)) + + if err := os.WriteFile(firstDictionaryPath, []byte("alpha\n"), 0o644); err != nil { + testingContext.Fatalf("os.WriteFile returned an error: %v", err) + } + + if err := os.WriteFile(secondDictionaryPath, []byte("beta\n"), 0o644); err != nil { + testingContext.Fatalf("os.WriteFile returned an error: %v", err) + } + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", combinedPathList) + + dictionary, err := nlp.NewDictionary() + + if err != nil { + testingContext.Fatalf("NewDictionary returned an error: %v", err) + } + + if !dictionary.IsWord("alpha") { + testingContext.Fatalf("Expected alpha to be recognized.") + } + + if !dictionary.IsWord("beta") { + testingContext.Fatalf("Expected beta to be recognized.") + } +} + +func TestDictionaryFailsWhenConfiguredPathHasNoWords(testingContext *testing.T) { + tempDirectory := testingContext.TempDir() + emptyDictionaryPath := filepath.Join(tempDirectory, "empty.txt") + + if err := os.WriteFile(emptyDictionaryPath, []byte("\n"), 0o644); err != nil { + testingContext.Fatalf("os.WriteFile returned an error: %v", err) + } + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", emptyDictionaryPath) + + _, err := nlp.NewDictionary() + + if err == nil { + testingContext.Fatalf("Expected NewDictionary to fail when configured dictionary has no usable words.") + } +} + +func TestDictionaryRecognizesDerivedForms(testingContext *testing.T) { + tempDirectory := testingContext.TempDir() + dictionaryPath := filepath.Join(tempDirectory, "base_words.txt") + + if err := os.WriteFile(dictionaryPath, []byte("trim\ntoken\n"), 0o644); err != nil { + testingContext.Fatalf("os.WriteFile returned an error: %v", err) + } + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPath) + + dictionary, err := nlp.NewDictionary() + + if err != nil { + testingContext.Fatalf("NewDictionary returned an error: %v", err) + } + + if !dictionary.IsWord("trimmed") { + testingContext.Fatalf("Expected trimmed to be recognized from trim.") + } + + if !dictionary.IsWord("tokenize") { + testingContext.Fatalf("Expected tokenize to be recognized from token.") + } +} + +func TestDictionaryRecognizesBritishAndAmericanVariants(testingContext *testing.T) { + tempDirectory := testingContext.TempDir() + dictionaryPath := filepath.Join(tempDirectory, "british_words.txt") + + if err := os.WriteFile(dictionaryPath, []byte("normalise\ncolour\ncentre\n"), 0o644); err != nil { + testingContext.Fatalf("os.WriteFile returned an error: %v", err) + } + + testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPath) + + dictionary, err := nlp.NewDictionary() + + if err != nil { + testingContext.Fatalf("NewDictionary returned an error: %v", err) + } + + if !dictionary.IsWord("normalize") { + testingContext.Fatalf("Expected normalize to be recognized from normalise.") + } + + if !dictionary.IsWord("color") { + testingContext.Fatalf("Expected color to be recognized from colour.") + } + + if !dictionary.IsWord("center") { + testingContext.Fatalf("Expected center to be recognized from centre.") + } +} diff --git a/internal/report/report.go b/internal/report/report.go new file mode 100644 index 0000000..a97039e --- /dev/null +++ b/internal/report/report.go @@ -0,0 +1,80 @@ +package report + +import ( + "encoding/json" + "fmt" + "github.com/Fuwn/kivia/internal/analyze" + "github.com/Fuwn/kivia/internal/collect" + "io" + "strings" +) + +func Render(writer io.Writer, result analyze.Result, format string, includeContext bool) error { + switch strings.ToLower(format) { + case "json": + return renderJSON(writer, result, includeContext) + case "text", "": + return renderText(writer, result, includeContext) + default: + return fmt.Errorf("Unsupported output format %q. Use \"text\" or \"json\".", format) + } +} + +func renderText(writer io.Writer, result analyze.Result, includeContext bool) error { + if len(result.Violations) == 0 { + _, err := fmt.Fprintln(writer, "No naming violations found.") + + return err + } + + for _, violation := range result.Violations { + if _, err := fmt.Fprintf(writer, "%s:%d:%d %s %q: %s\n", + violation.Identifier.File, + violation.Identifier.Line, + violation.Identifier.Column, + violation.Identifier.Kind, + violation.Identifier.Name, + violation.Reason, + ); err != nil { + return err + } + + if includeContext { + contextParts := make([]string, 0, 3) + + if violation.Identifier.Context.Type != "" { + contextParts = append(contextParts, "type="+violation.Identifier.Context.Type) + } + + if violation.Identifier.Context.ValueExpression != "" { + contextParts = append(contextParts, "value="+violation.Identifier.Context.ValueExpression) + } + + if violation.Identifier.Context.EnclosingFunction != "" { + contextParts = append(contextParts, "function="+violation.Identifier.Context.EnclosingFunction) + } + + if len(contextParts) > 0 { + if _, err := fmt.Fprintf(writer, " context: %s\n", strings.Join(contextParts, ", ")); err != nil { + return err + } + } + } + } + + return nil +} + +func renderJSON(writer io.Writer, result analyze.Result, includeContext bool) error { + if !includeContext { + for index := range result.Violations { + result.Violations[index].Identifier.Context = collect.Context{} + } + } + + encoder := json.NewEncoder(writer) + + encoder.SetIndent("", " ") + + return encoder.Encode(result) +} diff --git a/justfile b/justfile new file mode 100644 index 0000000..e2f536b --- /dev/null +++ b/justfile @@ -0,0 +1,20 @@ +set shell := ["bash", "-eu", "-o", "pipefail", "-c"] + +build: + mkdir -p bin + go build -o ./bin/kivia . + +format: + iku -w . || go fmt ./... + +test: + go test ./... + +run *args: + go run . {{args}} + +self: + go run . --path ./... --ignore file=testdata + +install: + go install . @@ -0,0 +1,196 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "github.com/Fuwn/kivia/internal/analyze" + "github.com/Fuwn/kivia/internal/collect" + "github.com/Fuwn/kivia/internal/report" + "os" + "slices" + "strings" +) + +type options struct { + Path string + OmitContext bool + MinimumEvaluationLength int + Format string + FailOnViolation bool + Ignore []string +} + +func parseOptions(arguments []string) (options, error) { + flagSet := flag.NewFlagSet("kivia", flag.ContinueOnError) + + flagSet.SetOutput(os.Stderr) + + var parsed options + var ignoreValues stringSliceFlag + + flagSet.StringVar(&parsed.Path, "path", "./...", "Path to analyze (directory, file, or ./...).") + flagSet.BoolVar(&parsed.OmitContext, "omit-context", false, "Hide usage context in output.") + flagSet.IntVar(&parsed.MinimumEvaluationLength, "min-eval-length", 1, "Minimum identifier length in runes to evaluate.") + flagSet.StringVar(&parsed.Format, "format", "text", "Output format: text or JSON.") + flagSet.BoolVar(&parsed.FailOnViolation, "fail-on-violation", false, "Exit with code 1 when violations are found.") + flagSet.Var(&ignoreValues, "ignore", "Ignore violations by matcher. Repeat this flag as needed. Prefixes: name=, kind=, file=, reason=, func=.") + + if err := flagSet.Parse(arguments); err != nil { + return options{}, err + } + + if parsed.MinimumEvaluationLength < 1 { + return options{}, errors.New("The --min-eval-length value must be at least 1.") + } + + parsed.Ignore = slices.Clone(ignoreValues) + + return parsed, nil +} + +func run(parsed options) error { + identifiers, err := collect.FromPath(parsed.Path) + + if err != nil { + return err + } + + result, err := analyze.Run(identifiers, analyze.Options{ + MinEvaluationLength: parsed.MinimumEvaluationLength, + }) + + if err != nil { + return err + } + + result = applyIgnoreFilters(result, parsed.Ignore) + + if err := report.Render(os.Stdout, result, parsed.Format, !parsed.OmitContext); err != nil { + return err + } + + if parsed.FailOnViolation && len(result.Violations) > 0 { + return exitCodeError(1) + } + + return nil +} + +func main() { + parsed, err := parseOptions(os.Args[1:]) + + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(2) + } + + if err := run(parsed); err != nil { + var codeError exitCodeError + + if errors.As(err, &codeError) { + os.Exit(int(codeError)) + } + + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } +} + +type exitCodeError int + +func (errorCode exitCodeError) Error() string { + return fmt.Sprintf("Process exited with code %d.", int(errorCode)) +} + +type stringSliceFlag []string + +func (values *stringSliceFlag) String() string { + if values == nil { + return "" + } + + return strings.Join(*values, ",") +} + +func (values *stringSliceFlag) Set(value string) error { + trimmed := strings.TrimSpace(value) + + if trimmed == "" { + return errors.New("Ignore matcher cannot be empty.") + } + + *values = append(*values, trimmed) + + return nil +} + +func applyIgnoreFilters(result analyze.Result, ignoreMatchers []string) analyze.Result { + if len(ignoreMatchers) == 0 || len(result.Violations) == 0 { + return result + } + + filteredViolations := make([]analyze.Violation, 0, len(result.Violations)) + + for _, violation := range result.Violations { + if shouldIgnoreViolation(violation, ignoreMatchers) { + continue + } + + filteredViolations = append(filteredViolations, violation) + } + + result.Violations = filteredViolations + + return result +} + +func shouldIgnoreViolation(violation analyze.Violation, ignoreMatchers []string) bool { + for _, matcher := range ignoreMatchers { + if matchesViolation(matcher, violation) { + return true + } + } + + return false +} + +func matchesViolation(matcher string, violation analyze.Violation) bool { + normalizedMatcher := strings.ToLower(strings.TrimSpace(matcher)) + + if normalizedMatcher == "" { + return false + } + + identifier := violation.Identifier + + if strings.HasPrefix(normalizedMatcher, "name=") { + return strings.Contains(strings.ToLower(identifier.Name), strings.TrimPrefix(normalizedMatcher, "name=")) + } + + if strings.HasPrefix(normalizedMatcher, "kind=") { + return strings.Contains(strings.ToLower(identifier.Kind), strings.TrimPrefix(normalizedMatcher, "kind=")) + } + + if strings.HasPrefix(normalizedMatcher, "file=") { + return strings.Contains(strings.ToLower(identifier.File), strings.TrimPrefix(normalizedMatcher, "file=")) + } + + if strings.HasPrefix(normalizedMatcher, "reason=") { + return strings.Contains(strings.ToLower(violation.Reason), strings.TrimPrefix(normalizedMatcher, "reason=")) + } + + if strings.HasPrefix(normalizedMatcher, "func=") { + return strings.Contains(strings.ToLower(identifier.Context.EnclosingFunction), strings.TrimPrefix(normalizedMatcher, "func=")) + } + + composite := strings.ToLower(strings.Join([]string{ + identifier.Name, + identifier.Kind, + identifier.File, + violation.Reason, + identifier.Context.EnclosingFunction, + }, " ")) + + return strings.Contains(composite, normalizedMatcher) +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..bc0d0ee --- /dev/null +++ b/main_test.go @@ -0,0 +1,103 @@ +package main + +import ( + "github.com/Fuwn/kivia/internal/analyze" + "github.com/Fuwn/kivia/internal/collect" + "testing" +) + +func TestParseOptionsDefaults(testingContext *testing.T) { + options, err := parseOptions([]string{"--path", "./testdata"}) + + if err != nil { + testingContext.Fatalf("parseOptions returned an error: %v", err) + } + + if options.MinimumEvaluationLength != 1 { + testingContext.Fatalf("Expected min-eval-length to default to 1, got %d.", options.MinimumEvaluationLength) + } +} + +func TestParseOptionsRejectsStrictFlag(testingContext *testing.T) { + _, err := parseOptions([]string{"--path", "./testdata", "--strict"}) + + if err == nil { + testingContext.Fatalf("Expected parseOptions to fail when --strict is provided.") + } +} + +func TestParseOptionsReadsMultipleIgnoreFlags(testingContext *testing.T) { + options, err := parseOptions([]string{ + "--path", "./testdata", + "--ignore", "name=ctx", + "--ignore", "file=_test.go", + "--ignore", "reason=too short", + }) + + if err != nil { + testingContext.Fatalf("parseOptions returned an error: %v", err) + } + + if len(options.Ignore) != 3 { + testingContext.Fatalf("Expected three ignore values, got %d.", len(options.Ignore)) + } +} + +func TestParseOptionsRejectsInvalidMinimumEvaluationLength(testingContext *testing.T) { + _, err := parseOptions([]string{"--path", "./testdata", "--min-eval-length", "0"}) + + if err == nil { + testingContext.Fatalf("Expected parseOptions to fail for min-eval-length=0.") + } +} + +func TestApplyIgnoreFilters(testingContext *testing.T) { + input := analyzeResultFixture() + filtered := applyIgnoreFilters(input, []string{ + "name=ctx", + "reason=too short", + "file=_test.go", + }) + + if len(filtered.Violations) != 1 { + testingContext.Fatalf("Expected one remaining violation, got %d.", len(filtered.Violations)) + } + + if filtered.Violations[0].Identifier.Name != "userNum" { + testingContext.Fatalf("Unexpected remaining violation: %q.", filtered.Violations[0].Identifier.Name) + } +} + +func analyzeResultFixture() analyze.Result { + return analyze.Result{ + Violations: []analyze.Violation{ + { + Identifier: collect.Identifier{ + Name: "ctx", + Kind: "parameter", + File: "sample.go", + Context: collect.Context{ + EnclosingFunction: "Handle", + }, + }, + Reason: "Contains abbreviation: ctx.", + }, + { + Identifier: collect.Identifier{ + Name: "t", + Kind: "parameter", + File: "main_test.go", + }, + Reason: "Name is too short to be self-documenting.", + }, + { + Identifier: collect.Identifier{ + Name: "userNum", + Kind: "parameter", + File: "sample.go", + }, + Reason: "Contains abbreviation: num.", + }, + }, + } +} diff --git a/testdata/dictionary/words.txt b/testdata/dictionary/words.txt new file mode 100644 index 0000000..e8e9975 --- /dev/null +++ b/testdata/dictionary/words.txt @@ -0,0 +1,9 @@ +option +has +resource +parsed +identifier +allowlist +context +error +expression diff --git a/testdata/samplepkg/sample.go b/testdata/samplepkg/sample.go new file mode 100644 index 0000000..ff80b69 --- /dev/null +++ b/testdata/samplepkg/sample.go @@ -0,0 +1,20 @@ +package samplepkg + +import "context" + +type UserCfg struct { + userNum int +} + +func Handle(ctx context.Context, userNum int) int { + resultVal := userNum + 1 + + for idx, usr := range []string{"a", "b"} { + _ = idx + _ = usr + } + + _ = ctx + + return resultVal +} |