Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apply fixes and report remaining issues
- Loading branch information
Showing
6 changed files
with
146 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function applyFixes(fixes, inputSrc) { | ||
if (fixes.length === 0) { | ||
return { | ||
fixed: false | ||
} | ||
} | ||
|
||
let output = '' | ||
|
||
let i = 0 | ||
let fixIndex = 0 | ||
|
||
while (i < inputSrc.length) { | ||
if (fixIndex < fixes.length) { | ||
output += inputSrc.slice(i, fixes[fixIndex].range[0]) | ||
output += fixes[fixIndex].text | ||
i = fixes[fixIndex].range[1] | ||
fixIndex += 1 | ||
} else { | ||
output += inputSrc.slice(i) | ||
break | ||
} | ||
} | ||
|
||
return { | ||
fixed: true, | ||
output | ||
} | ||
} | ||
|
||
module.exports = applyFixes |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const assert = require('assert') | ||
|
||
const applyFixes = require('../../lib/apply-fixes') | ||
|
||
describe('applyFixes', () => { | ||
it('should work when there are no reports', () => { | ||
const inputSrc = 'contract Foo {}' | ||
const fixes = [] | ||
|
||
const { fixed } = applyFixes(fixes, inputSrc) | ||
|
||
assert.equal(fixed, false) | ||
}) | ||
|
||
it('should work for a single replace', () => { | ||
const inputSrc = ` | ||
contract Foo { | ||
function foo() { | ||
throw; | ||
} | ||
}`.trim() | ||
|
||
const fixes = [ | ||
{ | ||
range: [38, 43], | ||
text: 'revert()' | ||
} | ||
] | ||
|
||
const { fixed, output } = applyFixes(fixes, inputSrc) | ||
|
||
assert.equal(fixed, true) | ||
assert.equal( | ||
output, | ||
` | ||
contract Foo { | ||
function foo() { | ||
revert(); | ||
} | ||
}`.trim() | ||
) | ||
}) | ||
|
||
it('should work for two fixes', () => { | ||
const inputSrc = ` | ||
contract Foo { | ||
function foo() { | ||
throw; | ||
throw; | ||
} | ||
}`.trim() | ||
|
||
const fixes = [ | ||
{ | ||
range: [38, 43], | ||
text: 'revert()' | ||
}, | ||
{ | ||
range: [49, 54], | ||
text: 'revert()' | ||
} | ||
] | ||
|
||
const { fixed, output } = applyFixes(fixes, inputSrc) | ||
|
||
assert.equal(fixed, true) | ||
assert.equal( | ||
output, | ||
` | ||
contract Foo { | ||
function foo() { | ||
revert(); | ||
revert(); | ||
} | ||
}`.trim() | ||
) | ||
}) | ||
}) |
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