Skip to content

Commit 710fe68

Browse files
committed
GetSourceFragmentFromCode and GetSourceFragmentFromLine methods of JsErrorHelpers class were replaced by the GetTextFragment and GetTextFragmentFromLine methods of TextHelpers class
1 parent a6620ba commit 710fe68

File tree

11 files changed

+274
-210
lines changed

11 files changed

+274
-210
lines changed

build/nuget-metadata.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<Title>$(Product)</Title>
44
<Authors>Andrey Taritsyn</Authors>
5-
<PackageLicenseUrl>https://github.com/Taritsyn/JavaScriptEngineSwitcher/blob/master/LICENSE</PackageLicenseUrl>
5+
<PackageLicense>https://github.com/Taritsyn/JavaScriptEngineSwitcher/blob/master/LICENSE</PackageLicense>
66
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
77
<PackageProjectUrl>https://github.com/Taritsyn/JavaScriptEngineSwitcher</PackageProjectUrl>
88
<RepositoryUrl>https://github.com/Taritsyn/JavaScriptEngineSwitcher</RepositoryUrl>

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "2.1.403"
3+
"version": "2.1.500"
44
}
55
}

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using ErrorLocationItem = JavaScriptEngineSwitcher.Core.Helpers.ErrorLocationItem;
1818
using CoreErrorHelpers = JavaScriptEngineSwitcher.Core.Helpers.JsErrorHelpers;
1919
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
20+
using TextHelpers = JavaScriptEngineSwitcher.Core.Helpers.TextHelpers;
2021
using WrapperCompilationException = JavaScriptEngineSwitcher.Core.JsCompilationException;
2122
using WrapperEngineException = JavaScriptEngineSwitcher.Core.JsEngineException;
2223
using WrapperEngineLoadException = JavaScriptEngineSwitcher.Core.JsEngineLoadException;
@@ -1006,7 +1007,7 @@ private static WrapperException WrapJsException(OriginalException originalExcept
10061007
{
10071008
JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId);
10081009
sourceLine = sourcePropertyValue.ConvertToString().ToString();
1009-
sourceFragment = CoreErrorHelpers.GetSourceFragmentFromLine(sourceLine, columnNumber);
1010+
sourceFragment = TextHelpers.GetTextFragmentFromLine(sourceLine, columnNumber);
10101011
}
10111012

10121013
JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");

src/JavaScriptEngineSwitcher.Core/Extensions/StringExtensions.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,39 @@ public static string[] SplitToLines(this string source)
7979

8080
return result;
8181
}
82+
83+
/// <summary>
84+
/// Gets a character at the specified index from the string.
85+
/// A return value indicates whether the receiving succeeded.
86+
/// </summary>
87+
/// <param name="source">The source string</param>
88+
/// <param name="index">The zero-based index of the character</param>
89+
/// <param name="result">When this method returns, contains the character from the string,
90+
/// if the receiving succeeded, or null character if the receiving failed.
91+
/// The receiving fails if the index out of bounds.</param>
92+
/// <returns>true if the character was received successfully; otherwise, false</returns>
93+
internal static bool TryGetChar(this string source, int index, out char result)
94+
{
95+
if (source == null)
96+
{
97+
throw new ArgumentNullException(nameof(source));
98+
}
99+
100+
bool isSuccess;
101+
int length = source.Length;
102+
103+
if (length > 0 && index >= 0 && index < length)
104+
{
105+
result = source[index];
106+
isSuccess = true;
107+
}
108+
else
109+
{
110+
result = '\0';
111+
isSuccess = false;
112+
}
113+
114+
return isSuccess;
115+
}
82116
}
83117
}

src/JavaScriptEngineSwitcher.Core/Helpers/JsErrorHelpers.cs

