Adds comments to tagged template literals.
- Removes commented out strings and values
- Supports single line and multiline comments
- Trims excess whitespace and new lines
npm install tagged-template-literal-comments
import { createCommentParser } from 'tagged-template-literal-comments';
// defaults to javascript parser
const jsCommentParser = createCommentParser();
const jsWithComments = jsCommentParser`
a | b | expected
// test a subset of issues
${0} | ${1} | ${1}
// test another subset of issues
${1} | ${1} | ${2}
${2} | ${2} | ${4} // fix for: https://github.com/facebook/jest/pull/8717
${3} | ${3} | ${6} // some random note
// enable later
// ${1} | ${1} | ${2}
// ${1} | ${1} | ${2}
`;
const jsWithoutComments = `
a | b | expected
${0} | ${1} | ${1}
${1} | ${1} | ${2}
${2} | ${2} | ${4}
${3} | ${3} | ${6}
`;
// jsWithComments === jsWithoutComments
const htmlCommentParser = createCommentParser({ language: 'html' });
const htmlWithComments = htmlCommentParser`
<!-- remove comment -->
<!--<p>${1}</p>-->
<p>${2}</p>
`;
const htmlWithoutComments = `
<p>${2}</p>
`;
// htmlWithComments === htmlWithoutComments
const parser = createCommentParser({
/**
* Comment language parser
*
* available: javascript, html, ignore
* default: 'javascript'
*/
language: 'javascript',
/**
* Set single line comment marker
*
* disable single line comments by setting to false
*/
singleLine: '//',
/**
* Set multiline comment marker
*
* disable multiline comments by setting to false
*/
multiline: { open: '/*', close: '*/' },
/**
* Throw on invalid syntax
*
* default: true
*/
throwOnSyntaxError: true,
});