constructor_super

package
v0.1.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConstructorSuperRule = rule.CreateRule(rule.Rule{
	Name: "constructor-super",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindConstructor: func(node *ast.Node) {

				if !isConstructor(node) {
					return
				}

				classNode := getClassNode(node)
				if classNode == nil {
					return
				}

				hasExtends := hasValidExtends(classNode)

				body := node.Body()

				analysis := analyzeSuperCallsInBody(body)

				if hasExtends {

					if !analysis.hasSuperCall {

						if allPathsTerminateEarly(body) {

						} else if hasBranchingWithEarlyReturn(body) {

							ctx.ReportNode(node, buildMissingSome())
						} else {

							ctx.ReportNode(node, buildMissingAll())
						}
					} else if !analysis.allPathsHaveSuper {

						ctx.ReportNode(node, buildMissingSome())
					} else if analysis.allPathsHaveSuper && len(analysis.superCallLocations) > 1 {

						duplicates := findDuplicateSuperCalls(body, analysis.superCallLocations)
						for _, superCall := range duplicates {

							superKeyword := superCall.Expression()
							if superKeyword != nil && superKeyword.Kind == ast.KindSuperKeyword {
								ctx.ReportNode(superKeyword, buildDuplicate())
							} else {
								ctx.ReportNode(superCall, buildDuplicate())
							}
						}
					}
				} else {

					if analysis.hasSuperCall {

						for _, superCall := range analysis.superCallLocations {

							superKeyword := superCall.Expression()
							if superKeyword != nil && superKeyword.Kind == ast.KindSuperKeyword {
								ctx.ReportNode(superKeyword, buildBadSuper())
							} else {
								ctx.ReportNode(superCall, buildBadSuper())
							}
						}
					}
				}
			},
		}
	},
})

ConstructorSuperRule enforces proper super() calls in constructors

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL