or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# Strip Comments

1

2

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.

3

4

## Package Information

5

6

- **Package Name**: strip-comments

7

- **Package Type**: npm

8

- **Language**: JavaScript

9

- **Installation**: `npm install strip-comments`

10

11

## Core Imports

12

13

```javascript

14

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

15

```

16

17

For ES modules:

18

19

```javascript

20

import strip from "strip-comments";

21

```

22

23

## Basic Usage

24

25

```javascript

26

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

27

28

const code = `

29

// This is a line comment

30

const foo = "bar"; /* block comment */

31

/*!

32

* Protected comment

33

*/

34

console.log(foo);

35

`;

36

37

// Remove all comments

38

const clean = strip(code);

39

console.log(clean);

40

// => const foo = "bar"; console.log(foo);

41

42

// Remove only block comments

43

const lineOnly = strip.block(code);

44

console.log(lineOnly);

45

// => // This is a line comment const foo = "bar"; console.log(foo);

46

47

// Remove only line comments

48

const blockOnly = strip.line(code);

49

console.log(blockOnly);

50

// => const foo = "bar"; /* block comment */ /*! * Protected comment */ console.log(foo);

51

```

52

53

## Capabilities

54

55

### Strip All Comments

56

57

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

58

59

```javascript { .api }

60

/**

61

* Strip all code comments from the given input, including protected

62

* comments that start with `!`, unless disabled by setting `options.keepProtected`

63

* to true.

64

* @param {string} input - string from which to strip comments

65

* @param {object} [options] - optional options

66

* @param {boolean} [options.line=true] - if `false` strip only block comments

67

* @param {boolean} [options.block=true] - if `false` strip only line comments

68

* @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `/*!` and `//!`)

69

* @param {boolean} [options.safe=false] - Alias for keepProtected

70

* @param {boolean} [options.preserveNewlines=false] - Preserve newlines after comments are stripped

71

* @returns {string} modified input

72

*/

73

function strip(input, options);

74

```

75

76

**Usage Examples:**

77

78

```javascript

79

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

80

81

// Basic usage - remove all comments

82

const result = strip('const foo = "bar";// this is a comment\n /* me too */');

83

console.log(result);

84

// => 'const foo = "bar";\n '

85

86

// Keep protected comments (using keepProtected)

87

const protected = strip('const foo = "bar";/*! important *//* regular */', {

88

keepProtected: true

89

});

90

console.log(protected);

91

// => 'const foo = "bar";/*! important */'

92

93

// Keep protected comments (using safe alias)

94

const protectedSafe = strip('const foo = "bar";/*! important *//* regular */', {

95

safe: true

96

});

97

console.log(protectedSafe);

98

// => 'const foo = "bar";/*! important */'

99

100

// Preserve newlines

101

const withNewlines = strip('// comment\nconst foo = "bar";', {

102

preserveNewlines: true

103

});

104

console.log(withNewlines);

105

// => '\nconst foo = "bar";'

106

```

107

108

### Strip Block Comments Only

109

110

Remove only block comments while preserving line comments.

111

112

```javascript { .api }

113

/**

114

* Strip only block comments.

115

* @param {string} input - string from which to strip comments

116

* @param {object} [options] - optional options

117

* @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `/*!`)

118

* @param {boolean} [options.safe=false] - Alias for keepProtected

119

* @returns {string} modified string

120

*/

121

strip.block(input, options);

122

```

123

124

**Usage Examples:**

125

126

```javascript

127

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

128

129

const code = 'const foo = "bar";// this is a comment\n /* me too */';

130

const result = strip.block(code);

131

console.log(result);

132

// => 'const foo = "bar";// this is a comment\n '

133

```

134

135

### Strip Line Comments Only

136

137

Remove only line comments while preserving block comments.

138

139

```javascript { .api }

140

/**

141

* Strip only line comments.

142

* @param {string} input - string from which to strip comments

143

* @param {object} [options] - optional options

144

* @param {boolean} [options.keepProtected=false] - Keep ignored comments (e.g. `//!`)

145

* @param {boolean} [options.safe=false] - Alias for keepProtected

146

* @returns {string} modified string

147

*/

148

strip.line(input, options);

149

```

150

151

**Usage Examples:**

152

153

```javascript

154

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

155

156

const code = 'const foo = "bar";// this is a comment\n /* me too */';

157

const result = strip.line(code);

158

console.log(result);

159

// => 'const foo = "bar";\n/* me too */'

160

```

161

162

### Strip First Comment Only

163

164

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

165

166

```javascript { .api }

167

/**

168

* Strip the first comment from the given input. Or, if `opts.keepProtected` is true,

169

* the first non-protected comment will be stripped.

170

* @param {string} input - string from which to strip comments

171

* @param {object} [options] - optional options

172

* @param {boolean} [options.keepProtected=false] - Keep comments with `!`

173

* @param {boolean} [options.safe=false] - Alias for keepProtected

174

* @returns {string} modified string

175

*/

