Skip to content
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

Support lexical scope in loose mode #1351

Merged
merged 2 commits into from
Oct 21, 2021
Merged
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
24 changes: 17 additions & 7 deletions packages/@glimmer/integration-tests/lib/test-helpers/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,29 @@ class FunctionalModifierManager implements ModifierManager<SimpleModifierState>
const FUNCTIONAL_MODIFIER_MANAGER = new FunctionalModifierManager();
const FUNCTIONAL_MODIFIER_MANAGER_FACTORY = () => FUNCTIONAL_MODIFIER_MANAGER;

export interface DefineComponentOptions {
// defaults to templateOnlyComponent
definition?: object;

// defaults to true when some scopeValues are passed and false otherwise
strictMode?: boolean;
}

export function defineComponent(
scopeValues: Record<string, unknown> | null,
templateSource: string,
definition: object = templateOnlyComponent()
options: DefineComponentOptions = {}
) {
let templateFactory = createTemplate(
templateSource,
{ strictMode: scopeValues !== null },
scopeValues ?? {}
);
let strictMode: boolean;
if (typeof options.strictMode === 'boolean') {
strictMode = options.strictMode;
} else {
strictMode = scopeValues !== null;
}

let definition = options.definition ?? templateOnlyComponent();
let templateFactory = createTemplate(templateSource, { strictMode }, scopeValues ?? {});
setComponentTemplate(templateFactory, definition);

return definition;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ class DynamicHelpersResolutionModeTest extends RenderTest {
'Can use a dynamic helper with nested helpers'() {
const foo = defineSimpleHelper(() => 'world!');
const bar = defineSimpleHelper((value: string) => 'Hello, ' + value);
const Bar = defineComponent(
{ foo },
'{{this.bar (foo)}}',
class extends GlimmerishComponent {
const Bar = defineComponent({ foo }, '{{this.bar (foo)}}', {
definition: class extends GlimmerishComponent {
bar = bar;
}
);
},
});

this.renderComponent(Bar);
this.assertHTML('Hello, world!');
Expand All @@ -41,14 +39,12 @@ class DynamicHelpersResolutionModeTest extends RenderTest {
'Can use a dynamic helper with nested dynamic helpers'() {
const foo = defineSimpleHelper(() => 'world!');
const bar = defineSimpleHelper((value: string) => 'Hello, ' + value);
const Bar = defineComponent(
{},
'{{this.bar (this.foo)}}',
class extends GlimmerishComponent {
const Bar = defineComponent({}, '{{this.bar (this.foo)}}', {
definition: class extends GlimmerishComponent {
foo = foo;
bar = bar;
}
);
},
});

this.renderComponent(Bar);
this.assertHTML('Hello, world!');
Expand Down
17 changes: 17 additions & 0 deletions packages/@glimmer/integration-tests/test/lexical-scope-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RenderTest, test, jitSuite, defineComponent } from '..';

class LexicalScopeTest extends RenderTest {
static suiteName = 'loose mode: lexical scope';

@test
'Can use a component in scope'() {
const Foo = defineComponent({}, 'Hello, world!', { strictMode: false });
const Bar = defineComponent({ Foo }, '<Foo/>', { strictMode: false });

this.renderComponent(Bar);
this.assertHTML('Hello, world!');
this.assertStableRerender();
}
}

jitSuite(LexicalScopeTest);
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,11 @@ class DynamicModifiersResolutionModeTest extends RenderTest {
const bar = defineSimpleModifier(
(element: Element, value: string) => (element.innerHTML = value)
);
const Bar = defineComponent(
{ foo },
'<div {{this.bar (foo)}}></div>',
class extends GlimmerishComponent {
const Bar = defineComponent({ foo }, '<div {{this.bar (foo)}}></div>', {
definition: class extends GlimmerishComponent {
bar = bar;
}
);
},
});

this.renderComponent(Bar);
this.assertHTML('<div>Hello, world!</div>');
Expand All @@ -159,14 +157,12 @@ class DynamicModifiersResolutionModeTest extends RenderTest {
const bar = defineSimpleModifier(
(element: Element, value: string) => (element.innerHTML = value)
);
const Bar = defineComponent(
{},
'<div {{this.bar (this.foo)}}></div>',
class extends GlimmerishComponent {
const Bar = defineComponent({}, '<div {{this.bar (this.foo)}}></div>', {
definition: class extends GlimmerishComponent {
foo = foo;
bar = bar;
}
);
},
});

this.renderComponent(Bar);
this.assertHTML('<div>Hello, world!</div>');
Expand Down
20 changes: 8 additions & 12 deletions packages/@glimmer/integration-tests/test/owner-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,23 @@ class MountManager implements WithCreateInstance<object>, WithSubOwner<object> {
setInternalComponentManager(new MountManager(), MountComponent);

function defineMountComponent(owner: object, scope: Record<string, unknown>, template: string) {
return defineComponent(
scope,
template,
class extends MountComponent {
return defineComponent(scope, template, {
definition: class extends MountComponent {
static owner = owner;
}
);
},
});
}

function defineCheckOwnerComponent(ownerToCheck: object | undefined, assert: Assert) {
return defineComponent(
{},
'{{yield}}',
class extends GlimmerishComponent {
return defineComponent({}, '{{yield}}', {
definition: class extends GlimmerishComponent {
constructor(owner: object, args: Record<string, unknown>) {
super(owner, args);

assert.equal(owner, ownerToCheck, 'owner is correct');
}
}
);
},
});
}

class OwnerTest extends RenderTest {
Expand Down
Loading