diff options
author | Jan Kuhle <jankuehle@google.com> | 2023-05-10 15:11:50 +0200 |
---|---|---|
committer | Krasimir Georgiev <krasimir@google.com> | 2023-05-10 15:27:03 +0200 |
commit | 34b422bafbd934ee3c644fd7a8f0b6803976c818 (patch) | |
tree | a90f600db1dc1bab0968b3f1dfefd24325b89738 /clang/unittests | |
parent | a414db1d8e3173faca024d4532a4c655a1fe02c0 (diff) | |
download | llvm-34b422bafbd934ee3c644fd7a8f0b6803976c818.tar.gz |
clang-format: [JS] support import/export type
Contributed by @jankuehle!
Users can choose to only import/export the type of the symbol (not value nor namespace) by adding a `type` keyword, e.g.:
```
import type {x} from 'y';
import {type x} from 'y';
export type {x};
export {type x};
```
Previously, this was not handled and would:
- Terminate import sorting
- Remove the space before the curly bracket in `export type {`
With this change, both formatting and import sorting work as expected.
Reviewed By: MyDeveloperDay, krasimir
Differential Revision: https://reviews.llvm.org/D150116
Diffstat (limited to 'clang/unittests')
-rw-r--r-- | clang/unittests/Format/FormatTestJS.cpp | 11 | ||||
-rw-r--r-- | clang/unittests/Format/SortImportsTestJS.cpp | 40 |
2 files changed, 51 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTestJS.cpp b/clang/unittests/Format/FormatTestJS.cpp index 6f07d8b084f5..ce81bd5d2f04 100644 --- a/clang/unittests/Format/FormatTestJS.cpp +++ b/clang/unittests/Format/FormatTestJS.cpp @@ -1476,6 +1476,17 @@ TEST_F(FormatTestJS, ImportExportASI) { " export class Y {}"); } +TEST_F(FormatTestJS, ImportExportType) { + verifyFormat("import type {x, y} from 'y';\n" + "import type * as x from 'y';\n" + "import type x from 'y';\n" + "import {x, type yu, z} from 'y';\n"); + verifyFormat("export type {x, y} from 'y';\n" + "export {x, type yu, z} from 'y';\n" + "export type {x, y};\n" + "export {x, type yu, z};\n"); +} + TEST_F(FormatTestJS, ClosureStyleCasts) { verifyFormat("var x = /** @type {foo} */ (bar);"); } diff --git a/clang/unittests/Format/SortImportsTestJS.cpp b/clang/unittests/Format/SortImportsTestJS.cpp index 1c7e50863036..9d779cd988d0 100644 --- a/clang/unittests/Format/SortImportsTestJS.cpp +++ b/clang/unittests/Format/SortImportsTestJS.cpp @@ -465,6 +465,46 @@ TEST_F(SortImportsTestJS, ImportEqAliases) { "console.log(Z);\n"); } +TEST_F(SortImportsTestJS, ImportExportType) { + verifySort("import type {sym} from 'a';\n" + "import {type sym} from 'b';\n" + "import {sym} from 'c';\n" + "import type sym from 'd';\n" + "import type * as sym from 'e';\n" + "\n" + "let x = 1;", + "import {sym} from 'c';\n" + "import type {sym} from 'a';\n" + "import type * as sym from 'e';\n" + "import type sym from 'd';\n" + "import {type sym} from 'b';\n" + "let x = 1;"); + + // Symbols within import statement + verifySort("import {type sym1, type sym2 as a, sym3} from 'b';\n", + "import {type sym2 as a, type sym1, sym3} from 'b';\n"); + + // Merging + verifySort("import {X, type Z} from 'a';\n" + "import type {Y} from 'a';\n" + "\n" + "X + Y + Z;\n", + "import {X} from 'a';\n" + "import {type Z} from 'a';\n" + "import type {Y} from 'a';\n" + "\n" + "X + Y + Z;\n"); + + // Merging: empty imports + verifySort("import type {A} from 'foo';\n", "import type {} from 'foo';\n" + "import type {A} from 'foo';"); + + // Merging: exports + verifySort("export {A, type B} from 'foo';\n", + "export {A} from 'foo';\n" + "export {type B} from 'foo';"); +} + } // end namespace } // end namespace format } // end namespace clang |