-
-
Notifications
You must be signed in to change notification settings - Fork 769
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
Refactoring: use ES5.1 features #1271
Conversation
@@ -109,7 +107,7 @@ var TYPE_MAP = { | |||
}; | |||
|
|||
function match(expectation, message) { | |||
var m = create(matcher); | |||
var m = Object.create(matcher); |
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.
The whole code looks much better and less error prone by using a native function instead of reimplementing it.
Well done!
for (var i = 0, l = this.expectedArguments.length; i < l; i += 1) { | ||
|
||
if (!verifyMatcher(this.expectedArguments[i], args[i])) { | ||
expectedArguments.forEach(function (expectedArgument, i) { |
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.
This looks a lot cleaner too. 👍
Awesome work! Anyway, philosophical reflections aside, this looks awesome. |
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.
Everything looks good, just wondering about the every
bit.
@@ -8,7 +8,6 @@ | |||
*/ | |||
"use strict"; | |||
|
|||
var create = require("./util/core/create"); | |||
var deepEqual = require("./util/core/deep-equal").use(match); // eslint-disable-line no-use-before-define | |||
var every = require("./util/core/every"); |
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.
Why can't we remove every use of every
as well in favor of Array#every
? Are they not doing the same thing?
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 am working on it :)
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.
It'll require a bit of refactoring of the code, as the every
function is designed to also work with Object
, because someone decided that they are "iterables"....
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.
Yeah, I remember we came to this conclusion when we talked about Array-like objects and Maps and Sets.
In this comment I talk about it. Actually this method is really useful when it comes to iterating through Map
and Set
objects in order to be able to check all of their elements for our related matchers. It is not a simple re-implementation of the original every
function, it is an slightly modified version to work with these kinds of objects.
Instead use Object.keys defined in ES5.1 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
ES5.1 defines Array.prototype.filter
931e667
to
4b4d0c0
Compare
lib/sinon/behavior.js
Outdated
@@ -377,11 +377,11 @@ function createAsyncVersion(syncFnName) { | |||
} | |||
|
|||
// create asynchronous versions of callsArg* and yields* methods | |||
for (var method in proto) { | |||
Object.keys(proto).forEach(function (method) { | |||
// need to avoid creating anotherasync versions of the newly added async methods | |||
if (proto.hasOwnProperty(method) && method.match(/^(callsArg|yields)/) && !method.match(/Async/)) { |
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.
You can remove the hasOwnProperty
check here.
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 pushed a review commit that removes it. Once we're happy with the PR, I'll rebase and squash this commit
lib/sinon/call.js
Outdated
var l = arguments.length; | ||
if (l > this.args.length) { | ||
var self = this; | ||
var calledWithArgs = Array.prototype.slice.call(arguments); |
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.
There is a slice
variable at the top of this file because it's used so frequently. Do you prefer using the lengthy version to be more explicit?
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 pushed a commit that uses the slice
convenience variable
ES5.1 defines Array.prototype.reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
ES5.1 defines Array.prototype.every https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
ES5.1 defines Array.prototype.map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
4b38568
to
44831d4
Compare
I have rebased and squashed the changes from code review into the relevant commits so the history reads better. I think this is good to merge. I would like to get this merged before the weekend, so we will have a cleaner codebase to work with. |
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.
LGTM 👍
Thanks |
Thank you everyone for your hard work on Sinon these years! It's been great for me, but now that you are leaving Node for IO.js, I must part ways! You were one of the last few modules to keep supporting Node, which I appreciate greatly. |
@erikpukinskis what are you referring to? Doesn't make much sense to me. All code in Sinon should run on pretty much any JS engine for the last 7 years as we only rely on ES5.1 constructs. And IO.js was merged into Node and no longer exists so... |
Hm. I think I got confused because sinon-chai hasn't been ported to sinon-2.0 yet, so I was getting npm errors, and I'm used to being dropped for runtime reasons! Looks like I can live a little longer on my old systems! I am still on old pre-IO.js versions of Node. I don't support the direction the ES6 community is going. I think it's important to maintain support for the old JavaScript runtimes. I appreciate your efforts at backwards compatibility. |
Purpose (TL;DR) - mandatory
Refactor code to use ES5.1 features and avoid many
for
andfor..in
loopsHow to verify - mandatory
npm install
npm test
Note
This PR is not ready to be merged yet, I haven't removed all the loops yet.
I would like to group the changes better into fewer commits (related changes together), but my brain is too tired to finish it tonight.