Skip to content

Commit 4ce6d0e

Browse files
committed
- ignoreTag change reverted
- placeholder in `missingLocal` replaced to `local`
1 parent 7b9bca4 commit 4ce6d0e

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

lib/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,12 @@ function clearRawTag (tree) {
438438
return tree.reduce((m, node) => {
439439
if (node.content) {
440440
node.content = clearRawTag(node.content)
441-
if (node.tag === ignored) {
442-
node.tag = false
443-
}
444441
}
442+
443+
if (node.tag === ignored) {
444+
node.tag = false
445+
}
446+
445447
m.push(node)
446448

447449
return m

lib/placeholders.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function placeholders (input, ctx, settings, opts) {
7373
throw new ReferenceError(`'${expression}' is not defined`)
7474
}
7575
} else if (typeof opts.missingLocal === 'string') {
76-
value = opts.missingLocal.replace('{expression}', match)
76+
value = opts.missingLocal.replace('{local}', match)
7777
}
7878
}
7979
// Escape html if necessary

readme.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,22 @@ posthtml(expressions({ locals: { foo: 'bar' } }))
126126
<div>Foo: bar</div>
127127
```
128128
#### Missing locals
129-
What to produce in case of referencing a value not in locals can be configured by the `missingLocal` and `strictMode` options
129+
What to produce in case of referencing a value not in `locals` can be configured by the `missingLocal` and `strictMode` options.
130+
131+
When `strictMode` is true (default) and leaving the `missingLocal` option `undefined`, then "'foo' is not defined" exception is thrown.
132+
133+
Setting `strictMode` false and leaving the `missingLocal` option `undefined` results the string `undefined` in the output
134+
135+
Setting the option `missingLocal` to a string will produce that string in the output regardless the value of option `strictMode`. `missingLocal` can contain the placeholder `{local}` which will be replaced with the name of the missing local in the output. This solution allows to:
136+
1. Silently ignore missing locals by setting `missingLocal` to `""`
137+
2. Include the name of the missing local in the output to help detect the which value is missing in `locals` like "#Missing value: {local}"
130138

131139
|`missingLocal`|`strictMode`|output|
132140
|:----:|:-----:|:----------|
133141
| `undefined` (default) | `true` (default) | Error is thrown |
134142
| `undefined` (default) | `false` | 'undefined' |
135143
| `''` | `false`/`true` | `''` (not output)
136-
| `{expression}` | `false`/`true` | [original local] like {{foo}}
144+
| `{local}` | `false`/`true` | original reference like `{{foo}}`
137145

138146
### Unescaped Locals
139147

test/test-core.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ test('local - missing - undefined', (t) => {
115115
})
116116

117117
test('local - missing - keep', (t) => {
118-
return process(t, 'local_missing_keep', { missingLocal: '{expression}' })
118+
return process(t, 'local_missing_keep', { missingLocal: '{local}' })
119119
})
120120

121121
test('local - missing - keep / strictMode:false', (t) => {
122-
return process(t, 'local_missing_keep', { missingLocal: '{expression}', strictMode: false })
122+
return process(t, 'local_missing_keep', { missingLocal: '{local}', strictMode: false })
123123
})
124124

125125
test('local - missing - replace', (t) => {
126-
return process(t, 'local_missing_replace', { missingLocal: 'Error: {expression} undefined' })
126+
return process(t, 'local_missing_replace', { missingLocal: 'Error: {local} undefined' })
127127
})

0 commit comments

Comments
 (0)