Skip to content

Commit

Permalink
Merge pull request #17 from grafana/15-types
Browse files Browse the repository at this point in the history
Check index.d.ts TypeScript API declaration file
  • Loading branch information
szkiba authored Nov 14, 2024
2 parents fa5fbfe + cf26d64 commit ecf1217
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 52 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The detailed result of the checks are described in a [JSON schema](https://grafa
- `build` - checks if k6 can be built with the extension
- `smoke` - checks if the smoke test script exists and runs successfully (`smoke.js`, `smoke.ts`, `smoke.test.js` or `smoke.test.ts` in the `test`,`tests`, `examples` or the base directory)
- `codeowners` - checks if there is a CODEOWNERS file (for official extensions)
- `types` - checks if the TypeScript API declaration file exists (`index.d.ts` in the `docs` or the base directory)

## Install

Expand Down
50 changes: 0 additions & 50 deletions checker_examples.go

This file was deleted.

104 changes: 104 additions & 0 deletions checker_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package k6lint

import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
Expand All @@ -14,6 +16,7 @@ import (
type moduleChecker struct {
file *modfile.File
exe string
js bool
}

func newModuleChecker() *moduleChecker {
Expand Down Expand Up @@ -58,6 +61,28 @@ func (mc *moduleChecker) canBuild(ctx context.Context, dir string) *checkResult
return checkError(err)
}

out, err := exec.CommandContext(ctx, exe, "version").CombinedOutput() //nolint:gosec
if err != nil {
return checkError(err)
}

rex, err := regexp.Compile("(?i) " + mc.file.Module.Mod.String() + "[^,]+, [^ ]+ \\[(?P<type>[a-z]+)\\]")
if err != nil {
return checkError(err)
}

subs := rex.FindAllSubmatch(out, -1)
if subs == nil {
return checkFailed(mc.file.Module.Mod.String() + " is not in the version command's output")
}

for _, one := range subs {
if string(one[rex.SubexpIndex("type")]) == "js" {
mc.js = true
break
}
}

mc.exe = exe

return checkPassed("can be built with the latest k6 version")
Expand All @@ -71,6 +96,10 @@ func (mc *moduleChecker) smoke(ctx context.Context, dir string) *checkResult {
return checkFailed("can't build")
}

if !mc.js {
return checkPassed("skipped due to output extension")
}

filename, shortname, err := findFile(reSmoke,
dir,
filepath.Join(dir, "test"),
Expand All @@ -96,3 +125,78 @@ func (mc *moduleChecker) smoke(ctx context.Context, dir string) *checkResult {

return checkPassed("`%s` successfully run with k6", shortname)
}

var reIndexDTS = regexp.MustCompile("^index.d.ts$")

func (mc *moduleChecker) types(_ context.Context, dir string) *checkResult {
if mc.exe == "" {
return checkFailed("can't build")
}

if !mc.js {
return checkPassed("skipped due to output extension")
}

_, shortname, err := findFile(reIndexDTS,
dir,
filepath.Join(dir, "docs"),
)
if err != nil {
return checkError(err)
}

if len(shortname) > 0 {
return checkPassed("found `index.d.ts` file")
}

return checkFailed("no `index.d.ts` file found")
}

//nolint:forbidigo
func (mc *moduleChecker) examples(_ context.Context, dir string) *checkResult {
if mc.exe == "" {
return checkFailed("can't build")
}

if !mc.js {
return checkPassed("skipped due to output extension")
}

dir = filepath.Join(dir, "examples")

info, err := os.Stat(dir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return checkFailed("missing `examples` directory")
}

return checkError(err)
}

if !info.IsDir() {
return checkFailed("`examples` is not a directory")
}

hasRegular := false

err = filepath.WalkDir(dir, func(_ string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}

if entry.Type().IsRegular() {
hasRegular = true
}

return nil
})
if err != nil {
return checkError(err)
}

if hasRegular {
return checkPassed("found `examples` as examples directory")
}

return checkFailed("no examples found in the `examples` directory")
}
3 changes: 2 additions & 1 deletion checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ func checkDefinitions(official bool) []checkDefinition {
{id: CheckerModule, score: 2, fn: modCheck.hasGoModule},
{id: CheckerReplace, score: 2, fn: modCheck.hasNoReplace},
{id: CheckerReadme, score: 5, fn: checkerReadme},
{id: CheckerExamples, score: 2, fn: checkerExamples},
{id: CheckerLicense, score: 5, fn: checkerLicense},
{id: CheckerGit, score: 1, fn: gitCheck.isWorkDir},
{id: CheckerVersions, score: 5, fn: gitCheck.hasVersions},
{id: CheckerBuild, score: 5, fn: modCheck.canBuild},
{id: CheckerSmoke, score: 2, fn: modCheck.smoke},
{id: CheckerExamples, score: 2, fn: modCheck.examples},
{id: CheckerTypes, score: 2, fn: modCheck.types},
}

if !official {
Expand Down
1 change: 1 addition & 0 deletions compliance_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion docs/compliance.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@
"versions",
"build",
"smoke",
"codeowners"
"codeowners",
"types"
]
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/compliance.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ $defs:
- build
- smoke
- codeowners
- types

0 comments on commit ecf1217

Please sign in to comment.