throw if no value not found in indented_data_reference (#25711)
* throw if no value not found in indented_data_reference * better comment * deliberate writer-error saple * all strings are truthy'ish * undo * fix tests
This commit is contained in:
@@ -1,5 +1,15 @@
|
|||||||
import assert from 'assert'
|
import assert from 'assert'
|
||||||
|
|
||||||
|
// If 'THROW_ON_EMPTY' is set and it's value is '0' or 'false' it becomes
|
||||||
|
// false. Or true if it's 'true' or '1'.
|
||||||
|
const THROW_ON_EMPTY = Boolean(
|
||||||
|
process.env.THROW_ON_EMPTY
|
||||||
|
? JSON.parse(process.env.THROW_ON_EMPTY)
|
||||||
|
: JSON.parse(process.env.CI || process.env.NODE_ENV !== 'production')
|
||||||
|
)
|
||||||
|
|
||||||
|
class IndentedDataReferenceError extends Error {}
|
||||||
|
|
||||||
// This class supports a tag that expects two parameters, a data reference and `spaces=NUMBER`:
|
// This class supports a tag that expects two parameters, a data reference and `spaces=NUMBER`:
|
||||||
//
|
//
|
||||||
// {% indented_data_reference foo.bar spaces=NUMBER %}
|
// {% indented_data_reference foo.bar spaces=NUMBER %}
|
||||||
@@ -33,9 +43,25 @@ export default {
|
|||||||
// Get the referenced value from the context
|
// Get the referenced value from the context
|
||||||
const value = await this.liquid.evalValue(`site.data.${dataReference}`, scope)
|
const value = await this.liquid.evalValue(`site.data.${dataReference}`, scope)
|
||||||
|
|
||||||
// If nothing is found in the context, exit with nothing; this may
|
// If value is falsy it can be because we completely failed to look
|
||||||
// feel weird and that we should throw an error, but this is "The Liquid Way TM"
|
// it up. But it can also be literally an empty string.
|
||||||
if (!value) return
|
// For example, the reusable could be entirely something like this:
|
||||||
|
//
|
||||||
|
// {% if some condition %}The meat{% endif %}
|
||||||
|
//
|
||||||
|
// Then it's working as expected. But if the reference is wrong, e.g.
|
||||||
|
//
|
||||||
|
// {% indented_data_reference reusables.foo.tyypu spaces=3 %}
|
||||||
|
//
|
||||||
|
// Or if the file simple doesn't exist, you get an undefined for the value.
|
||||||
|
if (typeof value !== 'string' && !value) {
|
||||||
|
const message = `Can't find the key 'site.data.${dataReference}' in the scope.`
|
||||||
|
if (THROW_ON_EMPTY) {
|
||||||
|
throw new IndentedDataReferenceError(message)
|
||||||
|
}
|
||||||
|
console.warn(message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// add spaces to each line
|
// add spaces to each line
|
||||||
const renderedReferenceWithIndent = value.replace(/^/gm, ' '.repeat(numSpaces))
|
const renderedReferenceWithIndent = value.replace(/^/gm, ' '.repeat(numSpaces))
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import path from 'path'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { get, isPlainObject, has } from 'lodash-es'
|
import { get, isPlainObject, has } from 'lodash-es'
|
||||||
import flat from 'flat'
|
import flat from 'flat'
|
||||||
|
import { ParseError } from 'liquidjs'
|
||||||
import loadSiteData from '../../lib/site-data.js'
|
import loadSiteData from '../../lib/site-data.js'
|
||||||
import patterns from '../../lib/patterns.js'
|
import patterns from '../../lib/patterns.js'
|
||||||
import { liquid } from '../../lib/render-content/index.js'
|
import { liquid } from '../../lib/render-content/index.js'
|
||||||
@@ -63,20 +64,22 @@ describe('siteData module (English)', () => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test('all Liquid templating is valid', async () => {
|
test('all Liquid tags are valid', async () => {
|
||||||
const dataMap = flat(data)
|
const dataMap = flat(data)
|
||||||
for (const key in dataMap) {
|
for (const key in dataMap) {
|
||||||
const value = dataMap[key]
|
const value = dataMap[key]
|
||||||
if (!patterns.hasLiquid.test(value)) continue
|
if (!patterns.hasLiquid.test(value)) continue
|
||||||
let message = `${key} contains a malformed Liquid expression`
|
|
||||||
let result = null
|
|
||||||
try {
|
try {
|
||||||
result = await liquid.parseAndRender(value)
|
await liquid.parseAndRender(value)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.trace(err)
|
if (err instanceof ParseError) {
|
||||||
message += `: ${err.message}`
|
console.warn('value that failed to parse:', value)
|
||||||
|
throw new Error(`Unable to parse with Liquid: ${err.message}`)
|
||||||
|
}
|
||||||
|
// Note, the parseAndRender() might throw other errors. For
|
||||||
|
// example errors about the the data. But at least it
|
||||||
|
// managed to get paste the Liquid parsing phase.
|
||||||
}
|
}
|
||||||
expect(typeof result, message).toBe('string')
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user