From b9f8759b70a60a6c881af09ded3a417230b165ba Mon Sep 17 00:00:00 2001 From: panos Date: Fri, 9 Sep 2016 04:50:25 +0300 Subject: [PATCH] Various syntax fixes, Add support for DocString errors, IDE config dir ignored --- .gitignore | 1 + lib/compiler.js | 89 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index ade14b9..1a6a5de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store npm-debug.log node_modules +.idea/ diff --git a/lib/compiler.js b/lib/compiler.js index d69c505..03030b4 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -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();