This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
/
TsRulesDefinition.java
217 lines (168 loc) · 7.61 KB
/
TsRulesDefinition.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package com.pablissimo.sonar;
import com.pablissimo.sonar.model.TsLintRule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.config.Settings;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.rule.RulesDefinition;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
public class TsRulesDefinition implements RulesDefinition {
private static final Logger LOG = LoggerFactory.getLogger(TsRulesDefinition.class);
public static final String REPOSITORY_NAME = "tslint";
public static final String DEFAULT_RULE_SEVERITY = Severity.defaultSeverity();
public static final String DEFAULT_RULE_DESCRIPTION = "No description provided for this TsLint rule";
public static final String DEFAULT_RULE_DEBT_SCALAR = "0min";
public static final String DEFAULT_RULE_DEBT_OFFSET = "0min";
public static final String DEFAULT_RULE_DEBT_TYPE = RuleType.CODE_SMELL.name();
private static final String CORE_RULES_CONFIG_RESOURCE_PATH = "/tslint/tslint-rules.properties";
/** The SonarQube rule that will contain all unknown TsLint issues. */
public static final TsLintRule TSLINT_UNKNOWN_RULE = new TsLintRule(
"tslint-issue",
Severity.MAJOR,
"tslint issues that are not yet known to the plugin",
"No description for TsLint rule");
private List<TsLintRule> tslintCoreRules = new ArrayList<>();
private List<TsLintRule> tslintRules = new ArrayList<>();
private final Settings settings;
public TsRulesDefinition() {
this(null);
}
public TsRulesDefinition(Settings settings) {
this.settings = settings;
loadCoreRules();
loadCustomRules();
}
private void loadCoreRules() {
InputStream coreRulesStream = TsRulesDefinition.class.getResourceAsStream(CORE_RULES_CONFIG_RESOURCE_PATH);
loadRules(coreRulesStream, tslintCoreRules);
}
private void loadCustomRules() {
if (this.settings == null)
return;
List<String> configKeys = settings.getKeysStartingWith(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS);
for (String cfgKey : configKeys) {
if (!cfgKey.endsWith("config"))
continue;
String rulesConfig = settings.getString(cfgKey);
InputStream rulesConfigStream = new ByteArrayInputStream(rulesConfig.getBytes(Charset.defaultCharset()));
loadRules(rulesConfigStream, tslintRules);
}
}
public static void loadRules(InputStream stream, List<TsLintRule> rulesCollection) {
Properties properties = new Properties();
try {
properties.load(stream);
} catch (IOException e) {
LOG.error("Error while loading TsLint rules: " + e.getMessage());
}
for(String propKey : properties.stringPropertyNames()) {
if (propKey.contains("."))
continue;
String ruleEnabled = properties.getProperty(propKey);
if (!ruleEnabled.equals("true"))
continue;
String ruleId = propKey;
String ruleName = properties.getProperty(propKey + ".name", ruleId.replace("-", " "));
String ruleSeverity = properties.getProperty(propKey + ".severity", DEFAULT_RULE_SEVERITY);
String ruleDescription = properties.getProperty(propKey + ".description", DEFAULT_RULE_DESCRIPTION);
String debtRemediationFunction = properties.getProperty(propKey + ".debtFunc", null);
String debtRemediationScalar = properties.getProperty(propKey + ".debtScalar", DEFAULT_RULE_DEBT_SCALAR);
String debtRemediationOffset = properties.getProperty(propKey + ".debtOffset", DEFAULT_RULE_DEBT_OFFSET);
String debtType = properties.getProperty(propKey + ".debtType", DEFAULT_RULE_DEBT_TYPE);
TsLintRule tsRule = null;
// try to apply the specified debt remediation function
if (debtRemediationFunction != null) {
DebtRemediationFunction.Type debtRemediationFunctionEnum = DebtRemediationFunction.Type.valueOf(debtRemediationFunction);
tsRule = new TsLintRule(
ruleId,
ruleSeverity,
ruleName,
ruleDescription,
debtRemediationFunctionEnum,
debtRemediationScalar,
debtRemediationOffset,
debtType
);
}
// no debt remediation function specified
if (tsRule == null) {
tsRule = new TsLintRule(
ruleId,
ruleSeverity,
ruleName,
ruleDescription
);
}
rulesCollection.add(tsRule);
}
Collections.sort(rulesCollection, new Comparator<TsLintRule>() {
@Override
public int compare(TsLintRule r1, TsLintRule r2) {
return r1.key.compareTo(r2.key);
}
});
}
private void createRule(NewRepository repository, TsLintRule tsRule) {
NewRule sonarRule =
repository
.createRule(tsRule.key)
.setName(tsRule.name)
.setSeverity(tsRule.severity)
.setHtmlDescription(tsRule.htmlDescription)
.setStatus(RuleStatus.READY);
if (tsRule.hasDebtRemediation) {
DebtRemediationFunction debtRemediationFn = null;
DebtRemediationFunctions funcs = sonarRule.debtRemediationFunctions();
switch (tsRule.debtRemediationFunction)
{
case LINEAR:
debtRemediationFn = funcs.linear(tsRule.debtRemediationScalar);
break;
case LINEAR_OFFSET:
debtRemediationFn = funcs.linearWithOffset(tsRule.debtRemediationScalar, tsRule.debtRemediationOffset);
break;
case CONSTANT_ISSUE:
debtRemediationFn = funcs.constantPerIssue(tsRule.debtRemediationScalar);
break;
}
sonarRule.setDebtRemediationFunction(debtRemediationFn);
}
RuleType type = null;
if (tsRule.debtType != null && RuleType.names().contains(tsRule.debtType)) {
// Try and parse it as a new-style rule type (since 5.5 SQALE's been replaced
// with something simpler, and there's really only three buckets)
type = RuleType.valueOf(tsRule.debtType);
}
if (type == null) {
type = RuleType.CODE_SMELL;
}
sonarRule.setType(type);
}
public void define(Context context) {
NewRepository repository =
context
.createRepository(REPOSITORY_NAME, TypeScriptLanguage.LANGUAGE_KEY)
.setName("TsLint Analyzer");
createRule(repository, TSLINT_UNKNOWN_RULE);
// add the TsLint builtin core rules
for (TsLintRule coreRule : tslintCoreRules) {
createRule(repository, coreRule);
}
// add additional custom TsLint rules
for (TsLintRule customRule : tslintRules) {
createRule(repository, customRule);
}
repository.done();
}
public List<TsLintRule> getCoreRules() {
return tslintCoreRules;
}
public List<TsLintRule> getRules() {
return tslintRules;
}
}