UNPKG

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