Skip to content

Commit

Permalink
Merge pull request #567 from stephensli/eslint-migration
Browse files Browse the repository at this point in the history
Eslint migration
  • Loading branch information
chriseth authored Dec 21, 2021
2 parents f239ac6 + 9af4205 commit d40c3f4
Show file tree
Hide file tree
Showing 18 changed files with 512 additions and 494 deletions.
18 changes: 18 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true
},
extends: [
'standard'
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module'
},
rules: {
semi: ['error', 'always']
},
ignorePatterns: ['dist', 'soljson.js']
};
10 changes: 5 additions & 5 deletions abi.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var semver = require('semver');
const semver = require('semver');

function update (compilerVersion, abi) {
var hasConstructor = false;
var hasFallback = false;
let hasConstructor = false;
let hasFallback = false;

for (var i = 0; i < abi.length; i++) {
var item = abi[i];
for (let i = 0; i < abi.length; i++) {
const item = abi[i];

if (item.type === 'constructor') {
hasConstructor = true;
Expand Down
24 changes: 12 additions & 12 deletions downloadCurrentVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
// This is used to download the correct binary version
// as part of the prepublish step.

var pkg = require('./package.json');
var fs = require('fs');
var https = require('follow-redirects').https;
var MemoryStream = require('memorystream');
var keccak256 = require('js-sha3').keccak256;
const pkg = require('./package.json');
const fs = require('fs');
const https = require('follow-redirects').https;
const MemoryStream = require('memorystream');
const keccak256 = require('js-sha3').keccak256;

function getVersionList (cb) {
console.log('Retrieving available version list...');

var mem = new MemoryStream(null, { readable: false });
const mem = new MemoryStream(null, { readable: false });
https.get('https://solc-bin.ethereum.org/bin/list.json', function (response) {
if (response.statusCode !== 200) {
console.log('Error downloading file: ' + response.statusCode);
Expand All @@ -39,7 +39,7 @@ function downloadBinary (outputName, version, expectedHash) {
process.exit(1);
});

var file = fs.createWriteStream(outputName, { encoding: 'binary' });
const file = fs.createWriteStream(outputName, { encoding: 'binary' });
https.get('https://solc-bin.ethereum.org/bin/' + version, function (response) {
if (response.statusCode !== 200) {
console.log('Error downloading file: ' + response.statusCode);
Expand All @@ -48,7 +48,7 @@ function downloadBinary (outputName, version, expectedHash) {
response.pipe(file);
file.on('finish', function () {
file.close(function () {
var hash = '0x' + keccak256(fs.readFileSync(outputName, { encoding: 'binary' }));
const hash = '0x' + keccak256(fs.readFileSync(outputName, { encoding: 'binary' }));
if (expectedHash !== hash) {
console.log('Hash mismatch: ' + expectedHash + ' vs ' + hash);
process.exit(1);
Expand All @@ -63,13 +63,13 @@ console.log('Downloading correct solidity binary...');

getVersionList(function (list) {
list = JSON.parse(list);
var wanted = pkg.version.match(/^(\d+\.\d+\.\d+)$/)[1];
var releaseFileName = list.releases[wanted];
var expectedFile = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0];
const wanted = pkg.version.match(/^(\d+\.\d+\.\d+)$/)[1];
const releaseFileName = list.releases[wanted];
const expectedFile = list.builds.filter(function (entry) { return entry.path === releaseFileName; })[0];
if (!expectedFile) {
console.log('Version list is invalid or corrupted?');
process.exit(1);
}
var expectedHash = expectedFile.keccak256;
const expectedHash = expectedFile.keccak256;
downloadBinary('soljson.js', releaseFileName, expectedHash);
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var wrapper = require('./wrapper.js');
const wrapper = require('./wrapper.js');

module.exports = wrapper(require('./soljson.js'));
36 changes: 18 additions & 18 deletions linker.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
var assert = require('assert');
var keccak256 = require('js-sha3').keccak256;
const assert = require('assert');
const keccak256 = require('js-sha3').keccak256;

function libraryHashPlaceholder (input) {
return '$' + keccak256(input).slice(0, 34) + '$';
}

var linkBytecode = function (bytecode, libraries) {
const linkBytecode = function (bytecode, libraries) {
assert(typeof bytecode === 'string');
assert(typeof libraries === 'object');
// NOTE: for backwards compatibility support old compiler which didn't use file names
var librariesComplete = {};
for (var libraryName in libraries) {
const librariesComplete = {};
for (const libraryName in libraries) {
if (typeof libraries[libraryName] === 'object') {
// API compatible with the standard JSON i/o
for (var lib in libraries[libraryName]) {
for (const lib in libraries[libraryName]) {
librariesComplete[lib] = libraries[libraryName][lib];
librariesComplete[libraryName + ':' + lib] = libraries[libraryName][lib];
}
} else {
// backwards compatible API for early solc-js versions
var parsed = libraryName.match(/^([^:]+):(.+)$/);
const parsed = libraryName.match(/^([^:]+):(.+)$/);
if (parsed) {
librariesComplete[parsed[2]] = libraries[libraryName];
}
librariesComplete[libraryName] = libraries[libraryName];
}
}

for (libraryName in librariesComplete) {
var hexAddress = librariesComplete[libraryName];
for (const libraryName in librariesComplete) {
let hexAddress = librariesComplete[libraryName];
if (hexAddress.slice(0, 2) !== '0x' || hexAddress.length > 42) {
throw new Error('Invalid address specified for ' + libraryName);
}
Expand All @@ -38,10 +38,10 @@ var linkBytecode = function (bytecode, libraries) {

// Support old (library name) and new (hash of library name)
// placeholders.
var replace = function (name) {
const replace = function (name) {
// truncate to 37 characters
var truncatedName = name.slice(0, 36);
var libLabel = '__' + truncatedName + Array(37 - truncatedName.length).join('_') + '__';
const truncatedName = name.slice(0, 36);
const libLabel = '__' + truncatedName + Array(37 - truncatedName.length).join('_') + '__';
while (bytecode.indexOf(libLabel) >= 0) {
bytecode = bytecode.replace(libLabel, hexAddress);
}
Expand All @@ -54,22 +54,22 @@ var linkBytecode = function (bytecode, libraries) {
return bytecode;
};

var findLinkReferences = function (bytecode) {
const findLinkReferences = function (bytecode) {
assert(typeof bytecode === 'string');
// find 40 bytes in the pattern of __...<36 digits>...__
// e.g. __Lib.sol:L_____________________________
var linkReferences = {};
var offset = 0;
const linkReferences = {};
let offset = 0;
while (true) {
var found = bytecode.match(/__(.{36})__/);
const found = bytecode.match(/__(.{36})__/);
if (!found) {
break;
}

var start = found.index;
const start = found.index;
// trim trailing underscores
// NOTE: this has no way of knowing if the trailing underscore was part of the name
var libraryName = found[1].replace(/_+$/gm, '');
const libraryName = found[1].replace(/_+$/gm, '');

if (!linkReferences[libraryName]) {
linkReferences[libraryName] = [];
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"solcjs": "solcjs"
},
"scripts": {
"lint": "node ./node_modules/semistandard/bin/cmd.js",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"updateBinary": "node downloadCurrentVersion.js && node verifyVersion.js",
"prepublishOnly": "npm run updateBinary",
"pretest": "npm run lint",
Expand Down Expand Up @@ -56,16 +57,15 @@
},
"devDependencies": {
"coveralls": "^3.0.0",
"eslint": "^7.32.0",
"eslint-config-standard": "^16.0.3",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^5.1.1",
"nyc": "^14.1.0",
"semistandard": "^12.0.0",
"tape": "^4.11.0",
"tape-spawn": "^1.4.2"
},
"semistandard": {
"ignore": [
"soljson.js"
]
},
"nyc": {
"exclude": [
"soljson.js"
Expand Down
10 changes: 5 additions & 5 deletions smtchecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
// another run.
// Returns null if no solving is requested.
function handleSMTQueries (inputJSON, outputJSON, solver) {
var auxInputReq = outputJSON.auxiliaryInputRequested;
const auxInputReq = outputJSON.auxiliaryInputRequested;
if (!auxInputReq) {
return null;
}

var queries = auxInputReq.smtlib2queries;
const queries = auxInputReq.smtlib2queries;
if (!queries || Object.keys(queries).length === 0) {
return null;
}

var responses = {};
for (var query in queries) {
const responses = {};
for (const query in queries) {
responses[query] = solver(queries[query]);
}

Expand All @@ -28,7 +28,7 @@ function handleSMTQueries (inputJSON, outputJSON, solver) {
function smtCallback (solver) {
return function (query) {
try {
var result = solver(query);
const result = solver(query);
return { contents: result };
} catch (err) {
return { error: err };
Expand Down
16 changes: 8 additions & 8 deletions smtsolver.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var commandExistsSync = require('command-exists').sync;
var execSync = require('child_process').execSync;
var fs = require('fs');
var tmp = require('tmp');
const commandExistsSync = require('command-exists').sync;
const execSync = require('child_process').execSync;
const fs = require('fs');
const tmp = require('tmp');

const timeout = 10000;

var potentialSolvers = [
const potentialSolvers = [
{
name: 'z3',
params: '-smt2 rlimit=20000000 rewriter.pull_cheap_ite=true fp.spacer.q3.use_qgen=true fp.spacer.mbqi=false fp.spacer.ground_pobs=false'
Expand All @@ -15,21 +15,21 @@ var potentialSolvers = [
params: '--lang=smt2 --tlimit=' + timeout
}
];
var solvers = potentialSolvers.filter(solver => commandExistsSync(solver.name));
const solvers = potentialSolvers.filter(solver => commandExistsSync(solver.name));

function solve (query) {
if (solvers.length === 0) {
throw new Error('No SMT solver available. Assertion checking will not be performed.');
}

var tmpFile = tmp.fileSync({ postfix: '.smt2' });
const tmpFile = tmp.fileSync({ postfix: '.smt2' });
fs.writeFileSync(tmpFile.name, query);
// TODO For now only the first SMT solver found is used.
// At some point a computation similar to the one done in
// SMTPortfolio::check should be performed, where the results
// given by different solvers are compared and an error is
// reported if solvers disagree (i.e. SAT vs UNSAT).
var solverOutput;
let solverOutput;
try {
solverOutput = execSync(
solvers[0].name + ' ' + solvers[0].params + ' ' + tmpFile.name, {
Expand Down
50 changes: 25 additions & 25 deletions test/abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,67 @@ tape('ABI translator', function (t) {
st.end();
});
t.test('0.1.1 (no constructor)', function (st) {
st.deepEqual(abi.update('0.1.1', []), [ { inputs: [], payable: true, stateMutability: 'payable', type: 'constructor' }, { payable: true, stateMutability: 'payable', type: 'fallback' } ]);
st.deepEqual(abi.update('0.1.1', []), [{ inputs: [], payable: true, stateMutability: 'payable', type: 'constructor' }, { payable: true, stateMutability: 'payable', type: 'fallback' }]);
st.end();
});
t.test('0.3.6 (constructor)', function (st) {
var input = [ { inputs: [], type: 'constructor' } ];
st.deepEqual(abi.update('0.3.6', input), [ { inputs: [], payable: true, stateMutability: 'payable', type: 'constructor' }, { payable: true, stateMutability: 'payable', type: 'fallback' } ]);
const input = [{ inputs: [], type: 'constructor' }];
st.deepEqual(abi.update('0.3.6', input), [{ inputs: [], payable: true, stateMutability: 'payable', type: 'constructor' }, { payable: true, stateMutability: 'payable', type: 'fallback' }]);
st.end();
});
t.test('0.3.6 (non-constant function)', function (st) {
var input = [ { inputs: [], type: 'function' } ];
st.deepEqual(abi.update('0.3.6', input), [ { inputs: [], payable: true, stateMutability: 'payable', type: 'function' }, { payable: true, stateMutability: 'payable', type: 'fallback' } ]);
const input = [{ inputs: [], type: 'function' }];
st.deepEqual(abi.update('0.3.6', input), [{ inputs: [], payable: true, stateMutability: 'payable', type: 'function' }, { payable: true, stateMutability: 'payable', type: 'fallback' }]);
st.end();
});
t.test('0.3.6 (constant function)', function (st) {
var input = [ { inputs: [], type: 'function', constant: true } ];
st.deepEqual(abi.update('0.3.6', input), [ { inputs: [], constant: true, stateMutability: 'view', type: 'function' }, { payable: true, stateMutability: 'payable', type: 'fallback' } ]);
const input = [{ inputs: [], type: 'function', constant: true }];
st.deepEqual(abi.update('0.3.6', input), [{ inputs: [], constant: true, stateMutability: 'view', type: 'function' }, { payable: true, stateMutability: 'payable', type: 'fallback' }]);
st.end();
});
t.test('0.3.6 (event)', function (st) {
var input = [ { inputs: [], type: 'event' } ];
st.deepEqual(abi.update('0.3.6', input), [ { inputs: [], type: 'event' }, { payable: true, stateMutability: 'payable', type: 'fallback' } ]);
const input = [{ inputs: [], type: 'event' }];
st.deepEqual(abi.update('0.3.6', input), [{ inputs: [], type: 'event' }, { payable: true, stateMutability: 'payable', type: 'fallback' }]);
st.end();
});
t.test('0.3.6 (has no fallback)', function (st) {
var input = [ { inputs: [], type: 'constructor' } ];
st.deepEqual(abi.update('0.3.6', input), [ { inputs: [], type: 'constructor', payable: true, stateMutability: 'payable' }, { type: 'fallback', payable: true, stateMutability: 'payable' } ]);
const input = [{ inputs: [], type: 'constructor' }];
st.deepEqual(abi.update('0.3.6', input), [{ inputs: [], type: 'constructor', payable: true, stateMutability: 'payable' }, { type: 'fallback', payable: true, stateMutability: 'payable' }]);
st.end();
});
t.test('0.4.0 (has fallback)', function (st) {
var input = [ { inputs: [], type: 'constructor' }, { type: 'fallback' } ];
st.deepEqual(abi.update('0.4.0', input), [ { inputs: [], type: 'constructor', payable: true, stateMutability: 'payable' }, { type: 'fallback', stateMutability: 'nonpayable' } ]);
const input = [{ inputs: [], type: 'constructor' }, { type: 'fallback' }];
st.deepEqual(abi.update('0.4.0', input), [{ inputs: [], type: 'constructor', payable: true, stateMutability: 'payable' }, { type: 'fallback', stateMutability: 'nonpayable' }]);
st.end();
});
t.test('0.4.0 (non-constant function)', function (st) {
var input = [ { inputs: [], type: 'function' } ];
st.deepEqual(abi.update('0.4.0', input), [ { inputs: [], stateMutability: 'nonpayable', type: 'function' } ]);
const input = [{ inputs: [], type: 'function' }];
st.deepEqual(abi.update('0.4.0', input), [{ inputs: [], stateMutability: 'nonpayable', type: 'function' }]);
st.end();
});
t.test('0.4.0 (constant function)', function (st) {
var input = [ { inputs: [], type: 'function', constant: true } ];
st.deepEqual(abi.update('0.4.0', input), [ { inputs: [], constant: true, stateMutability: 'view', type: 'function' } ]);
const input = [{ inputs: [], type: 'function', constant: true }];
st.deepEqual(abi.update('0.4.0', input), [{ inputs: [], constant: true, stateMutability: 'view', type: 'function' }]);
st.end();
});
t.test('0.4.0 (payable function)', function (st) {
var input = [ { inputs: [], payable: true, type: 'function' } ];
st.deepEqual(abi.update('0.4.0', input), [ { inputs: [], payable: true, stateMutability: 'payable', type: 'function' } ]);
const input = [{ inputs: [], payable: true, type: 'function' }];
st.deepEqual(abi.update('0.4.0', input), [{ inputs: [], payable: true, stateMutability: 'payable', type: 'function' }]);
st.end();
});
t.test('0.4.1 (constructor not payable)', function (st) {
var input = [ { inputs: [], payable: false, type: 'constructor' } ];
st.deepEqual(abi.update('0.4.1', input), [ { inputs: [], payable: true, stateMutability: 'payable', type: 'constructor' } ]);
const input = [{ inputs: [], payable: false, type: 'constructor' }];
st.deepEqual(abi.update('0.4.1', input), [{ inputs: [], payable: true, stateMutability: 'payable', type: 'constructor' }]);
st.end();
});
t.test('0.4.5 (constructor payable)', function (st) {
var input = [ { inputs: [], payable: false, type: 'constructor' } ];
st.deepEqual(abi.update('0.4.5', input), [ { inputs: [], payable: false, stateMutability: 'nonpayable', type: 'constructor' } ]);
const input = [{ inputs: [], payable: false, type: 'constructor' }];
st.deepEqual(abi.update('0.4.5', input), [{ inputs: [], payable: false, stateMutability: 'nonpayable', type: 'constructor' }]);
st.end();
});
t.test('0.4.16 (statemutability)', function (st) {
var input = [ { inputs: [], payable: false, stateMutability: 'pure', type: 'function' } ];
st.deepEqual(abi.update('0.4.16', input), [ { inputs: [], payable: false, stateMutability: 'pure', type: 'function' } ]);
const input = [{ inputs: [], payable: false, stateMutability: 'pure', type: 'function' }];
st.deepEqual(abi.update('0.4.16', input), [{ inputs: [], payable: false, stateMutability: 'pure', type: 'function' }]);
st.end();
});
});
Loading

0 comments on commit d40c3f4

Please sign in to comment.