es5-getter-ember-codemod icon indicating copy to clipboard operation
es5-getter-ember-codemod copied to clipboard

Support getWithDefault?

Open simonihmig opened this issue 7 years ago • 6 comments

First of all, this is really nice! :)

As you asked for feedback, how about supporting getWithDefault()?

// Before
person.getWithDefault('lastName', 'Doe');

// After
person.lastName !== undefined ? person.lastName : 'Doe';

simonihmig avatar Feb 07 '18 17:02 simonihmig

@simonihmig I think I spoke too soon. I'm going to mull this one over.

rondale-sc avatar Feb 07 '18 18:02 rondale-sc

My though was that .getWithDefault() is basically syntactic sugar for .get(), so if you want to get rid of .get() completely, you should also remove getWithDefault(). Same with getProperties(), which you already have covered nicely.

simonihmig avatar Feb 07 '18 18:02 simonihmig

@Turbo87 What do you think of this? I wasn't sure if it was in scope for this project, would be easy enough to implement.

rondale-sc avatar Mar 22 '18 17:03 rondale-sc

Should be possible, but I'm unclear about how getWithDefault() determines when to use the default value. Does it use the default value for everything that is falsey? or only for undefined? or something totally different? This has the potential to change behavior which we should try to avoid if possible.

Turbo87 avatar Mar 22 '18 17:03 Turbo87

Ah, agreed. @simonihmig I don't have a lot of experience using getWithDefault do you know the expected behavior in the current implementation?

rondale-sc avatar Mar 22 '18 17:03 rondale-sc

I wasn't sure about that, until I quickly looked it up: 😜 https://github.com/emberjs/ember.js/blob/dd933614629d1ac5cf118181661392faabb53641/packages/ember-metal/lib/property_get.js#L177-L182

So a strict equal check for undefined...

simonihmig avatar Mar 22 '18 17:03 simonihmig