[all-commits] [llvm/llvm-project] b2780c: clang-format: [JS] handle "off" in imports

Martin Probst via All-commits all-commits at lists.llvm.org
Fri Apr 30 05:19:18 PDT 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: b2780cd744eaad6f5c7f39165054cf7000a1ff07
      https://github.com/llvm/llvm-project/commit/b2780cd744eaad6f5c7f39165054cf7000a1ff07
  Author: Martin Probst <martin at probst.io>
  Date:   2021-04-30 (Fri, 30 Apr 2021)

  Changed paths:
    M clang/lib/Format/SortJavaScriptImports.cpp
    M clang/unittests/Format/SortImportsTestJS.cpp

  Log Message:
  -----------
  clang-format: [JS] handle "off" in imports

Previously, the JavaScript import sorter would ignore `// clang-format
off` and `on` comments. This change fixes that. It tracks whether
formatting is enabled for a stretch of imports, and then only sorts and
merges the imports where formatting is enabled, in individual chunks.

This means that there's no meaningful total order when module references are mixed
with blocks that have formatting disabled. The alternative approach
would have been to sort all imports that have formatting enabled in one
group. However that raises the question where to insert the
formatting-off block, which can also impact symbol visibility (in
particular for exports). In practice, sorting in chunks probably isn't a
big problem.

This change also simplifies the general algorithm: instead of tracking
indices separately and sorting them, it just sorts the vector of module
references. And instead of attempting to do fine grained tracking of
whether the code changed order, it just prints out the module references
text, and compares that to the previous text. Given that source files
typically have dozens, but not even hundreds of imports, the performance
impact seems negligible.

Differential Revision: https://reviews.llvm.org/D101515




More information about the All-commits mailing list