Index: cfe/trunk/lib/Format/SortJavaScriptImports.cpp =================================================================== --- cfe/trunk/lib/Format/SortJavaScriptImports.cpp +++ cfe/trunk/lib/Format/SortJavaScriptImports.cpp @@ -293,14 +293,19 @@ // of the import that immediately follows them by using the previously // set Start. Start = Line->First->Tok.getLocation(); - if (!Current) - continue; // Only comments on this line. + if (!Current) { + // Only comments on this line. Could be the first non-import line. + FirstNonImportLine = Line; + continue; + } JsModuleReference Reference; Reference.Range.setBegin(Start); if (!parseModuleReference(Keywords, Reference)) { - FirstNonImportLine = Line; + if (!FirstNonImportLine) + FirstNonImportLine = Line; // if no comment before. break; } + FirstNonImportLine = nullptr; AnyImportAffected = AnyImportAffected || Line->Affected; Reference.Range.setEnd(LineEnd->Tok.getEndLoc()); DEBUG({ Index: cfe/trunk/unittests/Format/SortImportsTestJS.cpp =================================================================== --- cfe/trunk/unittests/Format/SortImportsTestJS.cpp +++ cfe/trunk/unittests/Format/SortImportsTestJS.cpp @@ -121,6 +121,16 @@ "import {sym} from 'b'; // from //foo:bar\n" "// A very important import follows.\n" "import {sym} from 'a'; /* more comments */\n"); + verifySort("import {sym} from 'a';\n" + "import {sym} from 'b';\n" + "\n" + "/** Comment on variable. */\n" + "const x = 1;\n", + "import {sym} from 'b';\n" + "import {sym} from 'a';\n" + "\n" + "/** Comment on variable. */\n" + "const x = 1;\n"); } TEST_F(SortImportsTestJS, SortStar) {