remove unnecessary if elses

pull/5370/head
Iuri Matias 4 years ago
parent 1adb142b2e
commit 7c52faf4cb
  1. 55
      libs/remix-debug/src/source/astWalker.js
  2. 5
      libs/remix-debug/src/source/sourceLocationTracker.js
  3. 11
      libs/remix-debug/src/source/sourceMappingDecoder.js
  4. 7
      libs/remix-lib/src/storage.js
  5. 45
      libs/remix-lib/src/universalDapp.js
  6. 5
      libs/remix-lib/src/web3Provider/web3Providers.js
  7. 12
      libs/remix-lib/src/web3Provider/web3VmProvider.js

@ -0,0 +1,55 @@
'use strict'
/**
* Crawl the given AST through the function walk(ast, callback)
*/
function AstWalker () {} // eslint-disable-line
/**
* visit all the AST nodes
*
* @param {Object} ast - AST node
* @param {Object or Function} callback - if (Function) the function will be called for every node.
* - if (Object) callback[<Node Type>] will be called for
* every node of type <Node Type>. callback["*"] will be called for all other nodes.
* in each case, if the callback returns false it does not descend into children.
* If no callback for the current type, children are visited.
*/
AstWalker.prototype.walk = function (ast, callback) {
if (callback instanceof Function) {
callback = {'*': callback}
}
if (!('*' in callback)) {
callback['*'] = function () { return true }
}
const nodes = ast.nodes || (ast.body && ast.body.statements) || ast.declarations
if(ast.body && ast.initializationExpression) // 'for' loop handling
nodes.push(ast.initializationExpression)
if (manageCallBack(ast, callback) && nodes && nodes.length > 0) {
for (let k in nodes) {
const child = nodes[k]
this.walk(child, callback)
}
}
}
/**
* walk the given @astList
*
* @param {Object} sourcesList - sources list (containing root AST node)
* @param {Function} - callback used by AstWalker to compute response
*/
AstWalker.prototype.walkAstList = function (sourcesList, callback) {
const walker = new AstWalker()
for (let k in sourcesList) {
walker.walk(sourcesList[k].ast, callback)
}
}
function manageCallBack (node, callback) {
if (node.nodeType in callback) {
return callback[node.nodeType](node)
}
return callback['*'](node)
}
module.exports = AstWalker

@ -88,10 +88,9 @@ function extractSourceMap (self, codeManager, address, contracts) {
const sourceMap = getSourceMap(address, result.bytecode, contracts)
if (sourceMap) {
if (!helper.isContractCreation(address)) self.sourceMapByAddress[address] = sourceMap
resolve(sourceMap)
} else {
reject('no sourcemap associated with the code ' + address)
return resolve(sourceMap)
}
reject('no sourcemap associated with the code ' + address)
}).catch(reject)
})
}

