upgrade gopkg.in/editorconfig/editorconfig-core-go.v1 (#8501)

editorconfig-core-go made breaking api changes and has recently released
v2.1.1. This change consumes the new api and fixes up any breaking
references.
This commit is contained in:
Colin Arnott 2019-10-15 21:24:16 +00:00 committed by zeripath
parent 80655026d2
commit 66e99d722a
23 changed files with 394 additions and 121 deletions

View file

@ -3,11 +3,12 @@
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.go]
[{Makefile,go.mod,go.sum,*.go}]
indent_style = tab
indent_size = 8

View file

@ -0,0 +1 @@
* text=auto

View file

@ -0,0 +1,30 @@
---
language: go
dist: xenial
sudo: true
go:
- '1.11.x'
- '1.12.x'
compiler:
- gcc
install:
# first we create a directory for the CMake binaries
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
- mkdir ${DEPS_DIR} && cd ${DEPS_DIR}
# we use wget to fetch the cmake binaries
- travis_retry wget --no-check-certificate https://cmake.org/files/v3.14/cmake-3.14.6-Linux-x86_64.tar.gz
- echo "82e08e50ba921035efa82b859c74c5fbe27d3e49a4003020e3c77618a4e912cd cmake-3.14.6-Linux-x86_64.tar.gz" > sha256sum.txt
- sha256sum -c sha256sum.txt
- tar -xvf cmake-3.14.6-Linux-x86_64.tar.gz > /dev/null
- mv cmake-3.14.6-Linux-x86_64 cmake-install
- PATH=${DEPS_DIR}/cmake-install:${DEPS_DIR}/cmake-install/bin:$PATH
- cd ${TRAVIS_BUILD_DIR}
env:
- GO111MODULE=on
script:
- make test

View file

