source: imaps-frontend/node_modules/eslint/lib/rules/nonblock-statement-body-position.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/**
2 * @fileoverview enforce the location of single-line statements
3 * @author Teddy Katz
4 * @deprecated in ESLint v8.53.0
5 */
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12const POSITION_SCHEMA = { enum: ["beside", "below", "any"] };
13
14/** @type {import('../shared/types').Rule} */
15module.exports = {
16 meta: {
17 deprecated: true,
18 replacedBy: [],
19 type: "layout",
20
21 docs: {
22 description: "Enforce the location of single-line statements",
23 recommended: false,
24 url: "https://eslint.org/docs/latest/rules/nonblock-statement-body-position"
25 },
26
27 fixable: "whitespace",
28
29 schema: [
30 POSITION_SCHEMA,
31 {
32 properties: {
33 overrides: {
34 properties: {
35 if: POSITION_SCHEMA,
36 else: POSITION_SCHEMA,
37 while: POSITION_SCHEMA,
38 do: POSITION_SCHEMA,
39 for: POSITION_SCHEMA
40 },
41 additionalProperties: false
42 }
43 },
44 additionalProperties: false
45 }
46 ],
47
48 messages: {
49 expectNoLinebreak: "Expected no linebreak before this statement.",
50 expectLinebreak: "Expected a linebreak before this statement."
51 }
52 },
53
54 create(context) {
55 const sourceCode = context.sourceCode;
56
57 //----------------------------------------------------------------------
58 // Helpers
59 //----------------------------------------------------------------------
60
61 /**
62 * Gets the applicable preference for a particular keyword
63 * @param {string} keywordName The name of a keyword, e.g. 'if'
64 * @returns {string} The applicable option for the keyword, e.g. 'beside'
65 */
66 function getOption(keywordName) {
67 return context.options[1] && context.options[1].overrides && context.options[1].overrides[keywordName] ||
68 context.options[0] ||
69 "beside";
70 }
71
72 /**
73 * Validates the location of a single-line statement
74 * @param {ASTNode} node The single-line statement
75 * @param {string} keywordName The applicable keyword name for the single-line statement
76 * @returns {void}
77 */
78 function validateStatement(node, keywordName) {
79 const option = getOption(keywordName);
80
81 if (node.type === "BlockStatement" || option === "any") {
82 return;
83 }
84
85 const tokenBefore = sourceCode.getTokenBefore(node);
86
87 if (tokenBefore.loc.end.line === node.loc.start.line && option === "below") {
88 context.report({
89 node,
90 messageId: "expectLinebreak",
91 fix: fixer => fixer.insertTextBefore(node, "\n")
92 });
93 } else if (tokenBefore.loc.end.line !== node.loc.start.line && option === "beside") {
94 context.report({
95 node,
96 messageId: "expectNoLinebreak",
97 fix(fixer) {
98 if (sourceCode.getText().slice(tokenBefore.range[1], node.range[0]).trim()) {
99 return null;
100 }
101 return fixer.replaceTextRange([tokenBefore.range[1], node.range[0]], " ");
102 }
103 });
104 }
105 }
106
107 //----------------------------------------------------------------------
108 // Public
109 //----------------------------------------------------------------------
110
111 return {
112 IfStatement(node) {
113 validateStatement(node.consequent, "if");
114
115 // Check the `else` node, but don't check 'else if' statements.
116 if (node.alternate && node.alternate.type !== "IfStatement") {
117 validateStatement(node.alternate, "else");
118 }
119 },
120 WhileStatement: node => validateStatement(node.body, "while"),
121 DoWhileStatement: node => validateStatement(node.body, "do"),
122 ForStatement: node => validateStatement(node.body, "for"),
123 ForInStatement: node => validateStatement(node.body, "for"),
124 ForOfStatement: node => validateStatement(node.body, "for")
125 };
126 }
127};
Note: See TracBrowser for help on using the repository browser.