HomePhabricator

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

Authored by mprobst on Apr 29 2021, 2:35 AM.

Description

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

Details

Committed
mprobstApr 30 2021, 5:18 AM
Differential Revision
D101515: clang-format: [JS] handle "off" in imports
Parents
rG7861cb600cd6: [NARY] Don't optimize min/max if there are side uses (part2)
Branches
Unknown
Tags
Unknown