Skip to content

Commit a65cffc

Browse files
committed
Fixed a error, that occurred during the generation of error message
1 parent 84f7f4c commit a65cffc

File tree

7 files changed

+90
-10
lines changed

7 files changed

+90
-10
lines changed

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.ScriptSiteBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected ActiveScriptException CreateActiveScriptException(IActiveScriptError e
130130
string sourceLine;
131131
error.GetSourceLineText(out sourceLine);
132132

133-
sourceFragment = JsErrorHelpers.GetSourceFragment(sourceLine, columnNumber);
133+
sourceFragment = JsErrorHelpers.GetSourceFragmentFromLine(sourceLine, columnNumber);
134134
}
135135
else
136136
{

src/MsieJavaScriptEngine/Helpers/JsErrorHelpers.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public static JsEngineLoadException WrapEngineLoadException(Exception exception,
461461
/// <param name="sourceLine">Content of the source line</param>
462462
/// <param name="columnNumber">Column number</param>
463463
/// <param name="maxFragmentLength">Maximum length of the source fragment</param>
464-
internal static string GetSourceFragment(string sourceLine, int columnNumber,
464+
public static string GetSourceFragmentFromLine(string sourceLine, int columnNumber,
465465
int maxFragmentLength = 100)
466466
{
467467
if (string.IsNullOrEmpty(sourceLine))
@@ -480,7 +480,14 @@ internal static string GetSourceFragment(string sourceLine, int columnNumber,
480480

481481
var leftOffset = (int)Math.Floor((double)maxFragmentLength / 2);
482482
int fragmentStartPosition = columnNumber - leftOffset - 1;
483-
if (fragmentStartPosition < 0)
483+
if (fragmentStartPosition > 0)
484+
{
485+
if (lineLength - fragmentStartPosition < maxFragmentLength)
486+
{
487+
fragmentStartPosition = lineLength - maxFragmentLength;
488+
}
489+
}
490+
else
484491
{
485492
fragmentStartPosition = 0;
486493
}
@@ -489,12 +496,10 @@ internal static string GetSourceFragment(string sourceLine, int columnNumber,
489496
if (fragmentStartPosition > 0)
490497
{
491498
startPart = ellipsisSymbol;
492-
fragmentLength--;
493499
}
494-
if (fragmentStartPosition + maxFragmentLength < lineLength)
500+
if (fragmentStartPosition + fragmentLength < lineLength)
495501
{
496502
endPart = ellipsisSymbol;
497-
fragmentLength--;
498503
}
499504

500505
StringBuilder fragmentBuilder = StringBuilderPool.GetBuilder();

src/MsieJavaScriptEngine/JsRt/Edge/ChakraEdgeJsRtJsEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ private WrapperException WrapJsException(OriginalException originalException,
951951
sourceLine = sourcePropertyValue.ConvertToString().ToString();
952952
}
953953

954-
sourceFragment = JsErrorHelpers.GetSourceFragment(sourceLine, columnNumber);
954+
sourceFragment = JsErrorHelpers.GetSourceFragmentFromLine(sourceLine, columnNumber);
955955
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
956956
lineNumber, columnNumber, sourceFragment);
957957
}

src/MsieJavaScriptEngine/JsRt/Ie/ChakraIeJsRtJsEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ private WrapperException WrapJsException(OriginalException originalException,
978978
sourceLine = sourcePropertyValue.ConvertToString().ToString();
979979
}
980980

981-
sourceFragment = JsErrorHelpers.GetSourceFragment(sourceLine, columnNumber);
981+
sourceFragment = JsErrorHelpers.GetSourceFragmentFromLine(sourceLine, columnNumber);
982982
message = JsErrorHelpers.GenerateScriptErrorMessage(type, description, documentName,
983983
lineNumber, columnNumber, sourceFragment);
984984
}

src/MsieJavaScriptEngine/MsieJavaScriptEngine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<RepositoryUrl>https://github.com/Taritsyn/MsieJavaScriptEngine</RepositoryUrl>
2525
<RepositoryType>git</RepositoryType>
2626
<PackageTags>JavaScript;ECMAScript;MSIE;IE;Edge;Chakra</PackageTags>
27-
<PackageReleaseNotes>Changed a implementation of the `Dispose` method.</PackageReleaseNotes>
27+
<PackageReleaseNotes>Fixed a error, that occurred during the generation of error message.</PackageReleaseNotes>
2828
<NeutralLanguage>en-US</NeutralLanguage>
2929
<PackageOutputPath>../../nuget</PackageOutputPath>
3030
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

src/MsieJavaScriptEngine/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
=============
2222
RELEASE NOTES
2323
=============
24-
Changed a implementation of the `Dispose` method.
24+
Fixed a error, that occurred during the generation of error message.
2525

2626
============
2727
PROJECT SITE
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using NUnit.Framework;
2+
3+
using MsieJavaScriptEngine.Helpers;
4+
5+
namespace MsieJavaScriptEngine.Test.Common
6+
{
7+
[TestFixture]
8+
public class ErrorFormattingTests
9+
{
10+
[Test]
11+
public void GettingSourceFragmentFromLineIsCorrect()
12+
{
13+
// Arrange
14+
const string input1 = "";
15+
const string targetOutput1 = input1;
16+
17+
const string input2 = " \n";
18+
const string targetOutput2 = input2;
19+
20+
const string input3 = "var @variable3 = 678;";
21+
const string targetOutput3 = input3;
22+
23+
const string input4 = " Math.hasOwnProperty(\"log2\")||(Math.log2=function(n){" +
24+
"return Math.log(@n)*Math.LOG2E});";
25+
const string targetOutput4 = "…Math.hasOwnProperty(\"log2\")||(Math.log2=function(n){" +
26+
"return Math.log(@n)*Math.LOG2E});";
27+
28+
const string input5 = "function mix(destination,source){var propertyName;destination=destination||{};" +
29+
"for(propertyName in source){if(source.hasOwnProperty(propertyName){" +
30+
"destination[propertyName]=source[propertyName]}}return destination}"
31+
;
32+
const string targetOutput5 = "… in source){if(source.hasOwnProperty(propertyName){" +
33+
"destination[propertyName]=source[propertyName]}}r…";
34+
35+
const string input6 = "Object.hasOwnProperty(\"assign)||(Object.assign=function(n){" +
36+
"var u,i,f,t,r;if(typeof n==\"undefined\"||n===null)" +
37+
"throw new TypeError(\"Object.assign: argument is not an Object.\");" +
38+
"for(u=Object(n),f=arguments.length,i=1;i<f;i++)" +
39+
"if(t=arguments[i],typeof t!=\"undefined\"&&t!==null)for(r in t)" +
40+
"Object.prototype.hasOwnProperty.call(t,r)&&(u[r]=t[r]);return u});"
41+
;
42+
const string targetOutput6 = "Object.hasOwnProperty(\"assign)||(Object.assign=function(n){" +
43+
"var u,i,f,t,r;if(typeof n==\"undefined\"||n…";
44+
45+
const string input7 = "function base64_encode(a){" +
46+
"var b=\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\";" +
47+
"var c,o2,o3,h1,h2,h3,h4,bits,i=0,enc='';do{c=a.charCodeAt(i++);o2=a.charCodeAt(i++);" +
48+
"o3=a.charCodeAt(i++);bits=c<<16|o2<<8|o3;h1=bits>>18&0x3f;h2=bits>>12&0x3f;" +
49+
"h3=bits>>6&0x3f;h4=bits&0x3f;enc+=b.charAt(h1)+b.charAt(h2)+b.charAt(h3)+b.charAt(h4)}" +
50+
"while(i<a.length);switch(a.length%3){case 1:enc=enc.slice(0,-2)+'==';break;" +
51+
"case 2:enc=enc.slice(0,-1)+'=';break}return @enc}"
52+
;
53+
const string targetOutput7 = "…(a.length%3){case 1:enc=enc.slice(0,-2)+'==';break;" +
54+
"case 2:enc=enc.slice(0,-1)+'=';break}return @enc}";
55+
56+
// Act
57+
string output1 = JsErrorHelpers.GetSourceFragmentFromLine(input1, 1, 100);
58+
string output2 = JsErrorHelpers.GetSourceFragmentFromLine(input2, 1, 100);
59+
string output3 = JsErrorHelpers.GetSourceFragmentFromLine(input3, 5, 100);
60+
string output4 = JsErrorHelpers.GetSourceFragmentFromLine(input4, 70, 85);
61+
string output5 = JsErrorHelpers.GetSourceFragmentFromLine(input5, 145, 100);
62+
string output6 = JsErrorHelpers.GetSourceFragmentFromLine(input6, 23, 100);
63+
string output7 = JsErrorHelpers.GetSourceFragmentFromLine(input7, 465, 100);
64+
65+
// Assert
66+
Assert.AreEqual(targetOutput1, output1);
67+
Assert.AreEqual(targetOutput2, output2);
68+
Assert.AreEqual(targetOutput3, output3);
69+
Assert.AreEqual(targetOutput4, output4);
70+
Assert.AreEqual(targetOutput5, output5);
71+
Assert.AreEqual(targetOutput6, output6);
72+
Assert.AreEqual(targetOutput7, output7);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)