This is an archive of the discontinued LLVM Phabricator instance.

clang-format: [JS] Support shebang lines on the very first line.
ClosedPublic

Authored by mprobst on May 25 2016, 10:31 AM.

Details

Summary

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.

Diff Detail

Event Timeline

mprobst updated this revision to Diff 58451.May 25 2016, 10:31 AM
mprobst retitled this revision from to clang-format: [JS] Support shebang lines on the very first line..
mprobst updated this object.
mprobst added a reviewer: djasper.
mprobst added a subscriber: cfe-commits.
djasper edited edge metadata.May 25 2016, 10:47 AM

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..

mprobst updated this revision to Diff 58529.May 25 2016, 3:56 PM
mprobst edited edge metadata.
  • use #include style shebang parsing.
alexeagle added inline comments.
lib/Format/FormatTokenLexer.cpp
23 ↗(On Diff #58529)

revert this file

lib/Format/TokenAnnotator.cpp
699

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.

mprobst marked 2 inline comments as done.May 26 2016, 7:40 AM
mprobst added inline comments.
lib/Format/TokenAnnotator.cpp
699

Done. Doesn't make a big difference I guess, but it's better to be conservative.

mprobst updated this revision to Diff 58612.May 26 2016, 7:42 AM
mprobst marked an inline comment as done.

revert FormatTokenLexer, restrict to first token

ping @djasper we need this fix in order to format angular 2 repo again

Alex, do you accept this revision?

alexeagle accepted this revision.May 28 2016, 7:46 AM
alexeagle added a reviewer: alexeagle.
This revision is now accepted and ready to land.May 28 2016, 7:46 AM
djasper accepted this revision.May 29 2016, 7:04 AM
djasper edited edge metadata.

Looks good.

This revision was automatically updated to reflect the committed changes.