Skip to content

Commit

Permalink
Add support for languages with extentions like test.d.ts such as type…
Browse files Browse the repository at this point in the history
…script typings
  • Loading branch information
boyter committed Jul 25, 2018
1 parent 311dc1a commit c8b2a70
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
51 changes: 51 additions & 0 deletions languages.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,57 @@
]
]
},
"TypeScript Typings": {
"line_comment": [
"//"
],
"complexitychecks": [
"for ",
"for(",
"if ",
"if(",
"switch ",
"while ",
"else ",
"| ",
"|| ",
"& ",
"&& ",
"!= ",
"!== ",
"== ",
"=== ",
"foreach(",
"case ",
"case(",
"casex ",
"casex(",
"casez ",
"casez(",
"casexz ",
"casexz(",
"fork",
"?",
":",
"inside",
"with"
],
"extensions": [
"d.ts"
],
"multi_line": [
[
"/*",
"*/"
]
],
"quotes": [
[
"\"",
"\""
]
]
},
"Lisp": {
"line_comment": [
";"
Expand Down
2 changes: 1 addition & 1 deletion processor/constants.go

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions processor/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ func getExtension(name string) string {
return extension.(string)
}

loc := strings.LastIndex(name, ".")
locs := strings.Split(name, ".")

if loc == -1 || loc == 0 {
switch {
case len(locs) == 0 || len(locs) == 1 || strings.LastIndex(name, ".") == 0:
extension = name
} else {
extension = name[loc+1:]
case len(locs) == 2:
extension = locs[len(locs)-1]
default:
extension = locs[len(locs)-2] + "." + locs[len(locs)-1]
}

extensionCache.Store(name, extension)
Expand Down Expand Up @@ -109,15 +112,20 @@ func walkDirectoryParallel(root string, output *chan *FileJob) {
if !info.IsDir() {
if gitignoreerror != nil || !gitignore.Match(filepath.Join(root, info.Name()), false) {

extension = getExtension(info.Name())

// If unknown lookup in case the full name matches
// Lookup in case the full name matches
language, ok := extensionLookup[strings.ToLower(info.Name())]

// If no match check if we have a matching extension
if !ok {
extension = getExtension(info.Name())
language, ok = extensionLookup[extension]
}

// Convert from d.ts to ts and check that in case of multiple extensions
if !ok {
language, ok = extensionLookup[getExtension(extension)]
}

if ok {
*output <- &FileJob{Location: root, Filename: info.Name(), Extension: extension, Language: language}
} else if Verbose {
Expand Down
23 changes: 21 additions & 2 deletions processor/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGetExtensionNoExtension(t *testing.T) {

func TestGetExtensionMultipleDots(t *testing.T) {
got := getExtension(".travis.yml")
expected := "yml"
expected := "travis.yml"

if got != expected {
t.Errorf("Expected %s got %s", expected, got)
Expand All @@ -34,7 +34,7 @@ func TestGetExtensionMultipleDots(t *testing.T) {

func TestGetExtensionMultipleExtensions(t *testing.T) {
got := getExtension("something.go.yml")
expected := "yml"
expected := "go.yml"

if got != expected {
t.Errorf("Expected %s got %s", expected, got)
Expand All @@ -50,6 +50,25 @@ func TestGetExtensionStartsWith(t *testing.T) {
}
}

func TestGetExtensionTypeScriptDefinition(t *testing.T) {
got := getExtension("test.d.ts")
expected := "d.ts"

if got != expected {
t.Errorf("Expected %s got %s", expected, got)
}
}

func TestGetExtensionSecondPass(t *testing.T) {
got := getExtension("test.d.ts")
got = getExtension(got)
expected := "ts"

if got != expected {
t.Errorf("Expected %s got %s", expected, got)
}
}

func BenchmarkGetExtensionDifferent(b *testing.B) {
for i := 0; i < b.N; i++ {

Expand Down

0 comments on commit c8b2a70

Please sign in to comment.