Skip to content

Commit

Permalink
fixed #189
Browse files Browse the repository at this point in the history
  • Loading branch information
trullock committed Jan 4, 2021
1 parent 00a3fec commit 7f69746
Show file tree
Hide file tree
Showing 30 changed files with 113 additions and 253 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var a="a",b="b",c,d="d",e,f,g;d&&(e="e");f="f",g="g"
var a="a",b="b",c,d="d",e,f,g;d&&(e="e");f="f";g="g"
5 changes: 3 additions & 2 deletions src/NUglify.Tests/TestData/JS/Expected/Bugs/Bug189.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let x = 1; // a comment
let x = 1
// a comment
// another comment
let y = 2;
let y = 2

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*!
* this is an example of an "important" comment
* that we want to keep
*/
function foo(){
/*! this too */
}var a=12;
/*!
* this is another one, but it's between a couple var statements
*/
var b=13;
/** @preserve This is an important comment because of the @preserve token */
/**
*/function foo(){/*! this too */}var a=12/*! * this is another one, but it's between a couple var statements */;var b=13/** @preserve This is an important comment because of the @preserve token *//**
** @license And so is this, because of the @license token
*/
for(var ndx=0;ndx<10;++ndx)
/*! inside a block */
;
*/;for(var ndx=0;ndx<10;++ndx)/*! inside a block */;
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
var p,i;if(location.hash)throw"hash exists";else alert("no hash");for(p in window)alert(p+" = "+window[p]);for(;;)if(window.timer)break;do{
//! keep one
if(+new Date-1e6){
//! keep two
break}
/*! keep three */
}while(1);foo:for(i=0;i<100;++i)if(i%3)break foo
var p,i;if(location.hash)throw"hash exists";else alert("no hash");for(p in window)alert(p+" = "+window[p]);for(;;)if(window.timer)break;do{//! keep one
if(+new Date-1e6){//! keep two
break}/*! keep three */}while(1);foo:for(i=0;i<100;++i)if(i%3)break foo
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/*! Copyright 2010 by Contoso Corp. All rights reserved */
/*! Copyright 2010 by Contoso Corp. All rights reserved */
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/*! First Important Comment */
/*! Second Important Comment */
var a=10
/*! First Important Comment *//*! Second Important Comment */var a=10
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//! start my implicit module
function n(n,t){return n+t}export function mul(n,t){return n*t}function t(n){return mul(n,n)}function i(n,t){if(t==0)return 1;for(var i=1;--t;)i=mul(i,n);return i}export const pi=3.1415927,negOne=-1;export var accumulator=0,foo="bar";export{n as sum,t as square,i as pow};
//! end my implicit module
function n(n,t){return n+t}export function mul(n,t){return n*t}function t(n){return mul(n,n)}function i(n,t){if(t==0)return 1;for(var i=1;--t;)i=mul(i,n);return i}export const pi=3.1415927,negOne=-1;export var accumulator=0,foo="bar";export{n as sum,t as square,i as pow}//! end my implicit module
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
function foo(){for(var ndx=0;ndx<10;++ndx);}if(ndx)
/*! important */
;
function foo(){for(var ndx=0;ndx<10;++ndx);}if(ndx)/*! important */;
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
function foo() {/*! this too */}
/* regular comment that will get stripped */
var a = 12;
/*!
* this is another one, but it's between a couple var statements
*/
/*! * this is another one, but it's between a couple var statements */
var b = 13;

/** @preserve This is an important comment because of the @preserve token */
Expand Down
18 changes: 18 additions & 0 deletions src/NUglify.Tests/TestData/JS/Input/Operators/Comma.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
function uxp_toggleElementVisibility_return(e)
{
var o = e.style;
return 'none' === o.display
? (o.display = '', 1)
: (o.display = 'none', 0);
}

// we need to keep those parens around the comma operator
// so that we continue to have two arguments, the second of
// which is a comma-operator expression.
var t = foo(bar, (ack,gag));

// the comma-operators in this var statement should be wrapped in parens to
// distinguish them from the var comma separators
var y = (t, foo, bar);

// multiple commas, for op-prec sake
// because they are evaluated left-to-right, there should be no parens in this list
z = a, b, c, document;
1 change: 1 addition & 0 deletions src/NUglify/JavaScript/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class Comment
{
public SourceContext Context { get; set; }
public bool IsImportant { get; set; }
public bool IsMultiLine { get; set; }
}
}
14 changes: 5 additions & 9 deletions src/NUglify/JavaScript/JSParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,7 @@ AstNode ParseStatement(bool fSourceElement, bool skipComments = false)
var comment = comments[0];
comments.RemoveAt(0);

if (!comment.IsImportant)
return new StandardComment(comment.Context);

return new ImportantComment(comment.Context);
return new Syntax.Comment(comment.Context, comment.IsImportant, comment.IsMultiLine);
}

