Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions embedding/commentfilter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ var filtersByExtension = map[string]filterEntry{
".hxx": filterConfig(MarkerCommentFilter{Syntax: cStyleSyntax}, regularModes),

// JavaScript
".js": filterConfig(MarkerCommentFilter{Syntax: jsSyntax}, allModes),
".jsx": filterConfig(MarkerCommentFilter{Syntax: jsSyntax}, allModes),
".ts": filterConfig(MarkerCommentFilter{Syntax: jsSyntax}, allModes),
".tsx": filterConfig(MarkerCommentFilter{Syntax: jsSyntax}, allModes),
".js": filterConfig(JavaScriptCommentFilter{}, allModes),
".jsx": filterConfig(JavaScriptCommentFilter{}, allModes),
".ts": filterConfig(JavaScriptCommentFilter{}, allModes),
".tsx": filterConfig(JavaScriptCommentFilter{}, allModes),
Comment on lines 50 to +54

// Go
".go": filterConfig(MarkerCommentFilter{Syntax: goSyntax}, regularModes),
Expand Down Expand Up @@ -103,17 +103,6 @@ var javaSyntax = CommentMarker{
QuoteChars: "\"'",
}

var jsSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
{Start: cStyleBlockCommentStart, End: cStyleBlockCommentEnd},
},
Documentation: DocumentationMarker{
Block: []BlockMarker{{Start: cStyleDocCommentStart, End: cStyleBlockCommentEnd}},
},
QuoteChars: jsQuoteChars,
}

var csharpSyntax = CommentMarker{
Inline: []string{"//"},
Block: []BlockMarker{
Expand Down
66 changes: 65 additions & 1 deletion embedding/commentfilter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,84 @@ var _ = Describe("Comment filter", func() {
})

Describe("JavaScript and TypeScript", func() {
It("should strip comments without treating template literals as comments", func() {
It("should strip comments without treating regex and template text as comments", func() {
lines := []string{
"// module comment",
"const url = `http://example.org/*not-comment*/`;",
"const pattern = /https?:\\/\\/example\\.com\\/docs/;",
"const help = `Keep // and /* markers */ in template text`;",
"const nested = `${format(value /* remove this real comment */)}`;",
"const value = 42; // inline comment",
}

expected := []string{
"const url = `http://example.org/*not-comment*/`;",
"const pattern = /https?:\\/\\/example\\.com\\/docs/;",
"const help = `Keep // and /* markers */ in template text`;",
"const nested = `${format(value )}`;",
"const value = 42; ",
}

assertFiltered("sample.ts", RetainNone, lines, expected)
})

It("should preserve nested template literals inside template interpolations", func() {
lines := []string{
"const msg = `${items.map(i => `// ${i}`).join()}`;",
"const braces = `${items.map(i => `}`).join()}`; // real comment",
"const multiline = `${items.map(i => `// text",
"still } /* text */ ${i}`).join()}`; // real comment",
}

expected := []string{
"const msg = `${items.map(i => `// ${i}`).join()}`;",
"const braces = `${items.map(i => `}`).join()}`; ",
"const multiline = `${items.map(i => `// text",
"still } /* text */ ${i}`).join()}`; ",
}

assertFiltered("sample.ts", RetainNone, lines, expected)
})

It("should preserve multi-line template literal text", func() {
lines := []string{
"const help = `Keep // marker",
"and /* marker */ text`; // real comment",
}

expected := []string{
"const help = `Keep // marker",
"and /* marker */ text`; ",
}

assertFiltered("sample.ts", RetainNone, lines, expected)
})

It("should preserve regex literals after expression-starting keywords", func() {
lines := []string{
"function parse() { return /\"/; } // real comment",
"case /\"/.source: // real comment",
"const type = typeof /\"/; // real comment",
"const match = await /\"/; // real comment",
"const hasValue = name in /\"/; // real comment",
"const isPattern = value instanceof /\"/; // real comment",
"if (missing) {} else /\"/.test(value); // real comment",
"const ratio = value++ / 2; // real comment",
}

expected := []string{
"function parse() { return /\"/; } ",
"case /\"/.source: ",
"const type = typeof /\"/; ",
"const match = await /\"/; ",
"const hasValue = name in /\"/; ",
"const isPattern = value instanceof /\"/; ",
"if (missing) {} else /\"/.test(value); ",
"const ratio = value++ / 2; ",
}

assertFiltered("sample.ts", RetainNone, lines, expected)
})
})

Describe("C#", func() {
Expand Down
Loading
Loading