Skip to content

Commit

Permalink
- ignoreTag change reverted
Browse files Browse the repository at this point in the history
- placeholder in `missingLocal` replaced to `local`
  • Loading branch information
b-gyula committed Nov 7, 2022
1 parent 7b9bca4 commit 4ce6d0e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,12 @@ function clearRawTag (tree) {
return tree.reduce((m, node) => {
if (node.content) {
node.content = clearRawTag(node.content)
if (node.tag === ignored) {
node.tag = false
}
}

if (node.tag === ignored) {
node.tag = false
}

m.push(node)

return m
Expand Down
2 changes: 1 addition & 1 deletion lib/placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function placeholders (input, ctx, settings, opts) {
throw new ReferenceError(`'${expression}' is not defined`)
}
} else if (typeof opts.missingLocal === 'string') {
value = opts.missingLocal.replace('{expression}', match)
value = opts.missingLocal.replace('{local}', match)
}
}
// Escape html if necessary
Expand Down
12 changes: 10 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,22 @@ posthtml(expressions({ locals: { foo: 'bar' } }))
<div>Foo: bar</div>
```
#### Missing locals
What to produce in case of referencing a value not in locals can be configured by the `missingLocal` and `strictMode` options
What to produce in case of referencing a value not in `locals` can be configured by the `missingLocal` and `strictMode` options.

When `strictMode` is true (default) and leaving the `missingLocal` option `undefined`, then "'foo' is not defined" exception is thrown.

Setting `strictMode` false and leaving the `missingLocal` option `undefined` results the string `undefined` in the output

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:
1. Silently ignore missing locals by setting `missingLocal` to `""`
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}"

|`missingLocal`|`strictMode`|output|
|:----:|:-----:|:----------|
| `undefined` (default) | `true` (default) | Error is thrown |
| `undefined` (default) | `false` | 'undefined' |
| `''` | `false`/`true` | `''` (not output)
| `{expression}` | `false`/`true` | [original local] like {{foo}}
| `{local}` | `false`/`true` | original reference like `{{foo}}`

### Unescaped Locals

Expand Down
6 changes: 3 additions & 3 deletions test/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ test('local - missing - undefined', (t) => {
})

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

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

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

0 comments on commit 4ce6d0e

Please sign in to comment.