Skip to content

Commit 8c7385f

Browse files
authored
Eliminate addJSDocComment in favor of consistently using withJSDoc (#54802)
1 parent e4cc532 commit 8c7385f

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/compiler/parser.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,8 @@ namespace Parser {
17781778

17791779
const statements = parseList(ParsingContext.SourceElements, parseStatement);
17801780
Debug.assert(token() === SyntaxKind.EndOfFileToken);
1781-
const endOfFileToken = addJSDocComment(parseTokenNode<EndOfFileToken>());
1781+
const endHasJSDoc = hasPrecedingJSDocComment();
1782+
const endOfFileToken = withJSDoc(parseTokenNode<EndOfFileToken>(), endHasJSDoc);
17821783

17831784
const sourceFile = createSourceFile(fileName, languageVersion, scriptKind, isDeclarationFile, statements, endOfFileToken, sourceFlags, setExternalModuleIndicator);
17841785

@@ -1806,12 +1807,12 @@ namespace Parser {
18061807
}
18071808
}
18081809

1810+
let hasDeprecatedTag = false;
18091811
function withJSDoc<T extends HasJSDoc>(node: T, hasJSDoc: boolean): T {
1810-
return hasJSDoc ? addJSDocComment(node) : node;
1811-
}
1812+
if (!hasJSDoc) {
1813+
return node;
1814+
}
18121815

1813-
let hasDeprecatedTag = false;
1814-
function addJSDocComment<T extends HasJSDoc>(node: T): T {
18151816
Debug.assert(!node.jsDoc); // Should only be called once per node
18161817
const jsDoc = mapDefined(getJSDocCommentRanges(node, sourceText), comment => JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos));
18171818
if (jsDoc.length) node.jsDoc = jsDoc;
@@ -5025,13 +5026,14 @@ namespace Parser {
50255026
// binary expression here, so we pass in the 'lowest' precedence here so that it matches
50265027
// and consumes anything.
50275028
const pos = getNodePos();
5029+
const hasJSDoc = hasPrecedingJSDocComment();
50285030
const expr = parseBinaryExpressionOrHigher(OperatorPrecedence.Lowest);
50295031

50305032
// To avoid a look-ahead, we did not handle the case of an arrow function with a single un-parenthesized
50315033
// parameter ('x => ...') above. We handle it here by checking if the parsed expression was a single
50325034
// identifier and the current token is an arrow.
50335035
if (expr.kind === SyntaxKind.Identifier && token() === SyntaxKind.EqualsGreaterThanToken) {
5034-
return parseSimpleArrowFunctionExpression(pos, expr as Identifier, allowReturnTypeInArrowFunction, /*asyncModifier*/ undefined);
5036+
return parseSimpleArrowFunctionExpression(pos, expr as Identifier, allowReturnTypeInArrowFunction, hasJSDoc, /*asyncModifier*/ undefined);
50355037
}
50365038

50375039
// Now see if we might be in cases '2' or '3'.
@@ -5107,7 +5109,7 @@ namespace Parser {
51075109
}
51085110
}
51095111

5110-
function parseSimpleArrowFunctionExpression(pos: number, identifier: Identifier, allowReturnTypeInArrowFunction: boolean, asyncModifier?: NodeArray<Modifier> | undefined): ArrowFunction {
5112+
function parseSimpleArrowFunctionExpression(pos: number, identifier: Identifier, allowReturnTypeInArrowFunction: boolean, hasJSDoc: boolean, asyncModifier?: NodeArray<Modifier> | undefined): ArrowFunction {
51115113
Debug.assert(token() === SyntaxKind.EqualsGreaterThanToken, "parseSimpleArrowFunctionExpression should only have been called if we had a =>");
51125114
const parameter = factory.createParameterDeclaration(
51135115
/*modifiers*/ undefined,
@@ -5123,7 +5125,7 @@ namespace Parser {
51235125
const equalsGreaterThanToken = parseExpectedToken(SyntaxKind.EqualsGreaterThanToken);
51245126
const body = parseArrowFunctionExpressionBody(/*isAsync*/ !!asyncModifier, allowReturnTypeInArrowFunction);
51255127
const node = factory.createArrowFunction(asyncModifier, /*typeParameters*/ undefined, parameters, /*type*/ undefined, equalsGreaterThanToken, body);
5126-
return addJSDocComment(finishNode(node, pos));
5128+
return withJSDoc(finishNode(node, pos), hasJSDoc);
51275129
}
51285130

51295131
function tryParseParenthesizedArrowFunctionExpression(allowReturnTypeInArrowFunction: boolean): Expression | undefined {
@@ -5310,9 +5312,10 @@ namespace Parser {
53105312
if (token() === SyntaxKind.AsyncKeyword) {
53115313
if (lookAhead(isUnParenthesizedAsyncArrowFunctionWorker) === Tristate.True) {
53125314
const pos = getNodePos();
5315+
const hasJSDoc = hasPrecedingJSDocComment();
53135316
const asyncModifier = parseModifiersForArrowFunction();
53145317
const expr = parseBinaryExpressionOrHigher(OperatorPrecedence.Lowest);
5315-
return parseSimpleArrowFunctionExpression(pos, expr as Identifier, allowReturnTypeInArrowFunction, asyncModifier);
5318+
return parseSimpleArrowFunctionExpression(pos, expr as Identifier, allowReturnTypeInArrowFunction, hasJSDoc, asyncModifier);
53165319
}
53175320
}
53185321
return undefined;

0 commit comments

Comments
 (0)