Check that selection offset/length is valid in Extract Local refactoring.

R=brianwilkerson@google.com

Bug: https://github.com/dart-lang/sdk/issues/34475
Change-Id: Id2b44494a8464000180a8ca93cfcc770b256f59f
Reviewed-on: https://dart-review.googlesource.com/76063
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2018-09-24 14:55:33 +00:00 committed by commit-bot@chromium.org
parent 7ad1e5da99
commit 2d53894e3f
2 changed files with 50 additions and 2 deletions

View file

@ -227,6 +227,15 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
* location of this [Expression] in AST allows extracting.
*/
RefactoringStatus _checkSelection() {
if (selectionOffset <= 0) {
return new RefactoringStatus.fatal(
'The selection offset must be greater than zero.');
}
if (selectionOffset + selectionLength >= resolveResult.content.length) {
return new RefactoringStatus.fatal(
'The selection end offset must be less then the length of the file.');
}
String selectionStr;
// exclude whitespaces
{

View file

@ -52,6 +52,28 @@ main() {
expectedMessage: "The name 'res' is already used in the scope.");
}
test_checkInitialCondition_false_outOfRange_length() async {
await indexTestUnit('''
main() {
print(1 + 2);
}
''');
_createRefactoring(0, 1 << 20);
RefactoringStatus status = await refactoring.checkAllConditions();
assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL);
}
test_checkInitialCondition_outOfRange_offset() async {
await indexTestUnit('''
main() {
print(1 + 2);
}
''');
_createRefactoring(-10, 20);
RefactoringStatus status = await refactoring.checkAllConditions();
assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL);
}
test_checkInitialConditions_assignmentLeftHandSize() async {
await indexTestUnit('''
main() {
@ -68,8 +90,7 @@ main() {
test_checkInitialConditions_namePartOfDeclaration_function() async {
await indexTestUnit('''
main() {
}
void main() {}
''');
_createRefactoringWithSuffix('main', '()');
// check conditions
@ -695,6 +716,24 @@ main() {
expect(refactoring.names, unorderedEquals(['helloBob', 'bob']));
}
test_isAvailable_false_notPartOfFunction() async {
await indexTestUnit('''
var v = 1 + 2;
''');
_createRefactoringForString('1 + 2');
expect(refactoring.isAvailable(), isFalse);
}
test_isAvailable_true() async {
await indexTestUnit('''
main() {
print(1 + 2);
}
''');
_createRefactoringForString('1 + 2');
expect(refactoring.isAvailable(), isTrue);
}
test_occurrences_differentVariable() async {
await indexTestUnit('''
main() {