From 44e0f68cb0c71255200268620c418b313e91bdd8 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli Date: Fri, 17 Dec 2021 23:52:36 -0500 Subject: [PATCH] [Bugfix release] Allow class-based helpers in strict-mode https://github.com/emberjs/ember.js/issues/19877 --- .../@ember/-internals/glimmer/lib/helper.ts | 2 +- .../tests/integration/custom-helper-test.js | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 packages/@ember/-internals/glimmer/tests/integration/custom-helper-test.js diff --git a/packages/@ember/-internals/glimmer/lib/helper.ts b/packages/@ember/-internals/glimmer/lib/helper.ts index eb4e90e85a5..32994990709 100644 --- a/packages/@ember/-internals/glimmer/lib/helper.ts +++ b/packages/@ember/-internals/glimmer/lib/helper.ts @@ -179,7 +179,7 @@ class ClassicHelperManager implements HelperManager { } getDebugName(definition: ClassHelperFactory) { - return getDebugName!(definition.class!['prototype']); + return getDebugName!((definition.class || definition)!['prototype']); } } diff --git a/packages/@ember/-internals/glimmer/tests/integration/custom-helper-test.js b/packages/@ember/-internals/glimmer/tests/integration/custom-helper-test.js new file mode 100644 index 00000000000..92b81b1825f --- /dev/null +++ b/packages/@ember/-internals/glimmer/tests/integration/custom-helper-test.js @@ -0,0 +1,79 @@ +import { RenderingTestCase, moduleFor, strip } from 'internal-test-helpers'; + +import { precompileJSON } from '@glimmer/compiler'; +import { getTemplateLocals } from '@glimmer/syntax'; +import { createTemplateFactory } from '@ember/template-factory'; + +import { Helper } from '../../index'; +import { setComponentTemplate } from '@glimmer/manager'; +import { templateOnlyComponent } from '@glimmer/runtime'; + +// eslint-disable-next-line ember-internal/require-yuidoc-access +/** + * The template-compiler does not support strict mode at this time. + * precompile from ember-template-compiler returns a string and + * not a template-factory, so it doesn't help with strict-mode testing. + * + * We also can't import from `@ember/template-compiler` because it + * doesn't exist to this kind of test, otherwise we'd be able to use + * precompileTemplate, which would be perfect :D + * + * Copied(ish) from https://github.com/NullVoxPopuli/ember-repl/blob/main/addon/hbs.ts#L51 + */ +function precompileTemplate(source, { moduleName, scope = {} }) { + let locals = getTemplateLocals(source); + + let options = { + strictMode: true, + moduleName, + locals, + isProduction: false, + meta: { moduleName }, + }; + + // Copied from @glimmer/compiler/lib/compiler#precompile + let [block] = precompileJSON(source, options); + + let blockJSON = JSON.stringify(block); + let templateJSONObject = { + id: moduleName, + block: blockJSON, + moduleName: moduleName ?? '(unknown template module)', + scope, + isStrictMode: true, + }; + + let factory = createTemplateFactory(templateJSONObject); + + return factory; +} + +moduleFor( + 'Custom Helper test', + class extends RenderingTestCase { + ['@test works with strict-mode']() { + class Custom extends Helper { + compute([value]) { + return `${value}-custom`; + } + } + + let template = strip` + {{ (Custom 'my-test') }} + `; + + let precompiled = precompileTemplate(template, { + moduleName: 'strict-mode', + scope: () => ({ Custom }), + }); + + let strictMode = setComponentTemplate(precompiled, templateOnlyComponent()); + + this.render(``, { + strictMode, + }); + this.assertText('my-test-custom'); + this.assertStableRerender(); + } + } +);