From 142523e0b54463d61e7ef494b46703d5a83f30f9 Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Mon, 22 Feb 2021 09:45:41 -0800 Subject: [PATCH] Implements an option to support the template tag imports proposal Implements the `useTemplateTagProposalSemantics` option for individual modules. This proposal is outlined in the README for the template-imports addon, [here](https://github.com/ember-template-imports/ember-template-imports#using-template-tags-and-gjs-files) --- __tests__/tests.js | 234 +++++++++++++++++++++++++++++- index.js | 49 ++++++- src/template-literal-transform.js | 31 ++-- src/template-tag-transform.js | 137 +++++++++++++++++ 4 files changed, 427 insertions(+), 24 deletions(-) create mode 100644 src/template-tag-transform.js diff --git a/__tests__/tests.js b/__tests__/tests.js index 28b28228..01766b0f 100644 --- a/__tests__/tests.js +++ b/__tests__/tests.js @@ -791,6 +791,28 @@ describe('htmlbars-inline-precompile', function () { `); }); + it('works with templates assigned to class expressions', function () { + let transpiled = transform( + ` + import { hbs } from 'ember-template-imports'; + + const Foo = class { + static template = hbs\`hello\`; + } + ` + ); + + expect(transpiled).toMatchInlineSnapshot(` + "import { setComponentTemplate as _setComponentTemplate } from \\"@ember/component\\"; + + const Foo = _setComponentTemplate(Ember.HTMLBars.template( + /* + hello + */ + \\"precompiled(hello)\\"), class {});" + `); + }); + it('correctly handles scope', function () { let source = 'hello'; transform( @@ -825,14 +847,6 @@ describe('htmlbars-inline-precompile', function () { }).toThrow( /Attempted to use `hbs` to define a template in an unsupported way. Templates defined using this helper must be:/ ); - - expect(() => { - transform( - "import { hbs } from 'ember-template-imports';\n let Foo = class { static template = hbs`hello`; }" - ); - }).toThrow( - /Attempted to use `hbs` to define a template in an unsupported way. Templates defined using this helper must be:/ - ); }); it('errors if passed incorrect useTemplateLiteralProposalSemantics version', function () { @@ -851,4 +865,208 @@ describe('htmlbars-inline-precompile', function () { ); }); }); + + describe('with useTemplateTagProposalSemantics', function () { + beforeEach(() => { + plugins = [ + [ + HTMLBarsInlinePrecompile, + { + precompile() { + return precompile.apply(this, arguments); + }, + + modules: { + 'ember-template-imports': { + export: 'GLIMMER_TEMPLATE', + debugName: '