]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-unexpected-multiline.js
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",
23 category
: "Possible Errors",
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
) {
56 context
.report({ node
, loc
: openParen
.loc
.start
, messageId
, data
: { char: openParen
.value
} });
60 //--------------------------------------------------------------------------
62 //--------------------------------------------------------------------------
66 MemberExpression(node
) {
70 checkForBreakAfter(node
.object
, "property");
73 TaggedTemplateExpression(node
) {
74 if (node
.tag
.loc
.end
.line
=== node
.quasi
.loc
.start
.line
) {
78 // handle generics type parameters on template tags
79 const tokenBefore
= sourceCode
.getTokenBefore(node
.quasi
);
81 if (tokenBefore
.loc
.end
.line
=== node
.quasi
.loc
.start
.line
) {
85 context
.report({ node
, loc
: node
.loc
.start
, messageId
: "taggedTemplate" });
88 CallExpression(node
) {
89 if (node
.arguments
.length
=== 0) {
92 checkForBreakAfter(node
.callee
, "function");
95 "BinaryExpression[operator='/'] > BinaryExpression[operator='/'].left"(node
) {
96 const secondSlash
= sourceCode
.getTokenAfter(node
, token
=> token
.value
=== "/");
97 const tokenAfterOperator
= sourceCode
.getTokenAfter(secondSlash
);
100 tokenAfterOperator
.type
=== "Identifier" &&
101 REGEX_FLAG_MATCHER
.test(tokenAfterOperator
.value
) &&
102 secondSlash
.range
[1] === tokenAfterOperator
.range
[0]
104 checkForBreakAfter(node
.left
, "division");