Repository: editorconfig-checker/editorconfig-checker (go)

Issue:
Option to only disable indent_size check
It would be great to be able to just disable `indent_size` check but not `indent_style` check.

Currently it is only possible to `-disable-indentation` which means disabling `indent_size` and `indent_style` check.

Unfortunately there are many use cases where `indent_size` is not correct in complex files because every language handles some special cases differently. Also if you have _blank lines_  with just the correct number of spaces the check fails at the moment with "Wrong amount of left-padding spaces(want multiple of x)"

At the moment it is not possible in our workspace to fix all `indent_size` problems, but we would like to check the `indent_style` only but not the `indent_size`.



Pull request:
feat(disable): Disable only indentSize check
closes #94

Patch:
diff --git a/.ecrc b/.ecrc
index b8d0e8c..0492ace 100644
--- a/.ecrc
+++ b/.ecrc
@@ -11,6 +11,7 @@
     "EndOfLine": false,
     "Indentation": false,
     "InsertFinalNewline": false,
-    "TrimTrailingWhitespace": false
+    "TrimTrailingWhitespace": false,
+    "IndentSize": false
   }
 }
diff --git a/README.md b/README.md
index b3455ce..b74e724 100644
--- a/README.md
+++ b/README.md
@@ -82,6 +82,8 @@ USAGE:
         print debugging information
   -disable-end-of-line
         disables the trailing whitespace check
+  -disable-indent-size
+        disables only the indent-size check
   -disable-indentation
         disables the indentation check
   -disable-insert-final-newline
@@ -132,7 +134,8 @@ A sample `.ecrc` file can look like this and will be used from your current work
     "EndOfLine": false,
     "Indentation": false,
     "InsertFinalNewline": false,
