Skip to content

Commit

Permalink
Merge 04bee9c into 9c183f9
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomg committed Dec 17, 2019
2 parents 9c183f9 + 04bee9c commit d9fa255
Show file tree
Hide file tree
Showing 87 changed files with 596 additions and 3,269 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -1,8 +1,11 @@
language: node_js

node_js:
- "10"
- "stable"

before_script: npm i -g npm@6.9

script:
- npm run lint
- npm test
15 changes: 8 additions & 7 deletions lib/comment-directive-parser.js
@@ -1,21 +1,22 @@
class CommentDirectiveParser {
constructor(tokens) {
this.lastLine = tokens.tokenSource.line
const lastToken = tokens[tokens.length - 1]
this.lastLine = lastToken ? lastToken.loc.end.line : 0
this.ruleStore = new RuleStore(this.lastLine)

this.parseComments(tokens)
}

parseComments(tokens) {
const items = tokens.filterForChannel(0, tokens.tokens.length - 1, 1)
if (items) {
items.forEach(this.onComment.bind(this))
}
const items = tokens.filter(
token => token.type === 'Keyword' && /^(\/\/|\/\*)/.test(token.value)
)
items.forEach(item => this.onComment(item))
}

onComment(lexema) {
const text = lexema.text
const curLine = lexema.line
const text = lexema.value
const curLine = lexema.loc.start.line
const ruleStore = this.ruleStore

if (text.includes('solhint-disable-line')) {
Expand Down
10 changes: 5 additions & 5 deletions lib/common/ast-types.js
@@ -1,11 +1,11 @@
const { typeOf } = require('./tree-traversing')
const fallbackVisibility = ['external', 'public']

function isFallbackFunction(ctx) {
return ctx.children && ctx.children[1] && ctx.children[1].constructor.name !== 'IdentifierContext'
function isFallbackFunction(node) {
return node.name === '' && fallbackVisibility.includes(node.visibility)
}

function isFunctionDefinition(ctx) {
return typeOf(ctx) === 'functionDefinition'
function isFunctionDefinition(node) {
return node.type === 'FunctionDefinition'
}

module.exports = {
Expand Down
46 changes: 21 additions & 25 deletions lib/common/tree-traversing.js
@@ -1,6 +1,6 @@
class TreeTraversing {
statementNotContains(ctx, type) {
const statement = this.findParentStatement(ctx)
statementNotContains(node, type) {
const statement = this.findParentStatement(node)

if (!statement) {
return false
Expand All @@ -11,29 +11,25 @@ class TreeTraversing {
return itemOfType !== null
}

findParentStatement(ctx) {
while (ctx.parentCtx != null && ctx.parentCtx.constructor.name !== 'StatementContext') {
ctx = ctx.parentCtx
findParentStatement(node) {
while (node.parent != null && !node.parent.type.includes('Statement')) {
node = node.parent
}

return ctx.parentCtx
return node.parent
}

findParentType(ctx, type) {
while (ctx.parentCtx != null && ctx.parentCtx.constructor.name !== type) {
ctx = ctx.parentCtx
findParentType(node, type) {
while (node.parent !== undefined && node.parent.type !== type) {
node = node.parent
}

return ctx.parentCtx
return node.parent || null
}

findDownType(ctx, type) {
if (!ctx || ctx.constructor.name === type) {
return ctx
} else if (ctx.children) {
const items = ctx.children.map(i => this.findDownType(i, type)).filter(i => i !== null)

return (items.length > 0 && items[0]) || null
findDownType(node, type) {
if (!node || node.type === type) {
return node
} else {
return null
}
Expand Down Expand Up @@ -72,20 +68,20 @@ TreeTraversing.typeOf = function typeOf(ctx) {
return typeName[0].toLowerCase() + typeName.substring(1)
}

TreeTraversing.hasMethodCalls = function hasMethodCalls(ctx, methodNames) {
const text = ctx.getText()
TreeTraversing.hasMethodCalls = function hasMethodCalls(node, methodNames) {
const text = node.memberName

return methodNames.some(i => text.includes(`${i}(`))
return methodNames.includes(text)
}

TreeTraversing.findPropertyInParents = function findPropertyInParents(ctx, property) {
let curCtx = ctx
TreeTraversing.findPropertyInParents = function findPropertyInParents(node, property) {
let curNode = node

while (curCtx !== null && !curCtx[property]) {
curCtx = curCtx.parentCtx
while (curNode !== undefined && !curNode[property]) {
curNode = curNode.parent
}

return curCtx && curCtx[property]
return curNode && curNode[property]
}

module.exports = TreeTraversing
35 changes: 23 additions & 12 deletions lib/index.js
@@ -1,25 +1,36 @@
const fs = require('fs')
const antlr4 = require('antlr4')
const parser = require('solidity-parser-antlr')
const glob = require('glob')
const ignore = require('ignore')
const SolidityLexer = require('./grammar/SolidityLexer').SolidityLexer
const SolidityParser = require('./grammar/SolidityParser').SolidityParser
const astParents = require('ast-parents')
const Reporter = require('./reporter')
const TreeListener = require('./tree-listener')
const checkers = require('./rules/index')

function processStr(inputStr, config = {}, fileName = '') {
const chars = new antlr4.InputStream(inputStr)
const lexer = new SolidityLexer(chars)
const tokens = new antlr4.CommonTokenStream(lexer)
const parser = new SolidityParser(tokens)
parser.buildParseTrees = true
function parseInput(inputStr) {
try {
// first we try to parse the string as we normally do
return parser.parse(inputStr, { loc: true, range: true })
} catch (e) {
try {
// using 'loc' may throw when inputStr is empty or only has comments
return parser.parse(inputStr, {})
} catch (error) {
// if the parser fails in both scenarios (with and without 'loc')
// we throw the error, as we may be facing an invalid Solidity file
throw new Error(error)
}
}
}

const tree = parser.sourceUnit()
function processStr(inputStr, config = {}, fileName = '') {
const ast = parseInput(inputStr)
const tokens = parser.tokenize(inputStr, { loc: true })
const reporter = new Reporter(tokens, config)
const listener = new TreeListener(checkers(reporter, config, inputStr, tokens, fileName))

const listener = new TreeListener(checkers(reporter, config, inputStr, fileName))
antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree)
astParents(ast)
parser.visit(ast, listener)

return reporter
}
Expand Down
16 changes: 6 additions & 10 deletions lib/reporter.js
@@ -1,22 +1,18 @@
const CommentDirectiveParser = require('./comment-directive-parser')

class Reporter {
constructor(tokenStream, config) {
this.commentDirectiveParser = new CommentDirectiveParser(tokenStream)
constructor(tokens, config) {
this.commentDirectiveParser = new CommentDirectiveParser(tokens)
this.reports = []
this.tokenStream = tokenStream
this.config = config.rules || {}
}

addReport(line, column, severity, message, ruleId) {
this.reports.push({ line, column, severity, message, ruleId })
}

addMessage(interval, defaultSeverity, message, ruleId) {
const line = this.tokenStream.get(interval.start).line
const charAtLine = this.tokenStream.get(interval.start).column

this.addMessageExplicitLine(line, charAtLine, defaultSeverity, message, ruleId)
addMessage(loc, defaultSeverity, message, ruleId) {
this.addMessageExplicitLine(loc.start.line, loc.start.column, defaultSeverity, message, ruleId)
}

addMessageExplicitLine(line, column, defaultSeverity, message, ruleId) {
Expand All @@ -28,11 +24,11 @@ class Reporter {
}

error(ctx, ruleId, message) {
this.addMessage(ctx.getSourceInterval(), Reporter.SEVERITY.ERROR, message, ruleId)
this.addMessage(ctx.loc, Reporter.SEVERITY.ERROR, message, ruleId)
}

warn(ctx, ruleId, message) {
this.addMessage(ctx.getSourceInterval(), Reporter.SEVERITY.WARN, message, ruleId)
this.addMessage(ctx.loc, Reporter.SEVERITY.WARN, message, ruleId)
}

errorAt(line, column, ruleId, message) {
Expand Down
64 changes: 0 additions & 64 deletions lib/rules/align/array-declaration-spaces.js

This file was deleted.

0 comments on commit d9fa255

Please sign in to comment.