]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-unexpected-multiline.js
4447959ed9a17636d08668fb40df6a107fcfe79c
2 * @fileoverview Rule to spot scenarios where a newline looks like it is ending a statement, but is not.
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const astUtils
= require("./utils/ast-utils");
13 //------------------------------------------------------------------------------
15 //------------------------------------------------------------------------------
22 description
: "disallow confusing multiline expressions",
24 url
: "https://eslint.org/docs/rules/no-unexpected-multiline"
29 function: "Unexpected newline between function and ( of function call.",
30 property
: "Unexpected newline between object and [ of property access.",
31 taggedTemplate
: "Unexpected newline between template tag and template literal.",
32 division
: "Unexpected newline between numerator and division operator."
38 const REGEX_FLAG_MATCHER
= /^[gimsuy]+$/u;
40 const sourceCode
= context
.getSourceCode();
43 * Check to see if there is a newline between the node and the following open bracket
45 * @param {ASTNode} node The node to check.
46 * @param {string} messageId The error messageId to use.
50 function checkForBreakAfter(node
, messageId
) {
51 const openParen
= sourceCode
.getTokenAfter(node
, astUtils
.isNotClosingParenToken
);
52 const nodeExpressionEnd
= sourceCode
.getTokenBefore(openParen
);
54 if (openParen
.loc
.start
.line
!== nodeExpressionEnd
.loc
.end
.line
) {
63 //--------------------------------------------------------------------------
65 //--------------------------------------------------------------------------
69 MemberExpression(node
) {
70 if (!node
.computed
|| node
.optional
) {
73 checkForBreakAfter(node
.object
, "property");
76 TaggedTemplateExpression(node
) {
77 const { quasi
} = node
;
79 // handles common tags, parenthesized tags, and typescript's generic type arguments
80 const tokenBefore
= sourceCode
.getTokenBefore(quasi
);
82 if (tokenBefore
.loc
.end
.line
!== quasi
.loc
.start
.line
) {
86 start
: quasi
.loc
.start
,
88 line
: quasi
.loc
.start
.line
,
89 column
: quasi
.loc
.start
.column
+ 1
92 messageId
: "taggedTemplate"
97 CallExpression(node
) {
98 if (node
.arguments
.length
=== 0 || node
.optional
) {
101 checkForBreakAfter(node
.callee
, "function");
104 "BinaryExpression[operator='/'] > BinaryExpression[operator='/'].left"(node
) {
105 const secondSlash
= sourceCode
.getTokenAfter(node
, token
=> token
.value
=== "/");
106 const tokenAfterOperator
= sourceCode
.getTokenAfter(secondSlash
);
109 tokenAfterOperator
.type
=== "Identifier" &&
110 REGEX_FLAG_MATCHER
.test(tokenAfterOperator
.value
) &&
111 secondSlash
.range
[1] === tokenAfterOperator
.range
[0]
113 checkForBreakAfter(node
.left
, "division");