-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathlocation.ts
74 lines (71 loc) · 2.77 KB
/
location.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as estree from 'estree';
import { AST } from 'eslint';
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { EncodedMessage, IssueLocation } from 'eslint-plugin-sonarjs/lib/utils/locations';
export type LocationHolder = AST.Token | TSESTree.Node | estree.Node | { loc: AST.SourceLocation };
/**
* Encodes an ESLint descriptor message with secondary locations
*
* The encoding consists in stringifying a JavaScript object with
* `JSON.stringify` that includes the ESLint's descriptor message
* along with second location information: message and location.
*
* This encoded message is eventually decoded by the linter wrapper
* on the condition that the rule definition of the flagged problem
* defines the internal `sonar-runtime` parameter in its schema.
*
* @param message the ESLint descriptor message
* @param secondaryLocationsHolder the secondary locations
* @param secondaryMessages the messages for each secondary location
* @param cost the optional cost to fix
* @returns the encoded message with secondary locations
*/
export function toEncodedMessage(
message: string,
secondaryLocationsHolder: Array<LocationHolder> = [],
secondaryMessages?: (string | undefined)[],
cost?: number,
): string {
const encodedMessage: EncodedMessage = {
message,
cost,
secondaryLocations: secondaryLocationsHolder.map((locationHolder, index) =>
toSecondaryLocation(
locationHolder,
!!secondaryMessages ? secondaryMessages[index] : undefined,
),
),
};
return JSON.stringify(encodedMessage);
}
function toSecondaryLocation(locationHolder: LocationHolder, message?: string): IssueLocation {
if (!locationHolder.loc) {
throw new Error('Invalid secondary location');
}
return {
message,
column: locationHolder.loc.start.column,
line: locationHolder.loc.start.line,
endColumn: locationHolder.loc.end.column,
endLine: locationHolder.loc.end.line,
};
}