-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[@kbn/handlebars] Add support for partials #150151
Conversation
Documentation preview: |
Current CI failures are unrelated to this PR as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGMT! I couldn't confidently review all intricacies of the Handlebars logic due to lack of the specific knowledge, but as I said elsewhere, for this specific fork it feels reasonable to rely on the extensive test coverage that you provided.
@@ -747,6 +875,9 @@ class ElasticHandlebarsVisitor extends Handlebars.Visitor { | |||
); | |||
} | |||
|
|||
// inherit partials from parent program | |||
runtimeOptions.partials = runtimeOptions.partials || this.runtimeOptions!.partials; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
optional nit: I see we have multiple places in this function where we rely on !
, wondering if it'd make sense to move this.runtimeOptions!
to a local variable and use it instead? Unless I'm missing something...
// It should be defined because ...
const currentRuntimeOptions = this.runtimeOptions!;
runtimeOptions.data = runtimeOptions.data || currentRuntimeOptions.data;
if (runtimeOptions.blockParams) {
runtimeOptions.blockParams = runtimeOptions.blockParams.concat(
currentRuntimeOptions.blockParams
);
}
// inherit partials from parent program
runtimeOptions.partials = runtimeOptions.partials || currentRuntimeOptions.partials;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's because runtimeOptions
isn't set in the constructor
, but in render
, which technically means it's possible to get to this point in the code before it's set. But only theoretically as render
is ALWAYS called before anything else, so we can just treat it as if it is always set.
We use this.runtimeOptions!
all over the code, not only in this function, so I would prefer an approach that satisfied the entire class than to simply add a local variable in each function. Otherwise I prefer the !
approach.
@@ -82,6 +83,18 @@ class HandlebarsTestBench { | |||
return this; | |||
} | |||
|
|||
withPartial(name: string | number, partial: Handlebars.Template) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Is there a valid use case for name
to be a number
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it seems odd, but check out this test: https://github.com/elastic/kibana/pull/150151/files#diff-2e0dbeea10dcf18ef360e3f510487442df799be06fc2830afa55a3711ddc04ecR245-R250
}) | ||
.withHelper('partial', () => 'missing') | ||
.withPartial('dude', '{{name}} ({{url}}) ') | ||
.toThrow('The partial missing could not be found'); // TODO: Is there a way we can test that the error is of type `Handlebars.Exception`? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Cannot Jest's toThrow
(inside test_bench
) perform instanceof
check? https://jestjs.io/docs/expect#tothrowerror
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately only if you give up checking the message. You can either do:
.toThrow('The partial missing could not be found')
Or:
.toThrow(Handlebars.Exception)
You could technically do:
.toThrow(new Handlebars.Exception('The partial missing could not be found'))
But that is according to my research technically equivalent to just checking the string value as it doesn't seem to perform an instanceof
check.
But maybe I'm missing a completely other option. However, it's not super important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I could make some custom logic that after the toThrow
check, does a expect(e.message).toEqual(...)
check 🤷 (but that quickly becomes overly complicated, which is probably why I didn't do it)
💚 Build Succeeded
Metrics [docs]Public APIs missing comments
Async chunks
Unknown metric groupsAPI count
ESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: cc @watson |
Add support for [partials](https://handlebarsjs.com/guide/partials.html) to our own implementation of the [handlebars](https://handlebarsjs.com) template engine. Closes elastic#139068 (cherry picked from commit 2b82cb7)
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
# Backport This will backport the following commits from `main` to `8.6`: - [[@kbn/handlebars] Add support for partials (#150151)](#150151) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Thomas Watson","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-02T19:59:09Z","message":"[@kbn/handlebars] Add support for partials (#150151)\n\nAdd support for [partials](https://handlebarsjs.com/guide/partials.html)\r\nto our own implementation of the [handlebars](https://handlebarsjs.com)\r\ntemplate engine.\r\n\r\nCloses #139068","sha":"2b82cb7fa24e019a3717b60abbe0f814b5ddcd5a","branchLabelMapping":{"^v8.7.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:enhancement","backport:prev-minor","v8.7.0"],"number":150151,"url":"https://github.com/elastic/kibana/pull/150151","mergeCommit":{"message":"[@kbn/handlebars] Add support for partials (#150151)\n\nAdd support for [partials](https://handlebarsjs.com/guide/partials.html)\r\nto our own implementation of the [handlebars](https://handlebarsjs.com)\r\ntemplate engine.\r\n\r\nCloses #139068","sha":"2b82cb7fa24e019a3717b60abbe0f814b5ddcd5a"}},"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/150151","number":150151,"mergeCommit":{"message":"[@kbn/handlebars] Add support for partials (#150151)\n\nAdd support for [partials](https://handlebarsjs.com/guide/partials.html)\r\nto our own implementation of the [handlebars](https://handlebarsjs.com)\r\ntemplate engine.\r\n\r\nCloses #139068","sha":"2b82cb7fa24e019a3717b60abbe0f814b5ddcd5a"}}]}] BACKPORT--> Co-authored-by: Thomas Watson <[email protected]>
Pinging @elastic/kibana-security (Team:Security) |
Add support for partials to our own implementation of the handlebars template engine. The most important reason for this change is support for inline partials, which is a prerequisite for later changing default value of
csp.disableUnsafeEval
fromfalse
totrue
.Closes #139068
Release notes
Added support for "inline partials" in handlebars templates when
csp.disableUnsafeEval
is set totrue
. This allows users who rely on inline partials to remove theunsafe-eval
source expression in the Kibana Content Security Policy (CSP).