Skip to content

allow reuse of bindings #63

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('htmlbars-inline-precompile', function () {
});

afterEach(function () {
sharedStateToBabel = null;
sinon.restore();
});

Expand Down Expand Up @@ -654,13 +655,23 @@ describe('htmlbars-inline-precompile', function () {
);
});

let sharedStateToBabel = null as any;

let expressionTransform: ExtendedPluginBuilder = (env) => {
return {
name: 'expression-transform',
visitor: {
PathExpression(node, path) {
if (node.original === 'onePlusOne') {
let boundName = sharedStateToBabel?.boundName;
if (boundName) {
env.meta.jsutils.bindVariable(boundName);
return env.syntax.builders.path(boundName);
}
let name = env.meta.jsutils.bindExpression('1+1', path, { nameHint: 'two' });
if (sharedStateToBabel) {
sharedStateToBabel.boundName = name;
}
return env.syntax.builders.path(name);
}
return undefined;
Expand Down Expand Up @@ -1314,6 +1325,47 @@ describe('htmlbars-inline-precompile', function () {
`);
});

it('can reuse bindings', function () {
plugins = [
[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [expressionTransform] }],
];

sharedStateToBabel = {};

let transformed = transform(stripIndent`
import { precompileTemplate } from '@ember/template-compilation';
import Message from 'message';
const template = precompileTemplate('<Message @text={{onePlusOne}} />', {
scope: () => ({
Message
})
});
const template2 = precompileTemplate('<Message @text={{onePlusOne}} />', {
scope: () => ({
Message
})
});
`);

expect(transformed).toEqualCode(`
import { precompileTemplate } from '@ember/template-compilation';
import Message from 'message';
let two = 1 + 1;
const template = precompileTemplate("<Message @text={{two}} />", {
scope: () => ({
Message,
two
})
});
const template2 = precompileTemplate("<Message @text={{two}} />", {
scope: () => ({
Message,
two
})
});
`);
});

it('adds new locals to preexisting renamed scope', function () {
plugins = [
[HTMLBarsInlinePrecompile, { targetFormat: 'hbs', transforms: [expressionTransform] }],
Expand Down
16 changes: 15 additions & 1 deletion src/js-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class JSUtils {
#babel: typeof Babel;
#state: State;
#template: NodePath<t.Expression>;
#addedBinding: (name: string) => void;
#addedBinding: (name: string, jsName?: string) => void;
#importer: ImportUtil;

constructor(
Expand Down Expand Up @@ -84,6 +84,20 @@ export class JSUtils {
return name;
}

/**
* Allows (re)use of binding that you already have in the js code or created with bindExpression
* Especially useful if you have a custom transform that creates a binding that you want to reuse
*
* @param hbsName the name you are using in your template to access the binding
* @param jsName the name of a js variable
*
* @return The name you can use in your template to access the binding.
*/
bindVariable(hbsName: string, jsName?: string) {
this.#addedBinding(hbsName, jsName);
return hbsName;
}

#emitStatement<T extends t.Statement>(statement: T): NodePath<T> {
if (this.#state.lastInsertedPath) {
this.#state.lastInsertedPath = this.#state.lastInsertedPath.insertAfter(statement)[0];
Expand Down