sstwcw created this revision. sstwcw added a reviewer: clang-format. sstwcw added a project: clang-format. Herald added a project: All. sstwcw requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
[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. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D121906 Files: clang/lib/Format/ContinuationIndenter.cpp clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTestJS.cpp Index: clang/unittests/Format/FormatTestJS.cpp =================================================================== --- clang/unittests/Format/FormatTestJS.cpp +++ clang/unittests/Format/FormatTestJS.cpp @@ -1868,6 +1868,11 @@ " 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" Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1431,8 +1431,10 @@ 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.Language != FormatStyle::LK_JavaScript && + Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && (Line.Type == LT_PreprocessorDirective || Line.Type == LT_ImportStatement)) Indent = 0; Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -623,9 +623,11 @@ unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; - // Indent preprocessor directives after the hash if required. + // Indent preprocessor directives after the hash if required. In Javascript + // import statements are indented like normal statements. int PPColumnCorrection = 0; - if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && + if (Style.Language != FormatStyle::LK_JavaScript && + Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && Previous.is(tok::hash) && State.FirstIndent > 0 && (State.Line->Type == LT_PreprocessorDirective || State.Line->Type == LT_ImportStatement)) {
Index: clang/unittests/Format/FormatTestJS.cpp =================================================================== --- clang/unittests/Format/FormatTestJS.cpp +++ clang/unittests/Format/FormatTestJS.cpp @@ -1868,6 +1868,11 @@ " 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" Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1431,8 +1431,10 @@ 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.Language != FormatStyle::LK_JavaScript && + Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash && (Line.Type == LT_PreprocessorDirective || Line.Type == LT_ImportStatement)) Indent = 0; Index: clang/lib/Format/ContinuationIndenter.cpp =================================================================== --- clang/lib/Format/ContinuationIndenter.cpp +++ clang/lib/Format/ContinuationIndenter.cpp @@ -623,9 +623,11 @@ unsigned Spaces = Current.SpacesRequiredBefore + ExtraSpaces; - // Indent preprocessor directives after the hash if required. + // Indent preprocessor directives after the hash if required. In Javascript + // import statements are indented like normal statements. int PPColumnCorrection = 0; - if (Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && + if (Style.Language != FormatStyle::LK_JavaScript && + Style.IndentPPDirectives == FormatStyle::PPDIS_AfterHash && Previous.is(tok::hash) && State.FirstIndent > 0 && (State.Line->Type == LT_PreprocessorDirective || State.Line->Type == LT_ImportStatement)) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits