MCPcopy Create free account
hub / github.com/nodejs/node / findPrecedingMatchingToken

Function findPrecedingMatchingToken

test/fixtures/snapshot/typescript.js:127739–127774  ·  view source on GitHub ↗
(token, matchingTokenKind, sourceFile)

Source from the content-addressed store, hash-verified

127737 }
127738 ts.isInsideJsxElement = isInsideJsxElement;
127739 function findPrecedingMatchingToken(token, matchingTokenKind, sourceFile) {
127740 var closeTokenText = ts.tokenToString(token.kind);
127741 var matchingTokenText = ts.tokenToString(matchingTokenKind);
127742 var tokenFullStart = token.getFullStart();
127743 // Text-scan based fast path - can be bamboozled by comments and other trivia, but often provides
127744 // a good, fast approximation without too much extra work in the cases where it fails.
127745 var bestGuessIndex = sourceFile.text.lastIndexOf(matchingTokenText, tokenFullStart);
127746 if (bestGuessIndex === -1) {
127747 return undefined; // if the token text doesn't appear in the file, there can't be a match - super fast bail
127748 }
127749 // we can only use the textual result directly if we didn't have to count any close tokens within the range
127750 if (sourceFile.text.lastIndexOf(closeTokenText, tokenFullStart - 1) < bestGuessIndex) {
127751 var nodeAtGuess = findPrecedingToken(bestGuessIndex + 1, sourceFile);
127752 if (nodeAtGuess && nodeAtGuess.kind === matchingTokenKind) {
127753 return nodeAtGuess;
127754 }
127755 }
127756 var tokenKind = token.kind;
127757 var remainingMatchingTokens = 0;
127758 while (true) {
127759 var preceding = findPrecedingToken(token.getFullStart(), sourceFile);
127760 if (!preceding) {
127761 return undefined;
127762 }
127763 token = preceding;
127764 if (token.kind === matchingTokenKind) {
127765 if (remainingMatchingTokens === 0) {
127766 return token;
127767 }
127768 remainingMatchingTokens--;
127769 }
127770 else if (token.kind === tokenKind) {
127771 remainingMatchingTokens++;
127772 }
127773 }
127774 }
127775 ts.findPrecedingMatchingToken = findPrecedingMatchingToken;
127776 function removeOptionality(type, isOptionalExpression, isOptionalChain) {
127777 return isOptionalExpression ? type.getNonNullableType() :

Callers 1

Calls 1

findPrecedingTokenFunction · 0.85

Tested by

no test coverage detected