2022-09-01 14:06:47 +00:00
|
|
|
import type { TextDocument, Range } from 'vscode-languageserver'
|
2023-03-10 14:28:03 +00:00
|
|
|
import moo from 'moo'
|
2022-09-01 14:06:47 +00:00
|
|
|
|
|
|
|
export function getTextWithoutComments(
|
|
|
|
doc: TextDocument,
|
|
|
|
type: 'html' | 'js' | 'jsx' | 'css',
|
|
|
|
range?: Range
|
|
|
|
): string
|
|
|
|
export function getTextWithoutComments(text: string, type: 'html' | 'js' | 'jsx' | 'css'): string
|
|
|
|
export function getTextWithoutComments(
|
|
|
|
docOrText: TextDocument | string,
|
|
|
|
type: 'html' | 'js' | 'jsx' | 'css',
|
|
|
|
range?: Range
|
|
|
|
): string {
|
|
|
|
let text = typeof docOrText === 'string' ? docOrText : docOrText.getText(range)
|
|
|
|
|
|
|
|
if (type === 'js' || type === 'jsx') {
|
2023-03-10 14:28:03 +00:00
|
|
|
return getJsWithoutComments(text)
|
2022-09-01 14:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'css') {
|
|
|
|
return text.replace(/\/\*.*?\*\//gs, replace)
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.replace(/<!--.*?-->/gs, replace)
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace(match: string): string {
|
|
|
|
return match.replace(/./gs, (char) => (char === '\n' ? '\n' : ' '))
|
|
|
|
}
|
2023-03-10 14:28:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|