AstNode statement = null;
Expand Down Expand Up @@ -3012,7 +3009,7 @@ private void ParseFunctionBody(BlockStatement body)
};
}
}
else if (!m_newModule && !(statement is ImportantComment || statement is StandardComment))
else if (!m_newModule && !(statement is Syntax.Comment))
{
// no longer considering constant wrappers
possibleDirectivePrologue = false;
Expand Down Expand Up @@ -5752,9 +5749,7 @@ void AppendComments(BlockStatement block)
{
// we have comments before the EOF. Add the comment(s) to the program.
foreach (var comment in comments)
{
block.Append(comment.IsImportant ? new ImportantComment(comment.Context) : new StandardComment(comment.Context) as AstNode);
}
block.Append(new Syntax.Comment(comment.Context, comment.IsImportant, comment.IsMultiLine));

comments.Clear();
}
Expand Down Expand Up @@ -5822,7 +5817,8 @@ private SourceContext ScanNextToken()
comments.Add(new Comment
{
Context = nextToken.Clone(),
IsImportant = important
IsImportant = important,
IsMultiLine = nextToken.Is(JSToken.MultipleLineComment)
});
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/NUglify/JavaScript/Syntax/ClassNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,7 @@ public ClassNode(SourceContext context)

public override void Accept(IVisitor visitor)
{
if (visitor != null)
{
visitor.Visit(this);
}
visitor?.Visit(this);
}

public override IEnumerable<AstNode> Children
Expand Down
27 changes: 27 additions & 0 deletions src/NUglify/JavaScript/Syntax/Comment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUglify.JavaScript.Visitors;

namespace NUglify.JavaScript.Syntax
{
public class Comment : AstNode
{
// Indicates if this was a multiline comment (by syntax only, doesn't necessarily contain a \n)
public bool IsMultiLine { get; set; }
public bool IsImportant { get; set; }
public string Value { get; set; }

// this is for determining if a node in a block AFTER a return/break/continue should be removed. We don't want to remove an important comment, so SAY it's a declaration.
public override bool IsDeclaration => true;

public Comment(SourceContext context, bool isImportant, bool isMultiLine) : base(context)
{
IsImportant = isImportant;
IsMultiLine = isMultiLine;
Value = Context.Code;
}

public override void Accept(IVisitor visitor)
{
visitor?.Visit(this);
}
}
}
22 changes: 0 additions & 22 deletions src/NUglify/JavaScript/Syntax/ImportantComment.cs

This file was deleted.

24 changes: 0 additions & 24 deletions src/NUglify/JavaScript/Syntax/StandardComment.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/NUglify/JavaScript/Visitors/AnalyzeNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ static AstNode FindLastStatement(BlockStatement node)
// or important comments until we get the last statement
var lastStatementIndex = node.Count - 1;
while (lastStatementIndex >= 0
&& (node[lastStatementIndex] is FunctionObject || node[lastStatementIndex] is ImportantComment || node[lastStatementIndex] is StandardComment))
&& (node[lastStatementIndex] is FunctionObject || node[lastStatementIndex] is Syntax.Comment))
{
--lastStatementIndex;
}
Expand Down Expand Up @@ -1565,7 +1565,7 @@ static int PreviousStatementIndex(BlockStatement node, AstNode child)
// get the index of the statement before the last return
// (skip over function decls and importand comments)
var indexPrevious = node.IndexOf(child) - 1;
while (indexPrevious >= 0 && (node[indexPrevious] is FunctionObject || node[indexPrevious] is ImportantComment || node[indexPrevious] is StandardComment))
while (indexPrevious >= 0 && (node[indexPrevious] is FunctionObject || node[indexPrevious] is Syntax.Comment))
{
--indexPrevious;
}
Expand Down
7 changes: 1 addition & 6 deletions src/NUglify/JavaScript/Visitors/BindingsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,7 @@ public void Visit(IfStatement node)
ReportError(node);
}

public void Visit(StandardComment node)
{
ReportError(node);
}

public void Visit(ImportantComment node)
public void Visit(Syntax.Comment node)
{
ReportError(node);
}
Expand Down
3 changes: 1 addition & 2 deletions src/NUglify/JavaScript/Visitors/IVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public interface IVisitor
void Visit(GetterSetter node);
void Visit(GroupingOperator node);
void Visit(IfStatement node);
void Visit(StandardComment node);
void Visit(ImportantComment node);
void Visit(Syntax.Comment node);
void Visit(ImportExportSpecifier node);
void Visit(ImportStatement node);
void Visit(InitializerNode node);
Expand Down
7 changes: 1 addition & 6 deletions src/NUglify/JavaScript/Visitors/JsonOutputVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,12 +544,7 @@ public void Visit(IfStatement node)
IsValid = false;
}

public void Visit(StandardComment node)
{
// invalid! ignore
IsValid = false;
}
public void Visit(ImportantComment node)
public void Visit(Syntax.Comment node)
{
// invalid! ignore
IsValid = false;
Expand Down
6 changes: 1 addition & 5 deletions src/NUglify/JavaScript/Visitors/MatchPropertiesVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,7 @@ public void Visit(IfStatement node)
// not applicable; terminate
}

public void Visit(StandardComment node)
{
// not applicable; terminate
}
public void Visit(ImportantComment node)
public void Visit(Syntax.Comment node)
{
// not applicable; terminate
}
Expand Down
7 changes: 1 addition & 6 deletions src/NUglify/JavaScript/Visitors/NewParensVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,7 @@ public virtual void Visit(GroupingOperator node)
// output parens ourselves. And don't bother recursing.
}

public void Visit(StandardComment node)
{
// we're good?
}

public void Visit(ImportantComment node)
public void Visit(Syntax.Comment node)
{
// we're good?
}
Expand Down
Loading

0 comments on commit 7f69746

Please sign in to comment.