Skip to content

Commit f68cd3b

Browse files
lilnasyoverlookmotel
authored andcommitted
refactor : no branching
1 parent 159d1be commit f68cd3b

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

apps/oxlint/src-js/plugins/source_code.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,29 +253,32 @@ export const SOURCE_CODE = Object.freeze({
253253
getCommentsInside(node: Node): Comment[] {
254254
if (ast === null) initAst();
255255
const { comments } = ast;
256-
if (comments.length === 0) return [];
256+
257+
const commentsLength = comments.length;
257258
const [rangeStart, rangeEnd] = node.range;
258-
let indexStart = null, indexEnd = null;
259-
// Linear search for first comment within `node`'s range
259+
let indexStart: number = commentsLength;
260+
let indexEnd: number | undefined = undefined;
261+
262+
// Linear search for first comment within `node`'s range.
260263
// TODO: Use binary search.
261-
for (let i = 0; i < comments.length; i++) {
264+
for (let i = 0; i < commentsLength; i++) {
262265
const comment = comments[i];
263-
if (comment.start >= rangeStart && comment.end <= rangeEnd) {
266+
if (comment.start >= rangeStart) {
264267
indexStart = i;
265268
break;
266269
}
267270
}
268-
if (indexStart === null) return [];
269-
// Linear search for last comment within `node`'s range
270-
// TODO: Use binary search.
271-
for (let i = indexStart; i < comments.length; i++) {
271+
272+
// Continued linear search for last comment within `node`'s range.
273+
for (let i = indexStart; i < commentsLength; i++) {
272274
const comment = comments[i];
273-
if (comment.end > rangeEnd) {
275+
if (comment.start > rangeEnd) {
274276
indexEnd = i;
275277
break;
276278
}
277279
}
278-
return comments.slice(indexStart, indexEnd ?? comments.length);
280+
281+
return comments.slice(indexStart, indexEnd);
279282
},
280283

281284
/**

0 commit comments

Comments
 (0)