eslint 6.2.2 → 6.5.1

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 (63) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/README.md +9 -8
  3. package/bin/eslint.js +38 -12
  4. package/conf/config-schema.js +1 -0
  5. package/conf/default-cli-options.js +1 -1
  6. package/lib/cli-engine/cli-engine.js +2 -4
  7. package/lib/cli-engine/config-array/config-array.js +6 -0
  8. package/lib/cli-engine/config-array/extracted-config.js +6 -0
  9. package/lib/cli-engine/config-array/override-tester.js +2 -2
  10. package/lib/cli-engine/config-array-factory.js +2 -0
  11. package/lib/cli-engine/formatters/stylish.js +2 -1
  12. package/lib/cli-engine/ignored-paths.js +3 -3
  13. package/lib/cli-engine/lint-result-cache.js +0 -1
  14. package/lib/cli.js +13 -12
  15. package/lib/init/config-initializer.js +29 -0
  16. package/lib/init/config-rule.js +2 -2
  17. package/lib/init/npm-utils.js +9 -9
  18. package/lib/linter/apply-disable-directives.js +17 -9
  19. package/lib/linter/code-path-analysis/debug-helpers.js +1 -1
  20. package/lib/linter/linter.js +23 -4
  21. package/lib/options.js +7 -1
  22. package/lib/rule-tester/rule-tester.js +1 -2
  23. package/lib/rules/accessor-pairs.js +51 -11
  24. package/lib/rules/capitalized-comments.js +2 -2
  25. package/lib/rules/computed-property-spacing.js +18 -1
  26. package/lib/rules/default-param-last.js +61 -0
  27. package/lib/rules/eqeqeq.js +7 -19
  28. package/lib/rules/func-name-matching.js +1 -0
  29. package/lib/rules/function-paren-newline.js +2 -2
  30. package/lib/rules/indent-legacy.js +1 -1
  31. package/lib/rules/indent.js +44 -6
  32. package/lib/rules/index.js +3 -0
  33. package/lib/rules/new-parens.js +5 -1
  34. package/lib/rules/no-extra-bind.js +7 -1
  35. package/lib/rules/no-extra-boolean-cast.js +12 -2
  36. package/lib/rules/no-extra-label.js +9 -1
  37. package/lib/rules/no-extra-parens.js +23 -3
  38. package/lib/rules/no-import-assign.js +238 -0
  39. package/lib/rules/no-lone-blocks.js +6 -1
  40. package/lib/rules/no-obj-calls.js +29 -9
  41. package/lib/rules/no-octal-escape.js +14 -8
  42. package/lib/rules/no-regex-spaces.js +106 -45
  43. package/lib/rules/no-self-assign.js +17 -6
  44. package/lib/rules/no-sequences.js +2 -2
  45. package/lib/rules/no-undef-init.js +7 -1
  46. package/lib/rules/no-unsafe-negation.js +2 -10
  47. package/lib/rules/no-useless-rename.js +25 -13
  48. package/lib/rules/no-useless-return.js +3 -2
  49. package/lib/rules/object-curly-spacing.js +1 -1
  50. package/lib/rules/object-shorthand.js +35 -9
  51. package/lib/rules/prefer-named-capture-group.js +3 -15
  52. package/lib/rules/prefer-numeric-literals.js +4 -0
  53. package/lib/rules/prefer-regex-literals.js +125 -0
  54. package/lib/rules/quotes.js +6 -0
  55. package/lib/rules/space-before-function-paren.js +12 -1
  56. package/lib/rules/space-in-parens.js +77 -71
  57. package/lib/rules/use-isnan.js +67 -6
  58. package/lib/rules/yoda.js +11 -2
  59. package/lib/shared/logging.js +2 -0
  60. package/lib/shared/runtime-info.js +163 -0
  61. package/lib/shared/types.js +2 -0
  62. package/lib/source-code/source-code.js +3 -4
  63. package/package.json +3 -1
@@ -105,9 +105,9 @@ module.exports = {
105
105
  }
106
106
  }
107
107
 
108
- const child = sourceCode.getTokenAfter(node.expressions[0]);
108
+ const firstCommaToken = sourceCode.getTokenAfter(node.expressions[0], astUtils.isCommaToken);
109
109
 
110
- context.report({ node, loc: child.loc.start, message: "Unexpected use of comma operator." });
110
+ context.report({ node, loc: firstCommaToken.loc, message: "Unexpected use of comma operator." });
111
111
  }
