Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
npm-debug.log
node_modules
.idea/
89 changes: 63 additions & 26 deletions lib/compiler.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,85 @@
'use babel';

const fs = require('fs')
const solc = require('solc')
const path = require('path')
const fs = require('fs');
const solc = require('solc');
const path = require('path');

const rawFilePath = process.argv[2];

const dirPath = path.dirname(rawFilePath)
const filePath = path.basename(rawFilePath)
const input = {}; input[filePath] = fs.readFileSync(rawFilePath, 'utf8');
const input = {};
input[filePath] = fs.readFileSync(rawFilePath, 'utf8');

function parseErrors(dirPath, errors) {
return errors
.map(err => {
if(err.includes('Internal compiler error')) return {text: err, filePath};
const errorType = {
DOCSTRING: 0,
SYNTAX: 1
};

const filePath = path.resolve(dirPath, err.match(/\w+\.sol/)[0])
const raw_error_range = err.match(/\:\d+:\d+\:/)[0].split(':').slice(1,3)
const line = parseInt(raw_error_range[0])-1;
function parseErrors(dirPath, errors) {
return errors
.map(function (err) {
if (err.includes('Internal compiler error')) return {text: err};

const raw_error_range_segment = err.split('Error:')[1].split('\n')[2];
var fPath, raw_error_range, line, start, end, raw_error_range_segment, errorMsg = null;
var types = [];
types[errorType.DOCSTRING] = [
/End of tag (?:[\s\S]*)not found/img,
/End of param name not found(?:[\s\S]*)/img,
/Documented parameter "(?:[\s\S]*)" not found in the parameter list of the function\./img,
/Doc tag @(?:[\s\S]*) not valid for (?:[\s\S]*)\./img
];

const start= raw_error_range_segment.indexOf('^')
const end = raw_error_range_segment.lastIndexOf('^')
var errorInfo = {type: null, regex: null};

const text = err.split('Error:')[1].split('\n')[0];
for (var key in types) {
for (var value in types[key]) {
if (err.match(types[key][value]) != null) {
errorInfo.type = parseInt(key);
errorInfo.regex = types[key][value];
break;
}
}
if(errorInfo.type != null) break;
}

return {text, filePath, range: [[line, start], [line, end]]}
})
.map(err => {err.type = 'Error'; return err})
switch(errorInfo.type){
case errorType.SYNTAX:
fPath = path.resolve(dirPath, err.match(/\w+\.sol/)[0]);
raw_error_range = err.match(/\:\d+:\d+\:/)[0].split(':').slice(1, 3);
line = parseInt(raw_error_range[0]) - 1;
raw_error_range_segment = err.split('Error:')[1].split('\n')[2];
start = raw_error_range_segment.indexOf('^');
end = raw_error_range_segment.lastIndexOf('^');
errorMsg = err.split('Error:')[1].split('\n')[0];
return {text: errorMsg, filePath: fPath, range: [[line, start], [line, end]]};
break;
case errorType.DOCSTRING:
errorMsg = err.split('Error:')[1].replace("\n"," ");
return {text: errorMsg};
break;
default:
return null;
}
})
.map(function (err) {
err.type = 'Error';
return err
});
}

function findImports(dirPath) {
return function(file) {
try {
return {contents: fs.readFileSync(path.resolve(dirPath, file), 'utf8')}
} catch(err) {
return {error: err}
return function (file) {
try {
return {contents: fs.readFileSync(path.resolve(dirPath, file), 'utf8')}
} catch (err) {
return {error: err}
}
}
}
}

const output = solc.compile({sources: input}, 1, findImports(dirPath))
const parsed = output.errors ? parseErrors(dirPath, output.errors): [];
const output = solc.compile({sources: input}, 1, findImports(dirPath));
const parsed = output.errors ? parseErrors(dirPath, output.errors) : [];

process.stdout.write(JSON.stringify(parsed), 'utf8');
process.exit();