Skip to content

Ensure findPrecedingToken recurses into JSDoc children when needed #53487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 12, 2023
Merged
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
9 changes: 8 additions & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,14 @@ export function findPrecedingToken(position: number, sourceFile: SourceFileLike,
if (lookInPreviousChild) {
// actual start of the node is past the position - previous token should be at the end of previous child
const candidate = findRightmostChildNodeWithTokens(children, /*exclusiveStartPosition*/ i, sourceFile, n.kind);
return candidate && findRightmostToken(candidate, sourceFile);
if (candidate) {
// Ensure we recurse into JSDoc nodes with children.
if (!excludeJsdoc && isJSDocCommentContainingNode(candidate) && candidate.getChildren(sourceFile).length) {
return find(candidate);
}
return findRightmostToken(candidate, sourceFile);
}
return undefined;
}
else {
// candidate should be in this node
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import "./unittests/services/patternMatcher";
import "./unittests/services/preProcessFile";
import "./unittests/services/textChanges";
import "./unittests/services/transpile";
import "./unittests/services/utilities";
import "./unittests/tsbuild/amdModulesWithOut";
import "./unittests/tsbuild/clean";
import "./unittests/tsbuild/commandLine";
Expand Down
20 changes: 20 additions & 0 deletions src/testRunner/unittests/services/utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as ts from "../../_namespaces/ts";

describe("unittests:: services:: utilities", () => {
describe("Test findPrecedingMatchingToken,", () => {
it("should not infinite loop finding opening brace", () => {
const sourceFile = ts.createSourceFile("file.ts", `/// <reference path="./compiler.d.ts" />

Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need the triple slash reference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, probably not.

(/** @window => {
/** @type {Abcd123} */
const core = window.Abcd.core;
})();`, ts.ScriptTarget.ESNext, /*setParentNodes*/ true);
// can't use ts.getTokenAtPosition because it returns back the wrong token
const param = ts.forEachChildRecursively(sourceFile, node => node.kind === ts.SyntaxKind.Parameter ? node : undefined)!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what parameter is there in the example code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, window??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea, I just copied and pasted the test from the issue...

const jsDoc = param.getChildren()[0];
const token = jsDoc.getLastToken()!;
const result = ts.findPrecedingMatchingToken(token, ts.SyntaxKind.OpenBraceToken, sourceFile);
assert.isDefined(result);
});
});
});