-    "TrimTrailingWhitespace": false
+    "TrimTrailingWhitespace": false,
+    "IndentSize": false
   }
 }
 ```
diff --git a/cmd/editorconfig-checker/main.go b/cmd/editorconfig-checker/main.go
index 65ca1d5..b210d3b 100644
--- a/cmd/editorconfig-checker/main.go
+++ b/cmd/editorconfig-checker/main.go
@@ -46,6 +46,7 @@ func init() {
 	flag.BoolVar(&c.Disable.EndOfLine, "disable-end-of-line", false, "disables the trailing whitespace check")
 	flag.BoolVar(&c.Disable.InsertFinalNewline, "disable-insert-final-newline", false, "disables the final newline check")
 	flag.BoolVar(&c.Disable.Indentation, "disable-indentation", false, "disables the indentation check")
+	flag.BoolVar(&c.Disable.IndentSize, "disable-indent-size", false, "disables only the indent-size check")
 
 	flag.Parse()
 
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 37e71b9..bfd43c5 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -82,6 +82,7 @@ type DisabledChecks struct {
 	Indentation            bool
 	InsertFinalNewline     bool
 	TrimTrailingWhitespace bool
+	IndentSize             bool
 }
 
 // NewConfig initializes a new config
@@ -195,6 +196,10 @@ func (c *Config) mergeDisabled(disabled DisabledChecks) {
 	if disabled.Indentation {
 		c.Disable.Indentation = disabled.Indentation
 	}
+
+	if disabled.IndentSize {
+		c.Disable.IndentSize = disabled.IndentSize
+	}
 }
 
 // GetExcludesAsRegularExpression returns the excludes as a combined regular expression
diff --git a/pkg/validation/validators/validators.go b/pkg/validation/validators/validators.go
index 3de3e04..aa9ff45 100644
--- a/pkg/validation/validators/validators.go
+++ b/pkg/validation/validators/validators.go
@@ -14,7 +14,7 @@ import (
 // Indentation validates a files indentation
 func Indentation(line string, indentStyle string, indentSize int, config config.Config) error {
 	if indentStyle == "space" {
-		return Space(line, indentSize)
+		return Space(line, indentSize, config)
 	} else if indentStyle == "tab" {
 		return Tab(line, config)
 	}
@@ -24,17 +24,26 @@ func Indentation(line string, indentStyle string, indentSize int, config config.
 }
 
 // Space validates if a line is indented correctly respecting the indentSize
-func Space(line string, indentSize int) error {
+func Space(line string, indentSize int, config config.Config) error {
 	if len(line) > 0 && indentSize > 0 {
-		// match recurring spaces indentSize times - this can be recurring or never
-		// match either a space followed by a * and maybe a space (block-comments)
-		// or match everything despite a space or tab-character
-		regexpPattern := fmt.Sprintf("^( {%d})*( \\* ?|[^ \t]|$)", indentSize)
-
-		matched, _ := regexp.MatchString(regexpPattern, line)
+		if !config.Disable.IndentSize {
+			// match recurring spaces indentSize times - this can be recurring or never
+			// match either a space followed by a * and maybe a space (block-comments)
+			// or match everything despite a space or tab-character
+			regexpPattern := fmt.Sprintf("^( {%d})*( \\* ?|[^ \t]|$)", indentSize)
+			matched, _ := regexp.MatchString(regexpPattern, line)
+
+			if !matched {
+				return fmt.Errorf("Wrong amount of left-padding spaces(want multiple of %d)", indentSize)
+			}
+		} else {
+			// match recurring spaces and everything except tab characters
+			regexpPattern := fmt.Sprintf("^( )*([^ \t]|$)")
+			matched, _ := regexp.MatchString(regexpPattern, line)
 
-		if !matched {
-			return fmt.Errorf("Wrong amount of left-padding spaces(want multiple of %d)", indentSize)
+			if !matched {
+				return fmt.Errorf("Wrong indent style found (tabs instead of spaces)")
+			}
 		}
 
 	}


Test patch:
diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go
index 3b9981f..899bf60 100644
--- a/pkg/config/config_test.go
+++ b/pkg/config/config_test.go
@@ -96,6 +96,7 @@ func TestMerge(t *testing.T) {
 			EndOfLine:              true,
 			InsertFinalNewline:     true,
 			Indentation:            true,
+			IndentSize:             true,
 		},
 	}
 
@@ -168,7 +169,7 @@ func TestGetAsString(t *testing.T) {
 	_ = c.Parse()
 
 	actual := c.GetAsString()
-	expected := "Config: {Version:false Help:false DryRun:false Path:../../.ecrc Verbose:false Debug:false IgnoreDefaults:false SpacesAftertabs:false NoColor:false Exclude:[testfiles] AllowedContentTypes:[text/ application/octet-stream] PassedFiles:[] Disable:{EndOfLine:false Indentation:false InsertFinalNewline:false TrimTrailingWhitespace:false} Logger:{Verbosee:false Debugg:false NoColor:false}}"
+	expected := "Config: {Version:false Help:false DryRun:false Path:../../.ecrc Verbose:false Debug:false IgnoreDefaults:false SpacesAftertabs:false NoColor:false Exclude:[testfiles] AllowedContentTypes:[text/ application/octet-stream] PassedFiles:[] Disable:{EndOfLine:false Indentation:false InsertFinalNewline:false TrimTrailingWhitespace:false IndentSize:false} Logger:{Verbosee:false Debugg:false NoColor:false}}"
 
 	if actual != expected {
 		t.Errorf("Expected: %v, got: %v ", expected, actual)
diff --git a/pkg/validation/validators/validators_test.go b/pkg/validation/validators/validators_test.go
index 272108e..d02d052 100644
--- a/pkg/validation/validators/validators_test.go
+++ b/pkg/validation/validators/validators_test.go
@@ -131,27 +131,42 @@ func TestIndentation(t *testing.T) {
 }
 
 func TestSpace(t *testing.T) {
+	enabledIndentSizeConfig := config.Config{}
+	disabled := config.DisabledChecks{IndentSize: true}
+	disabledIndentSizeConfig := config.Config{
+		Disable: disabled,
+	}
+
 	spaceTests := []struct {
 		line       string
 		indentSize int
+		config     config.Config
 		expected   error
 	}{
-		{"", 4, nil},
-		{"x", 0, nil},
-		{"x", 4, nil},
-		{"    x", 4, nil},
+		{"", 4, enabledIndentSizeConfig, nil},
+		{"x", 0, enabledIndentSizeConfig, nil},
+		{"x", 4, enabledIndentSizeConfig, nil},
+		{"    x", 4, enabledIndentSizeConfig, nil},
 		// 5 spaces
-		{"     x", 4, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
+		{"     x", 4, enabledIndentSizeConfig, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
 		// 3 spaces
-		{"   x", 4, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
+		{"   x", 4, enabledIndentSizeConfig, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
 		// correct indented block comment, empty and non empty
-		{"     *", 4, nil},
-		{"     * some comment", 4, nil},
-		{"    ", 4, nil},
+		{"     *", 4, enabledIndentSizeConfig, nil},
+		{"     * some comment", 4, enabledIndentSizeConfig, nil},
+		{"    ", 4, enabledIndentSizeConfig, nil},
+		// disabled indent size
+		{"", 4, disabledIndentSizeConfig, nil},
+		{" x ", 4, disabledIndentSizeConfig, nil},
+		{"  x ", 4, disabledIndentSizeConfig, nil},
+		{"    x ", 4, disabledIndentSizeConfig, nil},
+		{"     x ", 4, disabledIndentSizeConfig, nil},
+		{"	x ", 4, disabledIndentSizeConfig, errors.New("Wrong indent style found (tabs instead of spaces)")},
+		{"    		x a", 4, disabledIndentSizeConfig, errors.New("Wrong indent style found (tabs instead of spaces)")},
 	}
 
 	for _, tt := range spaceTests {
-		actual := Space(tt.line, tt.indentSize)
+		actual := Space(tt.line, tt.indentSize, tt.config)
 		if !reflect.DeepEqual(actual, tt.expected) {
 			t.Errorf("Space(%s, %d): expected: %v, got: %v", tt.line, tt.indentSize, tt.expected, actual)
 		}
diff --git a/testfiles/generated-config.json b/testfiles/generated-config.json
index 8b070e8..3849367 100644
--- a/testfiles/generated-config.json
+++ b/testfiles/generated-config.json
@@ -11,6 +11,7 @@
     "EndOfLine": false,
     "Indentation": false,
     "InsertFinalNewline": false,
-    "TrimTrailingWhitespace": false
+    "TrimTrailingWhitespace": false,
+    "IndentSize": false
   }
 }
\ No newline at end of file