mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
[@kbn/handlebars] Improve print_ast.js script (#147086)
This commit is contained in:
parent
96b30745f2
commit
e5d02f5b97
2 changed files with 33 additions and 47 deletions
|
@ -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:
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue