Skip to content

Commit 48bbbbf

Browse files
authored
fix: don't bail on shorthand 'get' in literal exports (#37)
1 parent f9f6c4a commit 48bbbbf

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/parser.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -773,20 +773,14 @@ class CJSLexer {
773773
const char* endPos = pos;
774774
ch = commentWhitespace();
775775

776-
// Check if this is a getter syntax: get identifier()
777-
if (ch != ':' && endPos - startPos == 3 && matchesAt(startPos, end, "get")) {
778-
// Skip getter: get identifier() { ... }
779-
if (identifier(ch)) {
780-
ch = commentWhitespace();
781-
if (ch == '(') {
782-
// This is a getter, stop parsing here (early termination)
783-
pos = revertPos;
784-
return;
785-
}
776+
// Check if this is a getter syntax: get identifier() { ... }
777+
if (ch != ':' && endPos - startPos == 3 && matchesAt(startPos, end, "get") && identifier(ch)) {
778+
ch = commentWhitespace();
779+
if (ch == '(') {
780+
// This is a getter, stop parsing here (early termination)
781+
pos = revertPos;
782+
return;
786783
}
787-
// Not a getter, revert and fail
788-
pos = revertPos;
789-
return;
790784
}
791785

792786
if (ch == ':') {

tests/real_world_tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,18 @@ TEST(real_world_tests, exports_shorthand_syntax) {
11331133
SUCCEED();
11341134
}
11351135

1136+
TEST(real_world_tests, exports_shorthand_syntax_get) {
1137+
auto result = lexer::parse_commonjs("\
1138+
const get = 1, set = 2;\
1139+
module.exports = { get, set };\
1140+
");
1141+
ASSERT_TRUE(result.has_value());
1142+
ASSERT_EQ(result->exports.size(), 2);
1143+
ASSERT_EQ(lexer::get_string_view(result->exports[0]), "get");
1144+
ASSERT_EQ(lexer::get_string_view(result->exports[1]), "set");
1145+
SUCCEED();
1146+
}
1147+
11361148
TEST(real_world_tests, line_numbers_lf) {
11371149
auto result = lexer::parse_commonjs(
11381150
"// line 1\n"

0 commit comments

Comments
 (0)