Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Implements feature for issue #120 #122

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/main/java/com/pablissimo/sonar/TsRulesDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ private void loadCustomRules() {
if (this.settings == null)
return;

if (settings.getBoolean(TypeScriptPlugin.SETTING_TS_LINT_DISALLOW_CUSTOM_RULES)) {
LOG.info("Usage of custom rules is inhibited");
return;
}

List<String> configKeys = settings.getKeysStartingWith(TypeScriptPlugin.SETTING_TS_RULE_CONFIGS);

for (String cfgKey : configKeys) {
Expand Down Expand Up @@ -128,7 +133,7 @@ public static void loadRules(InputStream stream, List<TsLintRule> rulesCollectio
ruleDescription
);
}

rulesCollection.add(tsRule);
}

Expand All @@ -141,7 +146,7 @@ public int compare(TsLintRule r1, TsLintRule r2) {
}

private void createRule(NewRepository repository, TsLintRule tsRule) {
NewRule sonarRule =
NewRule sonarRule =
repository
.createRule(tsRule.key)
.setName(tsRule.name)
Expand Down Expand Up @@ -170,19 +175,19 @@ private void createRule(NewRepository repository, TsLintRule tsRule) {

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);
}

Expand All @@ -203,7 +208,7 @@ public void define(Context context) {
for (TsLintRule customRule : tslintRules) {
createRule(repository, customRule);
}

repository.done();
}

Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/pablissimo/sonar/TypeScriptPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@
description = "If set, the contents of this file will parsed for linting issues rather than the plugin running tslint itself",
project = true,
global = false
),
@Property(
key = TypeScriptPlugin.SETTING_TS_LINT_DISALLOW_CUSTOM_RULES,
defaultValue = "false",
type = PropertyType.BOOLEAN,
name = "Disallow the usage of custom rules",
description = "If set to true, custom rules will no longer be used for analysis",
project = false,
global = true
)
})
public class TypeScriptPlugin implements Plugin {
Expand All @@ -150,11 +159,11 @@ public class TypeScriptPlugin implements Plugin {

// Current settings
public static final String SETTING_EXCLUDE_TYPE_DEFINITION_FILES = "sonar.ts.excludeTypeDefinitionFiles";

public static final String SETTING_FORCE_ZERO_COVERAGE = "sonar.ts.coverage.forceZeroIfUnspecified";
public static final String SETTING_IGNORE_NOT_FOUND = "sonar.ts.coverage.ignoreNotFound";
public static final String SETTING_LCOV_REPORT_PATH = "sonar.ts.coverage.lcovReportPath";

public static final String SETTING_TS_LINT_ENABLED = "sonar.ts.tslint.enabled";
public static final String SETTING_TS_LINT_PATH = "sonar.ts.tslint.path";
public static final String SETTING_TS_LINT_CONFIG_PATH = "sonar.ts.tslint.configPath";
Expand All @@ -164,8 +173,9 @@ public class TypeScriptPlugin implements Plugin {
public static final String SETTING_TS_LINT_TYPECHECK = "sonar.ts.tslint.typeCheck";
public static final String SETTING_TS_LINT_PROJECT_PATH = "sonar.ts.tslint.projectPath";
public static final String SETTING_TS_LINT_OUTPUT_PATH = "sonar.ts.tslint.outputPath";
public static final String SETTING_TS_LINT_DISALLOW_CUSTOM_RULES = "sonar.ts.disallowcustomrules";



@Override
public void define(Context ctx) {
// Core components - the actual sensors doing the work or configuring
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/pablissimo/sonar/TsRulesDefinitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void ConfiguresAdditionalRules() {
assertEquals(Severity.MAJOR, rule5.severity());
assertEquals("#5 description", rule5.htmlDescription());
assertEquals(RuleType.VULNERABILITY, rule5.type());

assertEquals("30min", rule5.debtRemediationFunction().gapMultiplier());
assertEquals("15min", rule5.debtRemediationFunction().baseEffort());
}
Expand Down Expand Up @@ -193,6 +193,18 @@ public void CheckCustomRulesConfigNotProvided() {
assertEquals(0, rules.size());
}

@Test
public void CheckCustomRulesInhibited() {

Settings settings = mock(Settings.class);
when(settings.getBoolean(TypeScriptPlugin.SETTING_TS_LINT_DISALLOW_CUSTOM_RULES)).thenReturn(true);

TsRulesDefinition rulesDef = new TsRulesDefinition(settings);
List<TsLintRule> rules = rulesDef.getRules();
assertNotNull(rules);
assertEquals(0, rules.size());
}

private Rule getRule(String name) {
return this.context.repository(TsRulesDefinition.REPOSITORY_NAME).rule(name);
}
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/com/pablissimo/sonar/TypeScriptPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void definesExpectedProperties() {
Annotation annotation = plugin.getClass().getAnnotations()[0];
Properties propertiesAnnotation = (Properties) annotation;

assertEquals(12, propertiesAnnotation.value().length);
assertEquals(13, propertiesAnnotation.value().length);

Property[] properties = propertiesAnnotation.value();
assertNotNull(findPropertyByName(properties,
Expand All @@ -84,6 +84,8 @@ public void definesExpectedProperties() {
TypeScriptPlugin.SETTING_TS_LINT_PROJECT_PATH));
assertNotNull(findPropertyByName(properties,
TypeScriptPlugin.SETTING_TS_LINT_OUTPUT_PATH));
assertNotNull(findPropertyByName(properties,
TypeScriptPlugin.SETTING_TS_LINT_DISALLOW_CUSTOM_RULES));
}

@Test
Expand Down