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

Issue:
Allow disabling blocks of files
It is extremely common for Markdown files to have link references at the bottom, some of which go well-beyond line maximums.  Markdown syntax does not allow a "comment" to put `<!-- editorconfig-checker-disable-line -->` on these lines.

It would be helpful to be able to disable the checker for blocks of text, as an example (and recommendation):

```md
# My File

...text that is checked by `editorconfig-checker`...

<!-- editorconfig-checker-disable -->

[somelink]:  http://www.some.super.long.url.well.larger.than.the.maximum.line.length/that-I-have-no-control-over.html

<!-- editorconfig-checker-enable -->
```

Similar for other languages, like Python:

```python
import a from b

# editorconfig-checker-disable
some.line("out of compliance")
# editorconfig-checker-enable
```

Pull request:
Add support for disabling blocks, implements #124
As requested in #124, this augments the existing `editorconfig-checker-disable-line` functionality to allow disabling checks for a block of code by adding a comment with `editorconfig-checker-disable`. Checks can be re-enabled with a comment containing `editorconfig-checker-enable`. This is very similar to how ESLint facilitates excludes: https://eslint.org/docs/user-guide/configuring#disabling-rules-with-inline-comments

Note that this functionality basically renders `editorconfig-checker-disable-file` redundant, because you can accomplish the same thing by adding `editorconfig-checker-disable` to the top of the file. I wasn't sure if `editorconfig-checker-disable-file` should be preserved or not, so I didn't touch it. 

closes #124 

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c8e02a3..530b13e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 ## Unreleased
 ### BREAKING
 ### Added
