Skip to content

Commit

Permalink
Better comment removing
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
GnaspGames committed Aug 3, 2016
1 parent 2b98976 commit ba56c3f
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions FileParser.js
Expand Up @@ -157,10 +157,35 @@ var FileParser = (function ()

FileParser.prototype.removeComments = function(content)
{
// Remove mutliline comments (/* example */)
content = content.replace(new RegExp("\\/\\*[^\\*\\/]*\\*\\/", 'g'), "");
// Remove singleline comments (// example)
content = content.replace(new RegExp("\\/\\/.*$", 'gm'), "");
var blockComments = String.raw`\/\*(.|[\r\n])*?\*\/`;
var lineComments = String.raw`\/\/(.*?)\r?\n`;
var strings = String.raw`"((\\[^\n]|[^"\n])*)"`;

var expression = new RegExp(blockComments + "|" + lineComments + "|" + strings, 'g');

content = content.replace(expression, function(match, offset, str)
{
if(match.startsWith("//"))
{
// It's a line comment, replace with new line.
return "\n"
}
else if(match.startsWith("/*"))
{
// It's a block comment, remove it all.
return '';
}
else if(match[0] == `"`)
{
// It a string; keep it.
return match;
}
else
{
// It's none of the above. Keep just in case.
return match;
}
});

if(Settings.Current.Output.ShowDebugInfo)
{
Expand Down

0 comments on commit ba56c3f

Please sign in to comment.