-
-
Notifications
You must be signed in to change notification settings - Fork 698
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
Add fix suggestions when accessing a nonexistent property in proxy mode #782
Conversation
Great work here @not-an-aardvark! LGTM. This may make you the coolest non-aardvark I've met! @meeber @lucasfcosta thoughts? |
Hi @not-an-aardvark, thanks for your contribution, I'd like to frame this code and hang it on my wall! It looks really elegant! 😄 I've gotta confess I needed half an hour to truly understand how that works, but it was totally worth it. Just in case anyone else in the future also needs some help with this, take a look at this link, it's an excellent learning resource. LGTM! I'm not sure if I should merge this or not since @keithamus has tagged @meeber too. |
Thanks!
In addition, I would also recommend this Wikipedia article on Levenshtein distances. The implementation in this PR is based on the article's definition here. |
Please don't merge yet, I need to explore a few things with this. |
This looks pretty neat. But I think we should reduce the suggestion list to Chai keywords only. If we change
That leaves the following list with three needing to be manually filtered out: [ 'Arguments',
'NaN',
'Throw',
'__methods', # Manually filter out; internal use
'_obj', # Manually filter out; internal use
'a',
'above',
'all',
'an',
'and',
'any',
'approximately',
'arguments',
'assert', # Manually filter out; internal use
'at',
'be',
'been',
'below',
'but',
'by',
'change',
'changes',
'closeTo',
'constructor',
'contain',
'contains',
'decrease',
'decreases',
'deep',
'does',
'empty',
'eq',
'eql',
'eqls',
'equal',
'equals',
'exist',
'extensible',
'false',
'finite',
'frozen',
'greaterThan',
'gt',
'gte',
'has',
'have',
'haveOwnProperty',
'haveOwnPropertyDescriptor',
'include',
'includes',
'increase',
'increases',
'instanceOf',
'instanceof',
'is',
'itself',
'key',
'keys',
'least',
'length',
'lengthOf',
'lessThan',
'lt',
'lte',
'match',
'matches',
'members',
'most',
'nested',
'not',
'null',
'of',
'ok',
'oneOf',
'ordered',
'ownProperty',
'ownPropertyDescriptor',
'property',
'respondTo',
'respondsTo',
'same',
'satisfies',
'satisfy',
'sealed',
'string',
'that',
'throw',
'throws',
'to',
'true',
'undefined',
'which',
'with',
'within' ] |
That works, but it will cause issues if someone tries to subclass As an alternative, we could specifically exclude keys in getProperties(target).filter(function (property) {
return !Object.prototype.hasOwnProperty(property) && ['__flags', '__methods', '_obj', 'assert'].indexOf(property) === -1;
}); |
@not-an-aardvark Alternative looks good. |
f045a57
to
e8085b0
Compare
Updated to use the alternative version, and added a couple tests for it. |
@not-an-aardvark LGTM! Thanks for doing this :D Let's get one more LGTM before merging since there were changes. @keithamus @lucasfcosta? |
LGTM too! |
fixes #771
As suggested in #771, when a nonexistent property is accessed in proxy mode, this computes the levenshtein distance to all possible properties in order to suggest the best fix to the user.
I decided to not suggest a fix if the closest property has a string distance of 4 or more, simply because the suggestion probably will not be very useful at that point.