]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/nonblock-statement-body-position.js
7ed541b3802750a4f9f093569dbaeb3945fddc4e
2 * @fileoverview enforce the location of single-line statements
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
11 const POSITION_SCHEMA
= { enum: ["beside", "below", "any"] };
18 description
: "enforce the location of single-line statements",
20 url
: "https://eslint.org/docs/rules/nonblock-statement-body-position"
23 fixable
: "whitespace",
32 else: POSITION_SCHEMA
,
33 while: POSITION_SCHEMA
,
37 additionalProperties
: false
40 additionalProperties
: false
45 expectNoLinebreak
: "Expected no linebreak before this statement.",
46 expectLinebreak
: "Expected a linebreak before this statement."
51 const sourceCode
= context
.getSourceCode();
53 //----------------------------------------------------------------------
55 //----------------------------------------------------------------------
58 * Gets the applicable preference for a particular keyword
59 * @param {string} keywordName The name of a keyword, e.g. 'if'
60 * @returns {string} The applicable option for the keyword, e.g. 'beside'
62 function getOption(keywordName
) {
63 return context
.options
[1] && context
.options
[1].overrides
&& context
.options
[1].overrides
[keywordName
] ||
69 * Validates the location of a single-line statement
70 * @param {ASTNode} node The single-line statement
71 * @param {string} keywordName The applicable keyword name for the single-line statement
74 function validateStatement(node
, keywordName
) {
75 const option
= getOption(keywordName
);
77 if (node
.type
=== "BlockStatement" || option
=== "any") {
81 const tokenBefore
= sourceCode
.getTokenBefore(node
);
83 if (tokenBefore
.loc
.end
.line
=== node
.loc
.start
.line
&& option
=== "below") {
86 messageId
: "expectLinebreak",
87 fix
: fixer
=> fixer
.insertTextBefore(node
, "\n")
89 } else if (tokenBefore
.loc
.end
.line
!== node
.loc
.start
.line
&& option
=== "beside") {
92 messageId
: "expectNoLinebreak",
94 if (sourceCode
.getText().slice(tokenBefore
.range
[1], node
.range
[0]).trim()) {
97 return fixer
.replaceTextRange([tokenBefore
.range
[1], node
.range
[0]], " ");
103 //----------------------------------------------------------------------
105 //----------------------------------------------------------------------
109 validateStatement(node
.consequent
, "if");
111 // Check the `else` node, but don't check 'else if' statements.
112 if (node
.alternate
&& node
.alternate
.type
!== "IfStatement") {
113 validateStatement(node
.alternate
, "else");
116 WhileStatement
: node
=> validateStatement(node
.body
, "while"),
117 DoWhileStatement
: node
=> validateStatement(node
.body
, "do"),
118 ForStatement
: node
=> validateStatement(node
.body
, "for"),
119 ForInStatement
: node
=> validateStatement(node
.body
, "for"),
120 ForOfStatement
: node
=> validateStatement(node
.body
, "for")