summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-06-18 19:11:26 +0100
committerLinus Groh <mail@linusgroh.de>2021-06-18 20:35:23 +0100
commit597cf88c08540984d825195d5e46d62434b352d1 (patch)
treeff1ebb6a1e92ad50df6e3c28b9631043a819254f /Userland/Libraries
parent299c3069c1024514de26c697bfcee8b75d906762 (diff)
downloadserenity-597cf88c08540984d825195d5e46d62434b352d1.zip
LibJS: Implement the 'Hashbang Grammar for JS' proposal
Stage 3 since August 2019 - we already have shebang stripping implemented in js(1), so this removes it from there in favor of adding support to the lexer directly. Most straightforward proposal and implementation I've ever seen :^) https://github.com/tc39/proposal-hashbang
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Lexer.cpp4
-rw-r--r--Userland/Libraries/LibJS/Tests/comments-basic.js9
2 files changed, 12 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Lexer.cpp b/Userland/Libraries/LibJS/Lexer.cpp
index cf4c052072..1245817d5b 100644
--- a/Userland/Libraries/LibJS/Lexer.cpp
+++ b/Userland/Libraries/LibJS/Lexer.cpp
@@ -308,7 +308,9 @@ bool Lexer::is_line_comment_start(bool line_has_token_yet) const
// "-->" is considered a line comment start if the current line is only whitespace and/or
// other block comment(s); or in other words: the current line does not have a token or
// ongoing line comment yet
- || (match('-', '-', '>') && !line_has_token_yet);
+ || (match('-', '-', '>') && !line_has_token_yet)
+ // https://tc39.es/proposal-hashbang/out.html#sec-updated-syntax
+ || (match('#', '!') && m_position == 1);
}
bool Lexer::is_block_comment_start() const
diff --git a/Userland/Libraries/LibJS/Tests/comments-basic.js b/Userland/Libraries/LibJS/Tests/comments-basic.js
index 101e520a29..7ad452bf29 100644
--- a/Userland/Libraries/LibJS/Tests/comments-basic.js
+++ b/Userland/Libraries/LibJS/Tests/comments-basic.js
@@ -33,3 +33,12 @@ test("unterminated multi-line comment", () => {
expect("/* foo").not.toEval();
expect("foo /*").not.toEval();
});
+
+test("hashbang comments", () => {
+ expect("#!").toEvalTo(undefined);
+ expect("#!/bin/js").toEvalTo(undefined);
+ expect("#!\n1").toEvalTo(1);
+ expect(" #!").not.toEval();
+ expect("\n#!").not.toEval();
+ expect("#!\n#!").not.toEval();
+});