SourceLocation Start;
bool FoundLines = false;
AnnotatedLine *FirstNonImportLine = nullptr;
+ bool AnyImportAffected = false;
for (auto Line : AnnotatedLines) {
- if (!Line->Affected) {
- // Only sort the first contiguous block of affected lines.
- if (FoundLines)
- break;
- else
- continue;
- }
Current = Line->First;
LineEnd = Line->Last;
skipComments();
FirstNonImportLine = Line;
break;
}
+ AnyImportAffected = AnyImportAffected || Line->Affected;
Reference.Range.setEnd(LineEnd->Tok.getEndLoc());
DEBUG({
llvm::dbgs() << "JsModuleReference: {"
References.push_back(Reference);
Start = SourceLocation();
}
+ // Sort imports if any import line was affected.
+ if (!AnyImportAffected)
+ References.clear();
return std::make_pair(References, FirstNonImportLine);
}
}
TEST_F(SortImportsTestJS, AffectedRange) {
- // Sort excluding a suffix.
- verifySort("import {sym} from 'b';\n"
+ // Affected range inside of import statements.
+ verifySort("import {sym} from 'a';\n"
+ "import {sym} from 'b';\n"
"import {sym} from 'c';\n"
- "import {sym} from 'a';\n"
+ "\n"
"let x = 1;",
"import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
"let x = 1;",
0, 30);
- // Sort excluding a prefix.
+ // Affected range outside of import statements.
verifySort("import {sym} from 'c';\n"
- "import {sym} from 'a';\n"
- "import {sym} from 'b';\n"
- "\n"
- "let x = 1;",
- "import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
"\n"
"let x = 1;",
- 30, 0);
- // Sort a range within imports.
- verifySort("import {sym} from 'c';\n"
- "import {sym} from 'a';\n"
- "import {sym} from 'b';\n"
- "import {sym} from 'c';\n"
- "let x = 1;",
"import {sym} from 'c';\n"
"import {sym} from 'b';\n"
"import {sym} from 'a';\n"
- "import {sym} from 'c';\n"
+ "\n"
"let x = 1;",
- 24, 30);
+ 70, 1);
}
TEST_F(SortImportsTestJS, SortingCanShrink) {