no_dupe_keys

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoDupeKeysRule = rule.Rule{
	Name: "no-dupe-keys",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		return rule.RuleListeners{
			ast.KindObjectLiteralExpression: func(node *ast.Node) {
				objLit := node.AsObjectLiteralExpression()
				if objLit == nil || objLit.Properties == nil {
					return
				}

				type propInfo struct {
					get  bool
					set  bool
					init bool
				}
				properties := make(map[string]*propInfo)

				for _, prop := range objLit.Properties.Nodes {

					if prop.Kind == ast.KindSpreadAssignment {
						continue
					}

					nameNode := prop.Name()
					if nameNode == nil {
						continue
					}

					name, isStatic := utils.GetStaticPropertyName(nameNode)
					if !isStatic {
						continue
					}

					if name == "__proto__" && prop.Kind == ast.KindPropertyAssignment && nameNode.Kind != ast.KindComputedPropertyName {
						continue
					}

					info, exists := properties[name]
					if !exists {
						info = &propInfo{}
						properties[name] = info
					}

					switch prop.Kind {
					case ast.KindGetAccessor:
						if info.get || info.init {
							ctx.ReportNode(prop, rule.RuleMessage{
								Id:          "unexpected",
								Description: fmt.Sprintf("Duplicate key '%s'.", name),
							})
						}
						info.get = true
					case ast.KindSetAccessor:
						if info.set || info.init {
							ctx.ReportNode(prop, rule.RuleMessage{
								Id:          "unexpected",
								Description: fmt.Sprintf("Duplicate key '%s'.", name),
							})
						}
						info.set = true
					default:

						if info.init || info.get || info.set {
							ctx.ReportNode(prop, rule.RuleMessage{
								Id:          "unexpected",
								Description: fmt.Sprintf("Duplicate key '%s'.", name),
							})
						}
						info.init = true
					}
				}
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-dupe-keys

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