[8.6] [@kbn/handlebars] Improve print_ast.js script (#147086) (#147363)

# Backport

This will backport the following commits from `main` to `8.6`:
- [[@kbn/handlebars] Improve print_ast.js script
(#147086)](https://github.com/elastic/kibana/pull/147086)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Thomas
Watson","email":"watson@elastic.co"},"sourceCommit":{"committedDate":"2022-12-12T15:09:01Z","message":"[@kbn/handlebars]
Improve print_ast.js script
(#147086)","sha":"e5d02f5b9718bf1afce16c271869d455f0b95119","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:prev-minor","v8.7.0"],"number":147086,"url":"https://github.com/elastic/kibana/pull/147086","mergeCommit":{"message":"[@kbn/handlebars]
Improve print_ast.js script
(#147086)","sha":"e5d02f5b9718bf1afce16c271869d455f0b95119"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.7.0","labelRegex":"^v8.7.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/147086","number":147086,"mergeCommit":{"message":"[@kbn/handlebars]
Improve print_ast.js script
(#147086)","sha":"e5d02f5b9718bf1afce16c271869d455f0b95119"}}]}]
BACKPORT-->

Co-authored-by: Thomas Watson <watson@elastic.co>
This commit is contained in:
Kibana Machine 2022-12-12 11:13:45 -05:00 committed by GitHub
parent 7d81c5133e
commit 6ff092cf76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 47 deletions

View file

@ -129,48 +129,15 @@ Output:
},
params: [],
hash: undefined,
escaped: true,
strip: { open: false, close: false }
}
],
strip: {}
}
```
You can also filter which properties not to display, e.g:
```sh
./packages/kbn-handlebars/scripts/print_ast.js '{{#myBlock}}Hello {{name}}{{/myBlock}}' params,hash,loc,strip,data,depth,parts,inverse,openStrip,inverseStrip,closeStrip,blockParams,escaped
```
Output:
```js
{
type: 'Program',
body: [
{
type: 'BlockStatement',
path: { type: 'PathExpression', original: 'myBlock' },
program: {
type: 'Program',
body: [
{
type: 'ContentStatement',
original: 'Hello ',
value: 'Hello '
},
{
type: 'MustacheStatement',
path: { type: 'PathExpression', original: 'name' }
}
]
}
escaped: true
}
]
}
```
By default certain properties will be hidden in the output.
For more control over the output, check out the options by running the script without any arguments.
### Print generated code
It's possible to see the generated JavaScript code that `handlebars` create for a given template using the following command line tool:

View file

@ -5,20 +5,31 @@
*/
'use strict'; // eslint-disable-line strict
const { relative } = require('path');
const { inspect } = require('util');
const { parse } = require('handlebars');
const argv = require('minimist')(process.argv.slice(2));
const template = process.argv[2];
const filter = (process.argv[3] || 'loc').split(',');
const containsSubNodes = ['body', 'path', 'program', 'params'];
const DEFAULT_FILTER = 'loc,strip,openStrip,inverseStrip,closeStrip';
const filter = argv['show-all'] ? [''] : (argv.filter || DEFAULT_FILTER).split(',');
const hideEmpty = argv['hide-empty'] || false;
const template = argv._[0];
if (template === undefined) {
console.log(`Usage: ${process.argv[1]} <template> <filter>`);
const script = relative(process.cwd(), process.argv[1]);
console.log(`Usage: ${script} [options] <template>`);
console.log();
console.log('Options:');
console.log(' --filter=... A comma separated list of keys to filter from the output.');
console.log(` Default: ${DEFAULT_FILTER}`);
console.log(' --hide-empty Do not display empty properties.');
console.log(' --show-all Do not filter out any properties. Equivalent to --filter="".');
console.log();
console.log('Example:');
console.log(` ${script} --hide-empty -- 'hello {{name}}'`);
console.log();
console.log(
'By default, <filter> will be "loc", but can be set to any comma separated list of keys to filter from the output'
);
process.exit(1);
}
@ -34,9 +45,17 @@ function reduce(ast) {
delete ast[k];
}
for (const k of containsSubNodes) {
if (k in ast) {
ast[k] = reduce(ast[k]);
if (hideEmpty) {
for (const [k, v] of Object.entries(ast)) {
if (v === undefined || v === null || (Array.isArray(v) && v.length === 0)) {
delete ast[k];
}
}
}
for (const [k, v] of Object.entries(ast)) {
if (typeof v === 'object' && v !== null) {
ast[k] = reduce(v);
}
}
}