Skip to content

Commit fafb106

Browse files
Merge pull request #4 from gastonrobledo/bugfix/give-a-hoot-dont-pollute
Bugfix/give a hoot dont pollute
2 parents 3aa361a + 4f3887a commit fafb106

File tree

4 files changed

+48
-3293
lines changed

4 files changed

+48
-3293
lines changed

index.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
11
const { registerCoreHelpers } = require('./helpers')
22

3-
function asyncHelpers(handlebars) {
4-
const _compile = handlebars.compile,
5-
_template = handlebars.VM.template,
6-
_mergeSource = handlebars.JavaScriptCompiler.prototype.mergeSource
3+
function asyncHelpers(hbs) {
74

8-
handlebars.JavaScriptCompiler.prototype.mergeSource = function(varDeclarations) {
9-
const sources = _mergeSource.call(this, varDeclarations)
10-
return sources.prepend('return (async () => {').add(' })()')
11-
}
5+
const handlebars = hbs.create(),
6+
asyncCompiler = class extends hbs.JavaScriptCompiler {
127

13-
handlebars.JavaScriptCompiler.prototype.appendToBuffer = function(source, location, explicit) {
14-
// Force a source as this simplifies the merge logic.
15-
if (!Array.isArray(source)) {
16-
source = [source]
17-
}
18-
source = this.source.wrap(source, location)
19-
20-
if (this.environment.isSimple) {
21-
return ['return await ', source, ';']
22-
} if (explicit) {
23-
// This is a case where the buffer operation occurs as a child of another
24-
// construct, generally braces. We have to explicitly output these buffer
25-
// operations to ensure that the emitted code goes in the correct location.
26-
return ['buffer += await ', source, ';']
27-
}
28-
source.appendToBuffer = true
29-
source.prepend('await ')
30-
return source
8+
constructor() {
9+
super()
10+
this.compiler = asyncCompiler
11+
}
3112

32-
}
13+
mergeSource(varDeclarations) {
14+
const sources = super.mergeSource(varDeclarations)
15+
return sources.prepend('return (async () => {').add(' })()')
16+
}
3317

18+
appendToBuffer(source, location, explicit) {
19+
// Force a source as this simplifies the merge logic.
20+
if (!Array.isArray(source)) {
21+
source = [source]
22+
}
23+
source = this.source.wrap(source, location)
24+
25+
if (this.environment.isSimple) {
26+
return ['return await ', source, ';']
27+
} if (explicit) {
28+
// This is a case where the buffer operation occurs as a child of another
29+
// construct, generally braces. We have to explicitly output these buffer
30+
// operations to ensure that the emitted code goes in the correct location.
31+
return ['buffer += await ', source, ';']
32+
}
33+
source.appendToBuffer = true
34+
source.prepend('await ')
35+
return source
36+
37+
}
38+
39+
}
40+
handlebars.JavaScriptCompiler = asyncCompiler
41+
42+
const _compile = handlebars.compile,
43+
_template = handlebars.VM.template
3444
handlebars.template = function(spec) {
3545
spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => {
3646
// const main = await spec.main

0 commit comments

Comments
 (0)