Ejemplo n.º 1
0
        public void TSLintConverter_CreateResult_CreatesExpectedResult()
        {
            var            converter      = new TSLintConverter();
            TSLintLogEntry tSLintLogEntry = CreateTestLogEntry();

            Result actualResult   = converter.CreateResult(tSLintLogEntry);
            Result expectedResult = CreateTestResult();

            Result.ValueComparer.Equals(actualResult, expectedResult).Should().BeTrue();
        }
Ejemplo n.º 2
0
        private static void CompareEntries(TSLintLogEntry actualEntry, TSLintLogEntry expectedEntry)
        {
            actualEntry.EndPosition.Character.Should().Be(expectedEntry.EndPosition.Character);
            actualEntry.EndPosition.Line.Should().Be(expectedEntry.EndPosition.Line);
            actualEntry.EndPosition.Position.Should().Be(expectedEntry.EndPosition.Position);
            actualEntry.Failure.Should().Be(expectedEntry.Failure);

            CompareFixes(actualEntry.Fixes, expectedEntry.Fixes);

            actualEntry.Name.Should().Be(expectedEntry.Name);
            actualEntry.RuleName.Should().Be(expectedEntry.RuleName);
            actualEntry.RuleSeverity.Should().Be(expectedEntry.RuleSeverity);
            actualEntry.StartPosition.Character.Should().Be(expectedEntry.StartPosition.Character);
            actualEntry.EndPosition.Line.Should().Be(expectedEntry.StartPosition.Line);
            actualEntry.EndPosition.Position.Should().Be(expectedEntry.StartPosition.Position);
        }
Ejemplo n.º 3
0
        internal Result CreateResult(TSLintLogEntry entry)
        {
            entry = entry ?? throw new ArgumentNullException(nameof(entry));

            Result result = new Result()
            {
                RuleId  = entry.RuleName,
                Message = new Message {
                    Text = entry.Failure
                }
            };

            switch (entry.RuleSeverity)
            {
            case "ERROR":
                result.Level = ResultLevel.Error;
                break;

            case "WARN":
            case "WARNING":
                result.Level = ResultLevel.Warning;
                break;

            case "DEFAULT":
            default:
                result.Level = ResultLevel.Note;
                break;
            }

            Region region = new Region()
            {
                // The TSLint logs have line and column start at 0, Sarif has them starting at 1, so add 1 to each
                StartLine   = entry.StartPosition.Line + 1,
                StartColumn = entry.StartPosition.Character + 1,
                EndLine     = entry.EndPosition.Line + 1,
                EndColumn   = entry.EndPosition.Character + 1,

                CharOffset = entry.StartPosition.Position
            };

            int length = entry.EndPosition.Position - entry.StartPosition.Position;

            region.CharLength = length > 0 ? length : 0;

            Uri analysisTargetUri = new Uri(entry.Name, UriKind.Relative);

            var      physicalLocation = new PhysicalLocation(id: 0, fileLocation: new FileLocation(uri: analysisTargetUri, uriBaseId: null), region: region, contextRegion: null);
            Location location         = new Location()
            {
                PhysicalLocation = physicalLocation
            };

            result.Locations = new List <Location>()
            {
                location
            };

            if (entry.Fixes?.Any() == true)
            {
                IList <Replacement> replacements = new List <Replacement>();

                foreach (TSLintLogFix fix in entry.Fixes)
                {
                    Replacement replacement = new Replacement();

                    replacement.DeletedRegion = new Region
                    {
                        CharLength = fix.InnerLength,
                        CharOffset = fix.InnerStart
                    };

                    if (!string.IsNullOrEmpty(fix.InnerText))
                    {
                        replacement.InsertedContent = new FileContent
                        {
                            Text = fix.InnerText
                        };
                    }

                    replacements.Add(replacement);
                }

                FileChange sarifFileChange = new FileChange(fileLocation: new FileLocation(uri: analysisTargetUri, uriBaseId: null), replacements: replacements);

                Fix sarifFix = new Fix(description: null, fileChanges: new List <FileChange>()
                {
                    sarifFileChange
                });
                result.Fixes = new List <Fix> {
                    sarifFix
                };
            }

            return(result);
        }