Skip to content
Merged
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
24 changes: 24 additions & 0 deletions src/core/callback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package core

import (
"github.com/aviorstudio/gdlint/src/models"
"os"
"path/filepath"
"testing"
)

func TestUnusedAnalysisKeepsEngineCallbacks(t *testing.T) {
root := t.TempDir()
script := "@tool\nextends Control\n\nfunc _get_configuration_warnings() -> PackedStringArray:\n\treturn PackedStringArray()\n\nfunc _get_property_list() -> Array:\n\treturn []\n"
if err := os.WriteFile(filepath.Join(root, "example.gd"), []byte(script), 0600); err != nil {
t.Fatal(err)
}
config := &models.LintConfig{Errors: models.ErrorSection{UnusedFunctions: true}}
results, err := NewAnalyzer(root, config).Analyze()
if err != nil {
t.Fatal(err)
}
if len(results.UnusedFunctions) != 0 {
t.Fatalf("engine callbacks reported unused: %+v", results.UnusedFunctions)
}
}
19 changes: 5 additions & 14 deletions src/models/entity.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import "fmt"
import (
"fmt"
"github.com/aviorstudio/gdlint/src/patterns"
)

type EntityType string

Expand Down Expand Up @@ -49,19 +52,7 @@ func (e *Entity) IsProtected() bool {
return false
}

protectedPrefixes := []string{
"_ready", "_init", "_enter_tree", "_exit_tree",
"_process", "_physics_process", "_input", "_unhandled_input",
"_draw", "_gui_input", "_notification",
}

for _, prefix := range protectedPrefixes {
if e.Name == prefix {
return true
}
}

if len(e.Name) >= 4 && e.Name[:4] == "_on_" {
if patterns.IsProtectedFunction(e.Name) {
return true
}

Expand Down
16 changes: 16 additions & 0 deletions src/models/entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package models

import "testing"

func TestGodotVirtualCallbacksAreProtectedWithoutCalls(t *testing.T) {
for _, name := range []string{"_get_configuration_warnings", "_get_property_list", "_property_can_revert", "_property_get_revert", "_get", "_set", "_to_string", "_ready", "_on_pressed"} {
if !(&Entity{Type: EntityFunction, Name: name}).IsProtected() {
t.Errorf("engine callback %s must survive unused-code cleanup", name)
}
}
for _, entity := range []*Entity{{Type: EntityFunction, Name: "_get_configuration_warnings_extra"}, {Type: EntityFunction, Name: "_helper"}, {Type: EntitySignal, Name: "_get_configuration_warnings"}} {
if entity.IsProtected() {
t.Errorf("ordinary entity must still require a use: %+v", entity)
}
}
}
Loading