Skip to content
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

Merged
merged 22 commits into from
Feb 24, 2017
Merged

Conversation

mroderick
Copy link
Member

Purpose (TL;DR) - mandatory

Refactor code to use ES5.1 features and avoid many for and for..in loops

How to verify - mandatory

  1. Check out this branch (see github instructions below)
  2. npm install
  3. 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.

@@ -109,7 +107,7 @@ var TYPE_MAP = {
};

function match(expectation, message) {
var m = create(matcher);
var m = Object.create(matcher);
Copy link
Member

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) {
Copy link
Member

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. 👍

@lucasfcosta
Copy link
Member

Awesome work!
The code looks a lot more clear, especially when you substituted the old implementations for the native ones.
Its impressive to see how cleaner it gets when we start using functions instead of regular control structures. It gets even more interesting when you compare it to languages like Go.

Anyway, philosophical reflections aside, this looks awesome.

Copy link
Contributor

@fatso83 fatso83 left a 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");
Copy link
Contributor

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?

Copy link
Member Author

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 :)

Copy link
Member Author

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"....

Copy link
Member

@lucasfcosta lucasfcosta Feb 21, 2017

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.

@@ -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/)) {
Copy link
Member

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.

Copy link
Member Author

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

var l = arguments.length;
if (l > this.args.length) {
var self = this;
var calledWithArgs = Array.prototype.slice.call(arguments);
Copy link
Member

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?

Copy link
Member Author

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

@mroderick
Copy link
Member Author

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.

@coveralls
Copy link

coveralls commented Feb 24, 2017

Coverage Status

Coverage decreased (-0.009%) to 94.856% when pulling 44831d4 on mroderick:use-es5-features into afdf425 on sinonjs:master.

Copy link
Member

@mantoni mantoni left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@mroderick
Copy link
Member Author

Thanks

@mroderick mroderick merged commit 6b13ed7 into sinonjs:master Feb 24, 2017
@mroderick mroderick deleted the use-es5-features branch February 24, 2017 11:14
@erikpukinskis
Copy link

erikpukinskis commented Mar 15, 2017

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.

@fatso83
Copy link
Contributor

fatso83 commented Mar 15, 2017

@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...

@erikpukinskis
Copy link

erikpukinskis commented Mar 17, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants