Bladeren bron

quick commit of old stuff

bmallred 9 jaren geleden
bovenliggende
commit
8331c9a84d

+ 22 - 0
rules/closingSquareBracketsMustBeSpacedCorrectly.go

@ -0,0 +1,22 @@
1
package rules
2
3
import "regexp"
4
5
var closingSquareBracketsMustBeSpacedCorrectly = &Rule{
6
	Name:        "Closing square brackets must be spaced correctly",
7
	Enabled:     true,
8
	Apply:       applyClosingSquareBracketsMustBeSpacedCorrectly,
9
	Description: ``,
10
}
11
12
func applyClosingSquareBracketsMustBeSpacedCorrectly(source []byte) []byte {
13
	re := regexp.MustCompile(`([\S])([\t ]+)([\]])`)
14
	source = re.ReplaceAll(source, []byte("$1$3"))
15
16
	// re = regexp.MustCompile(`([\]])([\S])`)
17
	// source = re.ReplaceAll(source, []byte("$1 $2"))
18
	// re = regexp.MustCompile(`([\]]) ([;])`)
19
	// source = re.ReplaceAll(source, []byte("$1$2"))
20
21
	return source
22
}

+ 28 - 0
rules/closingSquareBracketsMustBeSpacedCorrectly_test.go

@ -0,0 +1,28 @@
1
package rules
2
3
import (
4
	"bytes"
5
	"fmt"
6
	"testing"
7
)
8
9
func TestClosingSquareBracketsMustBeSpacedCorrectly(t *testing.T) {
10
	input := []byte(`new string[ ]{};
11
new int[1 ] ;
12
new string[
13
	"blah",
14
	"[meh].[bleh]",
15
]`)
16
	expected := []byte(`new string[] {};
17
new int[1];
18
new string[
19
	"blah",
20
	"[meh].[bleh]",
21
]`)
22
23
	actual := applyClosingSquareBracketsMustBeSpacedCorrectly(input)
24
	if !bytes.Equal(expected, actual) {
25
		fmt.Println(string(actual))
26
		t.Fail()
27
	}
28
}

+ 19 - 0
rules/openingSquareBracketsMustBeSpacedCorrectly.go

@ -0,0 +1,19 @@
1
package rules
2
3
import "regexp"
4
5
var openingSquareBracketsMustBeSpacedCorrectly = &Rule{
6
	Name:        "Opening square brackets must be spaced correctly",
7
	Enabled:     true,
8
	Apply:       applyOpeningSquareBracketsMustBeSpacedCorrectly,
9
	Description: ``,
10
}
11
12
func applyOpeningSquareBracketsMustBeSpacedCorrectly(source []byte) []byte {
13
	re := regexp.MustCompile(`([\S])([\t ]+)([\[])`)
14
	source = re.ReplaceAll(source, []byte("$1$3"))
15
	re = regexp.MustCompile(`([\[])([\t ]+)([\S])`)
16
	source = re.ReplaceAll(source, []byte("$1$3"))
17
18
	return source
19
}

+ 30 - 0
rules/openingSquareBracketsMustBeSpacedCorrectly_test.go

@ -0,0 +1,30 @@
1
package rules
2
3
import (
4
	"bytes"
5
	"fmt"
6
	"testing"
7
)
8
9
func TestOpeningSquareBracketsMustBeSpacedCorrectly(t *testing.T) {
10
	input := []byte(`new string [] {};
11
new int [ 1];
12
new string [
13
	"blah",
14
	"meh",
15
	"bleh"
16
]`)
17
	expected := []byte(`new string[] {};
18
new int[1];
19
new string[
20
	"blah",
21
	"meh",
22
	"bleh"
23
]`)
24
25
	actual := applyOpeningSquareBracketsMustBeSpacedCorrectly(input)
26
	if !bytes.Equal(expected, actual) {
27
		fmt.Println(string(actual))
28
		t.Fail()
29
	}
30
}

+ 2 - 2
rules/rules.go

@ -185,8 +185,8 @@ var Spacing = []*Rule{
185 185
	// operatorKeywordMustBeFollowedBySpace,
186 186
	openingParenthesisMustBeSpacedCorrectly,
187 187
	closingParenthesisMustBeSpacedCorrectly,
188
	// openingSquareBracketsMustBeSpacedCorrectly,
189
	// closingSquareBracketsMustBeSpacedCorrectly,
188
	openingSquareBracketsMustBeSpacedCorrectly,
189
	closingSquareBracketsMustBeSpacedCorrectly,
190 190
	// openingCurlyBracketsMustBeSpacedCorrectly,
191 191
	// closingCurlyBracketsMustBeSpacedCorrectly,
192 192
	// openingGenericBracketsMustBeSpacedCorrectly,

+ 1 - 1
rules/symbolsMustBeSpacedCorrectly.go

@ -61,7 +61,7 @@ func applySymbolsMustBeSpacedCorrectly(source []byte) []byte {
61 61
			line = re.ReplaceAll(line, []byte("$1 $2 $3"))
62 62
63 63
			// Fix negatives
64
			re = regexp.MustCompile(`([\+=<>\?])([ ])([\-])([ ])([\d])`)
64
			re = regexp.MustCompile(`([\+=<>\?])( *)([\-])([ ]+)([\d])`)
65 65
			line = re.ReplaceAll(line, []byte("$1 $3$5"))
66 66
67 67
			// Fix generics