-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Ignore imports that are inside comments
- Loading branch information
1 parent
2e3fe28
commit 247887e
Showing
4 changed files
with
306 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @typedef CommentLocation | ||
* @property {number} start | ||
* @property {number} end | ||
*/ | ||
|
||
/** | ||
* @param {string} scriptSource | ||
*/ | ||
export function getCommentLocations(scriptSource) { | ||
/** @type {CommentLocation[]} */ | ||
const commentLocations = []; | ||
let totalIndex = 0; | ||
for (const line of scriptSource.split("\n")) { | ||
const commentIndex = line.indexOf("//"); | ||
if (commentIndex >= 0) { | ||
commentLocations.push({ | ||
start: totalIndex + commentIndex, | ||
end: totalIndex + line.length, | ||
}); | ||
} | ||
totalIndex += line.length + "\n".length; | ||
} | ||
|
||
const blockCommentRegex = /\/\*[\s\S]*\*\//g; | ||
for (const match of scriptSource.matchAll(blockCommentRegex)) { | ||
if (match.index == undefined) continue; | ||
commentLocations.push({ | ||
start: match.index, | ||
end: match.index + match[0].length, | ||
}); | ||
} | ||
|
||
commentLocations.sort((a, b) => a.start - b.start); | ||
|
||
// Merge overlapping comments | ||
const mergedCommentLocations = []; | ||
let lastCommentLocation = null; | ||
for (const location of commentLocations) { | ||
if (lastCommentLocation) { | ||
if (location.start >= lastCommentLocation.end) { | ||
mergedCommentLocations.push(location); | ||
lastCommentLocation = location; | ||
} else { | ||
lastCommentLocation.end = Math.max( | ||
lastCommentLocation.end, | ||
location.end, | ||
); | ||
} | ||
} else { | ||
mergedCommentLocations.push(location); | ||
lastCommentLocation = location; | ||
} | ||
} | ||
return mergedCommentLocations; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts"; | ||
import { getCommentLocations } from "../../../src/parseComments.js"; | ||
|
||
Deno.test({ | ||
name: "single line comment", | ||
fn() { | ||
const source = `// comment`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 0, end: 10 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "line comment after non comment", | ||
fn() { | ||
const source = ` | ||
not a comment // comment | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 21, end: 31 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "line comment in between non comments", | ||
fn() { | ||
const source = ` | ||
not a comment | ||
not a comment // comment | ||
not a comment | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 41, end: 51 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "multiple line comments", | ||
fn() { | ||
const source = ` | ||
// comment | ||
not a comment | ||
not a comment // comment | ||
not a comment // comment | ||
not a comment | ||
not a comment // comment | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 7, end: 17 }, | ||
{ start: 58, end: 68 }, | ||
{ start: 89, end: 99 }, | ||
{ start: 140, end: 150 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "block comment", | ||
fn() { | ||
const source = `/* comment */`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 0, end: 13 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "multi line block comment", | ||
fn() { | ||
const source = ` | ||
/* | ||
comment | ||
*/ | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 7, end: 34 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "jsdoc style comment", | ||
fn() { | ||
const source = ` | ||
/** | ||
* @fileoverview test | ||
*/ | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 7, end: 48 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "line comment inside block comment", | ||
fn() { | ||
const source = ` | ||
/* | ||
// comment | ||
*/ | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 7, end: 35 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "block comment between non comments", | ||
fn() { | ||
const source = ` | ||
not a comment | ||
/* comment */ | ||
not a comment | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 27, end: 40 }, | ||
]); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "block comment inside line comment", | ||
fn() { | ||
const source = ` | ||
// comment /* comment */ | ||
not a comment | ||
`; | ||
|
||
const result = getCommentLocations(source); | ||
|
||
assertEquals(result, [ | ||
{ start: 7, end: 31 }, | ||
]); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters