or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-strip-comments

Strip line and/or block comments from a string. Blazing fast, and works with JavaScript, Sass, CSS, Less.js, and a number of other languages.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/strip-comments@2.0.x

To install, run

npx @tessl/cli install tessl/npm-strip-comments@2.0.0

index.mddocs/

Strip Comments

Strip Comments is a high-performance utility library for removing line and/or block comments from source code strings. It supports multiple programming languages including JavaScript, CSS, Sass, Less.js, Python, C, Java, and many others, with flexible configuration options for selective comment removal and newline preservation.

Package Information

  • Package Name: strip-comments
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install strip-comments

Core Imports

const strip = require("strip-comments");

For ES modules:

import strip from "strip-comments";

Basic Usage

const strip = require("strip-comments");

const code = `
// This is a line comment
const foo = "bar"; /* block comment */
/*! 
 * Protected comment
 */
console.log(foo);
`;

// Remove all comments
const clean = strip(code);
console.log(clean);
// => const foo = "bar"; console.log(foo);

// Remove only block comments
const lineOnly = strip.block(code);
console.log(lineOnly);
// => // This is a line comment const foo = "bar"; console.log(foo);

// Remove only line comments  
const blockOnly = strip.line(code);
console.log(blockOnly);
// => const foo = "bar"; /* block comment */ /*! * Protected comment */ console.log(foo);

Capabilities

Strip All Comments

Remove all line and block comments from a string, with options to preserve protected comments and newlines.

/**
 * Strip all code comments from the given input, including protected
 * comments that start with `!`, unless disabled by setting `options.keepProtected`
 * to true.
 * @param {string} input - string from which to strip comments
 * @param {object} [options] - optional options
 * @param {boolean} [options.line=true] - if `false` strip only block comments
 * @param {boolean} [options.block=true] - if `false` strip only line comments  
 * @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `/*!` and `//!`)
 * @param {boolean} [options.safe=false] - Alias for keepProtected
 * @param {boolean} [options.preserveNewlines=false] - Preserve newlines after comments are stripped
 * @returns {string} modified input
 */
function strip(input, options);

Usage Examples:

const strip = require("strip-comments");

// Basic usage - remove all comments
const result = strip('const foo = "bar";// this is a comment\n /* me too */');
console.log(result);
// => 'const foo = "bar";\n '

// Keep protected comments (using keepProtected)
const protected = strip('const foo = "bar";/*! important *//* regular */', {
  keepProtected: true
});
console.log(protected);
// => 'const foo = "bar";/*! important */'

// Keep protected comments (using safe alias)
const protectedSafe = strip('const foo = "bar";/*! important *//* regular */', {
  safe: true
});
console.log(protectedSafe);
// => 'const foo = "bar";/*! important */'

// Preserve newlines
const withNewlines = strip('// comment\nconst foo = "bar";', {
  preserveNewlines: true
});
console.log(withNewlines);
// => '\nconst foo = "bar";'

Strip Block Comments Only

Remove only block comments while preserving line comments.

/**
 * Strip only block comments.
 * @param {string} input - string from which to strip comments
 * @param {object} [options] - optional options
 * @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `/*!`)
 * @param {boolean} [options.safe=false] - Alias for keepProtected
 * @returns {string} modified string
 */
strip.block(input, options);

Usage Examples:

const strip = require("strip-comments");

const code = 'const foo = "bar";// this is a comment\n /* me too */';
const result = strip.block(code);
console.log(result);
// => 'const foo = "bar";// this is a comment\n '

Strip Line Comments Only

Remove only line comments while preserving block comments.

/**
 * Strip only line comments.
 * @param {string} input - string from which to strip comments
 * @param {object} [options] - optional options
 * @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `//!`)
 * @param {boolean} [options.safe=false] - Alias for keepProtected
 * @returns {string} modified string
 */
strip.line(input, options);

Usage Examples:

const strip = require("strip-comments");

const code = 'const foo = "bar";// this is a comment\n /* me too */';
const result = strip.line(code);
console.log(result);
// => 'const foo = "bar";\n/* me too */'

Strip First Comment Only

Strip the first comment from the given input. Or, if keepProtected is true, the first non-protected comment will be stripped.

/**
 * Strip the first comment from the given input. Or, if `opts.keepProtected` is true,
 * the first non-protected comment will be stripped.
 * @param {string} input - string from which to strip comments
 * @param {object} [options] - optional options
 * @param {boolean} [options.keepProtected=false] - Keep comments with `!`
 * @param {boolean} [options.safe=false] - Alias for keepProtected
 * @returns {string} modified string
 */
strip.first(input, options);

Usage Examples:

const strip = require("strip-comments");

const code = '//! first comment\n// second comment\nconst foo = "bar";';
const result = strip.first(code, { keepProtected: true });
console.log(result);
// => '//! first comment\nconst foo = "bar";'

Parse to Syntax Tree

Parse a string and return a basic CST (Concrete Syntax Tree) for advanced processing.

/**
 * Parses a string and returns a basic CST (Concrete Syntax Tree).
 * @param {string} input - string to parse
 * @param {object} [options] - optional options
 * @param {string} [options.language='javascript'] - Language name for comment syntax
 * @param {boolean} [options.block=true] - Parse block comments
 * @param {boolean} [options.line=true] - Parse line comments
 * @param {boolean} [options.first=false] - Parse only first comment
 * @returns {Block} parsed syntax tree
 */
strip.parse(input, options);

Usage Examples:

const strip = require("strip-comments");

const code = 'const foo = "bar"; // comment';  
const cst = strip.parse(code);
console.log(cst);
// => Block { type: 'root', nodes: [...] }

Language Support

Strip Comments supports comment removal for the following languages through the language option:

JavaScript Family

  • javascript (default): // line comments, /* */ block comments
  • typescript: Same as JavaScript
  • css: /* */ block comments only
  • less: Same as JavaScript
  • sass: Same as JavaScript

C Family

  • c: Same as JavaScript
  • java: Same as JavaScript
  • csharp: // line comments, /* */ block comments
  • swift: Same as JavaScript
  • php: # and // line comments, /* */ block comments

Scripting Languages

  • python: # line comments, """ block comments
  • ruby: # line comments, =begin/=end block comments
  • perl: # line comments
  • lua: -- line comments, --[[/]] block comments

Other Languages

  • html/xml: <!-- --> block comments
  • haskell: -- line comments, {-/-} block comments
  • ada/sql: -- line comments
  • matlab: % line comments, %{/%} block comments
  • applescript/pascal/ocaml: (* / *) block comments
  • apl: line comments
  • shebang/hashbang: #! line comments

Usage with Different Languages:

const strip = require("strip-comments");

// Python code
const pythonCode = '# This is a comment\nprint("Hello")';
const cleanPython = strip(pythonCode, { language: 'python' });

// CSS code  
const cssCode = '/* Comment */ body { color: red; }';
const cleanCSS = strip(cssCode, { language: 'css' });

// HTML code
const htmlCode = '<!-- Comment --> <div>Hello</div>';
const cleanHTML = strip(htmlCode, { language: 'html' });

Error Handling

The library throws specific errors for invalid inputs:

  • TypeError: When input is not a string
  • Error: When an unsupported language is specified
  • SyntaxError: For unclosed block comments
const strip = require("strip-comments");

try {
  strip(123); // TypeError: Expected input to be a string
} catch (error) {
  console.error(error.message);
}

try {
  strip(code, { language: 'invalid' }); // Error: Language "invalid" is not supported
} catch (error) {
  console.error(error.message);  
}

Types

/**
 * Options object for strip functions
 */
interface StripOptions {
  /** if `false` strip only block comments, default `true` */
  line?: boolean;
  /** if `false` strip only line comments, default `true` */  
  block?: boolean;
  /** Keep ignored comments (e.g. `/*!` and `//!`) */
  keepProtected?: boolean;
  /** Alias for keepProtected */
  safe?: boolean;
  /** Preserve newlines after comments are stripped */
  preserveNewlines?: boolean;
  /** Language name for comment syntax (default: 'javascript') */
  language?: string;
  /** Parse only first comment (for parse function) */
  first?: boolean;
}

/**
 * Node in the concrete syntax tree
 */
class Node {
  type: string;
  value?: string;
  match?: RegExpMatchArray;
  newline: string;
  /** Whether this is a protected comment (starts with !) */
  readonly protected: boolean;
}

/**
 * Block node that can contain child nodes
 */
class Block extends Node {
  nodes: Node[];
  push(node: Node): void;
  /** Whether this block contains a protected comment */
  readonly protected: boolean;
}