Skip to content

feat: add adjacent-overload-signatures rule #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions cmd/rslint/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
rslintconfig "github.com/typescript-eslint/rslint/internal/config"
"github.com/typescript-eslint/rslint/internal/linter"
"github.com/typescript-eslint/rslint/internal/rule"
"github.com/typescript-eslint/rslint/internal/rules/adjacent_overload_signatures"
"github.com/typescript-eslint/rslint/internal/rules/await_thenable"
"github.com/typescript-eslint/rslint/internal/rules/no_array_delete"
"github.com/typescript-eslint/rslint/internal/rules/no_base_to_string"
Expand Down Expand Up @@ -98,6 +99,7 @@ func (h *IPCHandler) HandleLint(req ipc.LintRequest) (*ipc.LintResponse, error)

// Create rules
var rules = []rule.Rule{
adjacent_overload_signatures.AdjacentOverloadSignaturesRule,
await_thenable.AwaitThenableRule,
no_array_delete.NoArrayDeleteRule,
no_base_to_string.NoBaseToStringRule,
Expand Down
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/typescript-eslint/rslint/internal/rule"
"github.com/typescript-eslint/rslint/internal/rules/adjacent_overload_signatures"
"github.com/typescript-eslint/rslint/internal/rules/await_thenable"
"github.com/typescript-eslint/rslint/internal/rules/no_array_delete"
"github.com/typescript-eslint/rslint/internal/rules/no_base_to_string"
Expand Down Expand Up @@ -214,6 +215,8 @@ func (config RslintConfig) GetRulesForFile(filePath string) map[string]*RuleConf

// RegisterAllTypeSriptEslintPluginRules registers all available rules in the global registry
func RegisterAllTypeSriptEslintPluginRules() {
GlobalRuleRegistry.Register("@typescript-eslint/adjacent-overload-signatures", adjacent_overload_signatures.AdjacentOverloadSignaturesRule)
GlobalRuleRegistry.Register("adjacent-overload-signatures", adjacent_overload_signatures.AdjacentOverloadSignaturesRule)
GlobalRuleRegistry.Register("@typescript-eslint/await-thenable", await_thenable.AwaitThenableRule)
GlobalRuleRegistry.Register("@typescript-eslint/no-array-delete", no_array_delete.NoArrayDeleteRule)
GlobalRuleRegistry.Register("@typescript-eslint/no-base-to-string", no_base_to_string.NoBaseToStringRule)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package adjacent_overload_signatures

import (
"fmt"

"github.com/microsoft/typescript-go/shim/ast"
"github.com/typescript-eslint/rslint/internal/rule"
"github.com/typescript-eslint/rslint/internal/utils"
)

func buildAdjacentSignatureMessage() rule.RuleMessage {
return rule.RuleMessage{
Id: "adjacentSignature",
Description: "All {{name}} signatures should be adjacent.",
}
}

type Method struct {
CallSignature bool
Name string
Static bool
NameType utils.MemberNameType
}

// getMemberMethod gets the name and attribute of the member being processed.
// Returns the name and attribute of the member or nil if it's a member not relevant to the rule.
func getMemberMethod(ctx rule.RuleContext, member *ast.Node) *Method {
if member == nil {
return nil
}

switch member.Kind {
case ast.KindExportDeclaration:
// Export declarations (e.g., export { foo }) are not relevant for this rule
// They don't declare new functions/methods, just re-export existing ones
return nil

case ast.KindFunctionDeclaration:
funcDecl := member.AsFunctionDeclaration()
if funcDecl.Name() == nil {
return nil
}
name := funcDecl.Name().Text()
return &Method{
Name: name,
NameType: utils.MemberNameTypeNormal,
CallSignature: false,
Static: false,
}

case ast.KindMethodDeclaration:
methodDecl := member.AsMethodDeclaration()
name, nameType := utils.GetNameFromMember(ctx.SourceFile, methodDecl.Name())
return &Method{
Name: name,
NameType: nameType,
CallSignature: false,
Static: ast.IsStatic(member),
}

case ast.KindMethodSignature:
methodSig := member.AsMethodSignatureDeclaration()
name, nameType := utils.GetNameFromMember(ctx.SourceFile, methodSig.Name())
return &Method{
Name: name,
NameType: nameType,
CallSignature: false,
Static: false,
}

case ast.KindCallSignature:
return &Method{
Name: "call",
NameType: utils.MemberNameTypeNormal,
CallSignature: true,
Static: false,
}

case ast.KindConstructSignature:
return &Method{
Name: "new",
NameType: utils.MemberNameTypeNormal,
CallSignature: false,
Static: false,
}

case ast.KindConstructor:
return &Method{
Name: "constructor",
NameType: utils.MemberNameTypeNormal,
CallSignature: false,
Static: false,
}
}

return nil
}

func hasStaticModifier(modifiers []ast.ModifierLike) bool {
for _, modifier := range modifiers {
if modifier.Kind == ast.KindStaticKeyword {
return true
}
}
return false
}

func isSameMethod(method1 *Method, method2 *Method) bool {
if method2 == nil {
return false
}
return method1.Name == method2.Name &&
method1.Static == method2.Static &&
method1.CallSignature == method2.CallSignature &&
method1.NameType == method2.NameType
}

func getMembers(node *ast.Node) []*ast.Node {
switch node.Kind {
case ast.KindClassDeclaration:
classDecl := node.AsClassDeclaration()
return classDecl.Members.Nodes
case ast.KindSourceFile:
sourceFile := node.AsSourceFile()
return sourceFile.Statements.Nodes
case ast.KindModuleBlock:
moduleBlock := node.AsModuleBlock()
return moduleBlock.Statements.Nodes
case ast.KindInterfaceDeclaration:
interfaceDecl := node.AsInterfaceDeclaration()
return interfaceDecl.Members.Nodes
case ast.KindBlock:
block := node.AsBlock()
return block.Statements.Nodes
case ast.KindTypeLiteral:
typeLiteral := node.AsTypeLiteralNode()
return typeLiteral.Members.Nodes
}
return nil
}

func checkBodyForOverloadMethods(ctx rule.RuleContext, node *ast.Node) {
members := getMembers(node)
if members == nil {
return
}

// Keep track of the last method we saw for each name
// When we see a method again, check if it was the immediately previous member
methodLastSeenIndex := make(map[string]int)
var lastMethodKey string
lastMethodIdx := -1

for memberIdx, member := range members {
method := getMemberMethod(ctx, member)
if method == nil {
// This member is not a method/function
continue
}

// Create a key for this method (includes name, static, callSignature, nameType)
key := fmt.Sprintf("%s:%t:%t:%d", method.Name, method.Static, method.CallSignature, method.NameType)

if _, seen := methodLastSeenIndex[key]; seen {
// We've seen this method before
// Check if the previous occurrence was the last method we saw
if lastMethodKey != key || lastMethodIdx != memberIdx-1 {
// There was something between the last occurrence and this one
staticPrefix := ""
if method.Static {
staticPrefix = "static "
}
ctx.ReportNode(member, rule.RuleMessage{
Id: "adjacentSignature",
Description: fmt.Sprintf("All %s%s signatures should be adjacent.", staticPrefix, method.Name),
})
}
}

// Update the last seen index for this method
methodLastSeenIndex[key] = memberIdx
lastMethodKey = key
lastMethodIdx = memberIdx
}
}

var AdjacentOverloadSignaturesRule = rule.Rule{
Name: "adjacent-overload-signatures",
Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
// Check the source file at the beginning
checkBodyForOverloadMethods(ctx, &ctx.SourceFile.NodeBase.Node)

return rule.RuleListeners{
ast.KindBlock: func(node *ast.Node) {
checkBodyForOverloadMethods(ctx, node)
},
ast.KindClassDeclaration: func(node *ast.Node) {
checkBodyForOverloadMethods(ctx, node)
},
ast.KindInterfaceDeclaration: func(node *ast.Node) {
checkBodyForOverloadMethods(ctx, node)
},
ast.KindModuleBlock: func(node *ast.Node) {
checkBodyForOverloadMethods(ctx, node)
},
ast.KindTypeLiteral: func(node *ast.Node) {
checkBodyForOverloadMethods(ctx, node)
},
}
},
}
Loading