tagged-template-literal-comments
v2.0.0
Published
Adds comment parsing to javascript tagged template literals
Downloads
3
Maintainers
Readme
tagged-template-literal-comments
About
Adds comments to tagged template literals.
Features
- Removes commented out strings and values
- Supports single line and multiline comments
- Trims excess whitespace and new lines
Installation
npm install tagged-template-literal-comments
Usage
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
Options
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,
});