Skip to content

Commit e259fef

Browse files
authored
added extendsConfigPath field to config files, fixes #401 (#407)
* added `extendsConfigPath` field to config files, fixes #401
1 parent 357977f commit e259fef

File tree

9 files changed

+66
-12
lines changed

9 files changed

+66
-12
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## dev branch / next version (2.x.x)
2+
- Added `extendsConfigPath` field to config files, to allow one level of extension, fixes [#401](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/401) ([#407](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/407))
23

34
## version 2.2.2
45

gruntfile.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ module.exports = function(grunt) {
1515
haxe: haxeOptions(),
1616

1717
zip: {
18-
"checkstyle.zip": ["src/**", "resources/sample-config.json", "resources/logo.png", "haxelib.json", "run.n", "README.md"]
18+
"checkstyle.zip": [
19+
"src/**",
20+
"resources/sample-config.json", "resources/logo.png", "resources/codeclimate_pr.png",
21+
"haxelib.json", "run.n", "README.md", "CHANGES.md"
22+
]
1923
}
2024
});
2125

resources/default-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"extendsConfigPath": "",
23
"defineCombinations": [],
34
"defaultSeverity": "INFO",
45
"baseDefines": [],

src/checkstyle/Config.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package checkstyle;
33
import checkstyle.CheckMessage.SeverityLevel;
44

55
typedef Config = {
6+
@:optional var extendsConfigPath:String;
67
@:optional var defaultSeverity:SeverityLevel;
78
// defines that are always added
89
@:optional var baseDefines:Array<String>;

src/checkstyle/Main.hx

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,37 @@ class Main {
115115
excludePath = DEFAULT_EXCLUDE_CONFIG;
116116
}
117117

118-
if (configPath != null && FileSystem.exists(configPath) && !FileSystem.isDirectory(configPath)) loadConfig(configPath);
119-
else addAllChecks();
118+
loadConfig(configPath);
120119

121120
if (excludePath != null) loadExcludeConfig(excludePath);
122-
else start();
121+
start();
122+
}
123+
124+
public function loadConfig(path:String) {
125+
if (path != null && FileSystem.exists(path) && !FileSystem.isDirectory(path)) {
126+
parseAndValidateConfig(Json.parse(File.getContent(path)));
127+
}
128+
else addAllChecks();
123129
}
124130

125-
function loadConfig(path:String) {
126-
var config:Config = Json.parse(File.getContent(path));
131+
public function parseAndValidateConfig(config:Config, allowExtend:Bool = true) {
132+
127133
validateAllowedFields(config, Reflect.fields(getEmptyConfig()), "Config");
128134

135+
if (allowExtend && !config.extendsConfigPath.isEmpty()) {
136+
if (FileSystem.exists(config.extendsConfigPath) && !FileSystem.isDirectory(config.extendsConfigPath)) {
137+
parseAndValidateConfig(Json.parse(File.getContent(config.extendsConfigPath)), false);
138+
}
139+
else failWith('extendsConfig: Failed to load parent configuration file [${config.extendsConfigPath}]');
140+
}
141+
129142
if (config.exclude != null) parseExcludes(config.exclude);
130143

131-
for (checkConf in config.checks) {
132-
var check = createCheck(checkConf);
133-
if (check != null) setCheckProperties(check, checkConf, config.defaultSeverity);
144+
if (config.checks != null) {
145+
for (checkConf in config.checks) {
146+
var check = createCheck(checkConf);
147+
if (check != null) setCheckProperties(check, checkConf, config.defaultSeverity);
148+
}
134149
}
135150

136151
if (config.baseDefines != null) {
@@ -144,10 +159,9 @@ class Main {
144159
validateCheckerThreads(config.numberOfCheckerThreads);
145160
}
146161

147-
function loadExcludeConfig(path:String) {
162+
public function loadExcludeConfig(path:String) {
148163
var config = Json.parse(File.getContent(path));
149164
parseExcludes(config);
150-
start();
151165
}
152166

153167
function parseExcludes(config:ExcludeConfig) {
@@ -321,6 +335,7 @@ class Main {
321335
function getEmptyConfig():Config {
322336
return {
323337
defaultSeverity: SeverityLevel.INFO,
338+
extendsConfigPath: "",
324339
numberOfCheckerThreads: 5,
325340
baseDefines: [],
326341
defineCombinations: [],

src/checkstyle/import.hx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ import checkstyle.token.TokenTree;
1212
using checkstyle.utils.ArrayUtils;
1313
using checkstyle.utils.FieldUtils;
1414
using checkstyle.utils.ExprUtils;
15+
using checkstyle.utils.StringUtils;
1516
using StringTools;

src/checkstyle/utils/StringUtils.hx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ class StringUtils {
1313
var regex:EReg = ~/(^|[^$])\$(\{|[a-zA-Z0-9_]+)/;
1414
return regex.match(s);
1515
}
16+
17+
public static function isEmpty(s:String):Bool {
18+
return (s == null) || (s.length <= 0);
19+
}
1620
}

test/TestSuite.hx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import checks.CheckTestCase;
22
import misc.ThreadTest;
3+
import misc.ConfigTest;
34
import token.TokenTreeBuilderTest;
45
import token.TokenTreeBuilderParsingTest;
56
import token.verify.VerifyTokenTreeTest;
67

78
class TestSuite extends massive.munit.TestSuite {
9+
810
public function new() {
911
super();
1012

1113
CompileTime.importPackage("checks");
1214
CompileTime.importPackage("misc");
1315

16+
add(ConfigTest);
17+
add(ThreadTest);
1418
add(TokenTreeBuilderTest);
1519
add(TokenTreeBuilderParsingTest);
1620
add(VerifyTokenTreeTest);
17-
add(ThreadTest);
1821

1922
var tests = CompileTime.getAllClasses(CheckTestCase);
2023
for (testClass in tests) add(testClass);

test/misc/ConfigTest.hx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package misc;
2+
3+
import massive.munit.Assert;
4+
5+
import checkstyle.Main;
6+
7+
@:access(checkstyle)
8+
class ConfigTest {
9+
10+
@Test
11+
public function testExtendsConfigPath() {
12+
var main:Main = new Main();
13+
14+
Assert.isNotNull(main.checker.checks);
15+
Assert.isTrue(main.checker.checks.length == 0);
16+
17+
main.parseAndValidateConfig({
18+
extendsConfigPath: "checkstyle.json"
19+
});
20+
21+
Assert.isNotNull(main.checker.checks);
22+
Assert.isTrue(main.checker.checks.length > 0);
23+
}
24+
}

0 commit comments

Comments
 (0)