Shebang lines (#!/bin/blah) can be used in JavaScript scripts to indicate
they should be run using e.g. node. This change treats # lines on the first line
as line comments.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
Thinking some more, I think this is actually very close to what we do for #include(-like) statements. That is done in TokenAnnotator::parseLine() and TokenAnnotator::parseIncludeDirective(). Could you move the logic there?
How would I disable the formatting using that way?
When I special case parsePreprocessorDirective(), consume the line, and return LT_ImportStatement, clang-format still tries to format tokens within the shebang. E.g. I get #!/usr/bin / env node, note the whitespace after bin.
Turning the shebang into a comment seems like it has the advantage of being safe, its contents should simply never be touched.
That's the same for #include directives (with <>). Just turn the tokens into TT_ImplicitStringLiteral, same as is done for #includes. I am not saying it's better, but I don't think we should have to different approaches..
lib/Format/FormatTokenLexer.cpp | ||
---|---|---|
23 ↗ | (On Diff #58529) | revert this file |
lib/Format/TokenAnnotator.cpp | ||
698 ↗ | (On Diff #58529) | should we still restrict it to be on the first line? I suppose if you start some other line with #, that's not valid JS or TS so it doesn't matter what clang-format does to it? Maybe it's fine like this. |
lib/Format/TokenAnnotator.cpp | ||
---|---|---|
698 ↗ | (On Diff #58529) | Done. Doesn't make a big difference I guess, but it's better to be conservative. |