Skip to content

Commit b9a75e1

Browse files
authored
Merge pull request #566 from stephensli/typescript-support
Typescript Base Support
2 parents 2da719d + 70b489b commit b9a75e1

27 files changed

+211
-126
lines changed

.eslintrc.js

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ module.exports = {
77
extends: [
88
'standard'
99
],
10+
parser: '@typescript-eslint/parser',
11+
plugins: [
12+
'@typescript-eslint'
13+
],
1014
parserOptions: {
1115
ecmaVersion: 12,
1216
sourceType: 'module'

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ bin
3939
out
4040
*.bin
4141
*.abi
42+
43+
dist/**
44+
45+
.nyc_output

.nycrc

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"exclude": [
3+
"coverage",
4+
"dist/soljson.js",
5+
"**/test/**"
6+
],
7+
"extensions": [
8+
".js"
9+
],
10+
"report-dir": "./coverage",
11+
"reporter": [
12+
"lcov",
13+
"html",
14+
"text-summary"
15+
],
16+
"temp-directory": "./coverage/.nyc_output"
17+
}

abi.js renamed to abi.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const semver = require('semver');
1+
import * as semver from 'semver';
22

33
function update (compilerVersion, abi) {
44
let hasConstructor = false;
@@ -58,6 +58,6 @@ function update (compilerVersion, abi) {
5858
return abi;
5959
}
6060

61-
module.exports = {
62-
update: update
61+
export default {
62+
update
6363
};

build/clean.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const distFolder = path.join(__dirname, 'dist');
5+
6+
if (fs.existsSync(distFolder)) {
7+
fs.rmdirSync(distFolder);
8+
}

build/postbuild.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
fs.chmodSync(path.join(__dirname, '../dist', 'solc.js'), '755');

downloadCurrentVersion.js renamed to downloadCurrentVersion.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
// This is used to download the correct binary version
44
// as part of the prepublish step.
55

6-
const pkg = require('./package.json');
7-
const fs = require('fs');
8-
const https = require('follow-redirects').https;
9-
const MemoryStream = require('memorystream');
10-
const keccak256 = require('js-sha3').keccak256;
6+
import * as pkg from './package.json';
7+
import * as fs from 'fs';
8+
import { https } from 'follow-redirects';
9+
import * as MemoryStream from 'memorystream';
10+
import { keccak256 } from 'js-sha3';
1111

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

index.js

-3
This file was deleted.

index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import wrapper from './wrapper';
2+
3+
const soljson = require('./soljson.js');
4+
5+
export default wrapper(soljson);

linker.js renamed to linker.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const assert = require('assert');
2-
const keccak256 = require('js-sha3').keccak256;
1+
import * as assert from 'assert';
2+
import { keccak256 } from 'js-sha3';
33

44
function libraryHashPlaceholder (input) {
55
return '$' + keccak256(input).slice(0, 34) + '$';
@@ -88,7 +88,7 @@ const findLinkReferences = function (bytecode) {
8888
return linkReferences;
8989
};
9090

91-
module.exports = {
92-
linkBytecode: linkBytecode,
93-
findLinkReferences: findLinkReferences
91+
export default {
92+
linkBytecode,
93+
findLinkReferences
9494
};

package.json

+33-22
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@
22
"name": "solc",
33
"version": "0.8.11",
44
"description": "Solidity compiler",
5-
"main": "index.js",
5+
"main": "dist/index.js",
66
"bin": {
7-
"solcjs": "solc.js"
7+
"solcjs": "dist/solc.js"
88
},
99
"scripts": {
10-
"lint": "eslint .",
11-
"lint:fix": "eslint --fix .",
12-
"updateBinary": "node downloadCurrentVersion.js && node verifyVersion.js",
13-
"prepublishOnly": "npm run updateBinary",
14-
"pretest": "npm run lint",
15-
"test": "tape ./test/index.js",
16-
"coverage": "node ./node_modules/nyc/bin/nyc.js --reporter=lcov --reporter=text-summary ./node_modules/tape/bin/tape ./test/index.js",
17-
"coveralls": "npm run coverage && node ./node_modules/coveralls/bin/coveralls.js <coverage/lcov.info"
10+
"build": "tsc",
11+
"postbuild": "node build/postbuild.js",
12+
"lint": "eslint --ext .js,.ts .",
13+
"lint:fix": "eslint --fix --ext .js,.ts .",
14+
"updateBinary": "node build/clean.js && ts-node ./downloadCurrentVersion.ts && ts-node ./verifyVersion.ts",
15+
"prepublishOnly": "npm run updateBinary npm && npm run build",
16+
"copyTestFiles": "cp -r ./test/resources ./dist/test/",
17+
"pretest": "npm run lint && npm run build && npm run copyTestFiles",
18+
"test": "cd dist && tape ./test/index.js",
19+
"coverage": "nyc npm run test",
20+
"coveralls": "npm run coverage && coveralls <coverage/lcov.info"
1821
},
1922
"repository": {
2023
"type": "git",
@@ -29,15 +32,15 @@
2932
"node": ">=10.0.0"
3033
},
3134
"files": [
32-
"abi.js",
33-
"index.js",
34-
"linker.js",
35-
"smtchecker.js",
36-
"smtsolver.js",
37-
"solc.js",
38-
"soljson.js",
39-
"translate.js",
40-
"wrapper.js"
35+
"dist/abi.js",
36+
"dist/index.js",
37+
"dist/linker.js",
38+
"dist/smtchecker.js",
39+
"dist/smtsolver.js",
40+
"dist/solc.js",
41+
"dist/soljson.js",
42+
"dist/translate.js",
43+
"dist/wrapper.js"
4144
],
4245
"author": "chriseth",
4346
"license": "MIT",
@@ -55,19 +58,27 @@
5558
"tmp": "0.0.33"
5659
},
5760
"devDependencies": {
61+
"@types/node": "^16.11.7",
62+
"@types/semver": "^7.3.9",
63+
"@types/tape": "^4.13.2",
64+
"@typescript-eslint/eslint-plugin": "^5.8.0",
65+
"@typescript-eslint/parser": "^5.8.0",
5866
"coveralls": "^3.0.0",
5967
"eslint": "^7.32.0",
6068
"eslint-config-standard": "^16.0.3",
6169
"eslint-plugin-import": "^2.25.3",
6270
"eslint-plugin-node": "^11.1.0",
6371
"eslint-plugin-promise": "^5.1.1",
64-
"nyc": "^14.1.0",
72+
"nyc": "^15.1.0",
6573
"tape": "^4.11.0",
66-
"tape-spawn": "^1.4.2"
74+
"tape-spawn": "^1.4.2",
75+
"ts-node": "^10.4.0",
76+
"typescript": "^4.5.4"
6777
},
6878
"nyc": {
6979
"exclude": [
70-
"soljson.js"
80+
"soljson.js",
81+
"dist"
7182
]
7283
}
7384
}

smtchecker.js renamed to smtchecker.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// The function runs an SMT solver on each query and adjusts the input for
44
// another run.
55
// Returns null if no solving is requested.
6-
function handleSMTQueries (inputJSON, outputJSON, solverFunction, solver) {
6+
function handleSMTQueries (inputJSON: any, outputJSON: any, solverFunction: any, solver?: any) {
77
const auxInputReq = outputJSON.auxiliaryInputRequested;
88
if (!auxInputReq) {
99
return null;
@@ -25,7 +25,7 @@ function handleSMTQueries (inputJSON, outputJSON, solverFunction, solver) {
2525
return inputJSON;
2626
}
2727

28-
function smtCallback (solverFunction, solver) {
28+
function smtCallback (solverFunction, solver?: any) {
2929
return function (query) {
3030
try {
3131
const result = solverFunction(query, solver);
@@ -36,7 +36,7 @@ function smtCallback (solverFunction, solver) {
3636
};
3737
}
3838

39-
module.exports = {
40-
handleSMTQueries: handleSMTQueries,
41-
smtCallback: smtCallback
39+
export default {
40+
handleSMTQueries,
41+
smtCallback
4242
};

smtsolver.js renamed to smtsolver.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const commandExistsSync = require('command-exists').sync;
2-
const execSync = require('child_process').execSync;
3-
const fs = require('fs');
4-
const tmp = require('tmp');
1+
import { sync as commandExistsSync } from 'command-exists';
2+
import { execSync } from 'child_process';
3+
import * as fs from 'fs';
4+
import tmp from 'tmp';
55

66
// Timeout in ms.
77
const timeout = 10000;
@@ -67,7 +67,7 @@ function solve (query, solver) {
6767
return solverOutput;
6868
}
6969

70-
module.exports = {
70+
export default {
7171
smtSolver: solve,
7272
availableSolvers: solvers
7373
};

solc.js renamed to solc.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/usr/bin/env node
22

3-
const commander = require('commander');
4-
const fs = require('fs');
5-
const os = require('os');
6-
const path = require('path');
7-
const solc = require('./index.js');
8-
const smtchecker = require('./smtchecker.js');
9-
const smtsolver = require('./smtsolver.js');
3+
import * as commander from 'commander';
4+
import * as fs from 'fs';
5+
import * as os from 'os';
6+
import * as path from 'path';
7+
import solc from './index';
8+
import smtchecker from './smtchecker';
9+
import smtsolver from './smtsolver';
1010

1111
// hold on to any exception handlers that existed prior to this script running, we'll be adding them back at the end
1212
const originalUncaughtExceptionListeners = process.listeners('uncaughtException');
1313
// FIXME: remove annoying exception catcher of Emscripten
1414
// see https://github.com/chriseth/browser-solidity/issues/167
1515
process.removeAllListeners('uncaughtException');
1616

17-
const program = new commander.Command();
17+
const program: any = new commander.Command();
1818
const commanderParseInt = function (value) {
1919
const parsedValue = parseInt(value, 10);
2020
if (isNaN(parsedValue)) {

test/abi.js renamed to test/abi.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const tape = require('tape');
2-
const abi = require('../abi.js');
1+
import * as tape from 'tape';
2+
import abi from '../abi';
33

44
tape('ABI translator', function (t) {
55
t.test('Empty ABI', function (st) {

test/cli.js renamed to test/cli.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const tape = require('tape');
2-
const spawn = require('tape-spawn');
3-
const path = require('path');
4-
const pkg = require('../package.json');
1+
import * as tape from 'tape';
2+
import * as spawn from 'tape-spawn';
3+
import * as path from 'path';
4+
import * as pkg from '../package.json';
55

66
tape('CLI', function (t) {
77
t.test('--version', function (st) {

test/compiler.js renamed to test/compiler.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const assert = require('assert');
2-
const tape = require('tape');
3-
const semver = require('semver');
4-
const solc = require('../index.js');
5-
const linker = require('../linker.js');
6-
const execSync = require('child_process').execSync;
1+
import * as assert from 'assert';
2+
import * as tape from 'tape';
3+
import * as semver from 'semver';
4+
import solc from '../';
5+
import linker from '../linker';
6+
import { execSync } from 'child_process';
7+
import wrapper from '../wrapper';
78

89
const noRemoteVersions = (process.argv.indexOf('--no-remote-versions') >= 0);
910

@@ -68,10 +69,10 @@ function runTests (solc, versionText) {
6869
}
6970
}
7071

71-
function expectError (output, errorType, message) {
72+
function expectError (output: any, errorType: any, message: any) {
7273
if (output.errors) {
73-
for (let error in output.errors) {
74-
error = output.errors[error];
74+
for (const errorIndex in output.errors) {
75+
const error = output.errors[errorIndex];
7576
if (error.type === errorType) {
7677
if (message) {
7778
if (error.message.match(message) !== null) {
@@ -86,10 +87,10 @@ function runTests (solc, versionText) {
8687
return false;
8788
}
8889

89-
function expectNoError (output) {
90+
function expectNoError (output: any) {
9091
if (output.errors) {
91-
for (let error in output.errors) {
92-
error = output.errors[error];
92+
for (const errorIndex in output.errors) {
93+
const error = output.errors[errorIndex];
9394
if (error.severity === 'error') {
9495
return false;
9596
}
@@ -888,7 +889,7 @@ if (!noRemoteVersions) {
888889
for (let version in versions) {
889890
version = versions[version];
890891
execSync(`curl -L -o /tmp/${version}.js https://binaries.soliditylang.org/bin/soljson-${version}.js`);
891-
const newSolc = require('../wrapper.js')(require(`/tmp/${version}.js`));
892+
const newSolc = wrapper(require(`/tmp/${version}.js`));
892893
runTests(newSolc, version);
893894
}
894895
}

test/index.js

-13
This file was deleted.

test/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as semver from 'semver';
2+
3+
import('./linker');
4+
import('./translate');
5+
import('./compiler');
6+
import('./smtcallback');
7+
import('./smtchecker');
8+
import('./abi');
9+
10+
// The CLI doesn't support Node 4
11+
if (semver.gte(process.version, '5.0.0')) {
12+
import('./cli');
13+
}

test/linker.js renamed to test/linker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const tape = require('tape');
2-
const linker = require('../linker.js');
1+
import * as tape from 'tape';
2+
import linker from '../linker';
33

44
tape('Link references', function (t) {
55
t.test('Empty bytecode', function (st) {

0 commit comments

Comments
 (0)