Skip to content

Commit fc542be

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 36682. Check that selection offset/length is valid in Extract Method refactoring.
[email protected] Bug: #36682 Change-Id: I9b15983ff6a872eec14c48bdf29680e6db97a75e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/108480 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 803658a commit fc542be

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

pkg/analysis_server/lib/src/services/refactoring/extract_method.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,15 @@ class ExtractMethodRefactoringImpl extends RefactoringImpl
464464
* location of this [DartExpression] in AST allows extracting.
465465
*/
466466
RefactoringStatus _checkSelection() {
467+
if (selectionOffset <= 0) {
468+
return new RefactoringStatus.fatal(
469+
'The selection offset must be greater than zero.');
470+
}
471+
if (selectionOffset + selectionLength >= resolveResult.content.length) {
472+
return new RefactoringStatus.fatal(
473+
'The selection end offset must be less then the length of the file.');
474+
}
475+
467476
// Check for implicitly selected closure.
468477
{
469478
FunctionExpression function = _findFunctionExpression();

pkg/analysis_server/test/services/refactoring/extract_method_test.dart

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ main() {
296296

297297
test_bad_namePartOfDeclaration_function() async {
298298
await indexTestUnit('''
299-
main() {
299+
void main() {
300300
}
301301
''');
302302
_createRefactoringForString('main');
@@ -762,6 +762,28 @@ main() {
762762
expect(refactoring.createGetter, true);
763763
}
764764

765+
test_checkInitialCondition_false_outOfRange_length() async {
766+
await indexTestUnit('''
767+
main() {
768+
1 + 2;
769+
}
770+
''');
771+
_createRefactoring(0, 1 << 20);
772+
RefactoringStatus status = await refactoring.checkAllConditions();
773+
assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL);
774+
}
775+
776+
test_checkInitialCondition_outOfRange_offset() async {
777+
await indexTestUnit('''
778+
main() {
779+
1 + 2;
780+
}
781+
''');
782+
_createRefactoring(-10, 20);
783+
RefactoringStatus status = await refactoring.checkAllConditions();
784+
assertRefactoringStatus(status, RefactoringProblemSeverity.FATAL);
785+
}
786+
765787
test_checkName() async {
766788
await indexTestUnit('''
767789
main() {
@@ -1071,6 +1093,24 @@ class A {
10711093
expect(refactoring.refactoringName, 'Extract Method');
10721094
}
10731095

1096+
test_isAvailable_false_functionName() async {
1097+
await indexTestUnit('''
1098+
void main() {}
1099+
''');
1100+
_createRefactoringForString('main');
1101+
expect(refactoring.isAvailable(), isFalse);
1102+
}
1103+
1104+
test_isAvailable_true() async {
1105+
await indexTestUnit('''
1106+
main() {
1107+
1 + 2;
1108+
}
1109+
''');
1110+
_createRefactoringForString('1 + 2');
1111+
expect(refactoring.isAvailable(), isTrue);
1112+
}
1113+
10741114
test_names_singleExpression() async {
10751115
await indexTestUnit('''
10761116
class TreeItem {}

0 commit comments

Comments
 (0)