@ -0,0 +1,20 @@
# Change log
## v2.1.1 - 2019-08-18
- Fix a small path bug
([#17](https://github.com/editorconfig/editorconfig-core-go/issues/17),
[#18](https://github.com/editorconfig/editorconfig-core-go/pull/18)).
## v2.1.0 - 2019-08-10
- This package is now *way* more compliant with the Editorconfig definition
thanks to a refactor work made by [@greut](https://github.com/greut)
([#15](https://github.com/editorconfig/editorconfig-core-go/pull/15)).
## v2.0.0 - 2019-07-14
- This project now uses [Go Modules](https://blog.golang.org/using-go-modules)
([#14](https://github.com/editorconfig/editorconfig-core-go/pull/14)).
- The import path has been changed from `gopkg.in/editorconfig/editorconfig-core-go.v1`
to `github.com/editorconfig/editorconfig-core-go/v2`.

View file

@ -0,0 +1,5 @@
project(editorconfig-core-go)
cmake_minimum_required(VERSION 3.14)
enable_testing()
set(EDITORCONFIG_CMD ${CMAKE_CURRENT_LIST_DIR}/editorconfig)
add_subdirectory(core-test)

View file

@ -0,0 +1,19 @@
PROJECT_ROOT_DIR := $(CURDIR)
SRC := $(shell git ls-files *.go */*.go)
.PHONY: bin test test-go test-core submodule
test: test-go test-core
submodule:
git submodule update --init
editorconfig: $(SRC)
go build ./cmd/editorconfig
test-go:
go test -v ./...
test-core: editorconfig
cd core-test; cmake ..
cd core-test; ctest -E "(comments_after_section|octothorpe|unset_|_pre_0.9.0|max_|root_file_mixed_case)" --output-on-failure .

View file

@ -1,27 +1,30 @@
[![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-go.svg?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-go)
[![GoDoc](https://godoc.org/gopkg.in/editorconfig/editorconfig-core-go.v1?status.svg)](https://godoc.org/gopkg.in/editorconfig/editorconfig-core-go.v1)
[![Go Report Card](https://goreportcard.com/badge/gopkg.in/editorconfig/editorconfig-core-go.v1)](https://goreportcard.com/report/gopkg.in/editorconfig/editorconfig-core-go.v1)
<!-- Currently tests against core-test are not done so hide build status badge for now -->
<!-- [![Build Status](https://travis-ci.org/editorconfig/editorconfig-core-go.svg?branch=master)](https://travis-ci.org/editorconfig/editorconfig-core-go) -->
[![GoDoc](https://godoc.org/github.com/editorconfig/editorconfig-core-go?status.svg)](https://godoc.org/github.com/editorconfig/editorconfig-core-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/editorconfig/editorconfig-core-go)](https://goreportcard.com/report/github.com/editorconfig/editorconfig-core-go)
# Editorconfig Core Go
A [Editorconfig][editorconfig] file parser and manipulator for Go.
> This package is already working, but still under testing.
> Currently this package does some basic work but does not fully support
> EditorConfig specs, so using it in "real world" is not recommended.
## Missing features
- `unset`
- escaping comments in values, probably in [go-ini/ini](https://github.com/go-ini/ini)
## Installing
We recommend the use of [gopkg.in][gopkg] for this package:
```bash
go get -u gopkg.in/editorconfig/editorconfig-core-go.v1
```
We recommend the use of Go 1.11+ modules for this package.
Import by the same path. The package name you will use to access it is
`editorconfig`.
```go
import (
"gopkg.in/editorconfig/editorconfig-core-go.v1"
"github.com/editorconfig/editorconfig-core-go/v2"
)
```
@ -70,6 +73,7 @@ type Definition struct {
EndOfLine string
TrimTrailingWhitespace bool
InsertFinalNewline bool
Raw map[string]string
}
```
@ -115,8 +119,7 @@ if err != nil {
To run the tests:
```bash
go test -v
go test -v ./...
```
[editorconfig]: http://editorconfig.org/
[gopkg]: https://gopkg.in

View file

@ -15,6 +15,7 @@ import (
)
const (
// ConfigNameDefault represents the name of the configuration file
ConfigNameDefault = ".editorconfig"
)
@ -37,6 +38,7 @@ const (
CharsetUTF8 = "utf-8"
CharsetUTF16BE = "utf-16be"
CharsetUTF16LE = "utf-16le"
CharsetUTF8BOM = "utf-8 bom"
)
// Definition represents a definition inside the .editorconfig file.
@ -81,28 +83,21 @@ func ParseBytes(data []byte) (*Editorconfig, error) {
var (
iniSection = iniFile.Section(sectionStr)
definition = &Definition{}
raw = make(map[string]string)
raw = make(map[string]string)
)
err := iniSection.MapTo(&definition)
if err != nil {
return nil, err
}
// tab_width defaults to indent_size:
// https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#tab_width
if definition.TabWidth <= 0 {
if num, err := strconv.Atoi(definition.IndentSize); err == nil {
definition.TabWidth = num
}
}
// Shallow copy all properties
for k, v := range iniSection.KeysHash() {
raw[k] = v
raw[strings.ToLower(k)] = v
}
definition.Selector = sectionStr
definition.Raw = raw
definition.normalize()
editorConfig.Definitions = append(editorConfig.Definitions, definition)
}
return editorConfig, nil
@ -122,44 +117,18 @@ var (
regexpBraces = regexp.MustCompile("{.*}")
)
func filenameMatches(pattern, name string) bool {
// basic match
matched, _ := filepath.Match(pattern, name)
if matched {
return true
}
// foo/bar/main.go should match main.go
matched, _ = filepath.Match(pattern, filepath.Base(name))
if matched {
return true
}
// foo should match foo/main.go
matched, _ = filepath.Match(filepath.Join(pattern, "*"), name)
if matched {
return true
}
// *.{js,go} should match main.go
if str := regexpBraces.FindString(pattern); len(str) > 0 {
// remote initial "{" and final "}"
str = strings.TrimPrefix(str, "{")
str = strings.TrimSuffix(str, "}")
// normalize fixes some values to their lowercaes value
func (d *Definition) normalize() {
d.Charset = strings.ToLower(d.Charset)
d.EndOfLine = strings.ToLower(d.EndOfLine)
d.IndentStyle = strings.ToLower(d.IndentStyle)
// testing for empty brackets: "{}"
if len(str) == 0 {
patt := regexpBraces.ReplaceAllString(pattern, "*")
matched, _ = filepath.Match(patt, filepath.Base(name))
return matched
}
for _, patt := range strings.Split(str, ",") {
patt = regexpBraces.ReplaceAllString(pattern, patt)
matched, _ = filepath.Match(patt, filepath.Base(name))
if matched {
return true
}
}
// tab_width defaults to indent_size:
// https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#tab_width
num, err := strconv.Atoi(d.IndentSize)
if err == nil && d.TabWidth <= 0 {
d.TabWidth = num
}
return false
}
func (d *Definition) merge(md *Definition) {
@ -192,26 +161,71 @@ func (d *Definition) merge(md *Definition) {
}
}
// InsertToIniFile ... TODO
func (d *Definition) InsertToIniFile(iniFile *ini.File) {
iniSec := iniFile.Section(d.Selector)
for k, v := range d.Raw {
iniSec.Key(k).SetValue(v)
if k == "insert_final_newline" {
iniSec.Key(k).SetValue(strconv.FormatBool(d.InsertFinalNewline))
} else if k == "trim_trailing_whitespace" {
iniSec.Key(k).SetValue(strconv.FormatBool(d.TrimTrailingWhitespace))
} else if k == "charset" {
iniSec.Key(k).SetValue(d.Charset)
} else if k == "end_of_line" {
iniSec.Key(k).SetValue(d.EndOfLine)
} else if k == "indent_style" {
iniSec.Key(k).SetValue(d.IndentStyle)
} else if k == "tab_width" {
iniSec.Key(k).SetValue(strconv.Itoa(d.TabWidth))
} else if k == "indent_size" {
iniSec.Key(k).SetValue(d.IndentSize)
} else {
iniSec.Key(k).SetValue(v)
}
}
if _, ok := d.Raw["indent_size"]; !ok {
if d.TabWidth > 0 {
iniSec.Key("indent_size").SetValue(strconv.Itoa(d.TabWidth))
} else if d.IndentStyle == IndentStyleTab {
iniSec.Key("indent_size").SetValue(IndentStyleTab)
}
}
if _, ok := d.Raw["tab_width"]; !ok && len(d.IndentSize) > 0 {
if _, err := strconv.Atoi(d.IndentSize); err == nil {
iniSec.Key("tab_width").SetValue(d.IndentSize)
}
}
}
// GetDefinitionForFilename returns a definition for the given filename.
// The result is a merge of the selectors that matched the file.
// The last section has preference over the priors.
func (e *Editorconfig) GetDefinitionForFilename(name string) *Definition {
func (e *Editorconfig) GetDefinitionForFilename(name string) (*Definition, error) {
def := &Definition{}
def.Raw = make(map[string]string)
for i := len(e.Definitions) - 1; i >= 0; i-- {
actualDef := e.Definitions[i]
if filenameMatches(actualDef.Selector, name) {
selector := actualDef.Selector
if !strings.HasPrefix(selector, "/") {
if strings.ContainsRune(selector, '/') {
selector = "/" + selector
} else {
selector = "/**/" + selector
}
}
if !strings.HasPrefix(name, "/") {
name = "/" + name
}
ok, err := FnmatchCase(selector, name)
if err != nil {
return nil, err
}
if ok {
def.merge(actualDef)
}
}
return def
return def, nil
}
func boolToString(b bool) string {
@ -279,10 +293,23 @@ func GetDefinitionForFilenameWithConfigname(filename string, configname string)
if err != nil {
return nil, err
}
definition.merge(ec.GetDefinitionForFilename(filename))
relativeFilename := filename
if len(dir) < len(abs) {
relativeFilename = abs[len(dir):]
}
def, err := ec.GetDefinitionForFilename(relativeFilename)
if err != nil {
return nil, err
}
definition.merge(def)
if ec.Root {
break
}
}
return definition, nil
}

View file

@ -0,0 +1,177 @@
package editorconfig
import (
"fmt"
"regexp"
"strconv"
"strings"
)
var (
// findLeftBrackets matches the opening left bracket {
findLeftBrackets = regexp.MustCompile(`(^|[^\\])\{`)
// findLeftBrackets matches the closing right bracket {
findRightBrackets = regexp.MustCompile(`(^|[^\\])\}`)
// findNumericRange matches a range of number, e.g. -2..5
findNumericRange = regexp.MustCompile(`^([+-]?\d+)\.\.([+-]?\d+)$`)
)
// FnmatchCase tests whether the name matches the given pattern case included.
func FnmatchCase(pattern, name string) (bool, error) {
p, err := translate(pattern)
if err != nil {
return false, err
}
r, err := regexp.Compile(fmt.Sprintf("^%s$", p))
if err != nil {
return false, err
}
return r.MatchString(name), nil
}
func translate(pattern string) (string, error) {
index := 0
pat := []rune(pattern)
length := len(pat)
result := strings.Builder{}
braceLevel := 0
isEscaped := false
inBrackets := false
matchesBraces := len(findLeftBrackets.FindAllString(pattern, -1)) == len(findRightBrackets.FindAllString(pattern, -1))
for index < length {
r := pat[index]
index++
if r == '*' {
p := index
if p < length && pat[p] == '*' {
result.WriteString(".*")
index++
} else {
result.WriteString("[^/]*")
}
} else if r == '/' {
p := index
if p+2 < length && pat[p] == '*' && pat[p+1] == '*' && pat[p+2] == '/' {
result.WriteString("(?:/|/.*/)")
index += 3
} else {
result.WriteRune(r)
}
} else if r == '?' {
result.WriteString("[^/]")
} else if r == '[' {
if inBrackets {
result.WriteString("\\[")
} else {
hasSlash := false
res := strings.Builder{}
p := index
for p < length {
if pat[p] == ']' && pat[p-1] != '\\' {
break
}
res.WriteRune(pat[p])
if pat[p] == '/' && pat[p-1] != '\\' {
hasSlash = true
break
}
p++
}
if hasSlash {
result.WriteString("\\[" + res.String())
index = p + 1
} else {
inBrackets = true
if index < length && pat[index] == '!' || pat[index] == '^' {
index++
result.WriteString("[^")
} else {
result.WriteRune('[')
}
}
}
} else if r == ']' {
if inBrackets && pat[index-2] == '\\' {
result.WriteString("\\]")
} else {
result.WriteRune(r)
inBrackets = false
}
} else if r == '{' {
hasComma := false
p := index
res := strings.Builder{}
for p < length {
if pat[p] == '}' && pat[p-1] != '\\' {
break
}
res.WriteRune(pat[p])
if pat[p] == ',' && pat[p-1] != '\\' {
hasComma = true
break
}
p++
}
if !hasComma && p < length {
inner := res.String()
sub := findNumericRange.FindStringSubmatch(inner)
if len(sub) == 3 {
from, _ := strconv.Atoi(sub[1])
to, _ := strconv.Atoi(sub[2])
result.WriteString("(?:")
// XXX does not scale well
for i := from; i < to; i++ {
result.WriteString(strconv.Itoa(i))
result.WriteRune('|')
}
result.WriteString(strconv.Itoa(to))
result.WriteRune(')')
} else {
r, _ := translate(inner)
result.WriteString(fmt.Sprintf("\\{%s\\}", r))
}
index = p + 1
} else if matchesBraces {
result.WriteString("(?:")
braceLevel++
} else {
result.WriteString("\\{")
}
} else if r == '}' {
if braceLevel > 0 {
if isEscaped {
result.WriteRune('}')
isEscaped = false
} else {
result.WriteRune(')')
braceLevel--
}
} else {
result.WriteString("\\}")
}
} else if r == ',' {
if braceLevel == 0 || isEscaped {
result.WriteRune(r)
} else {
result.WriteRune('|')
}
} else if r != '\\' || isEscaped {
result.WriteString(regexp.QuoteMeta(string(r)))
isEscaped = false
} else {
isEscaped = true
}
}
return result.String(), nil
}

View file

@ -0,0 +1,9 @@
module github.com/editorconfig/editorconfig-core-go/v2
go 1.12
require (
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a // indirect
github.com/stretchr/testify v1.3.0
gopkg.in/ini.v1 v1.42.0
)

View file

@ -0,0 +1,22 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a h1:pa8hGb/2YqsZKovtsgrwcDH1RZhVbTKCjLp47XpqCDs=
github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/ini.v1 v1.42.0 h1:7N3gPTt50s8GuLortA00n8AqRTk75qOP98+mTPpgzRk=
gopkg.in/ini.v1 v1.42.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

View file

@ -1,5 +0,0 @@
* text eol=lf
*.jpg binary
*.jpeg binary
*.png binary
*.ico binary

View file

@ -1,14 +0,0 @@
---
language: go
sudo: false
go:
- '1.8'
- '1.9'
- '1.10'
go_import_path: gopkg.in/editorconfig/editorconfig-core-go.v1
install:
- make installdeps
script:
- make test

View file

@ -1,25 +0,0 @@
PROJECT_ROOT_DIR := $(CURDIR)
SRC := editorconfig.go cmd/editorconfig/main.go
.PHONY: bin test test-go test-core submodule installdeps
test: test-go test-core
submodule:
git submodule update --init
installdeps:
go get -t ./...
editorconfig: $(SRC)
go build ./cmd/editorconfig
test-go:
go test -v
test-core: editorconfig
cd $(PROJECT_ROOT_DIR)/core-test && \
cmake -DEDITORCONFIG_CMD="$(PROJECT_ROOT_DIR)/editorconfig" .
# Temporarily disable core-test
# cd $(PROJECT_ROOT_DIR)/core-test && \
# ctest --output-on-failure .

4
vendor/modules.txt vendored
View file

@ -119,6 +119,8 @@ github.com/denisenkom/go-mssqldb/internal/decimal
github.com/denisenkom/go-mssqldb/internal/querytext
# github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dgrijalva/jwt-go
# github.com/editorconfig/editorconfig-core-go/v2 v2.1.1
github.com/editorconfig/editorconfig-core-go/v2
# github.com/edsrzf/mmap-go v1.0.0
github.com/edsrzf/mmap-go
# github.com/emirpasic/gods v1.12.0
@ -537,8 +539,6 @@ google.golang.org/appengine/urlfetch
gopkg.in/alexcesaro/quotedprintable.v3
# gopkg.in/asn1-ber.v1 v1.0.0-20150924051756-4e86f4367175
gopkg.in/asn1-ber.v1
# gopkg.in/editorconfig/editorconfig-core-go.v1 v1.3.0
gopkg.in/editorconfig/editorconfig-core-go.v1
# gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/gomail.v2
# gopkg.in/ini.v1 v1.48.0