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

Function getIfElseOccurrences

test/fixtures/snapshot/typescript.js:135910–135941  ·  view source on GitHub ↗
(ifStatement, sourceFile)

Source from the content-addressed store, hash-verified

135908 }
135909 }
135910 function getIfElseOccurrences(ifStatement, sourceFile) {
135911 var keywords = getIfElseKeywords(ifStatement, sourceFile);
135912 var result = [];
135913 // We'd like to highlight else/ifs together if they are only separated by whitespace
135914 // (i.e. the keywords are separated by no comments, no newlines).
135915 for (var i = 0; i < keywords.length; i++) {
135916 if (keywords[i].kind === 91 /* SyntaxKind.ElseKeyword */ && i < keywords.length - 1) {
135917 var elseKeyword = keywords[i];
135918 var ifKeyword = keywords[i + 1]; // this *should* always be an 'if' keyword.
135919 var shouldCombineElseAndIf = true;
135920 // Avoid recalculating getStart() by iterating backwards.
135921 for (var j = ifKeyword.getStart(sourceFile) - 1; j >= elseKeyword.end; j--) {
135922 if (!ts.isWhiteSpaceSingleLine(sourceFile.text.charCodeAt(j))) {
135923 shouldCombineElseAndIf = false;
135924 break;
135925 }
135926 }
135927 if (shouldCombineElseAndIf) {
135928 result.push({
135929 fileName: sourceFile.fileName,
135930 textSpan: ts.createTextSpanFromBounds(elseKeyword.getStart(), ifKeyword.end),
135931 kind: "reference" /* HighlightSpanKind.reference */
135932 });
135933 i++; // skip the next keyword
135934 continue;
135935 }
135936 }
135937 // Ordinary case: just highlight the keyword.
135938 result.push(getHighlightSpanForNode(keywords[i], sourceFile));
135939 }
135940 return result;
135941 }
135942 function getIfElseKeywords(ifStatement, sourceFile) {
135943 var keywords = [];
135944 // Traverse upwards through all parent if-statements linked by their else-branches.

Callers 1

getHighlightSpansFunction · 0.85

Calls 3

getIfElseKeywordsFunction · 0.85
getHighlightSpanForNodeFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected