]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-unexpected-multiline.js
60d8f3164cd9f0e2f7c573dd446ff698f0f60f1c
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 //------------------------------------------------------------------------------
17 /** @type {import('../shared/types').Rule} */
23 description
: "disallow confusing multiline expressions",
25 url
: "https://eslint.org/docs/rules/no-unexpected-multiline"
30 function: "Unexpected newline between function and ( of function call.",
31 property
: "Unexpected newline between object and [ of property access.",
32 taggedTemplate
: "Unexpected newline between template tag and template literal.",
33 division
: "Unexpected newline between numerator and division operator."
39 const REGEX_FLAG_MATCHER
= /^[gimsuy]+$/u;
41 const sourceCode
= context
.getSourceCode();
44 * Check to see if there is a newline between the node and the following open bracket
46 * @param {ASTNode} node The node to check.
47 * @param {string} messageId The error messageId to use.
51 function checkForBreakAfter(node
, messageId
) {
52 const openParen
= sourceCode
.getTokenAfter(node
, astUtils
.isNotClosingParenToken
);
53 const nodeExpressionEnd
= sourceCode
.getTokenBefore(openParen
);
55 if (openParen
.loc
.start
.line
!== nodeExpressionEnd
.loc
.end
.line
) {
64 //--------------------------------------------------------------------------
66 //--------------------------------------------------------------------------
70 MemberExpression(node
) {
71 if (!node
.computed
|| node
.optional
) {
74 checkForBreakAfter(node
.object
, "property");
77 TaggedTemplateExpression(node
) {
78 const { quasi
} = node
;
80 // handles common tags, parenthesized tags, and typescript's generic type arguments
81 const tokenBefore
= sourceCode
.getTokenBefore(quasi
);
83 if (tokenBefore
.loc
.end
.line
!== quasi
.loc
.start
.line
) {
87 start
: quasi
.loc
.start
,
89 line
: quasi
.loc
.start
.line
,
90 column
: quasi
.loc
.start
.column
+ 1
93 messageId
: "taggedTemplate"
98 CallExpression(node
) {
99 if (node
.arguments
.length
=== 0 || node
.optional
) {
102 checkForBreakAfter(node
.callee
, "function");
105 "BinaryExpression[operator='/'] > BinaryExpression[operator='/'].left"(node
) {
106 const secondSlash
= sourceCode
.getTokenAfter(node
, token
=> token
.value
=== "/");
107 const tokenAfterOperator
= sourceCode
.getTokenAfter(secondSlash
);
110 tokenAfterOperator
.type
=== "Identifier" &&
111 REGEX_FLAG_MATCHER
.test(tokenAfterOperator
.value
) &&
112 secondSlash
.range
[1] === tokenAfterOperator
.range
[0]
114 checkForBreakAfter(node
.left
, "division");