mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-17 01:49:41 -05:00
Ensure findPrecedingToken recurses into JSDoc children when needed (#53487)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -61,6 +61,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";
|
||||
|
||||
20
src/testRunner/unittests/services/utilities.ts
Normal file
20
src/testRunner/unittests/services/utilities.ts
Normal file
@@ -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" />
|
||||
|
||||
(/** @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)!;
|
||||
const jsDoc = param.getChildren()[0];
|
||||
const token = jsDoc.getLastToken()!;
|
||||
const result = ts.findPrecedingMatchingToken(token, ts.SyntaxKind.OpenBraceToken, sourceFile);
|
||||
assert.isDefined(result);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user