no_useless_backreference

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: 8 Imported by: 0

Documentation

Overview

Package no_useless_backreference implements ESLint's no-useless-backreference rule. The rule walks each regex pattern looking for backreferences that always match the empty string regardless of input.

This file contains a minimal ECMAScript regex parser that produces just enough of an AST (Pattern → Alternatives → Groups / Lookarounds / Backreferences) to run the analysis. It is NOT a complete regex parser — it skips over content it doesn't need (characters, character classes, quantifiers) but always advances correctly past them.

On any syntax error the parser returns ok=false and the caller skips the regex entirely (matching ESLint's behavior of catching parser exceptions).

cspell:ignore rctx

Index

Constants

This section is empty.

Variables

View Source
var NoUselessBackreferenceRule = rule.Rule{
	Name: "no-useless-backreference",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		eval := newStaticEvalCtx(ctx)
		return rule.RuleListeners{
			ast.KindRegularExpressionLiteral: func(node *ast.Node) {
				if isRegexLiteralHandledByConstructor(ctx, node) {
					return
				}
				pattern, flags := utils.ExtractRegexPatternAndFlags(node.Text())
				if pattern == "" && flags == "" {
					return
				}
				rxFlags := utils.ParseRegexFlags(flags)
				checkRegex(ctx, node, pattern, rxFlags)
			},
			ast.KindCallExpression: func(node *ast.Node) {
				call := node.AsCallExpression()
				handleRegExpConstructor(ctx, node, call.Expression, call.Arguments, eval)
			},
			ast.KindNewExpression: func(node *ast.Node) {
				newExpr := node.AsNewExpression()
				handleRegExpConstructor(ctx, node, newExpr.Expression, newExpr.Arguments, eval)
			},
		}
	},
}

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

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