Skip to content

Commit a9641d5

Browse files
committed
Use prev to eliminate methods.preventFunctions
1 parent 0b20892 commit a9641d5

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function processBuiltString (method, str, buildState) {
308308
str = str.replace(`__%%%${x}%%%__`, item)
309309
})
310310

311-
const final = `(values, methods, notTraversed, asyncIterators, engine, above, coerceArray, downgrade, precoerceNumber) => ${buildState.asyncDetected ? 'async' : ''} (context ${buildState.extraArguments ? ',' + buildState.extraArguments : ''}) => { let prev; const result = ${str}; return result }`
311+
const final = `(values, methods, notTraversed, asyncIterators, engine, above, coerceArray, downgrade, precoerceNumber) => ${buildState.asyncDetected ? 'async' : ''} (context ${buildState.extraArguments ? ',' + buildState.extraArguments : ''}) => { ${str.includes('prev') ? 'let prev;' : ''} const result = ${str}; return result }`
312312
// console.log(str)
313313
// console.log(final)
314314
// eslint-disable-next-line no-eval

defaultMethods.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ const defaultMethods = {
404404
},
405405
compile: (data, buildState) => {
406406
function wrapNull (data) {
407-
if (!chainingSupported) return buildState.compile`(methods.preventFunctions(((a) => a === null || a === undefined ? null : a)(${data})))`
408-
return buildState.compile`(methods.preventFunctions(${data} ?? null))`
407+
let res
408+
if (!chainingSupported) res = buildState.compile`(((a) => a === null || a === undefined ? null : a)(${data}))`
409+
else res = buildState.compile`(${data} ?? null)`
410+
if (!buildState.engine.allowFunctions) res = buildState.compile`(typeof (prev = ${res}) === 'function' ? null : prev)`
411+
return res
409412
}
410413

411-
if (!buildState.engine.allowFunctions) buildState.methods.preventFunctions = a => typeof a === 'function' ? null : a
412-
else buildState.methods.preventFunctions = a => a
413-
414414
if (typeof data === 'object' && !Array.isArray(data)) {
415415
// If the input for this function can be inlined, we will do so right here.
416416
if (isSyncDeep(data, buildState.engine, buildState) && isDeterministic(data, buildState.engine, buildState) && !buildState.engine.disableInline) data = (buildState.engine.fallback || buildState.engine).run(data, buildState.context, { above: buildState.above })

legacy.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,20 @@ const legacyMethods = {
116116

117117
const pieces = splitPathMemoized(key)
118118

119-
if (!buildState.engine.allowFunctions) buildState.methods.preventFunctions = a => typeof a === 'function' ? null : a
120-
else buildState.methods.preventFunctions = a => a
121-
122119
// support older versions of node
123120
if (!chainingSupported) {
124-
return `(methods.preventFunctions(((a,b) => (typeof a === 'undefined' || a === null) ? b : a)(${pieces.reduce(
121+
const res = `((((a,b) => (typeof a === 'undefined' || a === null) ? b : a)(${pieces.reduce(
125122
(text, i) => `(${text}||0)[${JSON.stringify(i)}]`,
126123
'(context||0)'
127124
)}, ${buildString(defaultValue, buildState)})))`
125+
if (buildState.engine.allowFunctions) return res
126+
return `(typeof (prev = ${res}) === 'function' ? null : prev)`
128127
}
129-
return `(methods.preventFunctions(context${pieces
128+
const res = `(context${pieces
130129
.map((i) => `?.[${JSON.stringify(i)}]`)
131-
.join('')} ?? ${buildString(defaultValue, buildState)}))`
130+
.join('')} ?? ${buildString(defaultValue, buildState)})`
131+
if (buildState.engine.allowFunctions) return res
132+
return `(typeof (prev = ${res}) === 'function' ? null : prev)`
132133
}
133134
return false
134135
}

0 commit comments

Comments
 (0)