+* Add support for disabling validation via `editorconfig-checker-disable` and re-enabling with `editorconfig-checker-enable` [#126](https://github.com/editorconfig-checker/editorconfig-checker/pull/126)
 ### Deprecated
 ### Removed
 ### Fixed
diff --git a/README.md b/README.md
index 66be14e..3aed5c4 100644
--- a/README.md
+++ b/README.md
@@ -163,6 +163,19 @@ const myTemplateString = `
      wrongly indended line because it needs to be` // editorconfig-checker-disable-line
 ```
 
+### Excluding blocks
+
+To temporarily disable all checks, add a comment containing `editorconfig-checker-disable`. Re-enable with a comment containing `editorconfig-checker-enable`
+
+```js
+// editorconfig-checker-disable
+const myTemplateString = `
+  first line 
+     wrongly indended line because it needs to be
+`
+// editorconfig-checker-enable
+```
+
 ### Excluding files
 
 #### Inline
diff --git a/go.mod b/go.mod
index 5945773..c12f04a 100644
--- a/go.mod
+++ b/go.mod
@@ -4,7 +4,7 @@ go 1.12
 
 require (
 	github.com/davecgh/go-spew v1.1.1 // indirect
-	github.com/editorconfig/editorconfig-core-go/v2 v2.3.8
+	github.com/editorconfig/editorconfig-core-go/v2 v2.3.7
 	github.com/gopherjs/gopherjs v0.0.0-20190812055157-5d271430af9f // indirect
 	github.com/smartystreets/assertions v1.0.1 // indirect
 )
diff --git a/go.sum b/go.sum
index 2d8b560..c8c2dd4 100644
--- a/go.sum
+++ b/go.sum
@@ -26,7 +26,6 @@ github.com/editorconfig/editorconfig-core-go/v2 v2.3.6 h1:sJDkoFZCazT2lhtyUXKgzz
 github.com/editorconfig/editorconfig-core-go/v2 v2.3.6/go.mod h1:keiVCwWAotcoxvCkHCtJbGWoV7/oNgCmRjB5+LvuEE0=
 github.com/editorconfig/editorconfig-core-go/v2 v2.3.7 h1:e88U5ztaklGv7X0gHIgR/cCJmHkLyVAS8aOIoEPKJP0=
 github.com/editorconfig/editorconfig-core-go/v2 v2.3.7/go.mod h1:NifC+uYhAGV/U2AxhZa3bNy4EdFMHz9mVU02vbGSMWQ=
-github.com/editorconfig/editorconfig-core-go/v2 v2.3.8/go.mod h1:z7TIMh40583cev3v8ei7V1RRPKeHQbttoa4Vm5/5u7g=
 github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -82,6 +81,5 @@ gopkg.in/ini.v1 v1.60.1 h1:P5y5shSkb0CFe44qEeMBgn8JLow09MP17jlJHanke5g=
 gopkg.in/ini.v1 v1.60.1/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/ini.v1 v1.60.2 h1:7i8mqModL63zqi8nQn8Q3+0zvSCZy1AxhBgthKfi4WU=
 gopkg.in/ini.v1 v1.60.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
-gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
diff --git a/pkg/validation/validation.go b/pkg/validation/validation.go
index 5846317..046aebb 100644
--- a/pkg/validation/validation.go
+++ b/pkg/validation/validation.go
@@ -17,11 +17,18 @@ import (
 
 // ValidateFile Validates a single file and returns the errors
 func ValidateFile(filePath string, config config.Config) []error.ValidationError {
+	const directivePrefix = "editorconfig-checker-"
+	const directiveDisable = directivePrefix + "disable"
+	const directiveDisableFile = directivePrefix + "disable-file"
+	const directiveDisableLine = directivePrefix + "disable-line"
+	const directiveEnable = directivePrefix + "enable"
+
 	var validationErrors []error.ValidationError
+	var isDisabled bool = false
 	lines := files.ReadLines(filePath)
 
 	// return if first line contains editorconfig-checker-disable-file
-	if len(lines) == 0 || strings.Contains(lines[0], "editorconfig-checker-disable-file") {
+	if len(lines) == 0 || strings.Contains(lines[0], directiveDisableFile) {
 		return validationErrors
 	}
 
@@ -50,7 +57,18 @@ func ValidateFile(filePath string, config config.Config) []error.ValidationError
 	}
 
 	for lineNumber, line := range lines {
-		if strings.Contains(line, "editorconfig-checker-disable-line") {
+		if strings.Contains(line, directiveEnable) {
+			isDisabled = false
+		} else if directiveIndex := strings.Index(line, directiveDisable); directiveIndex != -1 {
+			if strings.HasPrefix(line[directiveIndex:], directiveDisableLine) {
+				// found editorconfig-checker-disable-line, so just skip current line
+				continue
+			} else {
+				isDisabled = true
+			}
+		}
+
+		if isDisabled {
 			continue
 		}
 


Test patch:
diff --git a/pkg/validation/validation_test.go b/pkg/validation/validation_test.go
index 0f6c84b..c3ae833 100644
--- a/pkg/validation/validation_test.go
+++ b/pkg/validation/validation_test.go
@@ -66,6 +66,16 @@ func TestValidateFile(t *testing.T) {
 		t.Error("Should have no errors when validating valid file, got", result)
 	}
 
+	result = ValidateFile("./../../testfiles/disabled-block.txt", configuration)
+	if len(result) != 0 {
+		t.Error("Should have no errors when validating valid file, got", result)
+	}
+
+	result = ValidateFile("./../../testfiles/disabled-block-with-error.txt", configuration)
+	if len(result) != 1 {
+		t.Error("Should have one error, got", result)
+	}
+
 	configuration = config.Config{SpacesAftertabs: false}
 	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", configuration)
 	if len(result) != 1 {
diff --git a/testfiles/disabled-block-with-error.txt b/testfiles/disabled-block-with-error.txt
new file mode 100644
index 0000000..22d3eb9
--- /dev/null
+++ b/testfiles/disabled-block-with-error.txt
@@ -0,0 +1,6 @@
+hello
+<!-- editorconfig-checker-disable -->
+	 world
+		 world2
+<!-- editorconfig-checker-enable -->
+	 wrong indentation
diff --git a/testfiles/disabled-block.txt b/testfiles/disabled-block.txt
new file mode 100644
index 0000000..e70b40a
--- /dev/null
+++ b/testfiles/disabled-block.txt
@@ -0,0 +1,6 @@
+hello
+<!-- editorconfig-checker-disable -->
+	 world
+		 world2
+<!-- editorconfig-checker-enable -->
+test