[clang] f6740fe - [clang-format] Indent import statements in JavaScript.
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 30 16:20:00 PDT 2022
Author: sstwcw
Date: 2022-03-30T23:17:27Z
New Revision: f6740fe483e9fa0c76aa9f176ff68f51f47a1302
URL: https://github.com/llvm/llvm-project/commit/f6740fe483e9fa0c76aa9f176ff68f51f47a1302
DIFF: https://github.com/llvm/llvm-project/commit/f6740fe483e9fa0c76aa9f176ff68f51f47a1302.diff
LOG: [clang-format] Indent import statements in JavaScript.
[clang-format] Indent import statements in JavaScript.
Take for example this piece of code found at
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import>.
```
for (const link of document.querySelectorAll("nav > a")) {
link.addEventListener("click", e => {
e.preventDefault();
import('/modules/my-module.js')
.then(module => {
module.loadPageInto(main);
})
.catch(err => {
main.textContent = err.message;
});
});
}
```
Previously the import line would be unindented, looking like this.
```
for (const link of document.querySelectorAll("nav > a")) {
link.addEventListener("click", e => {
e.preventDefault();
import('/modules/my-module.js')
.then(module => {
module.loadPageInto(main);
})
.catch(err => {
main.textContent = err.message;
});
});
}
```
Actually we were going to fix this along with fixing Verilog import
statements. But the patch got too big.
Reviewed By: MyDeveloperDay, curdeius
Differential Revision: https://reviews.llvm.org/D121906
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTestJS.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 1393a2a321183..e2dbc35c22004 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1431,8 +1431,10 @@ void UnwrappedLineFormatter::formatFirstToken(
if (Newlines)
Indent = NewlineIndent;
- // Preprocessor directives get indented before the hash only if specified
- if (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
+ // Preprocessor directives get indented before the hash only if specified. In
+ // Javascript import statements are indented like normal statements.
+ if (!Style.isJavaScript() &&
+ Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
(Line.Type == LT_PreprocessorDirective ||
Line.Type == LT_ImportStatement))
Indent = 0;
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp
index 6077d21c5e582..9883aae62191a 100644
--- a/clang/unittests/Format/FormatTestJS.cpp
+++ b/clang/unittests/Format/FormatTestJS.cpp
@@ -1875,6 +1875,11 @@ TEST_F(FormatTestJS, Modules) {
" myX} from 'm';");
verifyFormat("import * as lib from 'some/module.js';");
verifyFormat("var x = {import: 1};\nx.import = 2;");
+ // Ensure an import statement inside a block is at the correct level.
+ verifyFormat("function() {\n"
+ " var x;\n"
+ " import 'some/module.js';\n"
+ "}");
verifyFormat("export function fn() {\n"
" return 'fn';\n"
More information about the cfe-commits
mailing list