-
Notifications
You must be signed in to change notification settings - Fork 3
/
format.lua
212 lines (175 loc) · 4.54 KB
/
format.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
local nl = require("nattlua")
local runtime_syntax = require("nattlua.syntax.runtime")
local function GetFilesRecursively(dir, ext)
ext = ext or {".lua"}
local f = assert(io.popen("find " .. dir))
local lines = f:read("*all")
local paths = {}
for line in lines:gmatch("(.-)\n") do
for _, ext in ipairs(ext) do
if line:sub(-#ext) == ext then table.insert(paths, line) end
end
end
return paths
end
local function read_file(path)
local f = assert(io.open(path, "r"))
local contents = f:read("*all")
f:close()
return contents
end
local function write_file(path, contents)
local f = assert(io.open(path, "w"))
f:write(contents)
f:close()
end
local lua_files = GetFilesRecursively("./", {".lua", ".nlua"})
local config = {
preserve_whitespace = false,
string_quote = "\"",
no_semicolon = true,
comment_type_annotations = true,
type_annotations = "explicit",
force_parenthesis = true,
skip_import = true,
}
local blacklist = {
"test_focus_result%.lua",
"test_focus%.lua",
"build_output%.lua",
"nattlua_for_ai%.lua",
"language_server/json%.lua",
"examples/benchmarks/temp/10mb%.lua",
"examples/projects/luajit/out%.lua",
"examples/projects/love2d/love%-api/.*",
"test/tests/nattlua/analyzer/file_importing/deep_error/.*",
}
local function is_blacklisted(path)
for _, pattern in ipairs(blacklist) do
if path:find(pattern) then return true end
end
return false
end
local dictionary -- = {}
local AUTOFIX = false
local bad_names = { --v = "val",
--k = "key",
}
for _, path in ipairs(lua_files) do
local lua_code = read_file(path)
config.comment_type_annotations = path:sub(-#".lua") == ".lua"
config.comment_type_annotations = config.comment_type_annotations
local compiler = nl.Compiler(lua_code, "@" .. path, config)
if not is_blacklisted(path) then
for _, token in ipairs(assert(compiler:Lex()).Tokens) do
if token.type == "letter" and not runtime_syntax:IsKeyword(token) then
if dictionary then
dictionary[token.value] = (dictionary[token.value] or 0) + 1
end
if bad_names[token.value] then
print(
compiler:GetCode():BuildSourceCodePointMessage("non descriptive variable name", token.start, token.stop)
)
if AUTOFIX and type(bad_names[token.value]) == "string" then
token.value = bad_names[token.value]
end
end
end
end
assert(compiler:Parse())
local new_lua_code = assert(compiler:Emit())
if config.comment_type_annotations then
local ok, err = loadstring(new_lua_code, "@" .. path)
if not ok then
print(path)
print(new_lua_code)
error(err)
end
end
if new_lua_code:sub(#new_lua_code, #new_lua_code) ~= "\n" then
new_lua_code = new_lua_code .. "\n"
end
write_file(path, new_lua_code)
end
end
if dictionary then
local function levenshtein(str1, str2)
local len1 = string.len(str1)
local len2 = string.len(str2)
local matrix = {}
local cost = 0
-- quick cut-offs to save time
if (len1 == 0) then
return len2
elseif (len2 == 0) then
return len1
elseif (str1 == str2) then
return 0
end
-- initialise the base matrix values
for i = 0, len1, 1 do
matrix[i] = {}
matrix[i][0] = i
end
for j = 0, len2, 1 do
matrix[0][j] = j
end
-- actual Levenshtein algorithm
for i = 1, len1, 1 do
for j = 1, len2, 1 do
if (str1:byte(i) == str2:byte(j)) then
cost = 0
else
cost = 1
end
matrix[i][j] = math.min(matrix[i - 1][j] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j - 1] + cost)
end
end
-- return the last value - this is the Levenshtein distance
return matrix[len1][len2]
end
local sorted = {}
for word, count in pairs(dictionary) do
table.insert(sorted, {word = word, count = count})
end
table.sort(sorted, function(a, b)
return a.count > b.count
end)
for _, data in ipairs(sorted) do
print(data.word, " = ", data.count)
end
print("these identifiers are very similar:")
for _, a in ipairs(sorted) do
local astr = a.word:lower()
for _, b in ipairs(sorted) do
local bstr = b.word:lower()
local skip = astr == bstr
if
not skip and
(
astr:sub(1, 3) == "set" or
astr:sub(1, 3) == "get"
)
and
(
bstr:sub(1, 3) == "get" or
bstr:sub(1, 3) == "set"
)
and
astr:sub(4) == bstr:sub(4)
then
skip = true
end
if not skip then
local score = levenshtein(astr, bstr) / #a.word
if score < 0.2 then
if astr:sub(-1) == "s" and astr:sub(0, -2) == bstr then
elseif bstr:sub(-1) == "s" and bstr:sub(0, -2) == astr then
else
print("\t", a.word .. " ~ " .. b.word)
end
end
end
end
end
end