Skip to content

Commit

Permalink
Store hashbang comments with unique comment type
Browse files Browse the repository at this point in the history
Summary:
Currently, the Hermes parser stores hashbang comments (comments that begin with `#!` and start at the first byte of the file) as regular line comments. Let's introduce a new `Hashbang` comment type for these comments as they are not actually regular line comments, and so that they can be easily distinguished.

This will be used to distinguish these hashbang comments from other line comments in the Hermes parser compiled to WASM.

Reviewed By: avp

Differential Revision: D24352504

fbshipit-source-id: 653a8c2efd7bd188ee89a64c465905ad2ccdd6ca
  • Loading branch information
Hans Halverson authored and facebook-github-bot committed Oct 28, 2020
1 parent 7cf34b6 commit 8f6404a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 5 additions & 3 deletions include/hermes/Parser/JSLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ class StoredComment {
Line,
/// Comment that is delimited by "/*" and "*/".
Block,
/// Comment that begins with "#!" and starts at the first byte of the file
Hashbang,
};

StoredComment(Kind kind, SMRange range) : kind_(kind), range_(range) {}
Expand All @@ -276,14 +278,14 @@ class StoredComment {
return kind_;
}

/// \return the comment with delimiters (//, /*, */) stripped,
/// \return the comment with delimiters (//, /*, */, #!) stripped,
/// as a StringRef which points into the source buffer.
StringRef getString() const {
// Ignore opening delimiter.
const char *start = range_.Start.getPointer() + 2;
// Conditionally ignore closing delimiter.
const char *end = kind_ == Kind::Line ? range_.End.getPointer()
: range_.End.getPointer() - 2;
const char *end = kind_ == Kind::Block ? range_.End.getPointer() - 2
: range_.End.getPointer();
assert(end >= start && "invalid comment range");
return StringRef{start, (size_t)(end - start)};
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Parser/JSLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,9 @@ const char *JSLexer::skipLineComment(const char *start) {

if (storeComments_) {
commentStorage_.emplace_back(
StoredComment::Kind::Line, SMRange{lineCommentStart, lineCommentEnd});
start[0] == '/' ? StoredComment::Kind::Line
: StoredComment::Kind::Hashbang,
SMRange{lineCommentStart, lineCommentEnd});
}

return cur;
Expand Down
12 changes: 12 additions & 0 deletions unittests/Parser/JSLexerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,18 @@ TEST(JSLexerTest, StoreCommentsTest) {
EXPECT_EQ("world", lex.getStoredComments()[1].getString());
}

{
JSLexer lex("#! hello world\n;", sm, alloc, nullptr, true, false);
lex.setStoreComments(true);

ASSERT_EQ(TokenKind::semi, lex.advance()->getKind());

ASSERT_EQ(1, lex.getStoredComments().size());
EXPECT_EQ(
StoredComment::Kind::Hashbang, lex.getStoredComments()[0].getKind());
EXPECT_EQ(" hello world", lex.getStoredComments()[0].getString());
}

{
JSLexer lex("/**/;//\n", sm, alloc, nullptr, true, false);
lex.setStoreComments(true);
Expand Down

0 comments on commit 8f6404a

Please sign in to comment.