Skip to content

Commit

Permalink
Fix a problem where plain functions can't be used as helper if they a…
Browse files Browse the repository at this point in the history
…re not passed through the strict mode context object

It seems functions passed as strict mode context values follow a different code path than component arguments and class properties so the error wasn't shown there.
  • Loading branch information
Windvis committed Apr 13, 2022
1 parent 07db065 commit ebdc58d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
RenderTest,
test,
jitSuite,
GlimmerishComponent,
tracked,
defineComponent,
trackedObj,
Expand Down Expand Up @@ -57,6 +58,58 @@ class HelperManagerTest extends RenderTest {
this.assertHTML('plain function');
}

@test({ skip: SKIP_DEFAULT_HELPER_MANAGER_TESTS })
'(Default Helper Manager) plain functions passed as component arguments work as helpers'(
assert: Assert
) {
let count = 0;

const hello = () => {
count++;
return 'plain function';
};

const Main = defineComponent({}, '{{(@hello)}}');

this.renderComponent(Main, {
hello,
});

assert.equal(count, 1, 'rendered once');
this.assertHTML('plain function');

this.rerender();

assert.equal(count, 1, 'rendered once');
this.assertHTML('plain function');
}

@test({ skip: SKIP_DEFAULT_HELPER_MANAGER_TESTS })
'(Default Helper Manager) plain functions stored on component class properties work as helpers'(
assert: Assert
) {
let count = 0;

const Main = defineComponent({}, '{{(this.hello)}}', {
definition: class extends GlimmerishComponent {
hello = () => {
count++;
return 'plain function';
};
},
});

this.renderComponent(Main);

assert.equal(count, 1, 'rendered once');
this.assertHTML('plain function');

this.rerender();

assert.equal(count, 1, 'rendered once');
this.assertHTML('plain function');
}

@test({ skip: SKIP_DEFAULT_HELPER_MANAGER_TESTS })
'(Default Helper Manager) plain functions track positional args'(assert: Assert) {
let count = 0;
Expand Down
29 changes: 26 additions & 3 deletions packages/@glimmer/manager/lib/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,36 @@ export function getInternalComponentManager(
///////////

export function hasInternalComponentManager(definition: object): boolean {
return getManager(COMPONENT_MANAGERS, definition) !== undefined;
return (
hasDefaultComponentManager(definition) ||
getManager(COMPONENT_MANAGERS, definition) !== undefined
);
}

export function hasInternalHelperManager(definition: object): boolean {
return getManager(HELPER_MANAGERS, definition) !== undefined;
return (
hasDefaultHelperManager(definition) || getManager(HELPER_MANAGERS, definition) !== undefined
);
}

export function hasInternalModifierManager(definition: object): boolean {
return getManager(MODIFIER_MANAGERS, definition) !== undefined;
return (
hasDefaultModifierManager(definition) || getManager(MODIFIER_MANAGERS, definition) !== undefined
);
}

function hasDefaultComponentManager(_definition: object): boolean {
return false;
}

function hasDefaultHelperManager(definition: object): boolean {
if (FEATURE_DEFAULT_HELPER_MANAGER) {
return typeof definition === 'function';
}

return false;
}

function hasDefaultModifierManager(_definition: object): boolean {
return false;
}

0 comments on commit ebdc58d

Please sign in to comment.