1
0
mirror of synced 2025-12-22 11:26:57 -05:00

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:
Peter Bengtsson
2022-02-28 15:40:06 -05:00
committed by GitHub
parent a297f2c549
commit eef77058ab
2 changed files with 39 additions and 10 deletions

View File

@@ -1,5 +1,15 @@
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`:
//
// {% indented_data_reference foo.bar spaces=NUMBER %}
@@ -33,9 +43,25 @@ export default {
// Get the referenced value from the context
const value = await this.liquid.evalValue(`site.data.${dataReference}`, scope)
// If nothing is found in the context, exit with nothing; this may
// feel weird and that we should throw an error, but this is "The Liquid Way TM"
if (!value) return
// If value is falsy it can be because we completely failed to look
// it up. But it can also be literally an empty string.
// 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
const renderedReferenceWithIndent = value.replace(/^/gm, ' '.repeat(numSpaces))

View File

@@ -3,6 +3,7 @@ import path from 'path'
import fs from 'fs'
import { get, isPlainObject, has } from 'lodash-es'
import flat from 'flat'
import { ParseError } from 'liquidjs'
import loadSiteData from '../../lib/site-data.js'
import patterns from '../../lib/patterns.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)
for (const key in dataMap) {
const value = dataMap[key]
if (!patterns.hasLiquid.test(value)) continue
let message = `${key} contains a malformed Liquid expression`
let result = null
try {
result = await liquid.parseAndRender(value)
await liquid.parseAndRender(value)
} catch (err) {
console.trace(err)
message += `: ${err.message}`
if (err instanceof ParseError) {
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')
}
})