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

Implementation for issue #120 #121

Closed
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
29 changes: 13 additions & 16 deletions src/main/java/com/pablissimo/sonar/TsLintSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TsLintSensor implements Sensor {
private PathResolver resolver;
private TsLintExecutor executor;
private TsLintParser parser;

public TsLintSensor(Settings settings, PathResolver resolver, TsLintExecutor executor, TsLintParser parser) {
this.settings = settings;
this.resolver = resolver;
Expand All @@ -47,16 +47,13 @@ public void execute(SensorContext ctx) {

TsLintExecutorConfig config = TsLintExecutorConfig.fromSettings(this.settings, ctx, this.resolver);

if (!config.useExistingTsLintOutput()) {
if (config.getPathToTsLint() == null) {
LOG.warn("Path to tslint not defined or not found. Skipping tslint analysis.");
return;
} else {
if (config.getConfigFile() == null && config.getPathToTsConfig() == null) {
LOG.warn("Path to tslint.json and tsconfig.json configuration files either not defined or not found - at least one is required. Skipping tslint analysis.");
return;
}
}
if (config.getPathToTsLint() == null) {
LOG.warn("Path to tslint not defined or not found. Skipping tslint analysis.");
return;
}
else if (config.getConfigFile() == null && config.getPathToTsConfig() == null) {
LOG.warn("Path to tslint.json and tsconfig.json configuration files either not defined or not found - at least one is required. Skipping tslint analysis.");
return;
}

boolean skipTypeDefFiles = settings.getBoolean(TypeScriptPlugin.SETTING_EXCLUDE_TYPE_DEFINITION_FILES);
Expand All @@ -77,7 +74,7 @@ public void execute(SensorContext ctx) {
String pathAdjusted = file.absolutePath();
paths.add(pathAdjusted);
}

List<String> jsonResults = this.executor.execute(config, paths);

Map<String, List<TsLintIssue>> issues = this.parser.parse(jsonResults);
Expand All @@ -97,11 +94,11 @@ public void execute(SensorContext ctx) {

File matchingFile = ctx.fileSystem().resolvePath(filePath);
InputFile inputFile = null;

if (shouldSkipFile(matchingFile, skipTypeDefFiles)) {
continue;
}

if (matchingFile != null) {
try {
inputFile = ctx.fileSystem().inputFile(ctx.fileSystem().predicates().is(matchingFile));
Expand All @@ -111,7 +108,7 @@ public void execute(SensorContext ctx) {
continue;
}
}

if (inputFile == null) {
LOG.warn("TsLint reported issues against a file that isn't in the analysis set - will be ignored: " + filePath);
continue;
Expand Down Expand Up @@ -145,7 +142,7 @@ public void execute(SensorContext ctx) {
}
}
}

private boolean shouldSkipFile(File f, boolean skipTypeDefFiles) {
return skipTypeDefFiles && f.getName().toLowerCase().endsWith("." + TypeScriptLanguage.LANGUAGE_DEFINITION_EXTENSION);
}
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/pablissimo/sonar/TsRulesDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ private void loadCoreRules() {
}

private void loadCustomRules() {
if (this.settings == null)
if (this.settings == null )
return;

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

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

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

rulesCollection.add(tsRule);
}

Expand All @@ -141,7 +145,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 +174,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 +207,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