minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-sequences.js
1 /**
2  * @fileoverview Rule to flag use of comma operator
3  * @author Brandon Mills
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 module.exports = {
19     meta: {
20         type: "suggestion",
21
22         docs: {
23             description: "disallow comma operators",
24             category: "Best Practices",
25             recommended: false,
26             url: "https://eslint.org/docs/rules/no-sequences"
27         },
28
29         schema: []
30     },
31
32     create(context) {
33         const sourceCode = context.getSourceCode();
34
35         /**
36          * Parts of the grammar that are required to have parens.
37          */
38         const parenthesized = {
39             DoWhileStatement: "test",
40             IfStatement: "test",
41             SwitchStatement: "discriminant",
42             WhileStatement: "test",
43             WithStatement: "object",
44             ArrowFunctionExpression: "body"
45
46             /*
47              * Omitting CallExpression - commas are parsed as argument separators
48              * Omitting NewExpression - commas are parsed as argument separators
49              * Omitting ForInStatement - parts aren't individually parenthesised
50              * Omitting ForStatement - parts aren't individually parenthesised
51              */
52         };
53
54         /**
55          * Determines whether a node is required by the grammar to be wrapped in
56          * parens, e.g. the test of an if statement.
57          * @param {ASTNode} node The AST node
58          * @returns {boolean} True if parens around node belong to parent node.
59          */
60         function requiresExtraParens(node) {
61             return node.parent && parenthesized[node.parent.type] &&
62                     node === node.parent[parenthesized[node.parent.type]];
63         }
64
65         /**
66          * Check if a node is wrapped in parens.
67          * @param {ASTNode} node The AST node
68          * @returns {boolean} True if the node has a paren on each side.
69          */
70         function isParenthesised(node) {
71             return astUtils.isParenthesised(sourceCode, node);
72         }
73
74         /**
75          * Check if a node is wrapped in two levels of parens.
76          * @param {ASTNode} node The AST node
77          * @returns {boolean} True if two parens surround the node on each side.
78          */
79         function isParenthesisedTwice(node) {
80             const previousToken = sourceCode.getTokenBefore(node, 1),
81                 nextToken = sourceCode.getTokenAfter(node, 1);
82
83             return isParenthesised(node) && previousToken && nextToken &&
84                 astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
85                 astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
86         }
87
88         return {
89             SequenceExpression(node) {
90
91                 // Always allow sequences in for statement update
92                 if (node.parent.type === "ForStatement" &&
93                         (node === node.parent.init || node === node.parent.update)) {
94                     return;
95                 }
96
97                 // Wrapping a sequence in extra parens indicates intent
98                 if (requiresExtraParens(node)) {
99                     if (isParenthesisedTwice(node)) {
100                         return;
101                     }
102                 } else {
103                     if (isParenthesised(node)) {
104                         return;
105                     }
106                 }
107
108                 const firstCommaToken = sourceCode.getTokenAfter(node.expressions[0], astUtils.isCommaToken);
109
110                 context.report({ node, loc: firstCommaToken.loc, message: "Unexpected use of comma operator." });
111             }
112         };
113
114     }
115 };