no_unexpected_multiline

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoUnexpectedMultilineRule = rule.Rule{
	Name: "no-unexpected-multiline",
	Run: func(ctx rule.RuleContext, options any) rule.RuleListeners {
		sf := ctx.SourceFile
		text := sf.Text()
		lineMap := sf.ECMALineMap()

		reportTokenBreakAfter := func(expr *ast.Node, msg rule.RuleMessage) {
			exprEnd := expr.End()
			tokenStart := scanner.SkipTrivia(text, exprEnd)
			if tokenStart >= len(text) {
				return
			}
			exprEndLine := scanner.ComputeLineOfPosition(lineMap, exprEnd)
			tokenLine := scanner.ComputeLineOfPosition(lineMap, tokenStart)
			if exprEndLine == tokenLine {
				return
			}

			ctx.ReportRange(core.NewTextRange(tokenStart, tokenStart+1), msg)
		}

		return rule.RuleListeners{
			ast.KindElementAccessExpression: func(node *ast.Node) {

				if ast.IsOptionalChainRoot(node) {
					return
				}
				expr := node.AsElementAccessExpression().Expression
				reportTokenBreakAfter(expr, messageProperty)
			},

			ast.KindCallExpression: func(node *ast.Node) {
				if ast.IsOptionalChainRoot(node) {
					return
				}
				call := node.AsCallExpression()
				if call.Arguments == nil || len(call.Arguments.Nodes) == 0 {
					return
				}
				reportTokenBreakAfter(call.Expression, messageFunction)
			},

			ast.KindTaggedTemplateExpression: func(node *ast.Node) {
				tte := node.AsTaggedTemplateExpression()
				template := tte.Template

				backtick := scanner.SkipTrivia(text, template.Pos())
				if backtick >= len(text) {
					return
				}

				prevPos := template.Pos() - 1
				if prevPos < 0 {
					return
				}
				prevLine := scanner.ComputeLineOfPosition(lineMap, prevPos)
				backtickLine := scanner.ComputeLineOfPosition(lineMap, backtick)
				if prevLine == backtickLine {
					return
				}
				ctx.ReportRange(core.NewTextRange(backtick, backtick+1), messageTaggedTemplate)
			},

			ast.KindBinaryExpression: func(node *ast.Node) {
				bin := node.AsBinaryExpression()
				if bin.OperatorToken == nil || bin.OperatorToken.Kind != ast.KindSlashToken {
					return
				}

				outerChild := ast.WalkUpParenthesizedExpressions(node)
				outer := outerChild.Parent
				if outer == nil || outer.Kind != ast.KindBinaryExpression {
					return
				}
				outerBin := outer.AsBinaryExpression()
				if outerBin.OperatorToken == nil || outerBin.OperatorToken.Kind != ast.KindSlashToken {
					return
				}
				if outerBin.Left != outerChild {
					return
				}

				afterPos := outerBin.OperatorToken.End()
				if afterPos >= len(text) {
					return
				}
				identEnd, ok := scanIdentifier(text, afterPos)
				if !ok {
					return
				}
				if !regexFlagMatcher.MatchString(text[afterPos:identEnd]) {
					return
				}

				firstSlashRange := utils.TrimNodeTextRange(sf, bin.OperatorToken)
				leftEnd := bin.Left.End()
				leftLine := scanner.ComputeLineOfPosition(lineMap, leftEnd)
				slashLine := scanner.ComputeLineOfPosition(lineMap, firstSlashRange.Pos())
				if leftLine == slashLine {
					return
				}
				ctx.ReportRange(firstSlashRange, messageDivision)
			},
		}
	},
}

https://eslint.org/docs/latest/rules/no-unexpected-multiline

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