From c3ccc3103f8b03b3fc3aded6bba67082da8441a2 Mon Sep 17 00:00:00 2001 From: Stas Vilchik Date: Tue, 10 Oct 2017 11:27:50 +0200 Subject: [PATCH] SONAR-9749 Order secondary locations markers by line --- .../main/js/helpers/__tests__/issues-test.ts | 21 +++++++++++++++++-- .../sonar-web/src/main/js/helpers/issues.ts | 12 +++++++++-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/server/sonar-web/src/main/js/helpers/__tests__/issues-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/issues-test.ts index 034fcb4a916..90a80fd6f36 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/issues-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/issues-test.ts @@ -31,7 +31,7 @@ it('should populate comments data', () => { const issue = { comments: [ { - createdAt: '2017-04-11T10:38:09+0200', + createdAt: '2017-04-11T10:38:090200', htmlText: 'comment!', key: 'AVtcKbZkQmGLa7yW8J71', login: 'admin', @@ -47,7 +47,7 @@ it('should populate comments data', () => { authorAvatar: 'c1244e6857f7be3dc4549d9e9d51c631', authorLogin: 'admin', authorName: 'Admin Admin', - createdAt: '2017-04-11T10:38:09+0200', + createdAt: '2017-04-11T10:38:090200', htmlText: 'comment!', key: 'AVtcKbZkQmGLa7yW8J71', login: undefined, @@ -56,3 +56,20 @@ it('should populate comments data', () => { } ]); }); + +it('orders secondary locations', () => { + const issue = { + flows: [ + { locations: [{ textRange: { startLine: 68, startOffset: 5, endLine: 68, endOffset: 7 } }] }, + { locations: [{ textRange: { startLine: 43, startOffset: 8, endLine: 43, endOffset: 12 } }] }, + { locations: [{ textRange: { startLine: 43, startOffset: 6, endLine: 43, endOffset: 8 } }] }, + { locations: [{ textRange: { startLine: 70, startOffset: 12, endLine: 70, endOffset: 16 } }] } + ] + } as any; + expect(parseIssueFromResponse(issue).secondaryLocations).toEqual([ + { textRange: { startLine: 43, startOffset: 6, endLine: 43, endOffset: 8 } }, + { textRange: { startLine: 43, startOffset: 8, endLine: 43, endOffset: 12 } }, + { textRange: { startLine: 68, startOffset: 5, endLine: 68, endOffset: 7 } }, + { textRange: { startLine: 70, startOffset: 12, endLine: 70, endOffset: 16 } } + ]); +}); diff --git a/server/sonar-web/src/main/js/helpers/issues.ts b/server/sonar-web/src/main/js/helpers/issues.ts index cce6a2adc49..efa8b272af6 100644 --- a/server/sonar-web/src/main/js/helpers/issues.ts +++ b/server/sonar-web/src/main/js/helpers/issues.ts @@ -134,7 +134,7 @@ function reverseLocations(locations: FlowLocation[]): FlowLocation[] { function splitFlows( issue: RawIssue -): { secondaryLocations: Array; flows: Array> } { +): { secondaryLocations: FlowLocation[]; flows: FlowLocation[][] } { const parsedFlows = (issue.flows || []) .filter(flow => flow.locations != null) .map(flow => flow.locations!.filter(location => location.textRange != null)); @@ -142,10 +142,18 @@ function splitFlows( const onlySecondaryLocations = parsedFlows.every(flow => flow.length === 1); return onlySecondaryLocations - ? { secondaryLocations: flatten(parsedFlows), flows: [] } + ? { secondaryLocations: orderLocations(flatten(parsedFlows)), flows: [] } : { secondaryLocations: [], flows: parsedFlows.map(reverseLocations) }; } +function orderLocations(locations: FlowLocation[]) { + return sortBy( + locations, + location => location.textRange && location.textRange.startLine, + location => location.textRange && location.textRange.startOffset + ); +} + export function parseIssueFromResponse( issue: RawIssue, components?: Component[], -- 2.39.5