-
Notifications
You must be signed in to change notification settings - Fork 176
/
linter.ts
203 lines (185 loc) · 6.41 KB
/
linter.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* SonarQube JavaScript Plugin
* Copyright (C) 2011-2020 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 { rules as sonarjsRules } from 'eslint-plugin-sonarjs';
import { rules as chaiFriendlyRules } from 'eslint-plugin-chai-friendly';
import {
decorateTypescriptEslint,
decorateChaiFriendly,
} from './rules/no-unused-expressions-decorator';
import { rules as internalRules } from './rules/main';
import { Linter, SourceCode, Rule as ESLintRule } from 'eslint';
import { Rule, Issue, IssueLocation } from './analyzer';
/**
* In order to overcome ESLint limitation regarding issue reporting,
* ESLint-based rules send extra information by serializing in the issue message an object
* having the following structure
*/
interface EncodedMessage {
message: string;
cost?: number;
secondaryLocations: IssueLocation[];
}
export interface AdditionalRule {
ruleId: string;
ruleModule: ESLintRule.RuleModule;
ruleConfig: any[];
}
const linter = new Linter();
linter.defineRules(sonarjsRules);
linter.defineRules(internalRules);
let linterConfig: Linter.Config;
let rules: Map<string, ESLintRule.RuleModule>;
const NO_UNUSED_EXPRESSIONS = 'no-unused-expressions';
// core implementation of this rule raises FPs on chai framework
linter.defineRule(
NO_UNUSED_EXPRESSIONS,
decorateChaiFriendly(chaiFriendlyRules[NO_UNUSED_EXPRESSIONS]),
);
try {
// we load "@typescript-eslint/eslint-plugin" dynamically as it requires TS and so we don't need typescript dependency when analysing pure JS project
const typescriptEslintRules = require('@typescript-eslint/eslint-plugin').rules;
// TS implementation of no-throw-literal is not supporting JS code.
delete typescriptEslintRules['no-throw-literal'];
linter.defineRules(typescriptEslintRules);
const noUnusedExpressionsRule = typescriptEslintRules[NO_UNUSED_EXPRESSIONS];
if (noUnusedExpressionsRule) {
linter.defineRule(NO_UNUSED_EXPRESSIONS, decorateTypescriptEslint(noUnusedExpressionsRule));
}
} catch {
// do nothing, "typescript" is not there
}
/**
* 'additionalRules' - rules used for computing metrics (incl. highlighting) when it requires access to the rule context; resulting value is encoded in the message
*/
export function analyze(
sourceCode: SourceCode,
filePath: string,
inputRules: Rule[],
...additionalRules: AdditionalRule[]
) {
if (!linterConfig) {
additionalRules.forEach(additionalRule =>
linter.defineRule(additionalRule.ruleId, additionalRule.ruleModule),
);
rules = linter.getRules();
linterConfig = createLinterConfig(inputRules, additionalRules);
}
const issues = linter
.verify(sourceCode, linterConfig, {
filename: filePath,
allowInlineConfig: false,
})
.map(removeIrrelevantProperties)
.map(issue => {
if (!issue) {
return null;
}
return decodeSonarRuntimeIssue(rules.get(issue.ruleId), issue);
})
.filter((issue): issue is Issue => issue !== null)
.map(normalizeIssueLocation);
return { issues };
}
// exported for testing
export function decodeSonarRuntimeIssue(
ruleModule: ESLintRule.RuleModule | undefined,
issue: Issue,
): Issue | null {
if (hasSonarRuntimeOption(ruleModule, issue.ruleId)) {
try {
const encodedMessage: EncodedMessage = JSON.parse(issue.message);
return { ...issue, ...encodedMessage };
} catch (e) {
console.error(
`Failed to parse encoded issue message for rule ${issue.ruleId}:\n"${issue.message}"`,
e,
);
return null;
}
}
return issue;
}
function removeIrrelevantProperties(eslintIssue: Linter.LintMessage): Issue | null {
// ruleId equals 'null' for parsing error,
// but it should not happen because we lint ready AST and not file content
if (!eslintIssue.ruleId) {
console.error("Illegal 'null' ruleId for eslint issue");
return null;
}
return {
column: eslintIssue.column,
line: eslintIssue.line,
endColumn: eslintIssue.endColumn,
endLine: eslintIssue.endLine,
ruleId: eslintIssue.ruleId,
message: eslintIssue.message,
secondaryLocations: [],
};
}
function createLinterConfig(
inputRules: Rule[],
additionalRules: { ruleId: string; ruleModule: ESLintRule.RuleModule; ruleConfig: any[] }[],
) {
const ruleConfig: Linter.Config = {
rules: {},
parserOptions: { sourceType: 'module', ecmaVersion: 2018 },
};
inputRules.forEach(inputRule => {
const ruleModule = rules.get(inputRule.key);
ruleConfig.rules![inputRule.key] = ['error', ...getRuleConfig(ruleModule, inputRule)];
});
additionalRules.forEach(
additionalRule =>
(ruleConfig.rules![additionalRule.ruleId] = ['error', ...additionalRule.ruleConfig]),
);
return ruleConfig;
}
/**
* 'sonar-runtime' is the option used by eslint-plugin-sonarjs rules to distinguish
* when they are executed in a sonar* context or in eslint
*
* exported for testing
*/
export function getRuleConfig(ruleModule: ESLintRule.RuleModule | undefined, inputRule: Rule) {
const options = inputRule.configurations;
if (hasSonarRuntimeOption(ruleModule, inputRule.key)) {
return options.concat('sonar-runtime');
}
return options;
}
function hasSonarRuntimeOption(ruleModule: ESLintRule.RuleModule | undefined, ruleId: string) {
if (!ruleModule) {
console.log(`DEBUG ruleModule not found for rule ${ruleId}`);
return false;
}
if (!ruleModule.meta || !ruleModule.meta.schema) {
return false;
}
const { schema } = ruleModule.meta;
const props = Array.isArray(schema) ? schema : [schema];
return props.some(option => !!option.enum && option.enum.includes('sonar-runtime'));
}
function normalizeIssueLocation(issue: Issue) {
issue.column -= 1;
if (issue.endColumn) {
issue.endColumn -= 1;
}
return issue;
}