Skip to content

Commit

Permalink
Added eslint for promises. (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jul 3, 2018
1 parent 7ac8cb6 commit 4514229
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 138 deletions.
31 changes: 31 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"node": true
},
"extends": [
// "eslint:recommended",
// "plugin:promise/recommended"
],
"parserOptions": {
"ecmaVersion": 5
},
"plugins": [
"promise"
],
"rules": {
"promise/always-return": "error",
"promise/no-return-wrap": "error",
"promise/param-names": "error",
"promise/catch-or-return": "error",
"promise/no-native": "off",
// "promise/no-nesting": "warn",
"promise/no-promise-in-callback": "warn",
"promise/no-callback-in-promise": "warn",
// "promise/avoid-new": "warn",
"promise/no-new-statics": "error",
"promise/no-return-in-finally": "warn",
"promise/valid-params": "warn"
}
};
15 changes: 12 additions & 3 deletions contracts/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,15 @@ var Contract = /** @class */ (function () {
return;
}
properties_1.defineReadOnly(this, 'address', addressOrName);
properties_1.defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName));
properties_1.defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName).then(function (address) {
if (address == null) {
throw new Error('name not found');
}
return address;
}).catch(function (error) {
console.log('ERROR: Cannot find Contract - ' + addressOrName);
throw error;
}));
Object.keys(this.interface.functions).forEach(function (name) {
var run = runMethod(_this, name, false);
if (_this[name] == null) {
Expand All @@ -209,7 +217,7 @@ var Contract = /** @class */ (function () {
contract.addressPromise.then(function (address) {
// Not meant for us (the topics just has the same name)
if (address != log.address) {
return;
return null;
}
try {
var result = eventInfo.decode(log.data, log.topics);
Expand All @@ -233,7 +241,8 @@ var Contract = /** @class */ (function () {
setTimeout(function () { onerror_1(error); });
}
}
});
return null;
}).catch(function (error) { });
}
var property = {
enumerable: true,
Expand Down
92 changes: 48 additions & 44 deletions dist/ethers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9039,7 +9039,15 @@ var Contract = /** @class */ (function () {
return;
}
properties_1.defineReadOnly(this, 'address', addressOrName);
properties_1.defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName));
properties_1.defineReadOnly(this, 'addressPromise', this.provider.resolveName(addressOrName).then(function (address) {
if (address == null) {
throw new Error('name not found');
}
return address;
}).catch(function (error) {
console.log('ERROR: Cannot find Contract - ' + addressOrName);
throw error;
}));
Object.keys(this.interface.functions).forEach(function (name) {
var run = runMethod(_this, name, false);
if (_this[name] == null) {
Expand All @@ -9061,7 +9069,7 @@ var Contract = /** @class */ (function () {
contract.addressPromise.then(function (address) {
// Not meant for us (the topics just has the same name)
if (address != log.address) {
return;
return null;
}
try {
var result = eventInfo.decode(log.data, log.topics);
Expand All @@ -9085,7 +9093,8 @@ var Contract = /** @class */ (function () {
setTimeout(function () { onerror_1(error); });
}
}
});
return null;
}).catch(function (error) { });
}
var property = {
enumerable: true,
Expand Down Expand Up @@ -9796,6 +9805,7 @@ var EtherscanProvider = /** @class */ (function (_super) {
return self.getTransaction(log.transactionHash).then(function (tx) {
txs[log.transactionHash] = tx.blockHash;
log.blockHash = tx.blockHash;
return null;
});
}
return null;
Expand Down Expand Up @@ -9959,12 +9969,12 @@ var FallbackProvider = /** @class */ (function (_super) {
}
var provider = providers.shift();
provider.perform(method, params).then(function (result) {
resolve(result);
}, function (error) {
return resolve(result);
}).catch(function (error) {
if (!firstError) {
firstError = error;
}
next();
setTimeout(next, 0);
});
}
next();
Expand Down Expand Up @@ -10260,8 +10270,9 @@ var JsonRpcProvider = /** @class */ (function (_super) {
var ready = new Promise(function (resolve, reject) {
setTimeout(function () {
_this.send('net_version', []).then(function (result) {
var chainId = parseInt(result);
resolve(networks_1.getNetwork(chainId));
return resolve(networks_1.getNetwork(parseInt(result)));
}).catch(function (error) {
reject(error);
});
});
});
Expand Down Expand Up @@ -10361,6 +10372,7 @@ var JsonRpcProvider = /** @class */ (function (_super) {
seq = seq.then(function () {
return self.getTransaction(hash).then(function (tx) {
self.emit('pending', tx);
return null;
});
});
});
Expand All @@ -10373,11 +10385,12 @@ var JsonRpcProvider = /** @class */ (function (_super) {
return;
}
setTimeout(function () { poll(); }, 0);
});
return null;
}).catch(function (error) { });
}
poll();
return filterId;
});
}).catch(function (error) { });
};
JsonRpcProvider.prototype._stopPending = function () {
this._pendingFilter = null;
Expand Down Expand Up @@ -11064,7 +11077,8 @@ var Provider = /** @class */ (function () {
}
_this._emitted['t:' + event.hash.toLowerCase()] = receipt.blockNumber;
_this.emit(event.hash, receipt);
});
return null;
}).catch(function (error) { });
}
else if (event.type === 'address') {
if (_this._balances[event.address]) {
Expand All @@ -11077,7 +11091,8 @@ var Provider = /** @class */ (function () {
}
this._balances[event.address] = balance;
this.emit(event.address, balance);
});
return null;
}).catch(function (error) { });
}
else if (event.type === 'topic') {
_this.getLogs({
Expand All @@ -11093,12 +11108,14 @@ var Provider = /** @class */ (function () {
_this._emitted['t:' + log.transactionHash.toLowerCase()] = log.blockNumber;
_this.emit(event.topic, log);
});
});
return null;
}).catch(function (error) { });
}
});
_this._lastBlockNumber = blockNumber;
_this._balances = newBalances;
});
return null;
}).catch(function (error) { });
this.doPoll();
};
Provider.prototype.resetEventsBlock = function (blockNumber) {
Expand Down Expand Up @@ -11449,6 +11466,7 @@ var Provider = /** @class */ (function () {
}
promises.push(this.resolveName(result[key]).then(function (address) {
result[key] = address;
return;
}));
}, this);
return Promise.all(promises).then(function () { return result; });
Expand Down Expand Up @@ -13490,6 +13508,7 @@ function resolveProperties(object) {
if (value instanceof Promise) {
promises.push(value.then(function (value) {
result[key] = value;
return null;
}));
}
else {
Expand Down Expand Up @@ -14378,7 +14397,7 @@ function poll(func, options) {
}
var attempt = 0;
function check() {
func().then(function (result) {
return func().then(function (result) {
// If we have a result, or are allowed null then we're done
if (result !== undefined) {
if (cancel()) {
Expand All @@ -14400,6 +14419,7 @@ function poll(func, options) {
}
setTimeout(check, timeout);
}
return null;
}, function (error) {
if (cancel()) {
reject(error);
Expand Down Expand Up @@ -15315,37 +15335,21 @@ var Wallet = /** @class */ (function (_super) {
return Wallet.fromMnemonic(mnemonic, options.path, options.locale);
};
Wallet.fromEncryptedJson = function (json, password, progressCallback) {
if (progressCallback && typeof (progressCallback) !== 'function') {
throw new Error('invalid callback');
}
return new Promise(function (resolve, reject) {
if (secretStorage.isCrowdsaleWallet(json)) {
try {
var privateKey = secretStorage.decryptCrowdsale(json, password);
resolve(new Wallet(privateKey));
}
catch (error) {
reject(error);
}
}
else if (secretStorage.isValidWallet(json)) {
secretStorage.decrypt(json, password, progressCallback).then(function (signingKey) {
var wallet = new Wallet(signingKey);
/*
if (signingKey.mnemonic && signingKey.path) {
wallet.mnemonic = signingKey.mnemonic;
wallet.path = signingKey.path;
}
*/
resolve(wallet);
}, function (error) {
reject(error);
});
if (secretStorage.isCrowdsaleWallet(json)) {
try {
var privateKey = secretStorage.decryptCrowdsale(json, password);
return Promise.resolve(new Wallet(privateKey));
}
else {
reject('invalid wallet JSON');
catch (error) {
return Promise.reject(error);
}
});
}
else if (secretStorage.isValidWallet(json)) {
return secretStorage.decrypt(json, password, progressCallback).then(function (signingKey) {
return new Wallet(signingKey);
});
}
return Promise.reject('invalid wallet JSON');
};
Wallet.fromMnemonic = function (mnemonic, path, wordlist) {
if (!path) {
Expand Down
2 changes: 1 addition & 1 deletion dist/ethers.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ethers.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions providers/etherscan-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ var EtherscanProvider = /** @class */ (function (_super) {
return self.getTransaction(log.transactionHash).then(function (tx) {
txs[log.transactionHash] = tx.blockHash;
log.blockHash = tx.blockHash;
return null;
});
}
return null;
Expand Down
6 changes: 3 additions & 3 deletions providers/fallback-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ var FallbackProvider = /** @class */ (function (_super) {
}
var provider = providers.shift();
provider.perform(method, params).then(function (result) {
resolve(result);
}, function (error) {
return resolve(result);
}).catch(function (error) {
if (!firstError) {
firstError = error;
}
next();
setTimeout(next, 0);
});
}
next();
Expand Down
11 changes: 7 additions & 4 deletions providers/json-rpc-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ var JsonRpcProvider = /** @class */ (function (_super) {
var ready = new Promise(function (resolve, reject) {
setTimeout(function () {
_this.send('net_version', []).then(function (result) {
var chainId = parseInt(result);
resolve(networks_1.getNetwork(chainId));
return resolve(networks_1.getNetwork(parseInt(result)));
}).catch(function (error) {
reject(error);
});
});
});
Expand Down Expand Up @@ -282,6 +283,7 @@ var JsonRpcProvider = /** @class */ (function (_super) {
seq = seq.then(function () {
return self.getTransaction(hash).then(function (tx) {
self.emit('pending', tx);
return null;
});
});
});
Expand All @@ -294,11 +296,12 @@ var JsonRpcProvider = /** @class */ (function (_super) {
return;
}
setTimeout(function () { poll(); }, 0);
});
return null;
}).catch(function (error) { });
}
poll();
return filterId;
});
}).catch(function (error) { });
};
JsonRpcProvider.prototype._stopPending = function () {
this._pendingFilter = null;
Expand Down
13 changes: 9 additions & 4 deletions providers/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ var Provider = /** @class */ (function () {
}
_this._emitted['t:' + event.hash.toLowerCase()] = receipt.blockNumber;
_this.emit(event.hash, receipt);
});
return null;
}).catch(function (error) { });
}
else if (event.type === 'address') {
if (_this._balances[event.address]) {
Expand All @@ -586,7 +587,8 @@ var Provider = /** @class */ (function () {
}
this._balances[event.address] = balance;
this.emit(event.address, balance);
});
return null;
}).catch(function (error) { });
}
else if (event.type === 'topic') {
_this.getLogs({
Expand All @@ -602,12 +604,14 @@ var Provider = /** @class */ (function () {
_this._emitted['t:' + log.transactionHash.toLowerCase()] = log.blockNumber;
_this.emit(event.topic, log);
});
});
return null;
}).catch(function (error) { });
}
});
_this._lastBlockNumber = blockNumber;
_this._balances = newBalances;
});
return null;
}).catch(function (error) { });
this.doPoll();
};
Provider.prototype.resetEventsBlock = function (blockNumber) {
Expand Down Expand Up @@ -958,6 +962,7 @@ var Provider = /** @class */ (function () {
}
promises.push(this.resolveName(result[key]).then(function (address) {
result[key] = address;
return;
}));
}, this);
return Promise.all(promises).then(function () { return result; });
Expand Down
Loading

0 comments on commit 4514229

Please sign in to comment.