Skip to content

Conversation

ekashida
Copy link
Member

@ekashida ekashida commented Jul 6, 2020

Details

Fixes #1033 by implementing the proposed fix in #1496.

Does this PR introduce breaking changes?

  • No, it does not introduce breaking changes.

GUS work item

W-7111501

@@ -44,6 +44,9 @@ export function registerComponent(
Ctor: ComponentConstructor,
{ name, tmpl: template }: { name: string; tmpl: Template }
Copy link
Member

Choose a reason for hiding this comment

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

Based on the types, name can't be undefined. The type definition needs to be realigned.

pmdartus
pmdartus previously approved these changes Jul 6, 2020
): ComponentConstructor {
if (isUndefined(name)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

any particular reason for the compiler to provide an undefined name?

Copy link
Member

Choose a reason for hiding this comment

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

It has never been passed as far as I can tell:

return t.callExpression(id, [
node,
t.objectExpression([t.objectProperty(t.identifier('tmpl'), templateIdentifier)]),
]);
.

At best the compiler will be able to set the name as Ctor.name. IMO we should remove the optional name and just retrieve it from the Ctor.name.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agreed with that assessment because the compiler can't take it out of the Ctor, because it might be circular, and who knows what other quirk in platform. I'm ok with the runtime inference here.

@pmdartus pmdartus dismissed their stale review July 6, 2020 15:11

Outdated based on Caridy's comment.

const elm = createElement('x-anonymous', { is: Anonymous });
expect(elm.getToString()).toBe('[object BaseLightningElement]');
expect(elm.getToString()).toBe('[object BaseLightningElementConstructor]');
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure what to do here, the this value of a component is different in compat mode so String(this) returns _class.

@@ -237,16 +230,7 @@ export function getComponentInternalDef(Ctor: unknown, name?: string): Component
);
}

let meta = getComponentRegisteredMeta(Ctor);
if (isUndefined(meta)) {
// TODO [#1295]: remove this workaround after refactoring tests
Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks again, @ravijayaramappa!

@ekashida
Copy link
Member Author

ekashida commented Jul 7, 2020

@pmdartus @caridy ready for another pass

expect(elm.getToString()).toBe('[object BaseLightningElement]');
});
if (process.env.COMPAT !== true) {
it('should fallback to BaseLightningElementConstructor if constructor has no name', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

The constructor name ends up as _class in compat mode. This seems fine, given that it's no worse than the undefined that we render today.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm ok with that. Who cares about compat :)?

}

def = createComponentDef(Ctor, meta, name || Ctor.name);
def = createComponentDef(Ctor, name || Ctor.name);
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we need the second argument? if we are going to do that in createComponentDef?

@@ -96,11 +91,9 @@ function getCtorProto(Ctor: any, subclassComponentName: string): ComponentConstr

function createComponentDef(
Ctor: ComponentConstructor,
meta: ComponentMeta,
subclassComponentName: string
Copy link
Collaborator

Choose a reason for hiding this comment

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

what is the purpose of subclassComponentName at this point?

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like we were just passing around so that it could be used in some error logs inside getCtorProto(). I went ahead and removed all that!

Copy link
Collaborator

@caridy caridy left a comment

Choose a reason for hiding this comment

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

few minor questions... looking good.

Copy link
Member

@pmdartus pmdartus left a comment

Choose a reason for hiding this comment

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

I have some tiny type changes. Otherwise, the code looks great.

@@ -68,11 +63,11 @@ export interface ComponentDef {

const CtorToDefMap: WeakMap<any, ComponentDef> = new WeakMap();

function getCtorProto(Ctor: any, subclassComponentName: string): ComponentConstructor {
function getCtorProto(Ctor: any): ComponentConstructor {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
function getCtorProto(Ctor: any): ComponentConstructor {
function getCtorProto(Ctor: ComponentConstructor): ComponentConstructor {

Comment on lines 122 to 123
const superDef: ComponentDef =
(superProto as any) !== BaseLightningElement
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const superDef: ComponentDef =
(superProto as any) !== BaseLightningElement
const superDef =
superProto !== BaseLightningElement

const superDef: ComponentDef =
(superProto as any) !== BaseLightningElement
? getComponentInternalDef(superProto, subclassComponentName)
? getComponentInternalDef(superProto)
: lightingElementDef;
const SuperBridge = isNull(superDef) ? BaseBridgeElement : superDef.bridge;
Copy link
Member

Choose a reason for hiding this comment

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

Based on the types, superDef can't be null here anymore.

Suggested change
const SuperBridge = isNull(superDef) ? BaseBridgeElement : superDef.bridge;
const SuperBridge = superDef.bridge;

Copy link
Collaborator

@caridy caridy left a comment

Choose a reason for hiding this comment

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

LGTM

@ekashida ekashida merged commit e94ec15 into master Jul 9, 2020
@ekashida ekashida deleted the ekashida/fix-to-string branch July 9, 2020 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LightningElement.toString always returns undefined
3 participants