Improve JS comment handling (#727)

master
Brad Cornes 2023-03-10 14:28:03 +00:00 committed by GitHub
parent 7e3b93dd87
commit 05f8df0f60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import type { TextDocument, Range } from 'vscode-languageserver' import type { TextDocument, Range } from 'vscode-languageserver'
import moo from 'moo'
export function getTextWithoutComments( export function getTextWithoutComments(
doc: TextDocument, doc: TextDocument,
@ -14,7 +15,7 @@ export function getTextWithoutComments(
let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range) let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range)
if (type === 'js' || type === 'jsx') { if (type === 'js' || type === 'jsx') {
return text.replace(/\/\*.*?\*\//gs, replace).replace(/\/\/.*?$/gms, replace) return getJsWithoutComments(text)
} }
if (type === 'css') { if (type === 'css') {
@ -27,3 +28,35 @@ export function getTextWithoutComments(
function replace(match: string): string { function replace(match: string): string {
return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' ')) return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' '))
} }
let jsLexer: moo.Lexer
function getJsWithoutComments(text: string): string {
if (!jsLexer) {
jsLexer = moo.states({
main: {
commentLine: /\/\/.*?$/,
commentBlock: { match: /\/\*[^]*?\*\//, lineBreaks: true },
stringDouble: /"(?:[^"\\]|\\.)*"/,
stringSingle: /'(?:[^'\\]|\\.)*'/,
stringBacktick: /`(?:[^`\\]|\\.)*`/,
other: { match: /[^]/, lineBreaks: true },
},
})
}
let str = ''
jsLexer.reset(text)
for (let token of jsLexer) {
if (token.type === 'commentLine') {
str += ' '.repeat(token.value.length)
} else if (token.type === 'commentBlock') {
str += token.value.replace(/./g, ' ')
} else {
str += token.value
}
}
return str
}