Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion analysis/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

sitter "github.com/smacker/go-tree-sitter"

Expand Down Expand Up @@ -83,6 +84,66 @@ const (
LangSwift
)

func DecodeLanguage(language string) Language {
language = strings.ToLower(language)
switch language {
case "javascript", "js":
return LangJs
case "typescript", "ts":
return LangTs
case "jsx", "tsx":
return LangTsx
case "python", "py":
return LangPy
case "ocaml", "ml":
return LangOCaml
case "docker", "dockerfile":
return LangDockerfile
case "java":
return LangJava
case "kotlin", "kt":
return LangKotlin
case "rust", "rs":
return LangRust
case "ruby", "rb":
return LangRuby
case "lua":
return LangLua
case "yaml", "yml":
return LangYaml
case "sql":
return LangSql
case "css", "css3":
return LangCss
case "markdown", "md":
return LangMarkdown
case "sh", "bash":
return LangBash
case "csharp", "cs":
return LangCsharp
case "elixir", "ex":
return LangElixir
case "elm":
return LangElm
case "go":
return LangGo
case "groovy":
return LangGroovy
case "hcl", "tf":
return LangHcl
case "html":
return LangHtml
case "php":
return LangPhp
case "scala":
return LangScala
case "swift":
return LangSwift
default:
return LangUnknown
}
}

// tsGrammarForLang returns the tree-sitter grammar for the given language.
// May return `nil` when `lang` is `LangUnkown`.
func (lang Language) Grammar() *sitter.Language {
Expand Down Expand Up @@ -169,7 +230,7 @@ func LanguageFromFilePath(path string) Language {
return LangYaml
case ".css":
return LangCss
case ".dockerfile":
case ".dockerfile", ".Dockerfile":
return LangDockerfile
case ".md":
return LangMarkdown
Expand Down
2 changes: 1 addition & 1 deletion analysis/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ func buildScopeTree(
if builder.NodeCreatesScope(node) {
nextScope = NewScope(scope)
scopeOfNode[node] = nextScope
scope.AstNode = node
if scope != nil {
scope.Children = append(scope.Children, nextScope)
scope.AstNode = node
Copy link

Copilot AI Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assigns the AST node to the parent scope; it should set the child scope's AstNode (nextScope.AstNode = node) to correctly record which node created that scope.

Suggested change
scope.AstNode = node
nextScope.AstNode = node

Copilot uses AI. Check for mistakes.
} else {
scope = nextScope // root
}
Expand Down
11 changes: 11 additions & 0 deletions analysis/testdata/mock-checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: javascript
name: mock-checker
message: "This is just a mock checker"
category: style
severity: info
pattern:
(call_expression) @mock-checker
description: |
This is a mock checker.


9 changes: 9 additions & 0 deletions analysis/testdata/node-filter-checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: javascript
name: node-filter-checker
message: "Variable @var found inside function"
category: style
severity: info
pattern: (variable_declarator) @var @node-filter-checker
filters:
- pattern-inside: (function_declaration)
description: "Check for variables declared inside functions"
10 changes: 10 additions & 0 deletions analysis/testdata/node-filter-test-checker.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
console.log("Hello, world!");

function foo(){
// <expect-error>
console.log("This should be detected");

/*
console.log("This Should not be detected");
*/
}
15 changes: 15 additions & 0 deletions analysis/testdata/node-filter-test-checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: javascript
name: node-filter-test-checker
message: "Variable @var found inside function"
category: style
severity: info
pattern: >
(call_expression
function: (member_expression
object: (identifier) @obj
property: (property_identifier) @method
(#eq? @obj "console"))) @node-filter-test-checker
filters:
- pattern-inside: (function_declaration)
- pattern-not-inside: (comment)
description: "Check for variables declared inside functions"
7 changes: 7 additions & 0 deletions analysis/testrunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ func RunAnalyzerTests(testDir string, analyzers []*Analyzer) (string, string, bo

// if there's a test file in the testDir for which there's no analyzer,
// it's most likely a YAML checker test, so skip it

// yamlAnalyzers, err := discoverYamlAnalyzers(testDir)
// if err != nil {
// return "", "", false, err
// }
// analyzers = append(analyzers, yamlAnalyzers...)

likelyTestFiles := []string{}
for _, analyzer := range analyzers {
likelyTestFiles = append(likelyTestFiles, fmt.Sprintf("%s.test%s", analyzer.Name, GetExtFromLanguage(analyzer.Language)))
Expand Down
Loading
Loading