tagged-template-literal-comments

Adds comment parsing to javascript tagged template literals

Usage no npm install needed!

<script type="module">
  import taggedTemplateLiteralComments from 'https://cdn.skypack.dev/tagged-template-literal-comments';
</script>

README

tagged-template-literal-comments

npm Linux Build Status Windows Build Status Code Coverage

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,
});