forked from protofire/solhint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
83 changed files
with
14,155 additions
and
13,700 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,123 @@ | ||
|
||
|
||
class CommentDirectiveParser { | ||
|
||
constructor(tokens) { | ||
this.lastLine = tokens.tokenSource.line; | ||
this.ruleStore = new RuleStore(this.lastLine); | ||
|
||
this.parseComments(tokens); | ||
constructor(tokens) { | ||
this.lastLine = tokens.tokenSource.line | ||
this.ruleStore = new RuleStore(this.lastLine) | ||
|
||
this.parseComments(tokens) | ||
} | ||
|
||
parseComments(tokens) { | ||
const items = tokens.filterForChannel(0, tokens.tokens.length - 1, 1) | ||
items && items.forEach(this.onComment.bind(this)) | ||
} | ||
|
||
onComment(lexema) { | ||
const text = lexema.text | ||
const curLine = lexema.line | ||
const ruleStore = this.ruleStore | ||
|
||
if (text.includes('solhint-disable-line')) { | ||
const rules = this.parseRuleIds(text, 'solhint-disable-line') | ||
ruleStore.disableRules(curLine, rules) | ||
return | ||
} | ||
|
||
parseComments(tokens) { | ||
const items = tokens.filterForChannel(0, tokens.tokens.length - 1, 1); | ||
items && items.forEach(this.onComment.bind(this)); | ||
} | ||
|
||
onComment(lexema) { | ||
const text = lexema.text; | ||
const curLine = lexema.line; | ||
const ruleStore = this.ruleStore; | ||
|
||
if (text.includes('solhint-disable-line')) { | ||
const rules = this.parseRuleIds(text, 'solhint-disable-line'); | ||
ruleStore.disableRules(curLine, rules); | ||
return; | ||
} | ||
|
||
if (text.includes('solhint-disable-next-line')) { | ||
const rules = this.parseRuleIds(text, 'solhint-disable-next-line'); | ||
|
||
if (curLine + 1 <= this.lastLine) { | ||
ruleStore.disableRules(curLine + 1, rules); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (text.includes('solhint-disable')) { | ||
const rules = this.parseRuleIds(text, 'solhint-disable'); | ||
|
||
ruleStore.disableRulesToEndOfFile(curLine, rules); | ||
|
||
return; | ||
} | ||
if (text.includes('solhint-disable-next-line')) { | ||
const rules = this.parseRuleIds(text, 'solhint-disable-next-line') | ||
|
||
if (text.includes('solhint-enable')) { | ||
const rules = this.parseRuleIds(text, 'solhint-enable'); | ||
if (curLine + 1 <= this.lastLine) { | ||
ruleStore.disableRules(curLine + 1, rules) | ||
} | ||
|
||
ruleStore.enableRulesToEndOfFile(curLine, rules); | ||
} | ||
return | ||
} | ||
|
||
parseRuleIds(text, start) { | ||
const ruleIds = text | ||
.replace('//', '') | ||
.replace('/*', '') | ||
.replace('*/', '') | ||
.replace(start, ''); | ||
if (text.includes('solhint-disable')) { | ||
const rules = this.parseRuleIds(text, 'solhint-disable') | ||
|
||
const rules = ruleIds | ||
.split(',') | ||
.map(curRule => curRule.trim()) | ||
.filter(i => i.length > 0); | ||
ruleStore.disableRulesToEndOfFile(curLine, rules) | ||
|
||
return (rules.length > 0) ? rules : 'all'; | ||
return | ||
} | ||
|
||
isRuleEnabled(line, ruleId) { | ||
return this.ruleStore.isRuleEnabled(line, ruleId); | ||
if (text.includes('solhint-enable')) { | ||
const rules = this.parseRuleIds(text, 'solhint-enable') | ||
|
||
ruleStore.enableRulesToEndOfFile(curLine, rules) | ||
} | ||
} | ||
|
||
parseRuleIds(text, start) { | ||
const ruleIds = text | ||
.replace('//', '') | ||
.replace('/*', '') | ||
.replace('*/', '') | ||
.replace(start, '') | ||
|
||
const rules = ruleIds | ||
.split(',') | ||
.map(curRule => curRule.trim()) | ||
.filter(i => i.length > 0) | ||
|
||
return rules.length > 0 ? rules : 'all' | ||
} | ||
|
||
isRuleEnabled(line, ruleId) { | ||
return this.ruleStore.isRuleEnabled(line, ruleId) | ||
} | ||
} | ||
|
||
|
||
class RuleStore { | ||
|
||
constructor(lastLine) { | ||
this.disableRuleByLine = []; | ||
this.disableAllByLine = []; | ||
this.lastLine = lastLine; | ||
|
||
this.initRulesTable(); | ||
constructor(lastLine) { | ||
this.disableRuleByLine = [] | ||
this.disableAllByLine = [] | ||
this.lastLine = lastLine | ||
|
||
this.initRulesTable() | ||
} | ||
|
||
initRulesTable() { | ||
for (let i = 1; i <= this.lastLine; i += 1) { | ||
this.disableRuleByLine[i] = new Set() | ||
this.disableAllByLine[i] = false | ||
} | ||
|
||
initRulesTable() { | ||
for (let i = 1; i <= this.lastLine; i += 1) { | ||
this.disableRuleByLine[i] = new Set(); | ||
this.disableAllByLine[i] = false; | ||
} | ||
} | ||
|
||
disableRules(curLine, newRules) { | ||
if (newRules === 'all') { | ||
this.disableAllByLine[curLine] = true | ||
} else { | ||
const lineRules = this.disableRuleByLine[curLine] | ||
this.disableRuleByLine[curLine] = new Set([...lineRules, ...newRules]) | ||
} | ||
|
||
disableRules(curLine, newRules) { | ||
if (newRules === 'all') { | ||
this.disableAllByLine[curLine] = true; | ||
} else { | ||
const lineRules = this.disableRuleByLine[curLine]; | ||
this.disableRuleByLine[curLine] = new Set([...lineRules, ...newRules]); | ||
} | ||
} | ||
|
||
disableRulesToEndOfFile(startLine, rules) { | ||
this._toEndOfFile(startLine, i => this.disableRules(i, rules)) | ||
} | ||
|
||
enableRules(curLine, rules) { | ||
if (rules === 'all') { | ||
this.disableAllByLine[curLine] = false | ||
} else { | ||
const lineRules = this.disableRuleByLine[curLine] | ||
rules.forEach(curRule => lineRules.delete(curRule)) | ||
} | ||
} | ||
|
||
disableRulesToEndOfFile(startLine, rules) { | ||
this._toEndOfFile(startLine, i => this.disableRules(i, rules)); | ||
} | ||
enableRulesToEndOfFile(startLine, rules) { | ||
this._toEndOfFile(startLine, i => this.enableRules(i, rules)) | ||
} | ||
|
||
enableRules(curLine, rules) { | ||
if (rules === 'all') { | ||
this.disableAllByLine[curLine] = false; | ||
} else { | ||
const lineRules = this.disableRuleByLine[curLine]; | ||
rules.forEach(curRule => lineRules.delete(curRule)); | ||
} | ||
} | ||
|
||
enableRulesToEndOfFile(startLine, rules) { | ||
this._toEndOfFile(startLine, i => this.enableRules(i, rules)); | ||
} | ||
isRuleEnabled(line, ruleId) { | ||
return !this.disableAllByLine[line] && !this.disableRuleByLine[line].has(ruleId) | ||
} | ||
|
||
isRuleEnabled(line, ruleId) { | ||
return !this.disableAllByLine[line] && !this.disableRuleByLine[line].has(ruleId); | ||
} | ||
|
||
_toEndOfFile(from, callback) { | ||
for (let i = from; i <= this.lastLine; i += 1) { | ||
callback && callback(i); | ||
} | ||
_toEndOfFile(from, callback) { | ||
for (let i = from; i <= this.lastLine; i += 1) { | ||
callback && callback(i) | ||
} | ||
} | ||
} | ||
|
||
|
||
module.exports = CommentDirectiveParser; | ||
module.exports = CommentDirectiveParser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,31 @@ | ||
const antlr4 = require('antlr4'); | ||
|
||
const antlr4 = require('antlr4') | ||
|
||
class AstPrinter { | ||
constructor(tokenStream) { | ||
this.tokenStream = tokenStream | ||
} | ||
|
||
constructor(tokenStream) { | ||
this.tokenStream = tokenStream; | ||
} | ||
|
||
print(ctx) { | ||
this.explore(ctx, 0); | ||
} | ||
print(ctx) { | ||
this.explore(ctx, 0) | ||
} | ||
|
||
explore(ctx, indentation) { | ||
let ruleName = ctx.parser.ruleNames[ctx.ruleIndex]; | ||
explore(ctx, indentation) { | ||
let ruleName = ctx.parser.ruleNames[ctx.ruleIndex] | ||
|
||
console.log(' '.repeat(indentation + 1) + ruleName + ' ' + ctx.getText()); | ||
console.log(' '.repeat(indentation + 1) + ruleName + ' ' + ctx.getText()) | ||
|
||
for (let i = 0; i < ctx.getChildCount(); i++) { | ||
let element = ctx.getChild(i); | ||
for (let i = 0; i < ctx.getChildCount(); i++) { | ||
let element = ctx.getChild(i) | ||
|
||
if (element instanceof antlr4.ParserRuleContext) { | ||
this.explore(element, indentation + 1); | ||
} else { | ||
console.log(' '.repeat(indentation + 2) + element.constructor.name + ' ' + element.getText()); | ||
} | ||
} | ||
if (element instanceof antlr4.ParserRuleContext) { | ||
this.explore(element, indentation + 1) | ||
} else { | ||
console.log( | ||
' '.repeat(indentation + 2) + element.constructor.name + ' ' + element.getText() | ||
) | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
|
||
module.exports = AstPrinter; | ||
module.exports = AstPrinter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,41 @@ | ||
const { stopLine, lineOf } = require('./tokens'); | ||
|
||
const { stopLine, lineOf } = require('./tokens') | ||
|
||
class BlankLineCounter { | ||
constructor() { | ||
this.tokenLines = new Set() | ||
} | ||
|
||
constructor() { | ||
this.tokenLines = new Set(); | ||
} | ||
|
||
countOfEmptyLinesBetween(start, end) { | ||
return this.countOfEmptyLinesBetweenTokens(stopLine(start), lineOf(end)); | ||
} | ||
|
||
countOfEmptyLinesBetweenTokens(start, end) { | ||
let count = 0; | ||
countOfEmptyLinesBetween(start, end) { | ||
return this.countOfEmptyLinesBetweenTokens(stopLine(start), lineOf(end)) | ||
} | ||
|
||
for (let i = start + 1; i < end; i += 1) { | ||
!this.tokenLines.has(i) && count++; | ||
} | ||
countOfEmptyLinesBetweenTokens(start, end) { | ||
let count = 0 | ||
|
||
return count; | ||
for (let i = start + 1; i < end; i += 1) { | ||
!this.tokenLines.has(i) && count++ | ||
} | ||
|
||
calcTokenLines(ctx) { | ||
if (this.tokenLines.size === 0) { | ||
ctx.parser._input.tokens | ||
.forEach(i => this.addTokenLinesToMap(i)); | ||
} | ||
} | ||
return count | ||
} | ||
|
||
addTokenLinesToMap(token) { | ||
const HIDDEN = 1; | ||
if (token.channel === HIDDEN) { | ||
const linesCount = token.text.split('\n').length; | ||
for (let curLine = token.line; curLine < token.line + linesCount; curLine += 1) { | ||
this.tokenLines.add(curLine); | ||
} | ||
} else { | ||
this.tokenLines.add(token.line); | ||
} | ||
calcTokenLines(ctx) { | ||
if (this.tokenLines.size === 0) { | ||
ctx.parser._input.tokens.forEach(i => this.addTokenLinesToMap(i)) | ||
} | ||
|
||
} | ||
|
||
addTokenLinesToMap(token) { | ||
const HIDDEN = 1 | ||
if (token.channel === HIDDEN) { | ||
const linesCount = token.text.split('\n').length | ||
for (let curLine = token.line; curLine < token.line + linesCount; curLine += 1) { | ||
this.tokenLines.add(curLine) | ||
} | ||
} else { | ||
this.tokenLines.add(token.line) | ||
} | ||
} | ||
} | ||
|
||
|
||
module.exports = BlankLineCounter; | ||
module.exports = BlankLineCounter |
Oops, something went wrong.