Skip to content
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
8 changes: 4 additions & 4 deletions packages/lwc-engine/src/framework/__tests__/secure.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { secure, createElement, LightningElement } from '../main';
import { createElement, LightningElement } from '../main';

describe('secure', () => {
it('forbidden access to template', () => {
Expand All @@ -14,9 +14,9 @@ describe('secure', () => {
return html;
}
}
const elmt = createElement('x-foo', { is: Foo });
const elm = createElement('x-foo', { is: Foo });
expect(() => {
document.body.appendChild(elmt);
}).toThrowError('Unknown template');
document.body.appendChild(elm);
}).toThrowError('The template rendered by [object:vm Foo (1)] must return an imported template tag (e.g.: `import html from "./Foo.html"`) or undefined, instead, it has returned a function');
});
});
2 changes: 1 addition & 1 deletion packages/lwc-engine/src/framework/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function invokeComponentRenderMethod(vm: VM): VNodes {
result = evaluateTemplate(vm, html);
} else if (!isUndefined(html)) {
if (process.env.NODE_ENV !== 'production') {
assert.fail(`The template rendered by ${vm} must return an imported template tag (e.g.: \`import html from "./mytemplate.html"\`) or undefined, instead, it has returned ${html}.`);
assert.fail(`The template rendered by ${vm} must return an imported template tag (e.g.: \`import html from "./${vm.def.name}.html"\`) or undefined, instead, it has returned ${html}.`);
}
}
} catch (e) {
Expand Down
16 changes: 8 additions & 8 deletions packages/lwc-engine/src/framework/secure-template.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Template } from "./template";

const VERIFIED_TEMPLATE_SET = new Set();
const signedTemplateSet: Set<Template> = new Set();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just renaming and typing


export function isTemplateRegistered(tmpl: Template) {
if (!VERIFIED_TEMPLATE_SET.has(tmpl)) {
throw new TypeError('Unknown template');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving the throwing logic to the invoker of this method to we have access to the vm, and other details of the component to provide better message.

}
export function isTemplateRegistered(tpl: Template): boolean {
return signedTemplateSet.has(tpl);
}

export function registerTemplate(tmpl: Template): Template {
VERIFIED_TEMPLATE_SET.add(tmpl);
return tmpl;
// chaining this method as a way to wrap existing
// assignment of templates easily, without too much transformation
export function registerTemplate(tpl: Template): Template {
signedTemplateSet.add(tpl);
return tpl;
}
14 changes: 11 additions & 3 deletions packages/lwc-engine/src/framework/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export interface Template {
* This attribute is used for style encapsulation when the engine runs in fallback mode.
*/
shadowToken?: string;

/**
* List of property names that are accessed of the component instance
* from the template.
*/
ids?: string[];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this interface was incompleted, surfaced the issue after adding the proper type to the html argument in validateFields

}
const EmptySlots: SlotSet = create(null);

Expand All @@ -44,7 +50,7 @@ function validateSlots(vm: VM, html: any) {
}
}

function validateFields(vm: VM, html: any) {
function validateFields(vm: VM, html: Template) {
if (process.env.NODE_ENV === 'production') {
// this method should never leak to prod
throw new ReferenceError();
Expand Down Expand Up @@ -99,8 +105,10 @@ export function evaluateTemplate(vm: VM, html: Template): Array<VNode|null> {
resetShadowRoot(vm);
}

// Check that the template is built by the compiler
isTemplateRegistered(html);
// Check that the template was built by the compiler
if (!isTemplateRegistered(html)) {
throw new ReferenceError(`The template rendered by ${vm} must return an imported template tag (e.g.: \`import html from "./${vm.def.name}.html"\`) or undefined, instead, it has returned a function.`);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to add the component stack here since the invocation of this function is inside a try/catch in invoker, that's the one that will add the error details.

}

vm.cmpTemplate = html;

Expand Down