@ -94,12 +94,8 @@ SourceMappingDecoder.prototype.convertOffsetToLineColumn = function (sourceLocat
start: convertFromCharPosition(sourceLocation.start, lineBreakPositions),
end: convertFromCharPosition(sourceLocation.start + sourceLocation.length, lineBreakPositions)
}
} else {
return {
start: null,
end: null
}
}
return {start: null, end: null}
}
/**
@ -119,10 +115,7 @@ function convertFromCharPosition (pos, lineBreakPositions) {
}
const beginColumn = line === 0 ? 0 : (lineBreakPositions[line - 1] + 1)
const column = pos - beginColumn
return {
line: line,
column: column
}
return {line, column}
}
function sourceLocationFromAstNode (astNode) {

@ -27,10 +27,8 @@ function Storage (prefix) {
this.remove = function (name) {
if (typeof window !== 'undefined') {
window.localStorage.removeItem(prefix + name)
return true
} else {
return true
}
return true
}
this.rename = function (originalName, newName) {
@ -46,9 +44,8 @@ function Storage (prefix) {
// NOTE: this is a workaround for some browsers
if (typeof window !== 'undefined') {
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
} else {
return []
}
return []
}
this.keys = function () {

@ -91,10 +91,10 @@ module.exports = class UniversalDApp {
if (!this.config.get('settings/personal-mode')) {
return cb('Not running in personal mode')
}
passwordPromptCb((passphrase) => {
return passwordPromptCb((passphrase) => {
this.executionContext.web3().personal.newAccount(passphrase, cb)
})
} else {
}
let privateKey
do {
privateKey = crypto.randomBytes(32)
@ -102,7 +102,6 @@ module.exports = class UniversalDApp {
this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
}
}
/** Add an account to the list of account (only for Javascript VM) */
_addAccount (privateKey, balance) {
@ -110,7 +109,9 @@ module.exports = class UniversalDApp {
throw new Error('_addAccount() cannot be called in non-VM mode')
}
if (this.accounts) {
if (!this.accounts) {
return
}
privateKey = Buffer.from(privateKey, 'hex')
const address = privateToAddress(privateKey)
@ -126,7 +127,6 @@ module.exports = class UniversalDApp {
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}
}
/** Return the list of accounts */
getAccounts (cb) {
@ -175,36 +175,32 @@ module.exports = class UniversalDApp {
address = stripHexPrefix(address)
if (!this.executionContext.isVM()) {
this.executionContext.web3().eth.getBalance(address, (err, res) => {
return this.executionContext.web3().eth.getBalance(address, (err, res) => {
if (err) {
cb(err)
} else {
cb(null, res.toString(10))
return cb(err)
}
cb(null, res.toString(10))
})
} else {
}
if (!this.accounts) {
return cb('No accounts?')
}
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, res) => {
if (err) {
cb('Account not found')
} else {
cb(null, new BN(res.balance).toString(10))
return cb('Account not found')
}
cb(null, new BN(res.balance).toString(10))
})
}
}
/** Get the balance of an address, and convert wei to ether */
getBalanceInEther (address, callback) {
this.getBalance(address, (error, balance) => {
if (error) {
callback(error)
} else {
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether'))
return callback(error)
}
callback(null, this.executionContext.web3().utils.fromWei(balance, 'ether'))
})
}
@ -219,10 +215,7 @@ module.exports = class UniversalDApp {
* @param {Function} callback - callback.
*/
createContract (data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
this.runTx({data: data, useCall: false}, confirmationCb, continueCb, promptCb, callback)
}
/**
@ -235,10 +228,7 @@ module.exports = class UniversalDApp {
*/
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) {
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure'
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, callback)
}
/**
@ -249,10 +239,7 @@ module.exports = class UniversalDApp {
* @param {Function} callback - callback.
*/
sendRawTransaction (to, data, confirmationCb, continueCb, promptCb, callback) {
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, (error, txResult) => {
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})
this.runTx({to, data, useCall: false}, confirmationCb, continueCb, promptCb, callback)
}
context () {

@ -19,10 +19,9 @@ Web3Providers.prototype.addProvider = function (type, obj) {
Web3Providers.prototype.get = function (type, cb) {
if (this.modes[type]) {
cb(null, this.modes[type])
} else {
cb('error: this provider has not been setup (' + type + ')', null)
return cb(null, this.modes[type])
}
cb('error: this provider has not been setup (' + type + ')', null)
}
Web3Providers.prototype.addWeb3 = function (type, web3) {

@ -211,12 +211,11 @@ web3VmProvider.prototype.traceTransaction = function (txHash, options, cb) {
cb(null, this.vmTraces[txHash])
}
return this.vmTraces[txHash]
} else {
}
if (cb) {
cb('unable to retrieve traces ' + txHash, null)
}
}
}
web3VmProvider.prototype.storageRangeAt = function (blockNumber, txIndex, address, start, maxLength, cb) { // txIndex is the hash in the case of the VM
// we don't use the range params here
@ -232,9 +231,8 @@ web3VmProvider.prototype.storageRangeAt = function (blockNumber, txIndex, addres
storage: JSON.parse(JSON.stringify(storage)),
nextKey: null
})
} else {
cb('unable to retrieve storage ' + txIndex + ' ' + address)
}
cb('unable to retrieve storage ' + txIndex + ' ' + address)
}
web3VmProvider.prototype.getBlockNumber = function (cb) { cb(null, 'vm provider') }
@ -245,12 +243,11 @@ web3VmProvider.prototype.getTransaction = function (txHash, cb) {
cb(null, this.txs[txHash])
}
return this.txs[txHash]
} else {
}
if (cb) {
cb('unable to retrieve tx ' + txHash, null)
}
}
}
web3VmProvider.prototype.getTransactionReceipt = function (txHash, cb) {
// same as getTransaction but return the created address also
@ -259,12 +256,11 @@ web3VmProvider.prototype.getTransactionReceipt = function (txHash, cb) {
cb(null, this.txsReceipt[txHash])
}
return this.txsReceipt[txHash]
} else {
}
if (cb) {
cb('unable to retrieve txReceipt ' + txHash, null)
}
}
}
web3VmProvider.prototype.getTransactionFromBlock = function (blockNumber, txIndex, cb) {
const mes = 'not supposed to be needed by remix in vmmode'

Loading…
Cancel
Save