Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/babel/babel-generator/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,10 @@ namespace BabelGenerator::Buffer {
}
template<PositionType PROP>
void _normalizePosition(PTR(Node::Node) node, BASE::position_t columnOffset) {
auto const &pos = node->loc<PROP>();
auto const &has_pos = node->has_loc<PROP>();
auto &target = this->_sourcePosition;

if (pos) {
if (has_pos) {
// target.line = pos.line;
// TODO: Fix https://github.com/babel/babel/issues/15712 in downstream
// target.column = std::max(pos.column + columnOffset, 0);
Expand Down
13 changes: 6 additions & 7 deletions src/babel/babel-generator/generators/classes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ namespace BabelGenerator::Generators {
if (!node->isStatic() && !_this.format.preserveFormat) {
// catch up to property key, avoid line break
// between member TS modifiers and the property key.
// const endLine = node.key.loc?.end?.line;
// if (endLine) _this.catchUp(endLine);
auto endLine = node->key()->locEndLine(-1);
if (endLine >= 0) _this.catchUp(endLine);
}

classesTsPrintClassMemberModifiers(_this, node);
Expand Down Expand Up @@ -201,8 +201,8 @@ namespace BabelGenerator::Generators {

// catch up to property key, avoid line break
// between member modifiers and the property key.
// const endLine = node.key.loc?.end?.line;
// if (endLine) _this.catchUp(endLine);
auto endLine = node->key()->locEndLine(-1);
if (endLine >= 0) _this.catchUp(endLine);

// TS does not support class accessor property yet
classesTsPrintClassMemberModifiers(_this, node);
Expand Down Expand Up @@ -284,9 +284,8 @@ namespace BabelGenerator::Generators {
if (!_this.format.preserveFormat) {
// catch up to method key, avoid line break
// between member modifiers/method heads and the method key.
// const endLine = node.key.loc?.end?.line;
// auto const& endLine = node->key->locEnd();
// if (endLine) _this.catchUp(endLine);
auto endLine = node->key()->locEndLine(-1);
if (endLine >= 0) _this.catchUp(endLine);
}

classesTsPrintClassMemberModifiers(_this, node);
Expand Down
2 changes: 1 addition & 1 deletion src/babel/babel-generator/generators/typescript.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace BabelGenerator::Generators {

template<typename T>
void maybePrintTrailingCommaOrSemicolon(Printer::Printer &printer, PTR(T) node) {
if (!(printer.tokenMap != nullptr) || !node->position().has_start || !node->position().has_end) {
if (!(printer.tokenMap != nullptr) || !node->position().has_start() || !node->position().has_end()) {
printer.semicolon();
return;
}
Expand Down
115 changes: 57 additions & 58 deletions src/babel/babel-generator/printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -691,29 +691,29 @@
void _catchUp(PTR(Node::Node) node) {
auto const &format = this->format;
if (!format.preserveFormat) {
if (format.retainLines && node && node->loc<PROP>()) {
// this->catchUp(loc[prop].line);
if (format.retainLines && node && node->has_loc<PROP>()) {
this->catchUp(node->locLine<PROP>(-1));

Check warning on line 695 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L695

Added line #L695 was not covered by tests
}
return;
}

// catch up to this nodes newline if we're behind
auto const &pos = node ? node->loc<PROP>() : std::nullopt;
if (pos != std::nullopt)
this->_catchUpTo(*pos);
auto const &pos = node ? node->has_loc<PROP>() : false;

Check warning on line 701 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L701

Added line #L701 was not covered by tests
if (pos)
this->_catchUpTo(node->loc<PROP>());

Check warning on line 703 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L703

Added line #L703 was not covered by tests
}

void _catchUpTo(BabelParser::Util::Location::Position const &pos) {
// const count = line - this->_buf.getCurrentLine();
// if (count > 0 && this->_noLineTerminator) {
// // We cannot inject new lines when _noLineTemrinator is set
// // to `true`, or we would generate invalid code.
// return;
// }
auto count = pos.line() - this->_buf.getCurrentLine();

Check warning on line 707 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L707

Added line #L707 was not covered by tests
if (count > 0 && this->_noLineTerminator) {
// We cannot inject new lines when _noLineTemrinator is set
// to `true`, or we would generate invalid code.
return;

Check warning on line 711 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L711

Added line #L711 was not covered by tests
}

// for (let i = 0; i < count; i++) {
// this->_newline();
// }
for (size_t i = 0; i < count; i++) {
this->_newline();

Check warning on line 715 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L715

Added line #L715 was not covered by tests
}

// const spacesCount =
// count > 0 ? column : column - this->_buf.getCurrentColumn();
Expand Down Expand Up @@ -786,8 +786,7 @@
// }

auto oldInAux = this->_insideAux;
// this->_insideAux = node.loc == null;
this->_insideAux = false;
this->_insideAux = node->position().has_start() == false;
this->_maybeAddAuxComment(this->_insideAux && !oldInAux);

bool parenthesized = node->extra().parenthesized;
Expand Down Expand Up @@ -819,21 +818,22 @@
}

bool indentParenthesized = false;
// if (
// !shouldPrintParens &&
// this->_noLineTerminator &&
// (node.leadingComments?.some(commentIsNewline) ||
// (this->format.retainLines &&
// node.loc &&
// node.loc.start.line > this->_buf.getCurrentLine()))
// ) {
// shouldPrintParens = true;
// indentParenthesized = true;
// }
if (
!shouldPrintParens &&
this->_noLineTerminator &&
(
// node.leadingComments?.some(commentIsNewline) ||
(this->format.retainLines &&
node->position().has_start() &&
node->locStartLine(-1) > this->_buf.getCurrentLine()))

Check warning on line 828 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L827-L828

Added lines #L827 - L828 were not covered by tests
) {
shouldPrintParens = true;
indentParenthesized = true;

Check warning on line 831 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L830-L831

Added lines #L830 - L831 were not covered by tests
}

PTR(Node::Node) oldNoLineTerminatorAfterNode = nullptr;
bool oldInForStatementInitWasTrue = false;
// if (!shouldPrintParens) {
if (!shouldPrintParens) {
// noLineTerminatorAfter ||=
// parent &&
// this->_noLineTerminatorAfterNode === parent &&
Expand All @@ -846,7 +846,7 @@
// this->_noLineTerminatorAfterNode = node;
// }
// }
// }
}

if (shouldPrintParens) {
this->token(u"(");
Expand Down Expand Up @@ -998,13 +998,12 @@
if (iter_begin == iter_end) [[unlikely]]
return;

bool indent;
bool indent = false;
if (OPT.indent == BASE::Undefined && this->format.retainLines) {
// const startLine = nodes->at(0)->locStart()->line;
// if (startLine != null && startLine !== this->_buf.getCurrentLine()) {
// indent = true;
indent = true;
// }
auto position = (*iter_begin)->position();

Check warning on line 1003 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L1003

Added line #L1003 was not covered by tests
if (position.has_start() && position.locStart().line() != this->_buf.getCurrentLine()) {
indent = true;

Check warning on line 1005 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L1005

Added line #L1005 was not covered by tests
}
} else {
indent = toBoolean(OPT.indent);
}
Expand Down Expand Up @@ -1047,7 +1046,7 @@
this->newline(1);
} else {
auto nextNode = *iter_next;
// newlineOpts.nextNodeStartLine = nextNode.loc?.start.line || 0;
nextNodeStartLine = nextNode->locStartLine(0);

this->_printNewline(true, nextNodeStartLine);
}
Expand Down Expand Up @@ -1365,27 +1364,27 @@
) {
// const nodeLoc = node.loc;
// const len = comments.length;
bool hasLoc = node->locStart().has_value();
// const nodeStartLine = hasLoc ? nodeLoc.start.line : 0;
// const nodeEndLine = hasLoc ? nodeLoc.end.line : 0;
bool hasLoc = node->position().has_start();
auto nodeStartLine = node->locStartLine(0);
auto nodeEndLine = node->locEndLine(0);
int64_t lastLine = 0;
int64_t leadingCommentNewline = 0;

size_t i = 0;
for (auto iter = commentsHead; iter != nullptr; iter = iter->next(), i++) {
auto &comment = *iter;
if (comment->commentPos != commentPos) [[unlikely]] {
--i;
continue;
}
// if (comment->commentPos != commentPos) [[unlikely]] {
// --i;
// continue;
// }
PRINT_COMMENT_HINT::Value shouldPrint = this->_shouldPrintComment(comment /*, nextToken*/);
if (shouldPrint == PRINT_COMMENT_HINT::DEFER) {
hasLoc = false;
break;
}
if (hasLoc && shouldPrint == PRINT_COMMENT_HINT::ALLOW) {
auto commentStartLine = comment->locStart().line();
auto commentEndLine = comment->locEnd().line();
auto commentStartLine = comment->locStartLine(-1);
auto commentEndLine = comment->locEndLine(-1);
if constexpr (commentPos == CommentPosition::Leading) {
int64_t offset = 0;
if (i == 0) {
Expand All @@ -1404,29 +1403,29 @@

if (comment.next() == nullptr) {
this->maybeNewline(leadingCommentNewline);
// maybeNewline(
// Math.max(nodeStartLine - lastLine, leadingCommentNewline),
// );
// lastLine = nodeStartLine;
maybeNewline(
std::max(nodeStartLine - lastLine, leadingCommentNewline)
);
lastLine = nodeStartLine;
}
} else if constexpr (commentPos == CommentPosition::Inner) {
// const offset =
// commentStartLine - (i === 0 ? nodeStartLine : lastLine);
auto offset =
commentStartLine - (i == 0 ? nodeStartLine : lastLine);
lastLine = commentEndLine;

// maybeNewline(offset);
maybeNewline(offset);
this->_printComment<COMMENT_SKIP_NEWLINE::ALL>(comment);

if (comment.next() == nullptr) {
// maybeNewline(Math.min(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger
// lastLine = nodeEndLine;
maybeNewline(std::min<BASE::position_t>(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger
lastLine = nodeEndLine;
}
} else {
// const offset =
// commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine);
auto offset =
commentStartLine - (i == 0 ? nodeEndLine - lineOffset : lastLine);
lastLine = commentEndLine;

// maybeNewline(offset);
maybeNewline(offset);
this->_printComment<COMMENT_SKIP_NEWLINE::ALL>(comment);
}
} else {
Expand All @@ -1436,7 +1435,7 @@
}

if (commentsHead->next() == nullptr) {
bool singleLine = comment->locStart().line() == comment->locEnd().line();
bool singleLine = comment->locStartLine(-1) == comment->locEndLine(-1);

Check warning on line 1438 in src/babel/babel-generator/printer.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-generator/printer.hpp#L1438

Added line #L1438 was not covered by tests

bool shouldSkipNewline = singleLine && !BabelTypes::isStatement(node) && !BabelTypes::isClassBody(parent) &&
!BabelTypes::isTSInterfaceBody(parent) && !BabelTypes::isTSEnumMember(node);
Expand Down
26 changes: 13 additions & 13 deletions src/babel/babel-parser/parser/comments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,14 @@
auto comments = commentWS.comments;
if (commentWS.leadingNode != nullptr || commentWS.trailingNode != nullptr) {
if (commentWS.leadingNode != nullptr) {
setTrailingComments(commentWS.leadingNode, comments);
setTrailingComments(commentWS.leadingNode->makeComments(this->allocator.node()), comments);
}
if (commentWS.trailingNode != nullptr) {
setLeadingComments(commentWS.trailingNode, comments);
setLeadingComments(commentWS.trailingNode->makeComments(this->allocator.node()), comments);
}
} else {
/*:: invariant(commentWS.containingNode !== null) */
auto node = commentWS.containingNode;
auto node = commentWS.containingNode->makeComments(this->allocator.node());
auto commentStart = commentWS.start;
if (this->input[this->offsetToSourcePos(commentStart) - 1] == CharCodes::comma) {
// If a commentWhitespace follows a comma and the containingNode allows
Expand Down Expand Up @@ -173,16 +173,16 @@
case Node::Type::ImportDeclaration:
adjustInnerComments(node, NODES(NODEAS(node, ImportDeclaration)->specifiers), commentWS);
break;
// case "TSEnumDeclaration":
// if (!process.env.BABEL_8_BREAKING) {
// adjustInnerComments(node, node.members, commentWS);
// } else {
// setInnerComments(node, comments);
// }
// break;
// case "TSEnumBody":
// adjustInnerComments(node, node.members, commentWS);
// break;
case Node::Type::TSEnumDeclaration:

Check warning on line 176 in src/babel/babel-parser/parser/comments.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-parser/parser/comments.hpp#L176

Added line #L176 was not covered by tests
if (!BASE::process::env.BABEL_8_BREAKING) {
adjustInnerComments(node, NODES(NODEAS(node, TSEnumDeclaration)->members), commentWS);

Check warning on line 178 in src/babel/babel-parser/parser/comments.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-parser/parser/comments.hpp#L178

Added line #L178 was not covered by tests
} else {
setInnerComments(node, comments);

Check warning on line 180 in src/babel/babel-parser/parser/comments.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-parser/parser/comments.hpp#L180

Added line #L180 was not covered by tests
}
break;
case Node::Type::TSEnumBody:
adjustInnerComments(node, NODES(NODEAS(node, TSEnumBody)->members), commentWS);
break;

Check warning on line 185 in src/babel/babel-parser/parser/comments.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-parser/parser/comments.hpp#L182-L185

Added lines #L182 - L185 were not covered by tests
default:
{
setInnerComments(node, comments);
Expand Down
16 changes: 8 additions & 8 deletions src/babel/babel-parser/parser/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
// Store the first redefinition's position, otherwise ignore because
// we are parsing ambiguous pattern
if (refExpressionErrors->doubleProtoLoc == std::nullopt) {
refExpressionErrors->doubleProtoLoc = *key->locStart();
refExpressionErrors->doubleProtoLoc = key->locStart();
}
} else [[unlikely]] {
this->raise(Errors::DuplicateProto, key);
Expand Down Expand Up @@ -387,7 +387,7 @@
this->raise(Errors::PrivateInExpectedIn, left, value->string());
}

this->classScope->usePrivateName(value, *left->locStart());
this->classScope->usePrivateName(value, left->locStart());
}

auto op = this->state.type;
Expand Down Expand Up @@ -1318,7 +1318,7 @@
// then this method will throw a `PipeTopicUnconfiguredToken` error.
PTR(Node::Expression)
finishTopicReference(Node::PositionInfo const &startInfo, Proposal::Value pipeProposal, TokenType tokenType) {
if (this->testTopicReferenceConfiguration(pipeProposal, *startInfo.locStart(), tokenType)) {
if (this->testTopicReferenceConfiguration(pipeProposal, startInfo.locStart(), tokenType)) {
// The token matches the plugin’s configuration.
// The token is therefore a topic reference.

Expand All @@ -1331,7 +1331,7 @@
// Raise recoverable errors.
pipeProposal == Proposal::smart ? Errors::PrimaryTopicNotAllowed : // In this case, `pipeProposal === "hack"` is true.
Errors::PipeTopicUnbound,
*startInfo.locStart()
startInfo.locStart()
);
}
// Register the topic reference so that its pipe body knows
Expand Down Expand Up @@ -2170,7 +2170,7 @@
// IdentifierReference
// CoverInitializedName
// Note: `{ eval } = {}` will be checked in `checkLVal` later.
this->checkReservedWord(NODEAS(prop.key, Identifier)->name, *prop.key->locStart(), true, false);
this->checkReservedWord(NODEAS(prop.key, Identifier)->name, prop.key->locStart(), true, false);

if constexpr (TIsPattern) {
prop.value = this->parseMaybeDefault(startLoc, NODEAS(this->cloneIdentifier(NODEAS(prop.key, Identifier)), Pattern));
Expand Down Expand Up @@ -2440,9 +2440,9 @@
// @ts-expect-error key may not index node
!!inter.key
? // @ts-expect-error node.key has been guarded
*inter.key->locEnd()
inter.key->locEnd()
// : node,
: *inter.position.locEnd()
: inter.position.locEnd()

Check warning on line 2445 in src/babel/babel-parser/parser/expression.hpp

View check run for this annotation

Codecov / codecov/patch

src/babel/babel-parser/parser/expression.hpp#L2445

Added line #L2445 was not covered by tests
);
} else {
this->raise(Errors::IllegalLanguageModeDirective, inter.position);
Expand Down Expand Up @@ -2755,7 +2755,7 @@
PTR(Node::YieldExpression) parseYield() {
auto startInfo = this->startNodeInfo();

this->expressionScope->recordParameterInitializerError(Errors::YieldInParameter, *startInfo.locStart());
this->expressionScope->recordParameterInitializerError(Errors::YieldInParameter, startInfo.locStart());

this->next();
bool delegating = false;
Expand Down
Loading
Loading