eslint 6.3.0 → 6.6.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.
Files changed (189) hide show
  1. package/CHANGELOG.md +126 -0
  2. package/README.md +11 -10
  3. package/bin/eslint.js +38 -12
  4. package/lib/cli-engine/cascading-config-array-factory.js +2 -1
  5. package/lib/cli-engine/cli-engine.js +11 -11
  6. package/lib/cli-engine/config-array/config-array.js +0 -4
  7. package/lib/cli-engine/config-array/config-dependency.js +2 -0
  8. package/lib/cli-engine/config-array/override-tester.js +4 -2
  9. package/lib/cli-engine/config-array-factory.js +7 -1
  10. package/lib/cli-engine/file-enumerator.js +5 -3
  11. package/lib/cli-engine/formatters/html.js +1 -0
  12. package/lib/cli-engine/ignored-paths.js +4 -3
  13. package/lib/cli-engine/lint-result-cache.js +0 -1
  14. package/lib/cli.js +13 -12
  15. package/lib/init/autoconfig.js +1 -11
  16. package/lib/init/config-file.js +0 -1
  17. package/lib/init/config-initializer.js +0 -1
  18. package/lib/init/config-rule.js +3 -7
  19. package/lib/init/npm-utils.js +1 -6
  20. package/lib/linter/code-path-analysis/code-path-analyzer.js +24 -38
  21. package/lib/linter/code-path-analysis/code-path-segment.js +17 -25
  22. package/lib/linter/code-path-analysis/code-path-state.js +40 -81
  23. package/lib/linter/code-path-analysis/code-path.js +10 -11
  24. package/lib/linter/code-path-analysis/debug-helpers.js +9 -13
  25. package/lib/linter/code-path-analysis/fork-context.js +23 -34
  26. package/lib/linter/code-path-analysis/id-generator.js +2 -2
  27. package/lib/linter/linter.js +121 -96
  28. package/lib/linter/node-event-generator.js +3 -2
  29. package/lib/options.js +6 -0
  30. package/lib/rule-tester/rule-tester.js +7 -10
  31. package/lib/rules/accessor-pairs.js +59 -19
  32. package/lib/rules/array-bracket-newline.js +12 -15
  33. package/lib/rules/array-bracket-spacing.js +12 -12
  34. package/lib/rules/array-callback-return.js +6 -11
  35. package/lib/rules/array-element-newline.js +5 -8
  36. package/lib/rules/arrow-parens.js +0 -1
  37. package/lib/rules/block-scoped-var.js +3 -3
  38. package/lib/rules/block-spacing.js +4 -4
  39. package/lib/rules/capitalized-comments.js +2 -9
  40. package/lib/rules/class-methods-use-this.js +3 -3
  41. package/lib/rules/comma-dangle.js +15 -23
  42. package/lib/rules/comma-spacing.js +1 -1
  43. package/lib/rules/computed-property-spacing.js +28 -11
  44. package/lib/rules/consistent-return.js +4 -5
  45. package/lib/rules/consistent-this.js +5 -5
  46. package/lib/rules/constructor-super.js +14 -16
  47. package/lib/rules/curly.js +3 -5
  48. package/lib/rules/default-param-last.js +62 -0
  49. package/lib/rules/dot-location.js +11 -12
  50. package/lib/rules/eqeqeq.js +7 -19
  51. package/lib/rules/func-names.js +6 -6
  52. package/lib/rules/function-call-argument-newline.js +5 -5
  53. package/lib/rules/generator-star-spacing.js +4 -9
  54. package/lib/rules/getter-return.js +4 -7
  55. package/lib/rules/indent-legacy.js +1 -1
  56. package/lib/rules/indent.js +46 -8
  57. package/lib/rules/index.js +3 -0
  58. package/lib/rules/init-declarations.js +2 -2
  59. package/lib/rules/jsx-quotes.js +1 -1
  60. package/lib/rules/keyword-spacing.js +32 -56
  61. package/lib/rules/lines-around-directive.js +1 -1
  62. package/lib/rules/max-len.js +0 -5
  63. package/lib/rules/max-statements-per-line.js +3 -7
  64. package/lib/rules/multiline-ternary.js +3 -3
  65. package/lib/rules/new-parens.js +5 -1
  66. package/lib/rules/newline-after-var.js +6 -7
  67. package/lib/rules/newline-before-return.js +8 -9
  68. package/lib/rules/newline-per-chained-call.js +2 -4
  69. package/lib/rules/no-class-assign.js +2 -2
  70. package/lib/rules/no-compare-neg-zero.js +1 -2
  71. package/lib/rules/no-confusing-arrow.js +2 -2
  72. package/lib/rules/no-console.js +4 -8
  73. package/lib/rules/no-const-assign.js +1 -1
  74. package/lib/rules/no-dupe-args.js +1 -1
  75. package/lib/rules/no-dupe-class-members.js +3 -4
  76. package/lib/rules/no-dupe-keys.js +6 -5
  77. package/lib/rules/no-duplicate-imports.js +14 -18
  78. package/lib/rules/no-else-return.js +0 -8
  79. package/lib/rules/no-empty-function.js +2 -4
  80. package/lib/rules/no-eval.js +10 -18
  81. package/lib/rules/no-ex-assign.js +1 -1
  82. package/lib/rules/no-extra-bind.js +12 -13
  83. package/lib/rules/no-extra-boolean-cast.js +1 -3
  84. package/lib/rules/no-extra-label.js +13 -10
  85. package/lib/rules/no-extra-parens.js +32 -17
  86. package/lib/rules/no-extra-semi.js +5 -6
  87. package/lib/rules/no-fallthrough.js +6 -6
  88. package/lib/rules/no-func-assign.js +3 -3
  89. package/lib/rules/no-global-assign.js +4 -4
  90. package/lib/rules/no-implicit-coercion.js +10 -10
  91. package/lib/rules/no-implied-eval.js +0 -1
  92. package/lib/rules/no-import-assign.js +238 -0
  93. package/lib/rules/no-invalid-this.js +1 -3
  94. package/lib/rules/no-labels.js +3 -6
  95. package/lib/rules/no-lone-blocks.js +7 -2
  96. package/lib/rules/no-loop-func.js +6 -11
  97. package/lib/rules/no-magic-numbers.js +6 -6
  98. package/lib/rules/no-misleading-character-class.js +14 -7
  99. package/lib/rules/no-mixed-operators.js +13 -22
  100. package/lib/rules/no-mixed-requires.js +0 -1
  101. package/lib/rules/no-multi-spaces.js +1 -1
  102. package/lib/rules/no-native-reassign.js +4 -4
  103. package/lib/rules/no-obj-calls.js +29 -9
  104. package/lib/rules/no-octal-escape.js +14 -8
  105. package/lib/rules/no-param-reassign.js +28 -7
  106. package/lib/rules/no-redeclare.js +1 -1
  107. package/lib/rules/no-regex-spaces.js +105 -45
  108. package/lib/rules/no-restricted-imports.js +11 -11
  109. package/lib/rules/no-self-assign.js +18 -18
  110. package/lib/rules/no-sequences.js +5 -5
  111. package/lib/rules/no-shadow.js +1 -4
  112. package/lib/rules/no-tabs.js +8 -2
  113. package/lib/rules/no-this-before-super.js +12 -13
  114. package/lib/rules/no-trailing-spaces.js +19 -7
  115. package/lib/rules/no-undef-init.js +7 -1
  116. package/lib/rules/no-unmodified-loop-condition.js +16 -29
  117. package/lib/rules/no-unneeded-ternary.js +3 -3
  118. package/lib/rules/no-unreachable.js +7 -7
  119. package/lib/rules/no-unsafe-finally.js +4 -7
  120. package/lib/rules/no-unsafe-negation.js +34 -19
  121. package/lib/rules/no-unused-expressions.js +11 -7
  122. package/lib/rules/no-unused-labels.js +3 -6
  123. package/lib/rules/no-unused-vars.js +22 -29
  124. package/lib/rules/no-use-before-define.js +10 -15
  125. package/lib/rules/no-useless-call.js +4 -4
  126. package/lib/rules/no-useless-concat.js +4 -4
  127. package/lib/rules/no-useless-constructor.js +14 -22
  128. package/lib/rules/no-useless-escape.js +3 -5
  129. package/lib/rules/no-useless-rename.js +32 -20
  130. package/lib/rules/no-useless-return.js +11 -17
  131. package/lib/rules/no-var.js +12 -25
  132. package/lib/rules/no-warning-comments.js +0 -1
  133. package/lib/rules/no-whitespace-before-property.js +3 -3
  134. package/lib/rules/object-curly-newline.js +7 -10
  135. package/lib/rules/object-curly-spacing.js +14 -15
  136. package/lib/rules/object-shorthand.js +36 -10
  137. package/lib/rules/one-var-declaration-per-line.js +2 -2
  138. package/lib/rules/operator-assignment.js +22 -1
  139. package/lib/rules/padded-blocks.js +1 -1
  140. package/lib/rules/padding-line-between-statements.js +0 -16
  141. package/lib/rules/prefer-arrow-callback.js +6 -6
  142. package/lib/rules/prefer-const.js +13 -21
  143. package/lib/rules/prefer-destructuring.js +1 -7
  144. package/lib/rules/prefer-named-capture-group.js +3 -16
  145. package/lib/rules/prefer-numeric-literals.js +35 -3
  146. package/lib/rules/prefer-object-spread.js +7 -7
  147. package/lib/rules/prefer-regex-literals.js +125 -0
  148. package/lib/rules/prefer-rest-params.js +3 -6
  149. package/lib/rules/prefer-spread.js +4 -4
  150. package/lib/rules/prefer-template.js +5 -6
  151. package/lib/rules/quote-props.js +1 -1
  152. package/lib/rules/quotes.js +11 -6
  153. package/lib/rules/radix.js +5 -10
  154. package/lib/rules/require-await.js +2 -5
  155. package/lib/rules/require-yield.js +2 -2
  156. package/lib/rules/rest-spread-spacing.js +1 -1
  157. package/lib/rules/sort-imports.js +3 -4
  158. package/lib/rules/sort-keys.js +1 -3
  159. package/lib/rules/space-before-blocks.js +1 -2
  160. package/lib/rules/space-before-function-paren.js +12 -1
  161. package/lib/rules/space-in-parens.js +81 -75
  162. package/lib/rules/space-infix-ops.js +5 -5
  163. package/lib/rules/spaced-comment.js +15 -18
  164. package/lib/rules/strict.js +2 -4
  165. package/lib/rules/symbol-description.js +1 -2
  166. package/lib/rules/template-curly-spacing.js +2 -2
  167. package/lib/rules/use-isnan.js +104 -6
  168. package/lib/rules/utils/ast-utils.js +53 -81
  169. package/lib/rules/utils/fix-tracker.js +0 -6
  170. package/lib/rules/utils/lazy-loading-rule-map.js +0 -1
  171. package/lib/rules/vars-on-top.js +11 -11
  172. package/lib/shared/config-ops.js +2 -2
  173. package/lib/shared/logging.js +2 -0
  174. package/lib/shared/runtime-info.js +163 -0
  175. package/lib/shared/traverser.js +2 -0
  176. package/lib/source-code/source-code.js +11 -12
  177. package/lib/source-code/token-store/backward-token-comment-cursor.js +5 -5
  178. package/lib/source-code/token-store/backward-token-cursor.js +5 -5
  179. package/lib/source-code/token-store/cursors.js +17 -19
  180. package/lib/source-code/token-store/decorative-cursor.js +1 -1
  181. package/lib/source-code/token-store/filter-cursor.js +2 -2
  182. package/lib/source-code/token-store/forward-token-comment-cursor.js +5 -5
  183. package/lib/source-code/token-store/forward-token-cursor.js +5 -5
  184. package/lib/source-code/token-store/index.js +86 -92
  185. package/lib/source-code/token-store/limit-cursor.js +2 -2
  186. package/lib/source-code/token-store/padded-token-cursor.js +7 -7
  187. package/lib/source-code/token-store/skip-cursor.js +2 -2
  188. package/lib/source-code/token-store/utils.js +9 -13
  189. package/package.json +8 -5
