summaryrefslogtreecommitdiffstats
path: root/unittests
diff options
context:
space:
mode:
authorMartin Probst <martin@probst.io>2017-07-18 14:00:19 +0000
committerMartin Probst <martin@probst.io>2017-07-18 14:00:19 +0000
commit2e7468f0ae6383a87d726d31cde0620cb4b4852d (patch)
tree5379412a4b9a202e02f0f3ccb82ae08d5edd3351 /unittests
parentae9459cceb59b4f1f440dfaf07122c437ccd2d2a (diff)
clang-format: [JS] Correctly format JavaScript imports with long module paths
Currently the `UnwrappedLineParser` fails to correctly unwrap JavaScript imports where the module path is not on the same line as the `from` keyword. For example: import {A} from 'some/path/longer/than/column/limit/module.js';``` This causes issues when in the middle a list of imports because the formatter thinks it has reached the end of the imports, and therefore will not sort any imports lower in the list. The formatter will, however, split the `from` keyword and the module path if the path exceeds the column limit, which triggers the issue the next time the file is formatted. Patch originally by Jared Neil - thanks! Differential Revision: https://reviews.llvm.org/D34920 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@308306 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r--unittests/Format/FormatTestJS.cpp11
-rw-r--r--unittests/Format/SortImportsTestJS.cpp17
2 files changed, 28 insertions, 0 deletions
diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp
index 11e386a1c7..c256ebe462 100644
--- a/unittests/Format/FormatTestJS.cpp
+++ b/unittests/Format/FormatTestJS.cpp
@@ -1464,6 +1464,17 @@ TEST_F(FormatTestJS, ImportWrapping) {
" A,\n"
"} from 'some/module.js';",
Style);
+ Style.ColumnLimit = 40;
+ // Using this version of verifyFormat because test::messUp hides the issue.
+ verifyFormat("import {\n"
+ " A,\n"
+ "} from\n"
+ " 'some/path/longer/than/column/limit/module.js';",
+ " import { \n"
+ " A, \n"
+ " } from\n"
+ " 'some/path/longer/than/column/limit/module.js' ; ",
+ Style);
}
TEST_F(FormatTestJS, TemplateStrings) {
diff --git a/unittests/Format/SortImportsTestJS.cpp b/unittests/Format/SortImportsTestJS.cpp
index 7e766e1969..4208b29702 100644
--- a/unittests/Format/SortImportsTestJS.cpp
+++ b/unittests/Format/SortImportsTestJS.cpp
@@ -283,6 +283,23 @@ TEST_F(SortImportsTestJS, SortCaseInsensitive) {
"1;");
}
+TEST_F(SortImportsTestJS, SortMultiLine) {
+ // Reproduces issue where multi-line import was not parsed correctly.
+ verifySort("import {A} from 'a';\n"
+ "import {A} from 'b';\n"
+ "\n"
+ "1;",
+ "import\n"
+ "{\n"
+ "A\n"
+ "}\n"
+ "from\n"
+ "'b';\n"
+ "import {A} from 'a';\n"
+ "\n"
+ "1;");
+}
+
} // end namespace
} // end namespace format
} // end namespace clang