Skip to content

Commit 723b8f8

Browse files
committed
Added params.canOutputNil for disallowing !() and outputValue() from outputting nil.
1 parent c637c6e commit 723b8f8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

preprocess.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ local currentPathIn = ""
189189
local currentPathOut = ""
190190
local metaPathForErrorMessages = ""
191191
local outputFromMeta = nil
192+
local canOutputNil = true
192193

193194
--==============================================================
194195
--= Local Functions ============================================
@@ -1057,6 +1058,12 @@ function metaFuncs.outputValue(...)
10571058

10581059
for i = 1, argCount do
10591060
local v = select(i, ...)
1061+
1062+
if v == nil and not canOutputNil then
1063+
local ln = debug.getinfo(2, "l").currentline
1064+
errorOnLine(metaPathForErrorMessages, ln, "MetaProgram", "Trying to output nil which is disallowed through params.canOutputNil")
1065+
end
1066+
10601067
local ok, err = serialize(outputFromMeta, v)
10611068

10621069
if not ok then
@@ -1910,6 +1917,7 @@ local function _processFileOrString(params, isFile)
19101917

19111918
metaPathForErrorMessages = params.pathMeta or "<meta>"
19121919
outputFromMeta = {}
1920+
canOutputNil = params.canOutputNil ~= false
19131921

19141922
if params.pathMeta then
19151923
local file = assert(io.open(params.pathMeta, "wb"))
@@ -1942,6 +1950,7 @@ local function _processFileOrString(params, isFile)
19421950

19431951
metaPathForErrorMessages = ""
19441952
outputFromMeta = nil
1953+
canOutputNil = true
19451954

19461955
if params.onAfterMeta then
19471956
local luaModified = params.onAfterMeta(lua)
@@ -2042,6 +2051,7 @@ local function processFileOrString(params, isFile)
20422051
currentPathOut = ""
20432052
metaPathForErrorMessages = ""
20442053
outputFromMeta = nil
2054+
canOutputNil = true
20452055

20462056
-- Unhandled error.
20472057
if not (returnValues or errorToReturn) then
@@ -2091,7 +2101,8 @@ local lib = {
20912101
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
20922102
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
20932103
--
2094-
-- backtickStrings = boolean -- [Optional] Enable the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain backticks.
2104+
-- backtickStrings = boolean -- [Optional] Enable the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain backticks. (Default: false)
2105+
-- canOutputNil = boolean -- [Optional] Allow !() and outputValue() to output nil. (Default: true)
20952106
--
20962107
-- onInsert = function( name ) -- [Optional] Called for each @insert statement. It's expected to return a Lua string. By default 'name' is a path to a file to be inserted.
20972108
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
@@ -2113,7 +2124,8 @@ local lib = {
21132124
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
21142125
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
21152126
--
2116-
-- backtickStrings = boolean -- [Optional] Enable the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain backticks.
2127+
-- backtickStrings = boolean -- [Optional] Enable the backtick (`) to be used as string literal delimiters. Backtick strings don't interpret any escape sequences and can't contain backticks. (Default: false)
2128+
-- canOutputNil = boolean -- [Optional] Allow !() and outputValue() to output nil. (Default: true)
21172129
--
21182130
-- onInsert = function( name ) -- [Optional] Called for each @insert statement. It's expected to return a Lua string. By default 'name' is a path to a file to be inserted.
21192131
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.

0 commit comments

Comments
 (0)