diff options
author | Darius Jahandarie <djahandarie@gmail.com> | 2023-11-09 13:30:31 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-09 13:30:31 +0000 |
commit | 5c5a167b4792af379cdacf633513cebf20728cd2 (patch) | |
tree | 5b6be3620a557d0b9177047003f6d742d9d2a32d /dev/lib/handlebars/src/utils.ts | |
parent | b64f51c3b13a46af4dd7f1e43048ac19c781ca7b (diff) | |
parent | 0f4d36938fd0d844f548aa5a7f7e7842df8dfb41 (diff) |
Merge pull request #307 from themoeway/modernize
Modernize codebase
Diffstat (limited to 'dev/lib/handlebars/src/utils.ts')
-rw-r--r-- | dev/lib/handlebars/src/utils.ts | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/dev/lib/handlebars/src/utils.ts b/dev/lib/handlebars/src/utils.ts new file mode 100644 index 00000000..f55bd98a --- /dev/null +++ b/dev/lib/handlebars/src/utils.ts @@ -0,0 +1,69 @@ +/* + * Elasticsearch B.V licenses this file to you under the MIT License. + * See `packages/kbn-handlebars/LICENSE` for more information. + */ + +// @ts-expect-error: Could not find a declaration file for module +import { createFrame } from 'handlebars/dist/cjs/handlebars/utils'; + +import type { AmbiguousHelperOptions, DecoratorOptions } from './types'; + +export function isBlock(node: hbs.AST.Node): node is hbs.AST.BlockStatement { + return 'program' in node || 'inverse' in node; +} + +export function isDecorator( + node: hbs.AST.Node +): node is hbs.AST.Decorator | hbs.AST.DecoratorBlock { + return node.type === 'Decorator' || node.type === 'DecoratorBlock'; +} + +export function toDecoratorOptions(options: AmbiguousHelperOptions) { + // There's really no tests/documentation on this, but to match the upstream codebase we'll remove `lookupProperty` from the decorator context + delete (options as any).lookupProperty; + + return options as DecoratorOptions; +} + +export function noop() { + return ''; +} + +// liftet from handlebars lib/handlebars/runtime.js +export function initData(context: any, data: any) { + if (!data || !('root' in data)) { + data = data ? createFrame(data) : {}; + data.root = context; + } + return data; +} + +// liftet from handlebars lib/handlebars/compiler/compiler.js +export function transformLiteralToPath(node: { path: hbs.AST.PathExpression | hbs.AST.Literal }) { + const pathIsLiteral = 'parts' in node.path === false; + + if (pathIsLiteral) { + const literal = node.path; + // @ts-expect-error: Not all `hbs.AST.Literal` sub-types has an `original` property, but that's ok, in that case we just want `undefined` + const original = literal.original; + // Casting to string here to make false and 0 literal values play nicely with the rest + // of the system. + node.path = { + type: 'PathExpression', + data: false, + depth: 0, + parts: [original + ''], + original: original + '', + loc: literal.loc, + }; + } +} + +export function allowUnsafeEval() { + try { + new Function(); + return true; + } catch (e) { + return false; + } +} |