-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimized ranges (fix for issue #150) #173
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
093374b
fix for issue #159
glromeo a3c3926
fixed shebang fixture incorrectly having 2 branches
glromeo 825e320
removed trimRange (oops!)
glromeo c10c8ac
Temporary solution with 2 variants of sliceRange
glromeo db4e653
Refactored into a single sliceRange with inclusive option
glromeo 984557c
improved line coverage
glromeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,4 @@ | ||
[*.{.js,.json}] | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space |
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,3 +1,6 @@ | ||
test/fixtures/scripts/needs-compile.js eol=crlf | ||
test/fixtures/scripts/needs-compile.compiled.js eol=crlf | ||
test/fixtures/scripts/needs-compile.compiled.js.map eol=crlf | ||
test/fixtures/scripts/needs-compile.compiled.js.map eol=crlf | ||
test/fixtures/scripts/branches.covered.js eol=lf | ||
test/fixtures/scripts/branches.js eol=lf | ||
test/fixtures/scripts/functions.js eol=lf |
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 |
---|---|---|
|
@@ -4,3 +4,5 @@ coverage | |
node_modules | ||
.idea | ||
.vscode | ||
*.iml | ||
yarn.lock |
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,33 @@ | ||
/** | ||
* ...something resembling a binary search, to find the lowest line within the range. | ||
* And then you could break as soon as the line is longer than the range... | ||
*/ | ||
module.exports.sliceRange = (lines, startCol, endCol, inclusive = false) => { | ||
let start = 0 | ||
let end = lines.length - 1 | ||
|
||
/** | ||
* I consider this a temporary solution until I find an alternaive way to fix the "off by one issue" | ||
*/ | ||
const extStartCol = inclusive ? startCol - 1 : startCol | ||
|
||
while (start < end) { | ||
const mid = (start + end) >> 1 | ||
if (lines[mid].startCol <= startCol && lines[mid].endCol > extStartCol) { | ||
start = mid | ||
end = start | ||
} else if (lines[mid].startCol > startCol) { | ||
end = mid - 1 | ||
} else { | ||
start = mid + 1 | ||
} | ||
} | ||
if (start === end) { | ||
while (end < lines.length && extStartCol < lines[end].endCol && endCol >= lines[end].startCol) { | ||
++end | ||
} | ||
return lines.slice(start, end) | ||
} else { | ||
return [] | ||
} | ||
} |
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,35 @@ | ||
/* global describe, it */ | ||
const { sliceRange } = require('../lib/range') | ||
|
||
require('tap').mochaGlobals() | ||
require('should') | ||
|
||
describe('range', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thaniks for starting to flesh out this suite 👌 |
||
describe('slice range', () => { | ||
it('can deal with empty arrays', () => { | ||
sliceRange([], 0, 1).should.eql([]) | ||
}) | ||
it('can find lines that match exactly', () => { | ||
const THREE_LINES = [ | ||
{ startCol: 0, endCol: 10 }, | ||
{ startCol: 11, endCol: 20 }, | ||
{ startCol: 21, endCol: 30 } | ||
] | ||
sliceRange(THREE_LINES, 0, 10).should.eql([THREE_LINES[0]]) | ||
sliceRange(THREE_LINES, 11, 20).should.eql([THREE_LINES[1]]) | ||
sliceRange(THREE_LINES, 21, 30).should.eql([THREE_LINES[2]]) | ||
}) | ||
it('can cover a range that spans two lines', () => { | ||
const SIX_LINES = [ | ||
{ startCol: 0, endCol: 3 }, | ||
{ startCol: 4, endCol: 10 }, | ||
{ startCol: 11, endCol: 14 }, | ||
{ startCol: 15, endCol: 20 }, | ||
{ startCol: 21, endCol: 24 }, | ||
{ startCol: 25, endCol: 30 } | ||
] | ||
sliceRange(SIX_LINES, 5, 14).should.eql(SIX_LINES.slice(1, 3)) | ||
sliceRange(SIX_LINES, 15, 21).should.eql(SIX_LINES.slice(3, 5)) | ||
}) | ||
}) | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might not need to change this logic, if we figure out what's going on with the range detection logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I rearranged those ifs to fail fast and avoid the costly originalEndPositionFor...but admittedly it's a nano-optimization...