Skip to content

Commit

Permalink
Merge 54e0ed9 into 98c3ea4
Browse files Browse the repository at this point in the history
  • Loading branch information
mariano-aguero committed Aug 8, 2019
2 parents 98c3ea4 + 54e0ed9 commit 525e13a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
18 changes: 0 additions & 18 deletions lib/common/utils.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
const fs = require('fs')
const path = require('path')

const getLocFromIndex = (text, index) => {
let line = 1
let column = 0
let i = 0
while (i < index) {
if (text[i] === '\n') {
line++
column = 0
} else {
column++
}
i++
}

return { line, column }
}

const walkSync = (dir, filelist = []) => {
fs.readdirSync(dir).forEach(file => {
filelist = fs.statSync(path.join(dir, file)).isDirectory()
Expand All @@ -28,6 +11,5 @@ const walkSync = (dir, filelist = []) => {
}

module.exports = {
getLocFromIndex,
walkSync
}
7 changes: 6 additions & 1 deletion lib/rules/best-practises/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ class NoUnusedVarsChecker extends BaseChecker {
const idNode = isCtxIdentifier ? ctx : findIdentifierInChildren(ctx)
const funcScope = VarUsageScope.of(ctx)

if (idNode && funcScope) {
if (idNode && funcScope && !this._isCallDataStatement(ctx)) {
funcScope.addVar(idNode, idNode.getText())
}
}

_isCallDataStatement(ctx) {
const varName = ctx.children[1]
return varName && varName.getText() === 'calldata'
}

_trackVarUsage(ctx) {
const isFunctionName = typeOf(ctx.parentCtx) === 'functionDefinition'
const funcScope = VarUsageScope.of(ctx)
Expand Down
48 changes: 48 additions & 0 deletions test/rules/best-practises/no-unused-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,54 @@ describe('Linter - no-unused-vars', () => {
assert.ok(report.messages[0].message.includes('Avoid to use'))
})

it('should not raise unused var error for calldata', () => {
const code = contractWith(`function setBitExample(byte calldata bar) public pure {
uint n = 0;
n = n.setBit(0); // Set the 0th bit.
assert(n == 1); // 1
n = n.setBit(1); // Set the 1st bit.
assert(n == 3); // 11
n = n.setBit(2); // Set the 2nd bit.
assert(n == 7); // 111
n = n.setBit(3); // Set the 3rd bit.
assert(n == 15); // 1111
n = 1;
assert(n.setBit(0) == n);
}`)

const report = linter.processStr(code, {
rules: { 'no-unused-vars': 'error' }
})

assert.equal(report.errorCount, 0)
})

it('should raise var name error for some unused vars but not for calldata', () => {
const code = contractWith(`function setBitExample(bytes bar2, byte bar3, byte calldata bar4) public pure {
uint n = 0;
uint d;
n = n.setBit(0); // Set the 0th bit.
assert(n == 1); // 1
n = n.setBit(1); // Set the 1st bit.
assert(n == 3); // 11
n = n.setBit(2); // Set the 2nd bit.
assert(n == 7); // 111
n = n.setBit(3); // Set the 3rd bit.
assert(n == 15); // 1111
// x.bit(y) == 1 => x.setBit(y) == x
n = 1;
assert(n.setBit(0) == n);
}`)

const report = linter.processStr(code, {
rules: { 'no-unused-vars': 'error' }
})

assert.equal(report.errorCount, 3)
})

function label(data) {
const items = data.split('\n')
const lastItemIndex = items.length - 1
Expand Down

0 comments on commit 525e13a

Please sign in to comment.