strip-comments icon indicating copy to clipboard operation
strip-comments copied to clipboard

does not remove comments after empty string

Open dzanis opened this issue 5 years ago • 3 comments

Hi friend. I got in trouble.

'use strict';
const strip = require('strip-comments');
// its ok if different quotes
const str = strip("var s1= '';//empty quotes not the same with the next quotes\nvar s2= \"\";");
console.log(str);
// not ok
const str2 = strip("var s3= \"\";//empty quotes same with the next quotes\nvar s4= \"\";");
console.log(str2);

Output console

var s1= '';
var s2= "";
var s3= "";//empty quotes same with the next quotes
var s4= "";

Here I changed https://github.com/jonschlinkert/strip-comments/blob/8b2680975598e705b2c11e89afd70deec2c5cafc/lib/parse.js#L8 to

QUOTED_STRING_REGEX: /^(['"`])((?:\\.|[^\1])*?)(\1)/,

Here is an example https://regex101.com/r/bIK3CU/1/ But it’s not for nothing that you wrote so in the first place, what’s the catch? Thanks

dzanis avatar Dec 27 '19 16:12 dzanis

I have same problem https://github.com/jonschlinkert/strip-comments/issues/53

stantoxt avatar Mar 22 '20 01:03 stantoxt

I have the same issue.

Minimal reproduction code-block:

console.log("Test1:", strip(`
myVar1 = "";
//myVar2 = "anything";`));

Output:

Test1:
myVar1 = "";
//myVar2 = "anything";

As can be seen, when an empty string is encountered, comment-stripping seems to fail for the next comment.

Venryx avatar Jul 26 '21 08:07 Venryx

For now I'm fixing it by adding a custom regex-replacement at the end, to remove any missing single-line comments that remain:

function StripComments_Fixed(str: string) {
	let result = strip(str);

	// fix for issue: https://github.com/jonschlinkert/strip-comments/issues/48
	result = result.replace(/^(\s*)\/\/(.*)$/gm, "");

	return result;
}

Venryx avatar Jul 26 '21 08:07 Venryx