-
-
Notifications
You must be signed in to change notification settings - Fork 699
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
Un-deprecate length
and add guard
#897
Conversation
- The method part of the `length` assertion was slated for deprecation due to a compatibility issue in legacy environments. The decision to deprecate `length` was reversed per chaijs#684. This commit replaces the deprecation notice with a recommendation to favor `lengthOf` over `length` due to the compatibility issue. - The `lengthOf` assertion wasn't a true alias of `length` because it was a method assertion instead of a chainable method assertion. This commit changes `lengthOf` into a chainable method assertion that's identical to `length`, and updates tests and docs accordingly. - Updates docs to classify `length` as an alias of `lengthOf`. - Updates docs of related assertions to use `lengthOf` instead of `length`.
Great work, @meeber. However, I think trapping EDIT: why that's better:
|
@shvaikalesh Seems like a good suggestion, even if only for the benefit of "Works down to Node.js 4". Can you elaborate on the third point about optimization? |
Awesome work! Thanks @meeber! However, due to the fact that older browsers don't have neither |
@lucasfcosta Just to make sure we're on the same page regarding your second point: With this PR, the proxy-based |
Right now, we wrap every NowCorrect method
Misspelled method
SuggestedCorrect method
Misspelled method
EDIT: oops, sent too early. So, by putting proxies upper on the prototype chain, |
@shvaikalesh Gotcha. Seems like another strong reason to limit proxy protection to undefined properties. |
8cfcf1b
to
3397835
Compare
Pushed a new version of this that uses getters instead of proxies. |
var config = require('../config'); | ||
|
||
var fnDesc = Object.getOwnPropertyDescriptor(function () {}, 'length'); | ||
var isFnLengthConfigurable = typeof fnDesc === 'object' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Descriptor is guaranteed to be an object here.
*/ | ||
|
||
module.exports = function addLengthGuard (fn, assertionName, isChainable) { | ||
if (isFnLengthConfigurable !== true) return fn; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can do if (!fnDesc.configurable)
here?
|
||
Object.defineProperty(fn, 'length', { | ||
get: function () { | ||
if (isChainable === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if (isChainable)
will suffice?
Updated! |
|
||
describe('addLengthGuard', function () { | ||
var fnDesc = Object.getOwnPropertyDescriptor(function () {}, 'length'); | ||
if (typeof fnDesc !== 'object' || fnDesc.configurable !== true) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've missed this before. I believe, if (!fnDesc.configurable)
would be enough.
When the `length` assertion is chained directly off of an uninvoked method, it references `function`'s built-in `length` property instead of Chai's `length` assertion. This commit adds a guard to Chai methods to detect this problem and throw a helpful error message that advises the user on how to correct it.
Gottem. |
Thanks for addressing the issues, @meeber! LGTM. PS. Maybe we should change PR title to reflect the changes? |
length
and add proxy protectionlength
and add guard
This LGTM too. I'll force a merge, LGTM.co is no longer working so we need to disable and find a workable alternative. |
This PR is broken into two commits. The first commit makes
length
andlengthOf
true aliases, with documentation updated to remove the deprecation notice and favorlengthOf
overlength
. The second commit adds a guard against chaininglength
directly off of an uninvoked method assertion.length
as assertion =>lengthOf
#841