Skip to content

Commit

Permalink
Merge pull request #164 from FPtje/fp/rework-multiline-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
FPtje committed Jul 26, 2023
2 parents f33dcb2 + b9f15a7 commit 08cb841
Show file tree
Hide file tree
Showing 19 changed files with 697 additions and 170 deletions.
24 changes: 13 additions & 11 deletions app/GLuaFixer/Effects/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,15 @@ runOptions options =
pure exitCode
(Test, UseStdIn) -> do
(lintSettings, contents) <- getStdIn options.optsConfigFile options.optsOverridden
test lintSettings "stdin" contents
pure ExitSuccess
test ExitSuccess lintSettings "stdin" contents
(Test, UseFiles files) -> do
foldLuaFiles
options.optsConfigFile
options.optsOverridden
()
ExitSuccess
files
$ \() lintSettings filepath contents ->
test lintSettings filepath contents
pure ExitSuccess
$ \exitCode lintSettings filepath contents ->
test exitCode lintSettings filepath contents
(PrintVersion, _) -> do
putStrLnStdOut version
pure ExitSuccess
Expand Down Expand Up @@ -272,11 +270,12 @@ prettyprint lintSettings contents = do
-- | Test glualint itself against a file. TODO: Refactor this into a nicer command
test
:: (Logging :> es, Eff.Environment :> es)
=> LintSettings
=> ExitCode
-> LintSettings
-> FilePath
-> String
-> Eff es ()
test lintSettings filepath contents = do
-> Eff es ExitCode
test exitCode lintSettings filepath contents = do
putStrLnStdOut $ "Testing " <> filepath
let
(uu_lex, uu_lex_errors) = Interface.lexUU lintSettings contents
Expand All @@ -299,14 +298,16 @@ test lintSettings filepath contents = do
logFormat <- getLogFormat lintSettings.log_format

case Interface.lex lintSettings filepath contents of
Left msgs ->
Left msgs -> do
mapM_ (emitLintMessage logFormat) msgs
pure $ ExitFailure 1
Right tokens ->
case Interface.parse lintSettings filepath tokens of
Left msgs -> do
putStrLnStdOut $
"Errors when trying to parse '" ++ filepath ++ "' with parsec parser!"
mapM_ (emitLintMessage logFormat) msgs
pure $ ExitFailure 1
Right ast -> do
let
prettyprinted = Interface.prettyprint lintSettings ast
Expand All @@ -325,7 +326,8 @@ test lintSettings filepath contents = do
"Errors when trying to parse '" ++ filepath ++ "' with parsec parser after pretty print!"

putStrLnStdOut $ show err
Right _ast -> pure ()
pure $ ExitFailure 1
Right _ast -> pure exitCode

-- | Function to easily parse a file's contents into an AST. This will log any parse failures and
-- give an AST if it can.
Expand Down
400 changes: 276 additions & 124 deletions src/GLua/AG/PrettyPrint.ag

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions tests/golden/data/input/ambiguous-definition.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- Ambiguous without ; as foo(print)(a) is a valid call
foo = function() end
a, b = foo;
(print)(a)

-- NOT Ambiguous without ; as true(print)(a) is syntactically invalid
a, b = true;
(print)(a)

-- Ambiguous without ; as foo(print)(a) is a valid call
a, b = true, foo;
(print)(a)

-- Obviously not ambiguous without ;
a, b = foo;
print(a)

-- Ambiguous without ; as foo()(print)(a) is a valid call
foo();
(print)(a)

a, b = foo, true;
(print)(a)
3 changes: 0 additions & 3 deletions tests/golden/data/input/if-with-comment.lua

This file was deleted.

20 changes: 20 additions & 0 deletions tests/golden/data/input/issue-163.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- These should not be marked as multiline, because there is only one statement
-- in the body of each example.
timer.Create("name", 30, 0, function() if true then return end end)
timer.Create("name", 30, 0, function() return end)
timer.Create("name", 30, 0, function() end)
timer.Create("name", 30, 0)

func(function() if true then return end end)
func(function() anything() end)

-- These, on the other hand, _should_ be marked as multiline
timer.Create("name", 30, 0, function()
-- comment here
if true then return end
end)