Lines changed: 0 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -537,202 +537,6 @@ public static JsEngineLoadException WrapEngineLoadException(Exception exception,
537537

538538
#endregion
539539

540-
#region Misc
541-
542-
/// <summary>
543-
/// Gets a fragment from the source code
544-
/// </summary>
545-
/// <param name="sourceCode">Source code</param>
546-
/// <param name="lineNumber">Line number</param>
547-
/// <param name="columnNumber">Column number</param>
548-
/// <param name="maxFragmentLength">Maximum length of the source fragment</param>
549-
public static string GetSourceFragmentFromCode(string sourceCode, int lineNumber, int columnNumber,
550-
int maxFragmentLength = 100)
551-
{
552-
if (lineNumber <= 0 || string.IsNullOrEmpty(sourceCode))
553-
{
554-
return string.Empty;
555-
}
556-
557-
int lineStartPosition;
558-
int lineLength;
559-
GetPositionOfLine(sourceCode, lineNumber, out lineStartPosition, out lineLength);
560-
561-
string fragment = GetSourceFragment(sourceCode, lineStartPosition, lineLength, columnNumber,
562-
maxFragmentLength);
563-
564-
return fragment;
565-
}
566-
567-
/// <summary>
568-
/// Gets a fragment from the source line
569-
/// </summary>
570-
/// <param name="sourceLine">Content of the source line</param>
571-
/// <param name="columnNumber">Column number</param>
572-
/// <param name="maxFragmentLength">Maximum length of the source fragment</param>
573-
public static string GetSourceFragmentFromLine(string sourceLine, int columnNumber,
574-
int maxFragmentLength = 100)
575-
{
576-
if (string.IsNullOrEmpty(sourceLine))
577-
{
578-
return string.Empty;
579-
}
580-
581-
int lineStartPosition = 0;
582-
int lineLength = sourceLine.Length;
583-
string fragment = GetSourceFragment(sourceLine, lineStartPosition, lineLength,
584-
columnNumber, maxFragmentLength);
585-
586-
return fragment;
587-
}
588-
589-
private static string GetSourceFragment(string source, int position, int length,
590-
int columnNumber, int maxFragmentLength)
591-
{
592-
if (length == 0)
593-
{
594-
return string.Empty;
595-
}
596-
597-
string fragment;
598-
599-
if (length > maxFragmentLength)
600-
{
601-
const string ellipsisSymbol = "…";
602-
string startPart = string.Empty;
603-
string endPart = string.Empty;
604-
605-
var leftOffset = (int)Math.Floor((double)maxFragmentLength / 2);
606-
int fragmentStartPosition = columnNumber - leftOffset - 1;
607-
if (fragmentStartPosition > position)
608-
{
609-
if (length - fragmentStartPosition < maxFragmentLength)
610-
{
611-
fragmentStartPosition = length - maxFragmentLength;
612-
}
613-
}
614-
else
615-
{
616-
fragmentStartPosition = position;
617-
}
618-
int fragmentLength = maxFragmentLength;
619-
620-
if (fragmentStartPosition > position)
621-
{
622-
startPart = ellipsisSymbol;
623-
}
624-
if (fragmentStartPosition + fragmentLength < length)
625-
{
626-
endPart = ellipsisSymbol;
627-
}
628-
629-
StringBuilder fragmentBuilder = StringBuilderPool.GetBuilder();
630-
if (startPart.Length > 0)
631-
{
632-
fragmentBuilder.Append(startPart);
633-
}
634-
fragmentBuilder.Append(source.Substring(fragmentStartPosition, fragmentLength));
635-
if (endPart.Length > 0)
636-
{
637-
fragmentBuilder.Append(endPart);
638-
}
639-
640-
fragment = fragmentBuilder.ToString();
641-
StringBuilderPool.ReleaseBuilder(fragmentBuilder);
642-
}
643-
else
644-
{
645-
fragment = position > 0 || length < source.Length ?
646-
source.Substring(position, length) : source;
647-
}
648-
649-
return fragment;
650-
}
651-
652-
private static void GetPositionOfLine(string sourceCode, int lineNumber, out int position, out int length)
653-
{
654-
int currentLineNumber = 0;
655-
position = 0;
656-
length = 0;
657-
658-
int sourceCodeLength = sourceCode.Length;
659-
if (sourceCodeLength > 0)
660-
{
661-
int currentPosition;
662-
int currentLength;
663-
int sourceCodeEndPosition = sourceCodeLength - 1;
664-
int lineBreakPosition = int.MinValue;
665-
int lineBreakLength = 0;
666-
667-
do
668-
{
669-
currentLineNumber++;
670-
currentPosition = lineBreakPosition == int.MinValue ? 0 : lineBreakPosition + lineBreakLength;
671-
currentLength = sourceCodeEndPosition - currentPosition + 1;
672-
673-
FindLineBreak(sourceCode, currentPosition, currentLength,
674-
out lineBreakPosition, out lineBreakLength);
675-
676-
if (currentLineNumber == lineNumber)
677-
{
678-
if (lineBreakPosition != 0)
679-
{
680-
position = currentPosition;
681-
int endPosition = lineBreakPosition != -1 ?
682-
lineBreakPosition - 1 : sourceCodeEndPosition;
683-
length = endPosition - position + 1;
684-
}
685-
break;
686-
}
687-
}
688-
while (lineBreakPosition != -1 && lineBreakPosition <= sourceCodeEndPosition);
689-
}
690-
}
691-
692-
/// <summary>
693-
/// Finds a line break
694-
/// </summary>
695-
/// <param name="sourceCode">Source code</param>
696-
/// <param name="startPosition">Position in the input string that defines the leftmost
697-
/// position to be searched</param>
698-
/// <param name="lineBreakPosition">Position of line break</param>
699-
/// <param name="lineBreakLength">Length of line break</param>
700-
private static void FindLineBreak(string sourceCode, int startPosition,
701-
out int lineBreakPosition, out int lineBreakLength)
702-
{
703-
int length = sourceCode.Length - startPosition;
704-
705-
FindLineBreak(sourceCode, startPosition, length,
706-
out lineBreakPosition, out lineBreakLength);
707-
}
708-
709-
/// <summary>
710-
/// Finds a line break
711-
/// </summary>
712-
/// <param name="sourceCode">Source code</param>
713-
/// <param name="startPosition">Position in the input string that defines the leftmost
714-
/// position to be searched</param>
715-
/// <param name="length">Number of characters in the substring to include in the search</param>
716-
/// <param name="lineBreakPosition">Position of line break</param>
717-
/// <param name="lineBreakLength">Length of line break</param>
718-
private static void FindLineBreak(string sourceCode, int startPosition, int length,
719-
out int lineBreakPosition, out int lineBreakLength)
720-
{
721-
Match lineBreakMatch = _lineBreakRegex.Match(sourceCode, startPosition, length);
722-
if (lineBreakMatch.Success)
723-
{
724-
lineBreakPosition = lineBreakMatch.Index;
725-
lineBreakLength = lineBreakMatch.Length;
726-
}
727-
else
728-
{
729-
lineBreakPosition = -1;
730-
lineBreakLength = 0;
731-
}
732-
}
733-
734-
#endregion
735-
736540
#region Obsolete methods
737541

738542
/// <summary>

0 commit comments

Comments
 (0)