@@ -13,7 +13,7 @@ const astUtils = require("./utils/ast-utils");
13
13
 
14
14
  /**
15
15
  * Escapes the control characters of a given string.
16
- * @param {string} s - A string to escape.
16
+ * @param {string} s A string to escape.
17
17
  * @returns {string} An escaped string.
18
18
  */
19
19
  function escape(s) {
@@ -23,7 +23,7 @@ function escape(s) {
23
23
  /**
24
24
  * Escapes the control characters of a given string.
25
25
  * And adds a repeat flag.
26
- * @param {string} s - A string to escape.
26
+ * @param {string} s A string to escape.
27
27
  * @returns {string} An escaped string.
28
28
  */
29
29
  function escapeAndRepeat(s) {
@@ -33,7 +33,7 @@ function escapeAndRepeat(s) {
33
33
  /**
34
34
  * Parses `markers` option.
35
35
  * If markers don't include `"*"`, this adds `"*"` to allow JSDoc comments.
36
- * @param {string[]} [markers] - A marker list.
36
+ * @param {string[]} [markers] A marker list.
37
37
  * @returns {string[]} A marker list.
38
38
  */
39
39
  function parseMarkersOption(markers) {
@@ -51,8 +51,7 @@ function parseMarkersOption(markers) {
51
51
  * Generated pattern:
52
52
  *
53
53
  * 1. A space or an exception pattern sequence.
54
- *
55
- * @param {string[]} exceptions - An exception pattern list.
54
+ * @param {string[]} exceptions An exception pattern list.
56
55
  * @returns {string} A regular expression string for exceptions.
57
56
  */
58
57
  function createExceptionsPattern(exceptions) {
@@ -97,9 +96,8 @@ function createExceptionsPattern(exceptions) {
97
96
  *
98
97
  * 1. First, a marker or nothing.
99
98
  * 2. Next, a space or an exception pattern sequence.
100
- *
101
- * @param {string[]} markers - A marker list.
102
- * @param {string[]} exceptions - An exception pattern list.
99
+ * @param {string[]} markers A marker list.
100
+ * @param {string[]} exceptions An exception pattern list.
103
101
  * @returns {RegExp} A RegExp object for the beginning of a comment in `always` mode.
104
102
  */
105
103
  function createAlwaysStylePattern(markers, exceptions) {
@@ -135,8 +133,7 @@ function createAlwaysStylePattern(markers, exceptions) {
135
133
  *
136
134
  * 1. First, a marker or nothing (captured).
137
135
  * 2. Next, a space or a tab.
138
- *
139
- * @param {string[]} markers - A marker list.
136
+ * @param {string[]} markers A marker list.
140
137
  * @returns {RegExp} A RegExp object for `never` mode.
141
138
  */
142
139
  function createNeverStylePattern(markers) {
@@ -260,10 +257,10 @@ module.exports = {
260
257
 
261
258
  /**
262
259
  * Reports a beginning spacing error with an appropriate message.
263
- * @param {ASTNode} node - A comment node to check.
264
- * @param {string} message - An error message to report.
265
- * @param {Array} match - An array of match results for markers.
266
- * @param {string} refChar - Character used for reference in the error message.
260
+ * @param {ASTNode} node A comment node to check.
261
+ * @param {string} message An error message to report.
262
+ * @param {Array} match An array of match results for markers.
263
+ * @param {string} refChar Character used for reference in the error message.
267
264
  * @returns {void}
268
265
  */
269
266
  function reportBegin(node, message, match, refChar) {
@@ -293,9 +290,9 @@ module.exports = {
293
290
 
294
291
  /**
295
292
  * Reports an ending spacing error with an appropriate message.
296
- * @param {ASTNode} node - A comment node to check.
297
- * @param {string} message - An error message to report.
298
- * @param {string} match - An array of the matched whitespace characters.
293
+ * @param {ASTNode} node A comment node to check.
294
+ * @param {string} message An error message to report.
295
+ * @param {string} match An array of the matched whitespace characters.
299
296
  * @returns {void}
300
297
  */
301
298
  function reportEnd(node, message, match) {
@@ -317,7 +314,7 @@ module.exports = {
317
314
 
318
315
  /**
319
316
  * Reports a given comment if it's invalid.
320
- * @param {ASTNode} node - a comment node to check.
317
+ * @param {ASTNode} node a comment node to check.
321
318
  * @returns {void}
322
319
  */
323
320
  function checkCommentForSpace(node) {
@@ -43,8 +43,7 @@ function getUseStrictDirectives(statements) {
43
43
 
44
44
  /**
45
45
  * Checks whether a given parameter is a simple parameter.
46
- *
47
- * @param {ASTNode} node - A pattern node to check.
46
+ * @param {ASTNode} node A pattern node to check.
48
47
  * @returns {boolean} `true` if the node is an Identifier node.
49
48
  */
50
49
  function isSimpleParameter(node) {
@@ -53,8 +52,7 @@ function isSimpleParameter(node) {
53
52
 
54
53
  /**
55
54
  * Checks whether a given parameter list is a simple parameter list.
56
- *
57
- * @param {ASTNode[]} params - A parameter list to check.
55
+ * @param {ASTNode[]} params A parameter list to check.
58
56
  * @returns {boolean} `true` if the every parameter is an Identifier node.
59
57
  */
60
58
  function isSimpleParameterList(params) {
@@ -38,8 +38,7 @@ module.exports = {
38
38
  /**
39
39
  * Reports if node does not conform the rule in case rule is set to
40
40
  * report missing description
41
- *
42
- * @param {ASTNode} node - A CallExpression node to check.
41
+ * @param {ASTNode} node A CallExpression node to check.
43
42
  * @returns {void}
44
43
  */
45
44
  function checkArgument(node) {
@@ -53,7 +53,7 @@ module.exports = {
53
53
 
54
54
  /**
55
55
  * Checks spacing before `}` of a given token.
56
- * @param {Token} token - A token to check. This is a Template token.
56
+ * @param {Token} token A token to check. This is a Template token.
57
57
  * @returns {void}
58
58
  */
59
59
  function checkSpacingBefore(token) {
@@ -82,7 +82,7 @@ module.exports = {
82
82
 
83
83
  /**
84
84
  * Checks spacing after `${` of a given token.
85
- * @param {Token} token - A token to check. This is a Template token.
85
+ * @param {Token} token A token to check. This is a Template token.
86
86
  * @returns {void}
87
87
  */
88
88
  function checkSpacingAfter(token) {
@@ -5,6 +5,25 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const astUtils = require("./utils/ast-utils");
13
+
14
+ //------------------------------------------------------------------------------
15
+ // Helpers
16
+ //------------------------------------------------------------------------------
17
+
18
+ /**
19
+ * Determines if the given node is a NaN `Identifier` node.
20
+ * @param {ASTNode|null} node The node to check.
21
+ * @returns {boolean} `true` if the node is 'NaN' identifier.
22
+ */
23
+ function isNaNIdentifier(node) {
24
+ return Boolean(node) && node.type === "Identifier" && node.name === "NaN";
25
+ }
26
+
8
27
  //------------------------------------------------------------------------------
9
28
  // Rule Definition
10
29
  //------------------------------------------------------------------------------
@@ -20,21 +39,100 @@ module.exports = {
20
39
  url: "https://eslint.org/docs/rules/use-isnan"
21
40
  },
22
41
 
23
- schema: [],
42
+ schema: [
43
+ {
44
+ type: "object",
45
+ properties: {
46
+ enforceForSwitchCase: {
47
+ type: "boolean",
48
+ default: false
49
+ },
50
+ enforceForIndexOf: {
51
+ type: "boolean",
52
+ default: false
53
+ }
54
+ },
55
+ additionalProperties: false
56
+ }
57
+ ],
58
+
24
59
  messages: {
25
- useIsNaN: "Use the isNaN function to compare with NaN."
60
+ comparisonWithNaN: "Use the isNaN function to compare with NaN.",
61
+ switchNaN: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.",
62
+ caseNaN: "'case NaN' can never match. Use Number.isNaN before the switch.",
63
+ indexOfNaN: "Array prototype method '{{ methodName }}' cannot find NaN."
26
64
  }
27
65
  },
28
66
 
29
67
  create(context) {
30
68
 
31
- return {
32
- BinaryExpression(node) {
33
- if (/^(?:[<>]|[!=]=)=?$/u.test(node.operator) && (node.left.name === "NaN" || node.right.name === "NaN")) {
34
- context.report({ node, messageId: "useIsNaN" });
69
+ const enforceForSwitchCase = context.options[0] && context.options[0].enforceForSwitchCase;
70
+ const enforceForIndexOf = context.options[0] && context.options[0].enforceForIndexOf;
71
+
72
+ /**
73
+ * Checks the given `BinaryExpression` node for `foo === NaN` and other comparisons.
74
+ * @param {ASTNode} node The node to check.
75
+ * @returns {void}
76
+ */
77
+ function checkBinaryExpression(node) {
78
+ if (
79
+ /^(?:[<>]|[!=]=)=?$/u.test(node.operator) &&
80
+ (isNaNIdentifier(node.left) || isNaNIdentifier(node.right))
81
+ ) {
82
+ context.report({ node, messageId: "comparisonWithNaN" });
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Checks the discriminant and all case clauses of the given `SwitchStatement` node for `switch(NaN)` and `case NaN:`
88
+ * @param {ASTNode} node The node to check.
89
+ * @returns {void}
90
+ */
91
+ function checkSwitchStatement(node) {
92
+ if (isNaNIdentifier(node.discriminant)) {
93
+ context.report({ node, messageId: "switchNaN" });
94
+ }
95
+
96
+ for (const switchCase of node.cases) {
97
+ if (isNaNIdentifier(switchCase.test)) {
98
+ context.report({ node: switchCase, messageId: "caseNaN" });
35
99
  }
36
100
  }
101
+ }
102
+
103
+ /**
104
+ * Checks the the given `CallExpression` node for `.indexOf(NaN)` and `.lastIndexOf(NaN)`.
105
+ * @param {ASTNode} node The node to check.
106
+ * @returns {void}
107
+ */
108
+ function checkCallExpression(node) {
109
+ const callee = node.callee;
110
+
111
+ if (callee.type === "MemberExpression") {
112
+ const methodName = astUtils.getStaticPropertyName(callee);
113
+
114
+ if (
115
+ (methodName === "indexOf" || methodName === "lastIndexOf") &&
116
+ node.arguments.length === 1 &&
117
+ isNaNIdentifier(node.arguments[0])
118
+ ) {
119
+ context.report({ node, messageId: "indexOfNaN", data: { methodName } });
120
+ }
121
+ }
122
+ }
123
+
124
+ const listeners = {
125
+ BinaryExpression: checkBinaryExpression
37
126
  };
38
127
 
128
+ if (enforceForSwitchCase) {
129
+ listeners.SwitchStatement = checkSwitchStatement;
130
+ }
131
+
132
+ if (enforceForIndexOf) {
133
+ listeners.CallExpression = checkCallExpression;
134
+ }
135
+
136
+ return listeners;
39
137
  }
40
138
  };