Skip to content

Commit

Permalink
Add Dart library detection
Browse files Browse the repository at this point in the history
  • Loading branch information
codersrankOrg committed Oct 18, 2022
1 parent e6c5dd2 commit e85ae3c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
29 changes: 29 additions & 0 deletions librarydetection/languages/Dart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package languages

import (
"regexp"

"github.com/codersrank-org/repo_info_extractor/v2/librarydetection"
)

// NewDartScriptAnalyzer constructor
func NewDartScriptAnalyzer() librarydetection.Analyzer {
return &dartScriptAnalyzer{}
}

type dartScriptAnalyzer struct{}

func (a *dartScriptAnalyzer) ExtractLibraries(contents string) ([]string, error) {
// for imports like this: import 'dart:developer' as dev; OR import 'dart:developer';
importRegex, err := regexp.Compile(`import '([:a-zA-Z0-9_-]+)'(?:|as)`)
if err != nil {
return nil, err
}
// for imports like this: import 'package:flutter/material.dart';
importRegex2, err := regexp.Compile(`import '([:a-zA-Z0-9_-]+)/[\.a-zA-Z0-9_-]+'`)
if err != nil {
return nil, err
}

return executeRegexes(contents, []*regexp.Regexp{importRegex, importRegex2}), nil
}
34 changes: 34 additions & 0 deletions librarydetection/languages/Dart_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package languages_test

import (
"io/ioutil"

. "github.com/onsi/ginkgo"

"github.com/codersrank-org/repo_info_extractor/v2/librarydetection/languages"
)

var _ = Describe("DartLibraryDetection", func() {
fixture, err := ioutil.ReadFile("./fixtures/dart.fixture")
if err != nil {
panic(err)
}

expectedLibraries := []string{
"dart:async",
"dart:developer",
"package:flutter",
}

analyzer := languages.NewDartScriptAnalyzer()

Describe("Extract Dart Libraries", func() {
It("Should be able to extract libraries", func() {
libs, err := analyzer.ExtractLibraries(string(fixture))
if err != nil {
panic(err)
}
assertSameUnordered(libs, expectedLibraries)
})
})
})
3 changes: 3 additions & 0 deletions librarydetection/languages/fixtures/dart.fixture
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'dart:async';
import 'dart:developer' as dev;
import 'package:flutter/material.dart';

0 comments on commit e85ae3c

Please sign in to comment.