Skip to content

Commit f2fdcfb

Browse files
authored
Merge pull request #587 from ethereum/solcjs-lint
Rename solcjs to solc.js internally and apply lint changes
2 parents 014551e + 5f9f145 commit f2fdcfb

File tree

4 files changed

+90
-95
lines changed

4 files changed

+90
-95
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Solidity compiler",
55
"main": "index.js",
66
"bin": {
7-
"solcjs": "solcjs"
7+
"solcjs": "solc.js"
88
},
99
"scripts": {
1010
"lint": "eslint .",
@@ -34,7 +34,7 @@
3434
"linker.js",
3535
"smtchecker.js",
3636
"smtsolver.js",
37-
"solcjs",
37+
"solc.js",
3838
"soljson.js",
3939
"translate.js",
4040
"wrapper.js"

solcjs renamed to solc.js

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
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');
10+
311
// hold on to any exception handlers that existed prior to this script running, we'll be adding them back at the end
4-
var originalUncaughtExceptionListeners = process.listeners("uncaughtException");
5-
6-
var fs = require('fs');
7-
var os = require('os');
8-
var path = require('path');
9-
var solc = require('./index.js');
10-
var smtchecker = require('./smtchecker.js');
11-
var smtsolver = require('./smtsolver.js');
12+
const originalUncaughtExceptionListeners = process.listeners('uncaughtException');
1213
// FIXME: remove annoying exception catcher of Emscripten
1314
// see https://github.com/chriseth/browser-solidity/issues/167
1415
process.removeAllListeners('uncaughtException');
15-
var commander = require('commander');
1616

1717
const program = new commander.Command();
18-
const commanderParseInt = function(value) {
18+
const commanderParseInt = function (value) {
1919
const parsedValue = parseInt(value, 10);
2020
if (isNaN(parsedValue)) {
21-
throw new commander.InvalidArgumentError("Not a valid integer.");
21+
throw new commander.InvalidArgumentError('Not a valid integer.');
2222
}
2323
return parsedValue;
2424
};
@@ -51,46 +51,47 @@ program
5151
program.parse(process.argv);
5252
const options = program.opts();
5353

54-
var files = program.args;
55-
var destination = options.outputDir || '.'
54+
const files = program.args;
55+
const destination = options.outputDir || '.';
5656

5757
function abort (msg) {
5858
console.error(msg || 'Error occured');
5959
process.exit(1);
6060
}
6161

62-
function readFileCallback(sourcePath) {
63-
const prefixes = [options.basePath ? options.basePath : ""].concat(
62+
function readFileCallback (sourcePath) {
63+
const prefixes = [options.basePath ? options.basePath : ''].concat(
6464
options.includePath ? options.includePath : []
6565
);
6666
for (const prefix of prefixes) {
67-
const prefixedSourcePath = (prefix ? prefix + '/' : "") + sourcePath;
67+
const prefixedSourcePath = (prefix ? prefix + '/' : '') + sourcePath;
6868

6969
if (fs.existsSync(prefixedSourcePath)) {
7070
try {
71-
return {'contents': fs.readFileSync(prefixedSourcePath).toString('utf8')}
71+
return { contents: fs.readFileSync(prefixedSourcePath).toString('utf8') };
7272
} catch (e) {
73-
return {error: 'Error reading ' + prefixedSourcePath + ': ' + e};
73+
return { error: 'Error reading ' + prefixedSourcePath + ': ' + e };
7474
}
7575
}
7676
}
77-
return {error: 'File not found inside the base path or any of the include paths.'}
77+
return { error: 'File not found inside the base path or any of the include paths.' };
7878
}
7979

80-
function withUnixPathSeparators(filePath) {
81-
if (os.platform() !== 'win32')
82-
// On UNIX-like systems forward slashes in paths are just a part of the file name.
80+
function withUnixPathSeparators (filePath) {
81+
// On UNIX-like systems forward slashes in paths are just a part of the file name.
82+
if (os.platform() !== 'win32') {
8383
return filePath;
84+
}
8485

85-
return filePath.replace(/\\/g, "/");
86+
return filePath.replace(/\\/g, '/');
8687
}
8788

88-
function makeSourcePathRelativeIfPossible(sourcePath) {
89+
function makeSourcePathRelativeIfPossible (sourcePath) {
8990
const absoluteBasePath = (options.basePath ? path.resolve(options.basePath) : path.resolve('.'));
9091
const absoluteIncludePaths = (
91-
options.includePath ?
92-
options.includePath.map((prefix) => { return path.resolve(prefix); }) :
93-
[]
92+
options.includePath
93+
? options.includePath.map((prefix) => { return path.resolve(prefix); })
94+
: []
9495
);
9596

9697
// Compared to base path stripping logic in solc this is much simpler because path.resolve()
@@ -104,58 +105,52 @@ function makeSourcePathRelativeIfPossible(sourcePath) {
104105
for (const absolutePrefix of [absoluteBasePath].concat(absoluteIncludePaths)) {
105106
const relativeSourcePath = path.relative(absolutePrefix, absoluteSourcePath);
106107

107-
if (!relativeSourcePath.startsWith('../'))
108-
return withUnixPathSeparators(relativeSourcePath);
108+
if (!relativeSourcePath.startsWith('../')) { return withUnixPathSeparators(relativeSourcePath); }
109109
}
110110

111111
// File is not located inside base path or include paths so use its absolute path.
112112
return withUnixPathSeparators(absoluteSourcePath);
113113
}
114114

115-
function toFormattedJson(input) {
115+
function toFormattedJson (input) {
116116
return JSON.stringify(input, null, program.prettyJson ? 4 : 0);
117117
}
118118

119-
function reformatJsonIfRequested(inputJson) {
119+
function reformatJsonIfRequested (inputJson) {
120120
return (program.prettyJson ? toFormattedJson(JSON.parse(inputJson)) : inputJson);
121121
}
122122

123-
var callbacks = undefined
124-
if (options.basePath || !options.standardJson)
125-
callbacks = {'import': readFileCallback};
123+
let callbacks;
124+
if (options.basePath || !options.standardJson) { callbacks = { import: readFileCallback }; }
126125

127126
if (options.standardJson) {
128-
var input = fs.readFileSync(process.stdin.fd).toString('utf8');
129-
if (program.verbose)
130-
console.log('>>> Compiling:\n' + reformatJsonIfRequested(input) + "\n")
131-
var output = reformatJsonIfRequested(solc.compile(input, callbacks));
127+
const input = fs.readFileSync(process.stdin.fd).toString('utf8');
128+
if (program.verbose) { console.log('>>> Compiling:\n' + reformatJsonIfRequested(input) + '\n'); }
129+
let output = reformatJsonIfRequested(solc.compile(input, callbacks));
132130

133131
try {
134-
var inputJSON = smtchecker.handleSMTQueries(JSON.parse(input), JSON.parse(output), smtsolver.smtSolver);
132+
const inputJSON = smtchecker.handleSMTQueries(JSON.parse(input), JSON.parse(output), smtsolver.smtSolver);
135133
if (inputJSON) {
136-
if (program.verbose)
137-
console.log('>>> Retrying compilation with SMT:\n' + toFormattedJson(inputJSON) + "\n")
134+
if (program.verbose) { console.log('>>> Retrying compilation with SMT:\n' + toFormattedJson(inputJSON) + '\n'); }
138135
output = reformatJsonIfRequested(solc.compile(JSON.stringify(inputJSON), callbacks));
139136
}
140-
}
141-
catch (e) {
142-
var addError = {
143-
component: "general",
137+
} catch (e) {
138+
const addError = {
139+
component: 'general',
144140
formattedMessage: e.toString(),
145141
message: e.toString(),
146-
type: "Warning"
142+
type: 'Warning'
147143
};
148144

149-
var outputJSON = JSON.parse(output);
145+
const outputJSON = JSON.parse(output);
150146
if (!outputJSON.errors) {
151-
outputJSON.errors = []
147+
outputJSON.errors = [];
152148
}
153149
outputJSON.errors.push(addError);
154150
output = toFormattedJson(outputJSON);
155151
}
156152

157-
if (program.verbose)
158-
console.log('>>> Compilation result:')
153+
if (program.verbose) { console.log('>>> Compilation result:'); }
159154
console.log(output);
160155
process.exit(0);
161156
} else if (files.length === 0) {
@@ -171,14 +166,15 @@ if (!options.basePath && options.includePath && options.includePath.length > 0)
171166
abort('--include-path option requires a non-empty base path.');
172167
}
173168

174-
if (options.includePath)
175-
for (const includePath of options.includePath)
176-
if (!includePath)
177-
abort('Empty values are not allowed in --include-path.');
169+
if (options.includePath) {
170+
for (const includePath of options.includePath) {
171+
if (!includePath) { abort('Empty values are not allowed in --include-path.'); }
172+
}
173+
}
178174

179-
var sources = {};
175+
const sources = {};
180176

181-
for (var i = 0; i < files.length; i++) {
177+
for (let i = 0; i < files.length; i++) {
182178
try {
183179
sources[makeSourcePathRelativeIfPossible(files[i])] = {
184180
content: fs.readFileSync(files[i]).toString()
@@ -193,37 +189,36 @@ const cliInput = {
193189
settings: {
194190
optimizer: {
195191
enabled: options.optimize,
196-
runs: options.optimizeRuns,
192+
runs: options.optimizeRuns
197193
},
198194
outputSelection: {
199195
'*': {
200-
'*': [ 'abi', 'evm.bytecode' ]
196+
'*': ['abi', 'evm.bytecode']
201197
}
202198
}
203199
},
204200
sources: sources
205201
};
206-
if (program.verbose)
207-
console.log('>>> Compiling:\n' + toFormattedJson(cliInput) + "\n")
208-
var output = JSON.parse(solc.compile(JSON.stringify(cliInput), callbacks));
202+
if (program.verbose) { console.log('>>> Compiling:\n' + toFormattedJson(cliInput) + '\n'); }
203+
const output = JSON.parse(solc.compile(JSON.stringify(cliInput), callbacks));
209204

210205
let hasError = false;
211206

212207
if (!output) {
213208
abort('No output from compiler');
214-
} else if (output['errors']) {
215-
for (var error in output['errors']) {
216-
var message = output['errors'][error]
209+
} else if (output.errors) {
210+
for (const error in output.errors) {
211+
const message = output.errors[error];
217212
if (message.severity === 'warning') {
218-
console.log(message.formattedMessage)
213+
console.log(message.formattedMessage);
219214
} else {
220-
console.error(message.formattedMessage)
221-
hasError = true
215+
console.error(message.formattedMessage);
216+
hasError = true;
222217
}
223218
}
224219
}
225220

226-
fs.mkdirSync(destination, {recursive: true});
221+
fs.mkdirSync(destination, { recursive: true });
227222

228223
function writeFile (file, content) {
229224
file = path.join(destination, file);
@@ -234,9 +229,9 @@ function writeFile (file, content) {
234229
});
235230
}
236231

237-
for (var fileName in output.contracts) {
238-
for (var contractName in output.contracts[fileName]) {
239-
var contractFileName = fileName + ':' + contractName;
232+
for (const fileName in output.contracts) {
233+
for (const contractName in output.contracts[fileName]) {
234+
let contractFileName = fileName + ':' + contractName;
240235
contractFileName = contractFileName.replace(/[:./\\]/g, '_');
241236

242237
if (options.bin) {

0 commit comments

Comments
 (0)