timer.Create("name", 30, 0, function()
a = 1
return
end)
8 changes: 8 additions & 0 deletions tests/golden/data/input/issue-50.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
for num, ply in pairs(player.GetAll()) do --If the buddies are in the server then add them serverside
if ply:SteamID() == v.steamid then
-- update the name
sql.Query("UPDATE FPP_Buddies SET name = " .. sql.SQLStr(ply:Nick()) .. " WHERE steamid = " .. sql.SQLStr(v.steamid) .. ";")
FPP.Buddies[v.steamid].name = ply:Nick()
RunConsoleCommand("FPP_SetBuddy", ply:UserID(), v.physgun, v.gravgun, v.toolgun, v.playeruse, v.entitydamage)
end
end
51 changes: 51 additions & 0 deletions tests/golden/data/input/tables.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
a = {}
a = {1}
a = {1,2,3}
a = {
foo, bar;
baz
}
a = {
foo, bar;
baz,
}
a = {
foo, bar;
baz;
}
a = {
[1] = 1,
[2] = 3;
}
a = {
foo = 1,
2,
bar = 3
}
a = {
{1,2,3},
{1,2,3}
}
a = {
[1] = 1,
2, 3,
[4] = 4
}
a = {
a, b;
c
}
a = {
--[[foo]] a, b;
c
}
a = {
-- foo
a, b;
c
}
a = {
a-- foo
, b;
c
}
63 changes: 63 additions & 0 deletions tests/golden/data/input/various-comments.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- Comment before if-statement
if false then -- same-line comment
-- print("TODO")
end

if true then
a = 2 -- bar
end

-- Comment before if-statement
if false then --same-line comment
-- if-statement with multiple statements
a = 1 -- comment after a statement
b = 2
end

-- Comment before if-statement
if true then --[[ multiline comment on same line]] return end

-- Comment before if-statement
if true then
--[[ multiline comment on next line]]
return
end

-- Comment before if-statement
if true then
return -- comment after return on same line
end

-- Comment before if-statement
if true then
return
-- comment after return on new line
end

-- Comment before if-statement
if true then
return
end -- comment after end

if true --[[ comment in condition]] or false -- single line comment in condition
then return end

for var=1, 10, 1 do -- same-line comment
-- print("TODO")
end

for k, v in pairs(player.GetAll()) do -- same-line comment
-- print("TODO")
end

function foo() -- same-line-comment
end

local function foo() -- same-line-comment
end

function foo()
-- Comment before return
return 1, 2, 3 -- comment on same line as return
-- comment after return
end
18 changes: 18 additions & 0 deletions tests/golden/data/output/ambiguous-definition.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-- Ambiguous without ; as foo(print)(a) is a valid call
foo = function() end
a, b = foo;
print(a)
-- NOT Ambiguous without ; as true(print)(a) is syntactically invalid
a, b = true
print(a)
-- Ambiguous without ; as foo(print)(a) is a valid call
a, b = true, foo;
print(a)
-- Obviously not ambiguous without ;
a, b = foo
print(a)
-- Ambiguous without ; as foo()(print)(a) is a valid call
foo()
print(a)
a, b = foo, true
print(a)
20 changes: 18 additions & 2 deletions tests/golden/data/output/darkrp-jobrelated.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ TEAM_CITIZEN = DarkRP.createJob(
"Citizen",
{
color = Color(20, 150, 20, 255),
model = {"models/player/Group01/Female_01.mdl", "models/player/Group01/Female_02.mdl", "models/player/Group01/Female_03.mdl", "models/player/Group01/Female_04.mdl", "models/player/Group01/Female_06.mdl", "models/player/group01/male_01.mdl", "models/player/Group01/Male_02.mdl", "models/player/Group01/male_03.mdl", "models/player/Group01/Male_04.mdl", "models/player/Group01/Male_05.mdl", "models/player/Group01/Male_06.mdl", "models/player/Group01/Male_07.mdl", "models/player/Group01/Male_08.mdl", "models/player/Group01/Male_09.mdl"},
-- test comment -- another comment -- look at this comment
model = {
"models/player/Group01/Female_01.mdl",
"models/player/Group01/Female_02.mdl",
"models/player/Group01/Female_03.mdl", -- test comment
"models/player/Group01/Female_04.mdl",
"models/player/Group01/Female_06.mdl",
"models/player/group01/male_01.mdl",
"models/player/Group01/Male_02.mdl",
"models/player/Group01/male_03.mdl",
"models/player/Group01/Male_04.mdl",
"models/player/Group01/Male_05.mdl",
"models/player/Group01/Male_06.mdl",
"models/player/Group01/Male_07.mdl",
"models/player/Group01/Male_08.mdl",
"models/player/Group01/Male_09.mdl"
},
-- another comment
-- look at this comment
description = [[The Citizen is the most basic level of society you can hold besides being a hobo. You have no specific role in city life.]],
weapons = {},
command = "citizen",
Expand Down
1 change: 0 additions & 1 deletion tests/golden/data/output/if-with-comment.lua

This file was deleted.

6 changes: 5 additions & 1 deletion tests/golden/data/output/issue-106.lua
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
a({b}, function() end, function() end) -- comment
a(
{b},
function() end, -- comment
function() end
)
6 changes: 5 additions & 1 deletion tests/golden/data/output/issue-129.lua
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
local table = {function() return nil end} -- comment
local table = {
function()
return nil -- comment
end
}
Loading

0 comments on commit 08cb841

Please sign in to comment.