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
@@ -25,7 +25,6 @@ const CJS_IMPORT = /^require\(/u;
25
25
 
26
26
  /**
27
27
  * Creates tester which check if a node starts with specific keyword.
28
- *
29
28
  * @param {string} keyword The keyword to test.
30
29
  * @returns {Object} the created tester.
31
30
  * @private
@@ -39,7 +38,6 @@ function newKeywordTester(keyword) {
39
38
 
40
39
  /**
41
40
  * Creates tester which check if a node starts with specific keyword and spans a single line.
42
- *
43
41
  * @param {string} keyword The keyword to test.
44
42
  * @returns {Object} the created tester.
45
43
  * @private
@@ -54,7 +52,6 @@ function newSinglelineKeywordTester(keyword) {
54
52
 
55
53
  /**
56
54
  * Creates tester which check if a node starts with specific keyword and spans multiple lines.
57
- *
58
55
  * @param {string} keyword The keyword to test.
59
56
  * @returns {Object} the created tester.
60
57
  * @private
@@ -69,7 +66,6 @@ function newMultilineKeywordTester(keyword) {
69
66
 
70
67
  /**
71
68
  * Creates tester which check if a node is specific type.
72
- *
73
69
  * @param {string} type The node type to test.
74
70
  * @returns {Object} the created tester.
75
71
  * @private
@@ -83,7 +79,6 @@ function newNodeTypeTester(type) {
83
79
 
84
80
  /**
85
81
  * Checks the given node is an expression statement of IIFE.
86
- *
87
82
  * @param {ASTNode} node The node to check.
88
83
  * @returns {boolean} `true` if the node is an expression statement of IIFE.
89
84
  * @private
@@ -103,7 +98,6 @@ function isIIFEStatement(node) {
103
98
  /**
104
99
  * Checks whether the given node is a block-like statement.
105
100
  * This checks the last token of the node is the closing brace of a block.
106
- *
107
101
  * @param {SourceCode} sourceCode The source code to get tokens.
108
102
  * @param {ASTNode} node The node to check.
109
103
  * @returns {boolean} `true` if the node is a block-like statement.
@@ -187,7 +181,6 @@ function isDirectivePrologue(node, sourceCode) {
187
181
  *
188
182
  * foo()
189
183
  * ;[1, 2, 3].forEach(bar)
190
- *
191
184
  * @param {SourceCode} sourceCode The source code to get tokens.
192
185
  * @param {ASTNode} node The node to get.
193
186
  * @returns {Token} The actual last token.
@@ -224,7 +217,6 @@ function replacerToRemovePaddingLines(_, trailingSpaces, indentSpaces) {
224
217
  /**
225
218
  * Check and report statements for `any` configuration.
226
219
  * It does nothing.
227
- *
228
220
  * @returns {void}
229
221
  * @private
230
222
  */
@@ -236,7 +228,6 @@ function verifyForAny() {
236
228
  * This autofix removes blank lines between the given 2 statements.
237
229
  * However, if comments exist between 2 blank lines, it does not remove those
238
230
  * blank lines automatically.
239
- *
240
231
  * @param {RuleContext} context The rule context to report.
241
232
  * @param {ASTNode} _ Unused. The previous node to check.
242
233
  * @param {ASTNode} nextNode The next node to check.
@@ -276,7 +267,6 @@ function verifyForNever(context, _, nextNode, paddingLines) {
276
267
  * This autofix inserts a blank line between the given 2 statements.
277
268
  * If the `prevNode` has trailing comments, it inserts a blank line after the
278
269
  * trailing comments.
279
- *
280
270
  * @param {RuleContext} context The rule context to report.
281
271
  * @param {ASTNode} prevNode The previous node to check.
282
272
  * @param {ASTNode} nextNode The next node to check.
@@ -318,7 +308,6 @@ function verifyForAlways(context, prevNode, nextNode, paddingLines) {
318
308
  *
319
309
  * // comment.
320
310
  * bar();
321
- *
322
311
  * @param {Token} token The token to check.
323
312
  * @returns {boolean} `true` if the token is not a trailing comment.
324
313
  * @private
@@ -511,7 +500,6 @@ module.exports = {
511
500
 
512
501
  /**
513
502
  * Checks whether the given node matches the given type.
514
- *
515
503
  * @param {ASTNode} node The statement node to check.
516
504
  * @param {string|string[]} type The statement type to check.
517
505
  * @returns {boolean} `true` if the statement node matched the type.
@@ -531,7 +519,6 @@ module.exports = {
531
519
 
532
520
  /**
533
521
  * Finds the last matched configure from configureList.
534
- *
535
522
  * @param {ASTNode} prevNode The previous statement to match.
536
523
  * @param {ASTNode} nextNode The current statement to match.
537
524
  * @returns {Object} The tester of the last matched configure.
@@ -554,7 +541,6 @@ module.exports = {
554
541
  /**
555
542
  * Gets padding line sequences between the given 2 statements.
556
543
  * Comments are separators of the padding line sequences.
557
- *
558
544
  * @param {ASTNode} prevNode The previous statement to count.
559
545
  * @param {ASTNode} nextNode The current statement to count.
560
546
  * @returns {Array<Token[]>} The array of token pairs.
@@ -584,7 +570,6 @@ module.exports = {
584
570
 
585
571
  /**
586
572
  * Verify padding lines between the given node and the previous node.
587
- *
588
573
  * @param {ASTNode} node The node to verify.
589
574
  * @returns {void}
590
575
  * @private
@@ -616,7 +601,6 @@ module.exports = {
616
601
  /**
617
602
  * Verify padding lines between the given node and the previous node.
618
603
  * Then process to enter to new scope.
619
- *
620
604
  * @param {ASTNode} node The node to verify.
621
605
  * @returns {void}
622
606
  * @private
@@ -11,7 +11,7 @@
11
11
 
12
12
  /**
13
13
  * Checks whether or not a given variable is a function name.
14
- * @param {eslint-scope.Variable} variable - A variable to check.
14
+ * @param {eslint-scope.Variable} variable A variable to check.
15
15
  * @returns {boolean} `true` if the variable is a function name.
16
16
  */
17
17
  function isFunctionName(variable) {
@@ -20,9 +20,9 @@ function isFunctionName(variable) {
20
20
 
21
21
  /**
22
22
  * Checks whether or not a given MetaProperty node equals to a given value.
23
- * @param {ASTNode} node - A MetaProperty node to check.
24
- * @param {string} metaName - The name of `MetaProperty.meta`.
25
- * @param {string} propertyName - The name of `MetaProperty.property`.
23
+ * @param {ASTNode} node A MetaProperty node to check.
24
+ * @param {string} metaName The name of `MetaProperty.meta`.
25
+ * @param {string} propertyName The name of `MetaProperty.property`.
26
26
  * @returns {boolean} `true` if the node is the specific value.
27
27
  */
28
28
  function checkMetaProperty(node, metaName, propertyName) {
@@ -31,7 +31,7 @@ function checkMetaProperty(node, metaName, propertyName) {
31
31
 
32
32
  /**
33
33
  * Gets the variable object of `arguments` which is defined implicitly.
34
- * @param {eslint-scope.Scope} scope - A scope to get.
34
+ * @param {eslint-scope.Scope} scope A scope to get.
35
35
  * @returns {eslint-scope.Variable} The found variable object.
36
36
  */
37
37
  function getVariableOfArguments(scope) {
@@ -57,7 +57,7 @@ function getVariableOfArguments(scope) {
57
57
 
58
58
  /**
59
59
  * Checkes whether or not a given node is a callback.
60
- * @param {ASTNode} node - A node to check.
60
+ * @param {ASTNode} node A node to check.
61
61
  * @returns {Object}
62
62
  * {boolean} retv.isCallback - `true` if the node is a callback.
63
63
  * {boolean} retv.isLexicalThis - `true` if the node is with `.bind(this)`.
@@ -17,8 +17,7 @@ const DESTRUCTURING_HOST_TYPE = /^(?:VariableDeclarator|AssignmentExpression)$/u
17
17
 
18
18
  /**
19
19
  * Checks whether a given node is located at `ForStatement.init` or not.
20
- *
21
- * @param {ASTNode} node - A node to check.
20
+ * @param {ASTNode} node A node to check.
22
21
  * @returns {boolean} `true` if the node is located at `ForStatement.init`.
23
22
  */
24
23
  function isInitOfForStatement(node) {
@@ -27,8 +26,7 @@ function isInitOfForStatement(node) {
27
26
 
28
27
  /**
29
28
  * Checks whether a given Identifier node becomes a VariableDeclaration or not.
30
- *
31
- * @param {ASTNode} identifier - An Identifier node to check.
29
+ * @param {ASTNode} identifier An Identifier node to check.
32
30
  * @returns {boolean} `true` if the node can become a VariableDeclaration.
33
31
  */
34
32
  function canBecomeVariableDeclaration(identifier) {
@@ -51,9 +49,8 @@ function canBecomeVariableDeclaration(identifier) {
51
49
  /**
52
50
  * Checks if an property or element is from outer scope or function parameters
53
51
  * in destructing pattern.
54
- *
55
- * @param {string} name - A variable name to be checked.
56
- * @param {eslint-scope.Scope} initScope - A scope to start find.
52
+ * @param {string} name A variable name to be checked.
53
+ * @param {eslint-scope.Scope} initScope A scope to start find.
57
54
  * @returns {boolean} Indicates if the variable is from outer scope or function parameters.
58
55
  */
59
56
  function isOuterVariableInDestructing(name, initScope) {
@@ -76,8 +73,7 @@ function isOuterVariableInDestructing(name, initScope) {
76
73
  * belongs to.
77
74
  * This is used to detect a mix of reassigned and never reassigned in a
78
75
  * destructuring.
79
- *
80
- * @param {eslint-scope.Reference} reference - A reference to get.
76
+ * @param {eslint-scope.Reference} reference A reference to get.
81
77
  * @returns {ASTNode|null} A VariableDeclarator/AssignmentExpression node or
82
78
  * null.
83
79
  */
@@ -162,9 +158,8 @@ function hasMemberExpressionAssignment(node) {
162
158
  * `/*exported foo` directive comment makes such variables. This rule does not
163
159
  * warn such variables because this rule cannot distinguish whether the
164
160
  * exported variables are reassigned or not.
165
- *
166
- * @param {eslint-scope.Variable} variable - A variable to get.
167
- * @param {boolean} ignoreReadBeforeAssign -
161
+ * @param {eslint-scope.Variable} variable A variable to get.
162
+ * @param {boolean} ignoreReadBeforeAssign
168
163
  * The value of `ignoreReadBeforeAssign` option.
169
164
  * @returns {ASTNode|null}
170
165
  * An Identifier node if the variable should change to const.
@@ -262,9 +257,8 @@ function getIdentifierIfShouldBeConst(variable, ignoreReadBeforeAssign) {
262
257
  * reference of given variables belongs to.
263
258
  * This is used to detect a mix of reassigned and never reassigned in a
264
259
  * destructuring.
265
- *
266
- * @param {eslint-scope.Variable[]} variables - Variables to group by destructuring.
267
- * @param {boolean} ignoreReadBeforeAssign -
260
+ * @param {eslint-scope.Variable[]} variables Variables to group by destructuring.
261
+ * @param {boolean} ignoreReadBeforeAssign
268
262
  * The value of `ignoreReadBeforeAssign` option.
269
263
  * @returns {Map<ASTNode, ASTNode[]>} Grouped identifier nodes.
270
264
  */
@@ -308,10 +302,9 @@ function groupByDestructuring(variables, ignoreReadBeforeAssign) {
308
302
 
309
303
  /**
310
304
  * Finds the nearest parent of node with a given type.
311
- *
312
- * @param {ASTNode} node The node to search from.
313
- * @param {string} type The type field of the parent node.
314
- * @param {Function} shouldStop – a predicate that returns true if the traversal should stop, and false otherwise.
305
+ * @param {ASTNode} node The node to search from.
306
+ * @param {string} type The type field of the parent node.
307
+ * @param {Function} shouldStop A predicate that returns true if the traversal should stop, and false otherwise.
315
308
  * @returns {ASTNode} The closest ancestor with the specified type; null if no such ancestor exists.
316
309
  */
317
310
  function findUp(node, type, shouldStop) {
@@ -374,8 +367,7 @@ module.exports = {
374
367
  * nullable. In simple declaration or assignment cases, the length of
375
368
  * the array is 1. In destructuring cases, the length of the array can
376
369
  * be 2 or more.
377
- *
378
- * @param {(eslint-scope.Reference|null)[]} nodes -
370
+ * @param {(eslint-scope.Reference|null)[]} nodes
379
371
  * References which are grouped by destructuring to report.
380
372
  * @returns {void}
381
373
  */
@@ -103,6 +103,7 @@ module.exports = {
103
103
  // Helpers
104
104
  //--------------------------------------------------------------------------
105
105
 
106
+ // eslint-disable-next-line jsdoc/require-description
106
107
  /**
107
108
  * @param {string} nodeType "AssignmentExpression" or "VariableDeclarator"
108
109
  * @param {string} destructuringType "array" or "object"
@@ -119,7 +120,6 @@ module.exports = {
119
120
  *
120
121
  * This is used to differentiate array index access from object property
121
122
  * access.
122
- *
123
123
  * @param {ASTNode} node the node to evaluate
124
124
  * @returns {boolean} whether or not the node is an integer
125
125
  */
@@ -129,7 +129,6 @@ module.exports = {
129
129
 
130
130
  /**
131
131
  * Report that the given node should use destructuring
132
- *
133
132
  * @param {ASTNode} reportNode the node to report
134
133
  * @param {string} type the type of destructuring that should have been done
135
134
  * @param {Function|null} fix the fix function or null to pass to context.report
@@ -153,7 +152,6 @@ module.exports = {
153
152
  * Assignment expression is not fixed.
154
153
  * Array destructuring is not fixed.
155
154
  * Renamed property is not fixed.
156
- *
157
155
  * @param {ASTNode} node the the node to evaluate
158
156
  * @returns {boolean} whether or not the node should be fixed
159
157
  */
@@ -168,7 +166,6 @@ module.exports = {
168
166
  * Fix a node into object destructuring.
169
167
  * This function only handles the simplest case of object destructuring,
170
168
  * see {@link shouldFix}.
171
- *
172
169
  * @param {SourceCodeFixer} fixer the fixer object
173
170
  * @param {ASTNode} node the node to be fixed.
174
171
  * @returns {Object} a fix for the node
@@ -189,7 +186,6 @@ module.exports = {
189
186
  *
190
187
  * Pulled out into a separate method so that VariableDeclarators and
191
188
  * AssignmentExpressions can share the same verification logic.
192
- *
193
189
  * @param {ASTNode} leftNode the left-hand side of the assignment
194
190
  * @param {ASTNode} rightNode the right-hand side of the assignment
195
191
  * @param {ASTNode} reportNode the node to report the error on
@@ -231,7 +227,6 @@ module.exports = {
231
227
  /**
232
228
  * Check if a given variable declarator is coming from an property access
233
229
  * that should be using destructuring instead
234
- *
235
230
  * @param {ASTNode} node the variable declarator to check
236
231
  * @returns {void}
237
232
  */
@@ -252,7 +247,6 @@ module.exports = {
252
247
 
253
248
  /**
254
249
  * Run the `prefer-destructuring` check on an AssignmentExpression
255
- *
256
250
  * @param {ASTNode} node the AssignmentExpression node
257
251
  * @returns {void}
258
252
  */
@@ -49,17 +49,16 @@ module.exports = {
49
49
 
50
50
  /**
51
51
  * Function to check regular expression.
52
- *
53
- * @param {string} regex The regular expression to be check.
52
+ * @param {string} pattern The regular expression pattern to be check.
54
53
  * @param {ASTNode} node AST node which contains regular expression.
55
54
  * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
56
55
  * @returns {void}
57
56
  */
58
- function checkRegex(regex, node, uFlag) {
57
+ function checkRegex(pattern, node, uFlag) {
59
58
  let ast;
60
59
 
61
60
  try {
62
- ast = parser.parsePattern(regex, 0, regex.length, uFlag);
61
+ ast = parser.parsePattern(pattern, 0, pattern.length, uFlag);
63
62
  } catch (_) {
64
63
 
65
64
  // ignore regex syntax errors
@@ -69,21 +68,9 @@ module.exports = {
69
68
  regexpp.visitRegExpAST(ast, {
70
69
  onCapturingGroupEnter(group) {
71
70
  if (!group.name) {
72
- const locNode = node.type === "Literal" ? node : node.arguments[0];
73
-
74
71
  context.report({
75
72
  node,
76
73
  messageId: "required",
77
- loc: {
78
- start: {
79
- line: locNode.loc.start.line,
80
- column: locNode.loc.start.column + group.start + 1
81
- },
82
- end: {
83
- line: locNode.loc.start.line,
84
- column: locNode.loc.start.column + group.end + 1
85
- }
86
- },
87
74
  data: {
88
75
  group: group.raw
89
76
  }
@@ -5,6 +5,12 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const astUtils = require("./utils/ast-utils");
13
+
8
14
  //------------------------------------------------------------------------------
9
15
  // Helpers
10
16
  //------------------------------------------------------------------------------
@@ -94,9 +100,13 @@ module.exports = {
94
100
  functionName: sourceCode.getText(node.callee)
95
101
  },
96
102
  fix(fixer) {
97
- const newPrefix = prefixMap[node.arguments[1].value];
103
+ if (sourceCode.getCommentsInside(node).length) {
104
+ return null;
105
+ }
98
106
 
99
- if (+(newPrefix + node.arguments[0].value) !== parseInt(node.arguments[0].value, node.arguments[1].value)) {
107
+ const replacement = `${prefixMap[node.arguments[1].value]}${node.arguments[0].value}`;
108
+
109
+ if (+replacement !== parseInt(node.arguments[0].value, node.arguments[1].value)) {
100
110
 
101
111
  /*
102
112
  * If the newly-produced literal would be invalid, (e.g. 0b1234),
@@ -104,7 +114,29 @@ module.exports = {
104
114
  */
105
115
  return null;
106
116
  }
107
- return fixer.replaceText(node, prefixMap[node.arguments[1].value] + node.arguments[0].value);
117
+
118
+ const tokenBefore = sourceCode.getTokenBefore(node),
119
+ tokenAfter = sourceCode.getTokenAfter(node);
120
+ let prefix = "",
121
+ suffix = "";
122
+
123
+ if (
124
+ tokenBefore &&
125
+ tokenBefore.range[1] === node.range[0] &&
126
+ !astUtils.canTokensBeAdjacent(tokenBefore, replacement)
127
+ ) {
128
+ prefix = " ";
129
+ }
130
+
131
+ if (
132
+ tokenAfter &&
133
+ node.range[1] === tokenAfter.range[0] &&
134
+ !astUtils.canTokensBeAdjacent(replacement, tokenAfter)
135
+ ) {
136
+ suffix = " ";
137
+ }
138
+
139
+ return fixer.replaceText(node, `${prefix}${replacement}${suffix}`);
108
140
  }
109
141
  });
110
142
  }
@@ -18,7 +18,7 @@ const ANY_SPACE = /\s/u;
18
18
 
19
19
  /**
20
20
  * Helper that checks if the Object.assign call has array spread
21
- * @param {ASTNode} node - The node that the rule warns on
21
+ * @param {ASTNode} node The node that the rule warns on
22
22
  * @returns {boolean} - Returns true if the Object.assign call has array spread
23
23
  */
24
24
  function hasArraySpread(node) {
@@ -28,8 +28,8 @@ function hasArraySpread(node) {
28
28
  /**
29
29
  * Helper that checks if the node needs parentheses to be valid JS.
30
30
  * The default is to wrap the node in parentheses to avoid parsing errors.
31
- * @param {ASTNode} node - The node that the rule warns on
32
- * @param {Object} sourceCode - in context sourcecode object
31
+ * @param {ASTNode} node The node that the rule warns on
32
+ * @param {Object} sourceCode in context sourcecode object
33
33
  * @returns {boolean} - Returns true if the node needs parentheses
34
34
  */
35
35
  function needsParens(node, sourceCode) {
@@ -51,8 +51,8 @@ function needsParens(node, sourceCode) {
51
51
 
52
52
  /**
53
53
  * Determines if an argument needs parentheses. The default is to not add parens.
54
- * @param {ASTNode} node - The node to be checked.
55
- * @param {Object} sourceCode - in context sourcecode object
54
+ * @param {ASTNode} node The node to be checked.
55
+ * @param {Object} sourceCode in context sourcecode object
56
56
  * @returns {boolean} True if the node needs parentheses
57
57
  */
58
58
  function argNeedsParens(node, sourceCode) {
@@ -142,8 +142,8 @@ function getEndWithSpaces(token, sourceCode) {
142
142
 
143
143
  /**
144
144
  * Autofixes the Object.assign call to use an object spread instead.
145
- * @param {ASTNode|null} node - The node that the rule warns on, i.e. the Object.assign call
146
- * @param {string} sourceCode - sourceCode of the Object.assign call
145
+ * @param {ASTNode|null} node The node that the rule warns on, i.e. the Object.assign call
146
+ * @param {string} sourceCode sourceCode of the Object.assign call
147
147
  * @returns {Function} autofixer - replaces the Object.assign with a spread object.
148
148
  */
149
149
  function defineFixer(node, sourceCode) {
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @fileoverview Rule to disallow use of the `RegExp` constructor in favor of regular expression literals
3
+ * @author Milos Djermanovic
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const astUtils = require("./utils/ast-utils");
13
+ const { CALL, CONSTRUCT, ReferenceTracker, findVariable } = require("eslint-utils");
14
+
15
+ //------------------------------------------------------------------------------
16
+ // Helpers
17
+ //------------------------------------------------------------------------------
18
+
19
+ /**
20
+ * Determines whether the given node is a string literal.
21
+ * @param {ASTNode} node Node to check.
22
+ * @returns {boolean} True if the node is a string literal.
23
+ */
24
+ function isStringLiteral(node) {
25
+ return node.type === "Literal" && typeof node.value === "string";
26
+ }
27
+
28
+ /**
29
+ * Determines whether the given node is a template literal without expressions.
30
+ * @param {ASTNode} node Node to check.
31
+ * @returns {boolean} True if the node is a template literal without expressions.
32
+ */
33
+ function isStaticTemplateLiteral(node) {
34
+ return node.type === "TemplateLiteral" && node.expressions.length === 0;
35
+ }
36
+
37
+
38
+ //------------------------------------------------------------------------------
39
+ // Rule Definition
40
+ //------------------------------------------------------------------------------
41
+
42
+ module.exports = {
43
+ meta: {
44
+ type: "suggestion",
45
+
46
+ docs: {
47
+ description: "disallow use of the `RegExp` constructor in favor of regular expression literals",
48
+ category: "Best Practices",
49
+ recommended: false,
50
+ url: "https://eslint.org/docs/rules/prefer-regex-literals"
51
+ },
52
+
53
+ schema: [],
54
+
55
+ messages: {
56
+ unexpectedRegExp: "Use a regular expression literal instead of the 'RegExp' constructor."
57
+ }
58
+ },
59
+
60
+ create(context) {
61
+
62
+ /**
63
+ * Determines whether the given identifier node is a reference to a global variable.
64
+ * @param {ASTNode} node `Identifier` node to check.
65
+ * @returns {boolean} True if the identifier is a reference to a global variable.
66
+ */
67
+ function isGlobalReference(node) {
68
+ const scope = context.getScope();
69
+ const variable = findVariable(scope, node);
70
+
71
+ return variable !== null && variable.scope.type === "global" && variable.defs.length === 0;
72
+ }
73
+
74
+ /**
75
+ * Determines whether the given node is a String.raw`` tagged template expression
76
+ * with a static template literal.
77
+ * @param {ASTNode} node Node to check.
78
+ * @returns {boolean} True if the node is String.raw`` with a static template.
79
+ */
80
+ function isStringRawTaggedStaticTemplateLiteral(node) {
81
+ return node.type === "TaggedTemplateExpression" &&
82
+ node.tag.type === "MemberExpression" &&
83
+ node.tag.object.type === "Identifier" &&
84
+ node.tag.object.name === "String" &&
85
+ isGlobalReference(node.tag.object) &&
86
+ astUtils.getStaticPropertyName(node.tag) === "raw" &&
87
+ isStaticTemplateLiteral(node.quasi);
88
+ }
89
+
90
+ /**
91
+ * Determines whether the given node is considered to be a static string by the logic of this rule.
92
+ * @param {ASTNode} node Node to check.
93
+ * @returns {boolean} True if the node is a static string.
94
+ */
95
+ function isStaticString(node) {
96
+ return isStringLiteral(node) ||
97
+ isStaticTemplateLiteral(node) ||
98
+ isStringRawTaggedStaticTemplateLiteral(node);
99
+ }
100
+
101
+ return {
102
+ Program() {
103
+ const scope = context.getScope();
104
+ const tracker = new ReferenceTracker(scope);
105
+ const traceMap = {
106
+ RegExp: {
107
+ [CALL]: true,
108
+ [CONSTRUCT]: true
109
+ }
110
+ };
111
+
112
+ for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
113
+ const args = node.arguments;
114
+
115
+ if (
116
+ (args.length === 1 || args.length === 2) &&
117
+ args.every(isStaticString)
118
+ ) {
119
+ context.report({ node, messageId: "unexpectedRegExp" });
120
+ }
121
+ }
122
+ }
123
+ };
124
+ }
125
+ };
@@ -11,7 +11,7 @@
11
11
 
12
12
  /**
13
13
  * Gets the variable object of `arguments` which is defined implicitly.
14
- * @param {eslint-scope.Scope} scope - A scope to get.
14
+ * @param {eslint-scope.Scope} scope A scope to get.
15
15
  * @returns {eslint-scope.Variable} The found variable object.
16
16
  */
17
17
  function getVariableOfArguments(scope) {
@@ -41,8 +41,7 @@ function getVariableOfArguments(scope) {
41
41
  * - arguments[i] .... true // computed member access
42
42
  * - arguments[0] .... true // computed member access
43
43
  * - arguments.length .... false // normal member access
44
- *
45
- * @param {eslint-scope.Reference} reference - The reference to check.
44
+ * @param {eslint-scope.Reference} reference The reference to check.
46
45
  * @returns {boolean} `true` if the reference is not normal member access.
47
46
  */
48
47
  function isNotNormalMemberAccess(reference) {
@@ -78,8 +77,7 @@ module.exports = {
78
77
 
79
78
  /**
80
79
  * Reports a given reference.
81
- *
82
- * @param {eslint-scope.Reference} reference - A reference to report.
80
+ * @param {eslint-scope.Reference} reference A reference to report.
83
81
  * @returns {void}
84
82
  */
85
83
  function report(reference) {
@@ -92,7 +90,6 @@ module.exports = {
92
90
 
93
91
  /**
94
92
  * Reports references of the implicit `arguments` variable if exist.
95
- *
96
93
  * @returns {void}
97
94
  */
98
95
  function checkForArguments() {
@@ -13,7 +13,7 @@ const astUtils = require("./utils/ast-utils");
13
13
 
14
14
  /**
15
15
  * Checks whether or not a node is a `.apply()` for variadic.
16
- * @param {ASTNode} node - A CallExpression node to check.
16
+ * @param {ASTNode} node A CallExpression node to check.
17
17
  * @returns {boolean} Whether or not the node is a `.apply()` for variadic.
18
18
  */
19
19
  function isVariadicApplyCalling(node) {
@@ -31,9 +31,9 @@ function isVariadicApplyCalling(node) {
31
31
 
32
32
  /**
33
33
  * Checks whether or not `thisArg` is not changed by `.apply()`.
34
- * @param {ASTNode|null} expectedThis - The node that is the owner of the applied function.
35
- * @param {ASTNode} thisArg - The node that is given to the first argument of the `.apply()`.
36
- * @param {RuleContext} context - The ESLint rule context object.
34
+ * @param {ASTNode|null} expectedThis The node that is the owner of the applied function.
35
+ * @param {ASTNode} thisArg The node that is given to the first argument of the `.apply()`.
36
+ * @param {RuleContext} context The ESLint rule context object.
37
37
  * @returns {boolean} Whether or not `thisArg` is not changed by `.apply()`.
38
38
  */
39
39
  function isValidThisArg(expectedThis, thisArg, context) {