eslint 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -284,15 +284,16 @@ module.exports = function(context) {
284
284
  }
285
285
 
286
286
  /**
287
- * Check to see if the node arguments are spread across multiple lines
287
+ * Check to see if the argument before the callee node is multi-line and
288
+ * there should only be 1 argument before the callee node
288
289
  * @param {ASTNode} node node to check
289
290
  * @returns {boolean} True if arguments are multi-line
290
291
  */
291
- function isCallArgsMultiline(node) {
292
- var lastArg = node.arguments.length > 0 ? node.arguments[node.arguments.length - 1] : null;
292
+ function isArgBeforeCalleeNodeMultiline(node) {
293
+ var parent = node.parent;
293
294
 
294
- if (lastArg) {
295
- return lastArg.loc.end.line > node.loc.start.line;
295
+ if (parent.arguments.length >= 2 && parent.arguments[1] === node) {
296
+ return parent.arguments[0].loc.end.line > parent.arguments[0].loc.start.line;
296
297
  }
297
298
 
298
299
  return false;
@@ -337,8 +338,9 @@ module.exports = function(context) {
337
338
  indent = getNodeIndent(calleeParent);
338
339
  }
339
340
  } else {
340
- if (isCallArgsMultiline(calleeParent) &&
341
- calleeParent.callee.loc.start.line === calleeParent.callee.loc.end.line) {
341
+ if (isArgBeforeCalleeNodeMultiline(calleeNode) &&
342
+ calleeParent.callee.loc.start.line === calleeParent.callee.loc.end.line &&
343
+ !isNodeFirstInLine(calleeNode)) {
342
344
  indent = getNodeIndent(calleeParent);
343
345
  }
344
346
  }
@@ -434,7 +436,7 @@ module.exports = function(context) {
434
436
  } else if (parent.loc.start.line !== node.loc.start.line && parentVarNode === parentVarNode.parent.declarations[0]) {
435
437
  nodeIndent = nodeIndent + indentSize;
436
438
  }
437
- } else if (!parentVarNode && !isFirstArrayElementOnSameLine(parent) && effectiveParent.type !== "ExpressionStatement" && effectiveParent.type !== "AssignmentExpression" && effectiveParent.type !== "Property") {
439
+ } else if (!parentVarNode && !isFirstArrayElementOnSameLine(parent) && effectiveParent.type !== "MemberExpression" && effectiveParent.type !== "ExpressionStatement" && effectiveParent.type !== "AssignmentExpression" && effectiveParent.type !== "Property") {
438
440
  nodeIndent = nodeIndent + indentSize;
439
441
  }
440
442
 
@@ -98,31 +98,13 @@ module.exports = function(context) {
98
98
  beforeColon = +!!options.beforeColon, // Defaults to false
99
99
  afterColon = +!(options.afterColon === false); // Defaults to true
100
100
 
101
- /**
102
- * Gets the first node on the same line with the provided node.
103
- * @param {ASTNode} node The node to check find from
104
- * @returns {ASTNode} the first node on the given node's line
105
- */
106
- function getFirstNodeInLine(node) {
107
- var startLine, endLine, firstToken = node;
108
-
109
- do {
110
- node = firstToken;
111
- firstToken = context.getTokenBefore(node);
112
- startLine = node.loc.start.line;
113
- endLine = firstToken ? firstToken.loc.end.line : -1;
114
- } while (startLine === endLine);
115
-
116
- return node;
117
- }
118
-
119
101
  /**
120
102
  * Starting from the given a node (a property.key node here) looks forward
121
- * until it finds the first node before a colon punctuator and returns it.
122
- * @param {ASTNode} node The node to start looking from
123
- * @returns {ASTNode} the first node before a colon punctuator
103
+ * until it finds the last token before a colon punctuator and returns it.
104
+ * @param {ASTNode} node The node to start looking from.
105
+ * @returns {ASTNode} The last token before a colon punctuator.
124
106
  */
125
- function getFirstNodeBeforeColon(node) {
107
+ function getLastTokenBeforeColon(node) {
126
108
  var prevNode;
127
109
 
128
110
  while (node && (node.type !== "Punctuator" || node.value !== ":")) {
@@ -181,9 +163,15 @@ module.exports = function(context) {
181
163
  * @returns {int} Width of the key.
182
164
  */
183
165
  function getKeyWidth(property) {
184
- var key = property.key;
185
- var startToken = getFirstNodeInLine(key);
186
- var endToken = getFirstNodeBeforeColon(key);
166
+ var startToken, endToken;
167
+
168
+ // Ignore shorthand methods and properties, as they have no colon
169
+ if (property.method || property.shorthand) {
170
+ return 0;
171
+ }
172
+
173
+ startToken = context.getFirstToken(property);
174
+ endToken = getLastTokenBeforeColon(property.key);
187
175
 
188
176
  return endToken.range[1] - startToken.range[0];
189
177
  }
@@ -0,0 +1,88 @@
1
+ /**
2
+ * @fileoverview A rule to warn against using arrow functions in conditions.
3
+ * @author Jxck <https://github.com/Jxck>
4
+ * @copyright 2015 Luke Karrys. All rights reserved.
5
+ * The MIT License (MIT)
6
+
7
+ * Copyright (c) 2015 Jxck
8
+
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+
16
+ * The above copyright notice and this permission notice shall be included in all
17
+ * copies or substantial portions of the Software.
18
+
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ "use strict";
29
+
30
+ //------------------------------------------------------------------------------
31
+ // Helpers
32
+ //------------------------------------------------------------------------------
33
+
34
+ /**
35
+ * Checks whether or not a node is an arrow function expression.
36
+ * @param {ASTNode} node - node to test
37
+ * @returns {boolean} `true` if the node is an arrow function expression.
38
+ */
39
+ function isArrowFunction(node) {
40
+ return node.test && node.test.type === "ArrowFunctionExpression";
41
+ }
42
+
43
+ /**
44
+ * Checks whether or not a node is a conditional expression.
45
+ * @param {ASTNode} node - node to test
46
+ * @returns {boolean} `true` if the node is a conditional expression.
47
+ */
48
+ function isConditional(node) {
49
+ return node.body && node.body.type === "ConditionalExpression";
50
+ }
51
+
52
+ //------------------------------------------------------------------------------
53
+ // Rule Definition
54
+ //------------------------------------------------------------------------------
55
+
56
+ module.exports = function(context) {
57
+ /**
58
+ * Reports if a conditional statement is an arrow function.
59
+ * @param {ASTNode} node - A node to check and report.
60
+ * @returns {void}
61
+ */
62
+ function checkCondition(node) {
63
+ if (isArrowFunction(node)) {
64
+ context.report(node, "Arrow function `=>` used inside {{statementType}} instead of comparison operator.", {statementType: node.type});
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Reports if an arrow function contains an ambiguous conditional.
70
+ * @param {ASTNode} node - A node to check and report.
71
+ * @returns {void}
72
+ */
73
+ function checkArrowFunc(node) {
74
+ if (isConditional(node)) {
75
+ context.report(node, "Arrow function used ambiguously with a conditional expression.");
76
+ }
77
+ }
78
+
79
+ return {
80
+ "IfStatement": checkCondition,
81
+ "WhileStatement": checkCondition,
82
+ "ForStatement": checkCondition,
83
+ "ConditionalExpression": checkCondition,
84
+ "ArrowFunctionExpression": checkArrowFunc
85
+ };
86
+ };
87
+
88
+ module.exports.schema = [];
@@ -54,17 +54,17 @@ module.exports = function(context) {
54
54
  });
55
55
  },
56
56
 
57
- // handle the Object.defineProperty(Array.prototype) case
57
+ // handle the Object.definePropert[y|ies](Array.prototype) case
58
58
  "CallExpression": function(node) {
59
59
 
60
60
  var callee = node.callee,
61
61
  subject,
62
62
  object;
63
63
 
64
- // only worry about Object.defineProperty
64
+ // only worry about Object.definePropert[y|ies]
65
65
  if (callee.type === "MemberExpression" &&
66
66
  callee.object.name === "Object" &&
67
- callee.property.name === "defineProperty") {
67
+ (callee.property.name === "defineProperty" || callee.property.name === "defineProperties")) {
68
68
 
69
69
  // verify the object being added to is a native prototype
70
70
  subject = node.arguments[0];
@@ -63,9 +63,32 @@ module.exports = function(context) {
63
63
  return {
64
64
  "Literal": function(node) {
65
65
  var parent = node.parent,
66
+ value = node.value,
67
+ raw = node.raw,
66
68
  okTypes = detectObjects ? [] : ["ObjectExpression", "Property", "AssignmentExpression"];
67
69
 
68
- if (!isNumber(node) || shouldIgnoreNumber(node.value)) {
70
+ if (!isNumber(node)) {
71
+ return;
72
+ }
73
+
74
+ if (parent.type === "UnaryExpression" && parent.operator === "-") {
75
+ node = parent;
76
+ parent = node.parent;
77
+ value = -value;
78
+ raw = "-" + raw;
79
+ }
80
+
81
+ if (shouldIgnoreNumber(value)) {
82
+ return;
83
+ }
84
+
85
+ // don't warn on parseInt() or Number.parseInt() radix
86
+ if (parent.type === "CallExpression" && node === parent.arguments[1] &&
87
+ (parent.callee.name === "parseInt" ||
88
+ parent.callee.type === "MemberExpression" &&
89
+ parent.callee.object.name === "Number" &&
90
+ parent.callee.property.name === "parseInt")
91
+ ) {
69
92
  return;
70
93
  }
71
94
 
@@ -79,7 +102,7 @@ module.exports = function(context) {
79
102
  } else if (okTypes.indexOf(parent.type) === -1) {
80
103
  context.report({
81
104
  node: node,
82
- message: "No magic number: " + node.raw
105
+ message: "No magic number: " + raw
83
106
  });
84
107
  }
85
108
  }
@@ -14,9 +14,7 @@
14
14
  module.exports = function(context) {
15
15
 
16
16
  var smartTabs,
17
- templateLocs = [],
18
- lastTemplateLocIndex = 0,
19
- lastCommentLocIndex = 0;
17
+ ignoredLocs = [];
20
18
 
21
19
  switch (context.options[0]) {
22
20
  case true: // Support old syntax, maybe add deprecation warning here
@@ -64,7 +62,7 @@ module.exports = function(context) {
64
62
  return {
65
63
 
66
64
  "TemplateElement": function(node) {
67
- templateLocs.push(node.loc);
65
+ ignoredLocs.push(node.loc);
68
66
  },
69
67
 
70
68
  "Program:exit": function(node) {
@@ -78,6 +76,22 @@ module.exports = function(context) {
78
76
  lines = context.getSourceLines(),
79
77
  comments = context.getAllComments();
80
78
 
79
+ comments.forEach(function(comment) {
80
+ ignoredLocs.push(comment.loc);
81
+ });
82
+
83
+ ignoredLocs.sort(function(first, second) {
84
+ if (beforeLoc(first, second.start.line, second.start.column)) {
85
+ return 1;
86
+ }
87
+
88
+ if (beforeLoc(second, first.start.line, second.start.column)) {
89
+ return -1;
90
+ }
91
+
92
+ return 0;
93
+ });
94
+
81
95
  if (smartTabs) {
82
96
  /*
83
97
  * At least one space followed by a tab
@@ -93,28 +107,18 @@ module.exports = function(context) {
93
107
  var lineNumber = i + 1,
94
108
  column = match.index + 1;
95
109
 
96
- for (; lastCommentLocIndex < comments.length; lastCommentLocIndex++) {
97
- if (beforeLoc(comments[lastCommentLocIndex].loc, lineNumber, column)) {
110
+ for (var j = 0; j < ignoredLocs.length; j++) {
111
+ if (beforeLoc(ignoredLocs[j], lineNumber, column)) {
98
112
  continue;
99
113
  }
100
- if (afterLoc(comments[lastCommentLocIndex].loc, lineNumber, column)) {
101
- break;
102
- }
103
- return;
104
- }
105
-
106
- // make sure this isn't inside of a template element
107
- for (; lastTemplateLocIndex < templateLocs.length; lastTemplateLocIndex++) {
108
- if (beforeLoc(templateLocs[lastTemplateLocIndex], lineNumber, column)) {
114
+ if (afterLoc(ignoredLocs[j], lineNumber, column)) {
109
115
  continue;
110
116
  }
111
- if (afterLoc(templateLocs[lastTemplateLocIndex], lineNumber, column)) {
112
- break;
113
- }
117
+
114
118
  return;
115
119
  }
116
120
 
117
- context.report(node, { line: i + 1, column: column }, "Mixed spaces and tabs.");
121
+ context.report(node, { line: lineNumber, column: column }, "Mixed spaces and tabs.");
118
122
  }
119
123
  });
120
124
  }
@@ -13,13 +13,15 @@
13
13
  module.exports = function(context) {
14
14
 
15
15
  // Use options.max or 2 as default
16
- var numLines = 2;
16
+ var max = 2,
17
+ maxEOF;
17
18
 
18
19
  // store lines that appear empty but really aren't
19
20
  var notEmpty = [];
20
21
 
21
22
  if (context.options.length) {
22
- numLines = context.options[0].max;
23
+ max = context.options[0].max;
24
+ maxEOF = context.options[0].maxEOF;
23
25
  }
24
26
 
25
27
  //--------------------------------------------------------------------------
@@ -45,17 +47,28 @@ module.exports = function(context) {
45
47
  location,
46
48
  trimmedLines = lines.map(function(str) {
47
49
  return str.trim();
48
- });
50
+ }),
51
+ firstOfEndingBlankLines;
49
52
 
50
53
  // add the notEmpty lines in there with a placeholder
51
54
  notEmpty.forEach(function(x, i) {
52
55
  trimmedLines[i] = x;
53
56
  });
54
57
 
55
- // swallow the final newline, as some editors add it automatically
56
- // and we don't want it to cause an issue
57
- if (trimmedLines[trimmedLines.length - 1] === "") {
58
- trimmedLines = trimmedLines.slice(0, -1);
58
+ if (typeof maxEOF === "undefined") {
59
+ // swallow the final newline, as some editors add it
60
+ // automatically and we don't want it to cause an issue
61
+ if (trimmedLines[trimmedLines.length - 1] === "") {
62
+ trimmedLines = trimmedLines.slice(0, -1);
63
+ }
64
+ firstOfEndingBlankLines = trimmedLines.length;
65
+ } else {
66
+ // save the number of the first of the last blank lines
67
+ firstOfEndingBlankLines = trimmedLines.length;
68
+ while (trimmedLines[firstOfEndingBlankLines - 1] === ""
69
+ && firstOfEndingBlankLines > 0) {
70
+ firstOfEndingBlankLines--;
71
+ }
59
72
  }
60
73
 
61
74
  // Aggregate and count blank lines
@@ -67,12 +80,22 @@ module.exports = function(context) {
67
80
  if (lastLocation === currentLocation - 1) {
68
81
  blankCounter++;
69
82
  } else {
70
- if (blankCounter >= numLines) {
71
- location = {
72
- line: lastLocation + 1,
73
- column: lines[lastLocation].length
74
- };
75
- context.report(node, location, "Multiple blank lines not allowed.");
83
+ location = {
84
+ line: lastLocation + 1,
85
+ column: 1
86
+ };
87
+ if (lastLocation < firstOfEndingBlankLines) {
88
+ // within the file, not at the end
89
+ if (blankCounter >= max) {
90
+ context.report(node, location,
91
+ "Multiple blank lines not allowed.");
92
+ }
93
+ } else {
94
+ // inside the last blank lines
95
+ if (blankCounter >= maxEOF) {
96
+ context.report(node, location,
97
+ "Too many blank lines at the end of file.");
98
+ }
76
99
  }
77
100
 
78
101
  // Finally, reset the blank counter
@@ -90,6 +113,9 @@ module.exports.schema = [
90
113
  "properties": {
91
114
  "max": {
92
115
  "type": "integer"
116
+ },
117
+ "maxEOF": {
118
+ "type": "integer"
93
119
  }
94
120
  },
95
121
  "required": ["max"],
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * @fileoverview Rule to flag use of unary increment and decrement operators.
3
3
  * @author Ian Christian Myers
4
+ * @author Brody McKee (github.com/mrmckeb)
4
5
  */
5
6
 
6
7
  "use strict";
@@ -11,9 +12,19 @@
11
12
 
12
13
  module.exports = function(context) {
13
14
 
15
+ var config = context.options[0],
16
+ allowInForAfterthought = false;
17
+
18
+ if (typeof config === "object") {
19
+ allowInForAfterthought = config.allowForLoopAfterthoughts === true;
20
+ }
21
+
14
22
  return {
15
23
 
16
24
  "UpdateExpression": function(node) {
25
+ if (allowInForAfterthought && node.parent.type === "ForStatement") {
26
+ return;
27
+ }
17
28
  context.report(node, "Unary operator '" + node.operator + "' used.");
18
29
  }
19
30
 
@@ -21,4 +32,14 @@ module.exports = function(context) {
21
32
 
22
33
  };
23
34
 
24
- module.exports.schema = [];
35
+ module.exports.schema = [
36
+ {
37
+ "type": "object",
38
+ "properties": {
39
+ "allowForLoopAfterthoughts": {
40
+ "type": "boolean"
41
+ }
42
+ },
43
+ "additionalProperties": false
44
+ }
45
+ ];
@@ -14,9 +14,20 @@ module.exports = function(context) {
14
14
 
15
15
  var options = {
16
16
  builtinGlobals: Boolean(context.options[0] && context.options[0].builtinGlobals),
17
- hoist: (context.options[0] && context.options[0].hoist) || "functions"
17
+ hoist: (context.options[0] && context.options[0].hoist) || "functions",
18
+ allow: (context.options[0] && context.options[0].allow) || []
18
19
  };
19
20
 
21
+ /**
22
+ * Check if variable name is allowed.
23
+ *
24
+ * @param {ASTNode} variable The variable to check.
25
+ * @returns {boolean} Whether or not the variable name is allowed.
26
+ */
27
+ function isAllowed(variable) {
28
+ return options.allow.indexOf(variable.name) !== -1;
29
+ }
30
+
20
31
  /**
21
32
  * Checks if a variable of the class name in the class scope of ClassDeclaration.
22
33
  *
@@ -142,7 +153,8 @@ module.exports = function(context) {
142
153
  // Skip "arguments".
143
154
  variable.identifiers.length > 0 &&
144
155
  // Skip variables of a class name in the class scope of ClassDeclaration.
145
- !isDuplicatedClassNameVariable(variable)
156
+ !isDuplicatedClassNameVariable(variable) &&
157
+ !isAllowed(variable)
146
158
  );
147
159
  });
148
160
 
@@ -175,7 +187,13 @@ module.exports.schema = [
175
187
  "type": "object",
176
188
  "properties": {
177
189
  "builtinGlobals": {"type": "boolean"},
178
- "hoist": {"enum": ["all", "functions", "never"]}
190
+ "hoist": {"enum": ["all", "functions", "never"]},
191
+ "allow": {
192
+ "type": "array",
193
+ "items": {
194
+ "type": "string"
195
+ }
196
+ }
179
197
  },
180
198
  "additionalProperties": false
181
199
  }