typo_checker 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/LICENSE +21 -0
- data/README.md +119 -0
- data/bin/typo_checker +4 -0
- data/lib/data/typos.csv +118404 -0
- data/lib/typo_checker/cli.rb +14 -0
- data/lib/typo_checker/version.rb +5 -0
- data/lib/typo_checker.rb +87 -0
- metadata +70 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
# lib/typo_checker/cli.rb
|
2
|
+
require 'thor'
|
3
|
+
require 'typo_checker'
|
4
|
+
|
5
|
+
module TypoChecker
|
6
|
+
class CLI < Thor
|
7
|
+
desc 'scan REPO_PATH', 'Scan a repository for typos'
|
8
|
+
|
9
|
+
def scan(repo_path)
|
10
|
+
checker = TypoChecker::Checker.new
|
11
|
+
checker.scan_repo(repo_path)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/typo_checker.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require 'find'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
module TypoChecker
|
8
|
+
class Checker
|
9
|
+
attr_reader :typos
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
csv_file = File.expand_path('data/typos.csv', __dir__)
|
13
|
+
@typos = load_typos(csv_file)
|
14
|
+
end
|
15
|
+
|
16
|
+
def scan_repo(repo_path = Dir.pwd)
|
17
|
+
Find.find(repo_path) do |path|
|
18
|
+
scan_file(path) if File.file?(path) && text_file?(path)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def load_typos(csv_file)
|
25
|
+
typos = {}
|
26
|
+
CSV.foreach(csv_file, headers: false) do |row|
|
27
|
+
typos[row[0]] = row[1]
|
28
|
+
end
|
29
|
+
typos
|
30
|
+
end
|
31
|
+
|
32
|
+
def text_file?(path)
|
33
|
+
%w[
|
34
|
+
.rb .txt .md .html .css .js .py .java .php .go .swift .ts .scala .c .cpp .csharp .h .lua .pl .rs .kt
|
35
|
+
.d .r .m .sh .bash .bat .json .yaml .xml .scss .tsv .vb .ps1 .clj .elixir .f# .vhdl .verilog
|
36
|
+
.ada .ml .lisp .prolog .tcl .rexx .awk .sed .coffee .groovy .dart .haxe .zig .nim .crystal .reason .ocaml
|
37
|
+
.forth .v .xhtml .julia .racket .scheme .rust .graphql
|
38
|
+
].include? File.extname(path)
|
39
|
+
end
|
40
|
+
|
41
|
+
def scan_file(path)
|
42
|
+
File.foreach(path).with_index do |line, line_number|
|
43
|
+
words = line.split(/[^a-zA-Z0-9']+/)
|
44
|
+
check_words = words.map { |word| split_function_name(word) }.flatten
|
45
|
+
check_words.each do |word|
|
46
|
+
char_index = line.index(word)
|
47
|
+
check_word(word, path, line_number, char_index)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def check_word(word, file, line_num, char_index)
|
53
|
+
return unless typos.key?(word.downcase)
|
54
|
+
|
55
|
+
corrected_word = corrected_word(word, typos[word.downcase])
|
56
|
+
typo_path = "#{file}:#{line_num + 1}:#{char_index + 1}"
|
57
|
+
puts "Typo found in #{colorize_light_blue(typo_path)}: " \
|
58
|
+
"#{colorize_red(word)} -> #{colorize_green(corrected_word)}"
|
59
|
+
end
|
60
|
+
|
61
|
+
def split_function_name(name)
|
62
|
+
name.gsub(/([a-z])([A-Z])/, '\1 \2').split(/[\s_]+/)
|
63
|
+
end
|
64
|
+
|
65
|
+
def corrected_word(word, typo_correct_word)
|
66
|
+
if word == word.capitalize
|
67
|
+
typo_correct_word.capitalize
|
68
|
+
elsif word == word.upcase
|
69
|
+
typo_correct_word.upcase
|
70
|
+
else
|
71
|
+
typo_correct_word
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def colorize_red(text)
|
76
|
+
"\e[31m#{text}\e[0m"
|
77
|
+
end
|
78
|
+
|
79
|
+
def colorize_green(text)
|
80
|
+
"\e[32m#{text}\e[0m"
|
81
|
+
end
|
82
|
+
|
83
|
+
def colorize_light_blue(text)
|
84
|
+
"\e[94m#{text}\e[0m"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: typo_checker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- datpmt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thor
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.3.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.3.2
|
27
|
+
description: TypoChecker is a tool for scanning source code files for common typographical
|
28
|
+
errors. The tool checks through text-based files in a given repository to identify
|
29
|
+
and suggest corrections for any matches found.
|
30
|
+
email: datpmt.2k@gmail.com
|
31
|
+
executables:
|
32
|
+
- typo_checker
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- CHANGELOG.md
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- bin/typo_checker
|
40
|
+
- lib/data/typos.csv
|
41
|
+
- lib/typo_checker.rb
|
42
|
+
- lib/typo_checker/cli.rb
|
43
|
+
- lib/typo_checker/version.rb
|
44
|
+
homepage: https://rubygems.org/gems/typo_checker
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
source_code_uri: https://github.com/datpmt/typo_checker
|
49
|
+
changelog_uri: https://github.com/datpmt/typo_checker/blob/main/CHANGELOG.md
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 2.6.0
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubygems_version: 3.2.3
|
66
|
+
signing_key:
|
67
|
+
specification_version: 4
|
68
|
+
summary: TypoChecker is a tool for scanning source code files for common typographical
|
69
|
+
errors.
|
70
|
+
test_files: []
|