112
112
  };
113
113
 
@@ -37,7 +37,8 @@ module.exports = {
37
37
  init = node.init && node.init.name,
38
38
  scope = context.getScope(),
39
39
  undefinedVar = astUtils.getVariableByName(scope, "undefined"),
40
- shadowed = undefinedVar && undefinedVar.defs.length > 0;
40
+ shadowed = undefinedVar && undefinedVar.defs.length > 0,
41
+ lastToken = sourceCode.getLastToken(node);
41
42
 
42
43
  if (init === "undefined" && node.parent.kind !== "const" && !shadowed) {
43
44
  context.report({
@@ -54,6 +55,11 @@ module.exports = {
54
55
  // Don't fix destructuring assignment to `undefined`.
55
56
  return null;
56
57
  }
58
+
59
+ if (sourceCode.commentsExistBetween(node.id, lastToken)) {
60
+ return null;
61
+ }
62
+
57
63
  return fixer.removeRange([node.id.range[1], node.range[1]]);
58
64
  }
59
65
  });
@@ -51,7 +51,7 @@ module.exports = {
51
51
  },
52
52
 
53
53
  schema: [],
54
- fixable: "code",
54
+ fixable: null,
55
55
  messages: {
56
56
  unexpected: "Unexpected negating the left operand of '{{operator}}' operator."
57
57
  }
@@ -70,15 +70,7 @@ module.exports = {
70
70
  node,
71
71
  loc: node.left.loc,
72
72
  messageId: "unexpected",
73
- data: { operator: node.operator },
74
-
75
- fix(fixer) {
76
- const negationToken = sourceCode.getFirstToken(node.left);
77
- const fixRange = [negationToken.range[1], node.range[1]];
78
- const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
79
-
80
- return fixer.replaceTextRange(fixRange, `(${text})`);
81
- }
73
+ data: { operator: node.operator }
82
74
  });
83
75
  }
84
76
  }
@@ -36,7 +36,8 @@ module.exports = {
36
36
  },
37
37
 
38
38
  create(context) {
39
- const options = context.options[0] || {},
39
+ const sourceCode = context.getSourceCode(),
40
+ options = context.options[0] || {},
40
41
  ignoreDestructuring = options.ignoreDestructuring === true,
41
42
  ignoreImport = options.ignoreImport === true,
42
43
  ignoreExport = options.ignoreExport === true;
@@ -64,10 +65,18 @@ module.exports = {
64
65
  type
65
66
  },
66
67
  fix(fixer) {
68
+ if (sourceCode.commentsExistBetween(initial, result)) {
69
+ return null;
70
+ }
71
+
72
+ const replacementText = result.type === "AssignmentPattern"
73
+ ? sourceCode.getText(result)
74
+ : name;
75
+
67
76
  return fixer.replaceTextRange([
68
77
  initial.range[0],
69
78
  result.range[1]
70
- ], name);
79
+ ], replacementText);
71
80
  }
72
81
  });
73
82
  }
@@ -82,26 +91,29 @@ module.exports = {
82
91
  return;
83
92
  }
84
93
 
