Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 189 #228

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 39 additions & 41 deletions src/NUglify.Tests/Core/CommandLine.cs

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions src/NUglify.Tests/Css/Bugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public void Bug250()
TestHelper.Instance.RunTest();
}

[Test]
public void Bug189()
{
TestHelper.Instance.RunTest("-lines:m -comments:all");
}
[Test]
public void Bug270()
{
Expand Down
2 changes: 2 additions & 0 deletions src/NUglify.Tests/Css/Common/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static TestHelper Instance
/// </summary>
TestHelper()
{
Trace.Listeners.Add(new ConsoleTraceListener());

// start with the unit test DLL. All test data folders will be deployed there by testrun configuration.
// In order to do that, make sure that "Deployment" section in .testrunconfig file contains the "TestData" folder. If
// this is the case, then everything in the folder will be copied down right next to unit test DLL.
Expand Down
13 changes: 13 additions & 0 deletions src/NUglify.Tests/JavaScript/Bugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ public void Bug181()
Assert.AreEqual("function foo()\n{\n return 1\n}", uglifyResult.Code);
}

[Test]
public void Bug189()
{
TestHelper.Instance.RunTest("-pretty -comments:all");
}


[Test]
public void Bug189Important()
{
TestHelper.Instance.RunTest("-pretty -comments:important");
}

[Test]
public void Bug197()
{
Expand Down
4 changes: 2 additions & 2 deletions src/NUglify.Tests/JavaScript/JsPopular.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static void AssertCompile(string url, string file)
// https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js
var client = new WebClient();
var jqueryText = client.DownloadString(url);
var result = Uglify.Js(jqueryText, file, new CodeSettings()
var result = Uglify.Js(jqueryText, file, new CodeSettings
{
PreserveImportantComments = false,
CommentMode = JsComment.PreserveNone,
StripDebugStatements = true,
LineBreakThreshold = int.MaxValue,
});
Expand Down
18 changes: 18 additions & 0 deletions src/NUglify.Tests/NUglify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,9 @@
<Content Include="TestData\CSS\Expected\Bugs\Bug270.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Expected\Bugs\Bug189.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Expected\Bugs\Bug33.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -437,6 +440,9 @@
<Content Include="TestData\CSS\Input\Bugs\Bug250.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Input\Bugs\Bug189.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\CSS\Input\Bugs\Bug74.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -470,6 +476,9 @@
<Content Include="TestData\JS\Expected\Bugs\Bug241.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\Bugs\Bug189Important.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\Bugs\Bug197.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand All @@ -494,6 +503,9 @@
<Content Include="TestData\JS\Expected\Bugs\Bug199JS.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\Bugs\Bug189.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Expected\Bugs\Bug199JSON.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -2833,6 +2845,9 @@
<Content Include="TestData\JS\Input\Bugs\Bug293.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Bugs\Bug189Important.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Bugs\Bug279.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand All @@ -2854,6 +2869,9 @@
<Content Include="TestData\JS\Input\Bugs\Bug197.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Bugs\Bug189.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="TestData\JS\Input\Bugs\Bug199JS.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
6 changes: 6 additions & 0 deletions src/NUglify.Tests/TestData/CSS/Expected/Bugs/Bug189.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
h6
{
font-family: 'Hammersmith One',sans-serif;/*
font-family: 'Questrial', sans-serif;
*/
}
7 changes: 7 additions & 0 deletions src/NUglify.Tests/TestData/CSS/Input/Bugs/Bug189.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
h6
{
font-family: 'Hammersmith One', sans-serif;
/*
font-family: 'Questrial', sans-serif;
*/
}

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

8 changes: 8 additions & 0 deletions src/NUglify.Tests/TestData/JS/Expected/Bugs/Bug189.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let x = 1
// a comment
// another comment
//! important comment
/*!
another important comment
*/;
let y = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let x = 1
//! important comment
/*!
another important comment
*/;
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 */;
4 changes: 2 additions & 2 deletions src/NUglify.Tests/TestData/JS/Input/BlockOpts/AfterReturn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function test(a,b)
{
function test(a,b) {

var d = foo(c);
return a + b + d;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function one(a,b)

function two(c,d)
{
// no semi-colons -- they are implicit and should still count
//! no semi-colons -- they are implicit and should still count
"some other prologue"
"use strict"
c = d + c + d;
Expand Down
7 changes: 7 additions & 0 deletions src/NUglify.Tests/TestData/JS/Input/Bugs/Bug189.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let x = 1; // a comment
// another comment
//! important comment
/*!
another important comment
*/
let y = 2;
7 changes: 7 additions & 0 deletions src/NUglify.Tests/TestData/JS/Input/Bugs/Bug189Important.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
let x = 1; // a comment
// another comment
//! important comment
/*!
another important comment
*/
let y = 2;
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
7 changes: 3 additions & 4 deletions src/NUglify/Css/CssParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3923,15 +3923,14 @@ string NextSignificantToken()

// add the comment to the builder
sb.Append(commentText);
if(Settings.OutputMode == OutputMode.MultipleLines)
sb.Append(Settings.LineTerminator);
}
}

// next token
m_currentToken = m_scanner.NextToken(!m_insideCalc);
if (EchoWriter != null)
{
EchoWriter.Write(CurrentTokenText);
}
EchoWriter?.Write(CurrentTokenText);

m_encounteredNewLine = m_encounteredNewLine || m_scanner.GotEndOfLine;
}
Expand Down
20 changes: 12 additions & 8 deletions src/NUglify/JavaScript/CodeSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public CodeSettings()
this.EvalTreatment = EvalTreatment.Ignore;
this.InlineSafeStrings = true;
this.MacSafariQuirks = true;
this.PreserveImportantComments = true;
this.CommentMode = JsComment.PreserveImportant;
this.QuoteObjectLiteralProperties = false;
this.StrictMode = false;
this.StripDebugStatements = true;
Expand Down Expand Up @@ -114,7 +114,7 @@ public CodeSettings Clone()
PreprocessOnly = this.PreprocessOnly,
PreprocessorDefineList = this.PreprocessorDefineList,
PreserveFunctionNames = this.PreserveFunctionNames,
PreserveImportantComments = this.PreserveImportantComments,
CommentMode = this.CommentMode,
QuoteObjectLiteralProperties = this.QuoteObjectLiteralProperties,
RemoveFunctionExpressionNames = this.RemoveFunctionExpressionNames,
RemoveUnneededCode = this.RemoveUnneededCode,
Expand Down Expand Up @@ -742,16 +742,20 @@ public bool PreserveFunctionNames
get; set;
}

/// <summary>
/// Gets or sets a value indicating whether to preserve important comments in the output.
/// Default is true, preserving important comments. Important comments have an exclamation
/// mark as the very first in-comment character (//! or /*!).
/// </summary>
[Obsolete("Use CommentMode instead")]
public bool PreserveImportantComments
{
get; set;
get => this.CommentMode != JsComment.PreserveNone;
set => this.CommentMode = this.CommentMode == JsComment.PreserveNone ? JsComment.PreserveImportant : this.CommentMode;
}

/// <summary>
/// Gets or sets a value indicating whether to preserve comments in the output.
/// Default is Important, preserving important comments only.
/// Important comments have an exclamation mark as the very first in-comment character (//! or /*!).
/// </summary>
public JsComment CommentMode { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to always quote object literal property names.
/// Default is false.
Expand Down
12 changes: 12 additions & 0 deletions src/NUglify/JavaScript/Comment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Diagnostics;

namespace NUglify.JavaScript
{
[DebuggerDisplay("{Context}")]
public class Comment
{
public SourceContext Context { get; set; }
public bool IsImportant { get; set; }
public bool IsMultiLine { get; set; }
}
}
Loading