176

strip.first(input, options);

177

```

178

179

**Usage Examples:**

180

181

```javascript

182

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

183

184

const code = '//! first comment\n// second comment\nconst foo = "bar";';

185

const result = strip.first(code, { keepProtected: true });

186

console.log(result);

187

// => '//! first comment\nconst foo = "bar";'

188

```

189

190

### Parse to Syntax Tree

191

192

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

193

194

```javascript { .api }

195

/**

196

* Parses a string and returns a basic CST (Concrete Syntax Tree).

197

* @param {string} input - string to parse

198

* @param {object} [options] - optional options

199

* @param {string} [options.language='javascript'] - Language name for comment syntax

200

* @param {boolean} [options.block=true] - Parse block comments

201

* @param {boolean} [options.line=true] - Parse line comments

202

* @param {boolean} [options.first=false] - Parse only first comment

203

* @returns {Block} parsed syntax tree

204

*/

205

strip.parse(input, options);

206

```

207

208

**Usage Examples:**

209

210

```javascript

211

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

212

213

const code = 'const foo = "bar"; // comment';

214

const cst = strip.parse(code);

215

console.log(cst);

216

// => Block { type: 'root', nodes: [...] }

217

```

218

219

## Language Support

220

221

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

222

223

### JavaScript Family

224

- **javascript** (default): `//` line comments, `/* */` block comments

225

- **typescript**: Same as JavaScript

226

- **css**: `/* */` block comments only

227

- **less**: Same as JavaScript

228

- **sass**: Same as JavaScript

229

230

### C Family

231

- **c**: Same as JavaScript

232

- **java**: Same as JavaScript

233

- **csharp**: `//` line comments, `/* */` block comments

234

- **swift**: Same as JavaScript

235

- **php**: `#` and `//` line comments, `/* */` block comments

236

237

### Scripting Languages

238

- **python**: `#` line comments, `"""` block comments

239

- **ruby**: `#` line comments, `=begin`/`=end` block comments

240

- **perl**: `#` line comments

241

- **lua**: `--` line comments, `--[[`/`]]` block comments

242

243

### Other Languages

244

- **html/xml**: `<!-- -->` block comments

245

- **haskell**: `--` line comments, `{-`/`-}` block comments

246

- **ada/sql**: `--` line comments

247

- **matlab**: `%` line comments, `%{`/`%}` block comments

248

- **applescript/pascal/ocaml**: `(*` / `*)` block comments

249

- **apl**: `⍝` line comments

250

- **shebang/hashbang**: `#!` line comments

251

252

**Usage with Different Languages:**

253

254

```javascript

255

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

256

257

// Python code

258

const pythonCode = '# This is a comment\nprint("Hello")';

259

const cleanPython = strip(pythonCode, { language: 'python' });

260

261

// CSS code

262

const cssCode = '/* Comment */ body { color: red; }';

263

const cleanCSS = strip(cssCode, { language: 'css' });

264

265

// HTML code

266

const htmlCode = '<!-- Comment --> <div>Hello</div>';

267

const cleanHTML = strip(htmlCode, { language: 'html' });

268

```

269

270

## Error Handling

271

272

The library throws specific errors for invalid inputs:

273

274

- **TypeError**: When input is not a string

275

- **Error**: When an unsupported language is specified

276

- **SyntaxError**: For unclosed block comments

277

278

```javascript

279

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

280

281

try {

282

strip(123); // TypeError: Expected input to be a string

283

} catch (error) {

284

console.error(error.message);

285

}

286

287

try {

288

strip(code, { language: 'invalid' }); // Error: Language "invalid" is not supported

289

} catch (error) {

290

console.error(error.message);

291

}

292

```

293

294

## Types

295

296

```javascript { .api }

297

/**

298

* Options object for strip functions

299

*/

300

interface StripOptions {

301

/** if `false` strip only block comments, default `true` */

302

line?: boolean;

303

/** if `false` strip only line comments, default `true` */

304

block?: boolean;

305

/** Keep ignored comments (e.g. `/*!` and `//!`) */

306

keepProtected?: boolean;

307

/** Alias for keepProtected */

308

safe?: boolean;

309

/** Preserve newlines after comments are stripped */

310

preserveNewlines?: boolean;

311

/** Language name for comment syntax (default: 'javascript') */

312

language?: string;

313

/** Parse only first comment (for parse function) */

314

first?: boolean;

315

}

316

317

/**

318

* Node in the concrete syntax tree

319

*/

320

class Node {

321

type: string;

322

value?: string;

323

match?: RegExpMatchArray;

324

newline: string;

325

/** Whether this is a protected comment (starts with !) */

326

readonly protected: boolean;

327

}

328

329

/**

330

* Block node that can contain child nodes

331

*/

332

class Block extends Node {

333

nodes: Node[];

334

push(node: Node): void;

335

/** Whether this block contains a protected comment */

336

readonly protected: boolean;

337

}

338

```