no_useless_computed_key

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoUselessComputedKeyRule = rule.Rule{
	Name: "no-useless-computed-key",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		opts := parseOptions(options)

		check := func(container *ast.Node, cpn *ast.Node) {
			computed := cpn.AsComputedPropertyName()
			if computed == nil || computed.Expression == nil {
				return
			}

			inner := ast.SkipParentheses(computed.Expression)
			var value string
			isString := false
			switch inner.Kind {
			case ast.KindStringLiteral:
				value = inner.AsStringLiteral().Text
				isString = true
			case ast.KindNumericLiteral:
				value = inner.AsNumericLiteral().Text
			default:
				return
			}

			switch container.Kind {
			case ast.KindPropertyAssignment:
				gp := container.Parent
				if gp == nil || gp.Kind != ast.KindObjectLiteralExpression {
					return
				}

				if isString && value == "__proto__" && !isObjectLiteralAssignmentPattern(gp) {
					return
				}
			case ast.KindMethodDeclaration, ast.KindGetAccessor, ast.KindSetAccessor:
				gp := container.Parent
				if gp == nil {
					return
				}
				switch gp.Kind {
				case ast.KindObjectLiteralExpression:
					if isString && value == "__proto__" && !isObjectLiteralAssignmentPattern(gp) {
						return
					}
				case ast.KindClassDeclaration, ast.KindClassExpression:
					if !opts.EnforceForClassMembers {
						return
					}

					if ast.HasSyntacticModifier(container, ast.ModifierFlagsAbstract) {
						return
					}
					if ast.IsStatic(container) {
						if isString && value == "prototype" {
							return
						}
					} else {
						if isString && value == "constructor" {
							return
						}
					}
				default:
					return
				}
			case ast.KindPropertyDeclaration:
				gp := container.Parent
				if gp == nil {
					return
				}
				if gp.Kind != ast.KindClassDeclaration && gp.Kind != ast.KindClassExpression {
					return
				}
				if !opts.EnforceForClassMembers {
					return
				}

				if ast.HasSyntacticModifier(container, ast.ModifierFlagsAbstract) {
					return
				}
				if ast.HasSyntacticModifier(container, ast.ModifierFlagsAccessor) {
					return
				}
				if ast.IsStatic(container) {
					if isString && (value == "constructor" || value == "prototype") {
						return
					}
				} else {
					if isString && value == "constructor" {
						return
					}
				}
			case ast.KindBindingElement:

			default:
				return
			}

			sf := ctx.SourceFile
			sourceText := sf.Text()

			leftBracketPos := scanner.SkipTrivia(sourceText, cpn.Pos())
			endPos := cpn.End()
			keyStart := scanner.SkipTrivia(sourceText, inner.Pos())
			keyEnd := inner.End()
			keyRaw := sourceText[keyStart:keyEnd]

			msg := rule.RuleMessage{
				Id:          "unnecessarilyComputedProperty",
				Description: fmt.Sprintf("Unnecessarily computed property [%s] found.", keyRaw),
			}

			if hasCommentsBetween(sourceText, leftBracketPos+1, endPos-1) {
				ctx.ReportNode(container, msg)
				return
			}

			replacement := keyRaw
			if leftBracketPos > 0 && len(keyRaw) > 0 {
				prev := sourceText[leftBracketPos-1]

				if isIdentPart(prev) && isIdentPart(keyRaw[0]) {
					replacement = " " + keyRaw
				}
			}

			fix := rule.RuleFixReplaceRange(
				core.NewTextRange(leftBracketPos, endPos),
				replacement,
			)
			ctx.ReportNodeWithFixes(container, msg, fix)
		}

		nameIfComputed := func(n *ast.Node) *ast.Node {
			if n == nil {
				return nil
			}
			name := n.Name()
			if name == nil || name.Kind != ast.KindComputedPropertyName {
				return nil
			}
			return name
		}

		handle := func(node *ast.Node) {
			if cpn := nameIfComputed(node); cpn != nil {
				check(node, cpn)
			}
		}

		bindingElementHandle := func(node *ast.Node) {
			be := node.AsBindingElement()
			if be == nil || be.PropertyName == nil {
				return
			}
			if be.PropertyName.Kind != ast.KindComputedPropertyName {
				return
			}
			check(node, be.PropertyName)
		}

		return rule.RuleListeners{
			ast.KindPropertyAssignment:  handle,
			ast.KindMethodDeclaration:   handle,
			ast.KindGetAccessor:         handle,
			ast.KindSetAccessor:         handle,
			ast.KindPropertyDeclaration: handle,
			ast.KindBindingElement:      bindingElementHandle,
		}
	},
}

https://eslint.org/docs/latest/rules/no-useless-computed-key

Functions

This section is empty.

Types

type Options

type Options struct {
	EnforceForClassMembers bool
}

Jump to

Keyboard shortcuts

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