85
- const properties = node.properties;
94
+ for (const property of node.properties) {
86
95
 
87
- for (let i = 0; i < properties.length; i++) {
88
- if (properties[i].shorthand) {
96
+ /*
97
+ * TODO: Remove after babel-eslint removes ExperimentalRestProperty
98
+ * https://github.com/eslint/eslint/issues/12335
99
+ */
100
+ if (property.type === "ExperimentalRestProperty") {
89
101
  continue;
90
102
  }
91
103
 
92
104
  /**
93
- * If an ObjectPattern property is computed, we have no idea
94
- * if a rename is useless or not. If an ObjectPattern property
95
- * lacks a key, it is likely an ExperimentalRestProperty and
96
- * so there is no "renaming" occurring here.
105
+ * Properties using shorthand syntax and rest elements can not be renamed.
106
+ * If the property is computed, we have no idea if a rename is useless or not.
97
107
  */
98
- if (properties[i].computed || !properties[i].key) {
108
+ if (property.shorthand || property.type === "RestElement" || property.computed) {
99
109
  continue;
100
110
  }
101
111
 
102
- if (properties[i].key.type === "Identifier" && properties[i].key.name === properties[i].value.name ||
103
- properties[i].key.type === "Literal" && properties[i].key.value === properties[i].value.name) {
104
- reportError(properties[i], properties[i].key, properties[i].value, "Destructuring assignment");
112
+ const key = (property.key.type === "Identifier" && property.key.name) || (property.key.type === "Literal" && property.key.value);
113
+ const renamedKey = property.value.type === "AssignmentPattern" ? property.value.left.name : property.value.name;
114
+
115
+ if (key === renamedKey) {
116
+ reportError(property, property.key, property.value, "Destructuring assignment");
105
117
  }
106
118
  }
107
119
  }
@@ -82,6 +82,7 @@ module.exports = {
82
82
  create(context) {
83
83
  const segmentInfoMap = new WeakMap();
84
84
  const usedUnreachableSegments = new WeakSet();
85
+ const sourceCode = context.getSourceCode();
85
86
  let scopeInfo = null;
86
87
 
87
88
  /**
@@ -216,7 +217,7 @@ module.exports = {
216
217
  loc: node.loc,
217
218
  message: "Unnecessary return statement.",
218
219
  fix(fixer) {
219
- if (isRemovable(node)) {
220
+ if (isRemovable(node) && !sourceCode.getCommentsInside(node).length) {
220
221
 
221
222
  /*
222
223
  * Extend the replacement range to include the
@@ -224,7 +225,7 @@ module.exports = {
224
225
  * no-else-return.
225
226
  * https://github.com/eslint/eslint/issues/8026
226
227
  */
227
- return new FixTracker(fixer, context.getSourceCode())
228
+ return new FixTracker(fixer, sourceCode)
228
229
  .retainEnclosingFunction(node)
229
230
  .remove(node);
230
231
  }
@@ -167,7 +167,7 @@ module.exports = {
167
167
  if (options.spaced && !firstSpaced) {
168
168
  reportRequiredBeginningSpace(node, first);
169
169
  }
170
- if (!options.spaced && firstSpaced) {
170
+ if (!options.spaced && firstSpaced && second.type !== "Line") {
171
171
  reportNoBeginningSpace(node, first);
172
172
  }
173
173
  }
@@ -244,6 +244,7 @@ module.exports = {
244
244
  const keyText = sourceCode.text.slice(firstKeyToken.range[0], lastKeyToken.range[1]);
245
245
  let keyPrefix = "";
246
246
 
247
+ // key: /* */ () => {}
247
248
  if (sourceCode.commentsExistBetween(lastKeyToken, node.value)) {
248
249
  return null;
249
250
  }
@@ -255,24 +256,49 @@ module.exports = {
255
256
  keyPrefix += "*";
256
257
  }
257
258
 
259
+ const fixRange = [firstKeyToken.range[0], node.range[1]];
260
+ const methodPrefix = keyPrefix + keyText;
261
+
258
262
  if (node.value.type === "FunctionExpression") {
259
263
  const functionToken = sourceCode.getTokens(node.value).find(token => token.type === "Keyword" && token.value === "function");
260
264
  const tokenBeforeParams = node.value.generator ? sourceCode.getTokenAfter(functionToken) : functionToken;
261
265
 
262
266
  return fixer.replaceTextRange(
263
- [firstKeyToken.range[0], node.range[1]],
264
- keyPrefix + keyText + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1])
267
+ fixRange,
268
+ methodPrefix + sourceCode.text.slice(tokenBeforeParams.range[1], node.value.range[1])
265
269
  );
266
270
  }
267
- const arrowToken = sourceCode.getTokenBefore(node.value.body, { filter: token => token.value === "=>" });
268
- const tokenBeforeArrow = sourceCode.getTokenBefore(arrowToken);
269
- const hasParensAroundParameters = tokenBeforeArrow.type === "Punctuator" && tokenBeforeArrow.value === ")";
270
- const oldParamText = sourceCode.text.slice(sourceCode.getFirstToken(node.value, node.value.async ? 1 : 0).range[0], tokenBeforeArrow.range[1]);
271
- const newParamText = hasParensAroundParameters ? oldParamText : `(${oldParamText})`;
271
+
272
+ const arrowToken = sourceCode.getTokenBefore(node.value.body, astUtils.isArrowToken);
273
+ const fnBody = sourceCode.text.slice(arrowToken.range[1], node.value.range[1]);
274
+
275
+ let shouldAddParensAroundParameters = false;
276
+ let tokenBeforeParams;
277
+
278
+ if (node.value.params.length === 0) {
279
+ tokenBeforeParams = sourceCode.getFirstToken(node.value, astUtils.isOpeningParenToken);
280
+ } else {
281
+ tokenBeforeParams = sourceCode.getTokenBefore(node.value.params[0]);
282
+ }
283
+
284
+ if (node.value.params.length === 1) {
285
+ const hasParen = astUtils.isOpeningParenToken(tokenBeforeParams);
286
+ const isTokenOutsideNode = tokenBeforeParams.range[0] < node.range[0];
287
+
288
+ shouldAddParensAroundParameters = !hasParen || isTokenOutsideNode;
289
+ }
290
+
291
+ const sliceStart = shouldAddParensAroundParameters
292
+ ? node.value.params[0].range[0]
293
+ : tokenBeforeParams.range[0];
294
+ const sliceEnd = sourceCode.getTokenBefore(arrowToken).range[1];
295
+
296
+ const oldParamText = sourceCode.text.slice(sliceStart, sliceEnd);
297
+ const newParamText = shouldAddParensAroundParameters ? `(${oldParamText})` : oldParamText;
272
298
 
273
299
  return fixer.replaceTextRange(
274
- [firstKeyToken.range[0], node.range[1]],
275
- keyPrefix + keyText + newParamText + sourceCode.text.slice(arrowToken.range[1], node.value.range[1])
300
+ fixRange,
301
+ methodPrefix + newParamText + fnBody
276
302
  );
277
303
 
278
304
  }
@@ -50,16 +50,16 @@ module.exports = {
50
50
  /**
51
51
  * Function to check regular expression.
52
52
  *
53
- * @param {string} regex The regular expression to be check.
53
+ * @param {string} pattern The regular expression pattern to be check.
54
54
  * @param {ASTNode} node AST node which contains regular expression.
55
55
  * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
56
56
  * @returns {void}
57
57
  */
58
- function checkRegex(regex, node, uFlag) {
58
+ function checkRegex(pattern, node, uFlag) {
59
59
  let ast;
60
60
 
61
61
  try {
62
- ast = parser.parsePattern(regex, 0, regex.length, uFlag);
62
+ ast = parser.parsePattern(pattern, 0, pattern.length, uFlag);
63
63
  } catch (_) {
64
64
 
65
65
  // ignore regex syntax errors
@@ -69,21 +69,9 @@ module.exports = {
69
69
  regexpp.visitRegExpAST(ast, {
70
70
  onCapturingGroupEnter(group) {
71
71
  if (!group.name) {
72
- const locNode = node.type === "Literal" ? node : node.arguments[0];
73
-
74
72
  context.report({
75
73
  node,
76
74
  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
75
  data: {
88
76
  group: group.raw
89
77
  }
@@ -96,6 +96,10 @@ module.exports = {
96
96
  fix(fixer) {
97
97
  const newPrefix = prefixMap[node.arguments[1].value];
98
98
 
99
+ if (sourceCode.getCommentsInside(node).length) {
100
+ return null;
101
+ }
102
+
99
103
  if (+(newPrefix + node.arguments[0].value) !== parseInt(node.arguments[0].value, node.arguments[1].value)) {
100
104
 
101
105
  /*
@@ -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
+ };
@@ -279,6 +279,12 @@ module.exports = {
279
279
  description: settings.description
280
280
  },
281
281
  fix(fixer) {
282
+ if (quoteOption === "backtick" && astUtils.hasOctalEscapeSequence(rawVal)) {
283
+
284
+ // An octal escape sequence in a template literal would produce syntax error, even in non-strict mode.
285
+ return null;
286
+ }
287
+
282
288
  return fixer.replaceText(node, settings.convert(node.raw));
283
289
  }
284
290
  });
@@ -124,7 +124,18 @@ module.exports = {
124
124
  node,
125
125
  loc: leftToken.loc.end,
126
126
  message: "Unexpected space before function parentheses.",
127
- fix: fixer => fixer.removeRange([leftToken.range[1], rightToken.range[0]])
127
+ fix(fixer) {
128
+ const comments = sourceCode.getCommentsBefore(rightToken);
129
+
130
+ // Don't fix anything if there's a single line comment between the left and the right token
131
+ if (comments.some(comment => comment.type === "Line")) {
132
+ return null;
133
+ }
134
+ return fixer.replaceTextRange(
135
+ [leftToken.range[1], rightToken.range[0]],
136
+ comments.reduce((text, comment) => text + sourceCode.getText(comment), "")
137
+ );
138
+ }
128
139
  });
129
140
  } else if (!hasSpacing && functionConfig === "always") {
130
141
  context.report({