Skip to content

Commit

Permalink
Bring in selene and stylua. Format using stylua.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthargett committed Nov 1, 2021
1 parent 2ea111e commit 3d6fda0
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 180 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/site/
/*.rbxmx
/*.rbxm

# Selene
roblox.toml
4 changes: 4 additions & 0 deletions foreman.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[tools]
rojo = { source = "rojo-rbx/rojo", version = "6.2.0" }
selene = { source = "Kampfkarren/selene", version = "0.14" }
stylua = { source = "JohnnyMorganz/StyLua", version = "0.11" }
19 changes: 19 additions & 0 deletions selene.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
std = "roblox+testez"

[config]
empty_if = { comments_count = true }
unused_variable = { ignore_pattern = "result|ok|^_" }
# this comes up when translating nested try/finally scenarios
shadowing = { ignore_pattern = "result|ok|^_" }
# feature request for this config: https://github.com/Kampfkarren/selene/issues/181
# global_usage = { ignore_pattern = "^__" }

[rules]
# remove this once the feature request here is implemented: https://github.com/Kampfkarren/selene/issues/181
global_usage = "allow"
unused_variable = "allow"
# remove when the Luau type narrowing issues (and the workarounds) are resolved
shadowing = "allow"

# remove when this issue is fixed: https://github.com/Kampfkarren/selene/issues/179
if_same_then_else = "allow"
2 changes: 1 addition & 1 deletion src/NoYield.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ return function()
expect(err:find("foo")).to.be.ok()
expect(err:find("NoYield.spec")).to.be.ok()
end)
end
end
27 changes: 13 additions & 14 deletions src/Signal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Signal.__index = Signal
function Signal.new(store)
local self = {
_listeners = {},
_store = store
_store = store,
}

setmetatable(self, Signal)
Expand All @@ -53,30 +53,29 @@ function Signal:connect(callback)

if self._store and self._store._isDispatching then
error(
'You may not call store.changed:connect() while the reducer is executing. ' ..
'If you would like to be notified after the store has been updated, subscribe from a ' ..
'component and invoke store:getState() in the callback to access the latest state. '
"You may not call store.changed:connect() while the reducer is executing. "
.. "If you would like to be notified after the store has been updated, subscribe from a "
.. "component and invoke store:getState() in the callback to access the latest state. "
)
end

local listener = {
callback = callback,
disconnected = false,
connectTraceback = debug.traceback(),
disconnectTraceback = nil
disconnectTraceback = nil,
}

self._listeners = immutableAppend(self._listeners, listener)

local function disconnect()
if listener.disconnected then
error((
"Listener connected at: \n%s\n" ..
"was already disconnected at: \n%s\n"
):format(
tostring(listener.connectTraceback),
tostring(listener.disconnectTraceback)
))
error(
("Listener connected at: \n%s\n" .. "was already disconnected at: \n%s\n"):format(
tostring(listener.connectTraceback),
tostring(listener.disconnectTraceback)
)
)
end

if self._store and self._store._isDispatching then
Expand All @@ -89,7 +88,7 @@ function Signal:connect(callback)
end

return {
disconnect = disconnect
disconnect = disconnect,
}
end

Expand All @@ -101,4 +100,4 @@ function Signal:fire(...)
end
end

return Signal
return Signal
6 changes: 3 additions & 3 deletions src/Signal.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ return function()

it("should throw an error when subscribing during dispatch", function()
local mockStore = {
_isDispatching = false
_isDispatching = false,
}
local signal = Signal.new(mockStore)

Expand All @@ -149,7 +149,7 @@ return function()

it("should throw an error when unsubscribing during dispatch", function()
local mockStore = {
_isDispatching = false
_isDispatching = false,
}
local signal = Signal.new(mockStore)

Expand All @@ -163,4 +163,4 @@ return function()
signal:fire()
end).to.throw()
end)
end
end
51 changes: 23 additions & 28 deletions src/Store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Store.new(reducer, initialState, middlewares, errorReporter)
assert(typeof(reducer) == "function", "Bad argument #1 to Store.new, expected function.")
assert(middlewares == nil or typeof(middlewares) == "table", "Bad argument #3 to Store.new, expected nil or table.")
if middlewares ~= nil then
for i=1, #middlewares, 1 do
for i = 1, #middlewares, 1 do
assert(
typeof(middlewares[i]) == "function",
("Expected the middleware ('%s') at index %d to be a function."):format(tostring(middlewares[i]), i)
Expand Down Expand Up @@ -107,9 +107,13 @@ end
]]
function Store:getState()
if self._isDispatching then
error(("You may not call store:getState() while the reducer is executing. " ..
"The reducer (%s) has already received the state as an argument. " ..
"Pass it down from the top reducer instead of reading it from the store."):format(tostring(self._reducer)))
error(
(
"You may not call store:getState() while the reducer is executing. "
.. "The reducer (%s) has already received the state as an argument. "
.. "Pass it down from the top reducer instead of reading it from the store."
):format(tostring(self._reducer))
)
end

return self._state
Expand All @@ -124,16 +128,16 @@ end
]]
function Store:dispatch(action)
if typeof(action) ~= "table" then
error(("Actions must be tables. " ..
"Use custom middleware for %q actions."):format(typeof(action)),
2
)
error(("Actions must be tables. " .. "Use custom middleware for %q actions."):format(typeof(action)), 2)
end

if action.type == nil then
error("Actions may not have an undefined 'type' property. " ..
"Have you misspelled a constant? \n" ..
tostring(action), 2)
error(
"Actions may not have an undefined 'type' property. "
.. "Have you misspelled a constant? \n"
.. tostring(action),
2
)
end

if self._isDispatching then
Expand All @@ -149,14 +153,10 @@ function Store:dispatch(action)
self._isDispatching = false

if not ok then
self._errorReporter.reportReducerError(
self._state,
action,
{
message = "Caught error in reducer",
thrownValue = result,
}
)
self._errorReporter.reportReducerError(self._state, action, {
message = "Caught error in reducer",
thrownValue = result,
})
end

if #self._actionLog == ACTION_LOG_LENGTH then
Expand Down Expand Up @@ -200,15 +200,10 @@ function Store:flush()
end, tracebackReporter)

if not ok then
self._errorReporter.reportUpdateError(
self._lastState,
state,
self._actionLog,
{
message = "Caught error flushing store updates",
thrownValue = errorResult,
}
)
self._errorReporter.reportUpdateError(self._lastState, state, self._actionLog, {
message = "Caught error flushing store updates",
thrownValue = errorResult,
})
end

self._lastState = state
Expand Down
46 changes: 16 additions & 30 deletions src/Store.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ return function()
end,
reportUpdateError = function()
-- no op
end
end,
}

local innerErrorMessage = "Z4PH0D"
Expand All @@ -163,23 +163,17 @@ return function()

local store
store = Store.new(reducerThatErrors, {
Value = 1
Value = 1,
}, nil, mockErrorReporter)

expect(caughtState.Value).to.equal(1)
expect(caughtAction.type).to.equal("@@INIT")
expect(caughtErrorResult.message).to.equal("Caught error in reducer with init")
expect(string.find(
caughtErrorResult.thrownValue,
innerErrorMessage
)).to.be.ok()
expect(string.find(caughtErrorResult.thrownValue, innerErrorMessage)).to.be.ok()
-- We want to verify that this is a stacktrace without caring too
-- much about the format, so we look for the stack frame associated
-- with this test file
expect(string.find(
caughtErrorResult.thrownValue,
script.Name
)).to.be.ok()
expect(string.find(caughtErrorResult.thrownValue, script.Name)).to.be.ok()

store:destruct()
end)
Expand All @@ -194,7 +188,7 @@ return function()
end,
reportUpdateError = function()
-- no op
end
end,
}

local innerErrorMessage = "Z4PH0D"
Expand All @@ -203,7 +197,7 @@ return function()
error(innerErrorMessage)
elseif action.type == "Increment" then
return {
Value = state.Value + 1
Value = state.Value + 1,
}
end
return state
Expand All @@ -218,27 +212,20 @@ return function()
expect(caughtAction).to.equal(nil)
expect(caughtErrorResult).to.equal(nil)

store:dispatch({type = "Increment"})
store:dispatch({type = "ThrowError"})
store:dispatch({ type = "Increment" })
store:dispatch({ type = "ThrowError" })

expect(caughtState.Value).to.equal(2)
expect(caughtAction.type).to.equal("ThrowError")
expect(caughtErrorResult.message).to.equal("Caught error in reducer")
expect(string.find(
caughtErrorResult.thrownValue,
innerErrorMessage
)).to.be.ok()
expect(string.find(caughtErrorResult.thrownValue, innerErrorMessage)).to.be.ok()
-- We want to verify that this is a stacktrace without caring too
-- much about the format, so we look for the stack frame associated
-- with this test file
expect(string.find(
caughtErrorResult.thrownValue,
script.Name
)).to.be.ok()
expect(string.find(caughtErrorResult.thrownValue, script.Name)).to.be.ok()

store:destruct()
end)

end)

describe("getState", function()
Expand Down Expand Up @@ -456,13 +443,13 @@ return function()
caughtState = state
caughtActionLog = actionLog
caughtErrorResult = errorResult
end
end,
}

local reducer = function(state, action)
if action.type == "Increment" then
return {
Value = state.Value + action.amount
Value = state.Value + action.amount,
}
end
return state
Expand All @@ -479,9 +466,9 @@ return function()
end)

local actions = {
{type = "Increment", amount = 1},
{type = "Increment", amount = 3},
{type = "Increment", amount = 10},
{ type = "Increment", amount = 1 },
{ type = "Increment", amount = 3 },
{ type = "Increment", amount = 10 },
}
for _, action in ipairs(actions) do
store:dispatch(action)
Expand Down Expand Up @@ -524,8 +511,7 @@ return function()

describe("flush", function()
it("should not fire a changed event if there were no dispatches", function()
local store = Store.new(function()
end)
local store = Store.new(function() end)

local count = 0
store.changed:connect(function()
Expand Down
4 changes: 2 additions & 2 deletions src/createReducer.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ return function()
local reducer = createReducer({
a = 0,
b = 0,
-- We don't care about the actions here
-- We don't care about the actions here
}, {})

local newState = reducer(nil, {})
Expand All @@ -56,7 +56,7 @@ return function()
foo = function(state, action)
callCount = callCount + 1
return nil
end
end,
})

expect(callCount).to.equal(0)
Expand Down
2 changes: 1 addition & 1 deletion src/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ return function()
expect(Rodux.Store).to.be.ok()
end)
end)
end
end
7 changes: 3 additions & 4 deletions src/loggerMiddleware.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ function loggerMiddleware.middleware(nextDispatch, store)
return function(action)
local result = nextDispatch(action)

loggerMiddleware.outputFunction(("Action dispatched: %s\nState changed to: %s"):format(
prettyPrint(action),
prettyPrint(store:getState())
))
loggerMiddleware.outputFunction(
("Action dispatched: %s\nState changed to: %s"):format(prettyPrint(action), prettyPrint(store:getState()))
)

return result
end
Expand Down
4 changes: 3 additions & 1 deletion src/loggerMiddleware.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ return function()
barValue = {
bazValue = "hiBaz",
},
}, { loggerMiddleware.middleware })
}, {
loggerMiddleware.middleware,
})

loggerMiddleware.outputFunction = function(message)
outputCount = outputCount + 1
Expand Down
Loading

0 comments on commit 3d6fda0

Please sign in to comment.