Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Global debug flag, better error messages #188

Merged
merged 15 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Introduced Roact Bindings. ([#159](https://github.com/Roblox/roact/pull/159))
* Introduce setState suspension to replace event suspension. ([#183](https://github.com/Roblox/roact/pull/183))
* By default, disable the warning for an element changing types during reconciliation ([#168](https://github.com/Roblox/roact/pull/168))
* Refactor debugging asserts to be behind config flags ([#188](https://github.com/Roblox/roact/pull/188))

## 1.0.0 Prerelease 2 (March 22, 2018)
* Removed `is*Element` methods, this is unlikely to affect anyone ([#50](https://github.com/Roblox/roact/pull/50))
Expand Down
8 changes: 8 additions & 0 deletions bin/run-tests.server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Roact = require(ReplicatedStorage.Roact)
local TestEZ = require(ReplicatedStorage.TestEZ)

Roact.setGlobalConfig({
["internalTypeChecks"] = true,
["typeChecks"] = true,
["elementTracing"] = true,
["propertyValidation"] = true,
})
local results = TestEZ.TestBootstrap:run(ReplicatedStorage.Roact, TestEZ.Reporters.TextReporter)

if __LEMUR__ then
Expand Down
6 changes: 6 additions & 0 deletions lib/Binding.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local createSignal = require(script.Parent.createSignal)
local Symbol = require(script.Parent.Symbol)
local Type = require(script.Parent.Type)

local config = require(script.Parent.GlobalConfig).get()

--[[
Default mapping function used for non-mapped bindings
]]
Expand Down Expand Up @@ -46,6 +48,10 @@ end
Creates a new binding from this one with the given mapping.
]]
function bindingPrototype:map(valueTransform)
if config.typeChecks then
assert(typeof(valueTransform) == "function", "Bad arg #1 to binding:map: expected function")
end

local binding = Binding.create(valueTransform(self:getValue()))

binding[InternalData].valueTransform = valueTransform
Expand Down
53 changes: 38 additions & 15 deletions lib/Component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ local ComponentLifecyclePhase = require(script.Parent.ComponentLifecyclePhase)
local Type = require(script.Parent.Type)
local Symbol = require(script.Parent.Symbol)
local invalidSetStateMessages = require(script.Parent.invalidSetStateMessages)
local GlobalConfig = require(script.Parent.GlobalConfig)
local internalAssert = require(script.Parent.internalAssert)

local config = require(script.Parent.GlobalConfig).get()

--[[
Calling setState during certain lifecycle allowed methods has the potential
Expand Down Expand Up @@ -41,8 +43,10 @@ Component.__componentName = "Component"
PureComponent.
]]
function Component:extend(name)
assert(Type.of(self) == Type.StatefulComponentClass)
assert(typeof(name) == "string")
if config.typeChecks then
assert(Type.of(self) == Type.StatefulComponentClass, "Invalid `self` argument to `extend`.")
assert(typeof(name) == "string", "Component class name must be a string")
end

local class = {}

Expand All @@ -65,7 +69,9 @@ function Component:extend(name)
end

function Component:__getDerivedState(incomingProps, incomingState)
assert(Type.of(self) == Type.StatefulComponentInstance)
if config.internalTypeChecks then
internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__getDerivedState`")
end

local internalData = self[InternalData]
local componentClass = internalData.componentClass
Expand All @@ -74,7 +80,9 @@ function Component:__getDerivedState(incomingProps, incomingState)
local derivedState = componentClass.getDerivedStateFromProps(incomingProps, incomingState)

if derivedState ~= nil then
assert(typeof(derivedState) == "table", "getDerivedStateFromProps must return a table!")
if config.typeChecks then
assert(typeof(derivedState) == "table", "getDerivedStateFromProps must return a table!")
end

return derivedState
end
Expand All @@ -84,7 +92,9 @@ function Component:__getDerivedState(incomingProps, incomingState)
end

function Component:setState(mapState)
assert(Type.of(self) == Type.StatefulComponentInstance)
if config.typeChecks then
assert(Type.of(self) == Type.StatefulComponentInstance, "Invalid `self` argument to `extend`.")
end

local internalData = self[InternalData]
local lifecyclePhase = internalData.lifecyclePhase
Expand Down Expand Up @@ -194,7 +204,7 @@ end
error.
]]
function Component:__validateProps(props)
if not GlobalConfig.getValue("propertyValidation") then
if not config.propertyValidation then
return
end

Expand Down Expand Up @@ -227,9 +237,10 @@ end
instance and attach it to the given virtualNode.
]]
function Component:__mount(reconciler, virtualNode)
assert(Type.of(self) == Type.StatefulComponentClass)
assert(reconciler ~= nil)
assert(Type.of(virtualNode) == Type.VirtualNode)
if config.internalTypeChecks then
internalAssert(Type.of(self) == Type.StatefulComponentClass, "Invalid use of `__mount`")
internalAssert(Type.of(virtualNode) == Type.VirtualNode, "Expected arg #2 to be of type VirtualNode")
end

local currentElement = virtualNode.currentElement
local hostParent = virtualNode.hostParent
Expand Down Expand Up @@ -298,7 +309,9 @@ end
this component instance.
]]
function Component:__unmount()
assert(Type.of(self) == Type.StatefulComponentInstance)
if config.internalTypeChecks then
internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__unmount`")
end

local internalData = self[InternalData]
local virtualNode = internalData.virtualNode
Expand All @@ -321,9 +334,17 @@ end
Returns true if the update was completed, false if it was cancelled by shouldUpdate
]]
function Component:__update(updatedElement, updatedState)
assert(Type.of(self) == Type.StatefulComponentInstance)
assert(Type.of(updatedElement) == Type.Element or updatedElement == nil)
assert(typeof(updatedState) == "table" or updatedState == nil)
if config.internalTypeChecks then
internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__update`")
internalAssert(
Type.of(updatedElement) == Type.Element or updatedElement == nil,
"Expected arg #1 to be of type Element or nil"
)
internalAssert(
typeof(updatedState) == "table" or updatedState == nil,
"Expected arg #2 to be of type table or nil"
)
end

local internalData = self[InternalData]
local componentClass = internalData.componentClass
Expand Down Expand Up @@ -390,7 +411,9 @@ end
Returns true if the update was completed, false if it was cancelled by shouldUpdate
]]
function Component:__resolveUpdate(incomingProps, incomingState)
assert(Type.of(self) == Type.StatefulComponentInstance)
if config.internalTypeChecks then
internalAssert(Type.of(self) == Type.StatefulComponentInstance, "Invalid use of `__resolveUpdate`")
end

local internalData = self[InternalData]
local virtualNode = internalData.virtualNode
Expand Down
Loading