eslint 6.0.0 → 6.2.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 (47) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +3 -5
  3. package/bin/eslint.js +4 -1
  4. package/conf/config-schema.js +1 -0
  5. package/conf/environments.js +72 -15
  6. package/lib/cli-engine/cascading-config-array-factory.js +15 -3
  7. package/lib/cli-engine/cli-engine.js +13 -3
  8. package/lib/cli-engine/config-array/config-array.js +7 -0
  9. package/lib/cli-engine/config-array/extracted-config.js +16 -1
  10. package/lib/cli-engine/config-array-factory.js +9 -6
  11. package/lib/cli-engine/file-enumerator.js +5 -13
  12. package/lib/init/config-initializer.js +19 -9
  13. package/lib/init/npm-utils.js +2 -2
  14. package/lib/linter/code-path-analysis/code-path-analyzer.js +1 -0
  15. package/lib/linter/linter.js +49 -16
  16. package/lib/rule-tester/rule-tester.js +1 -1
  17. package/lib/rules/accessor-pairs.js +195 -35
  18. package/lib/rules/arrow-body-style.js +2 -2
  19. package/lib/rules/class-methods-use-this.js +10 -3
  20. package/lib/rules/dot-location.js +21 -17
  21. package/lib/rules/dot-notation.js +6 -2
  22. package/lib/rules/func-call-spacing.js +30 -20
  23. package/lib/rules/func-names.js +4 -0
  24. package/lib/rules/function-call-argument-newline.js +120 -0
  25. package/lib/rules/function-paren-newline.js +34 -22
  26. package/lib/rules/indent.js +13 -2
  27. package/lib/rules/index.js +1 -0
  28. package/lib/rules/new-cap.js +2 -1
  29. package/lib/rules/no-dupe-keys.js +1 -1
  30. package/lib/rules/no-duplicate-case.js +10 -8
  31. package/lib/rules/no-extra-bind.js +1 -0
  32. package/lib/rules/no-extra-boolean-cast.js +44 -5
  33. package/lib/rules/no-extra-parens.js +295 -39
  34. package/lib/rules/no-mixed-operators.js +48 -13
  35. package/lib/rules/no-param-reassign.js +12 -1
  36. package/lib/rules/no-restricted-syntax.js +2 -2
  37. package/lib/rules/no-unused-vars.js +1 -1
  38. package/lib/rules/prefer-const.js +9 -3
  39. package/lib/rules/prefer-template.js +1 -10
  40. package/lib/rules/sort-keys.js +11 -3
  41. package/lib/rules/utils/ast-utils.js +45 -3
  42. package/lib/rules/yoda.js +1 -1
  43. package/lib/{cli-engine → shared}/naming.js +0 -0
  44. package/lib/shared/types.js +2 -0
  45. package/messages/extend-config-missing.txt +2 -0
  46. package/messages/print-config-with-directory-path.txt +2 -0
  47. package/package.json +22 -21
@@ -69,6 +69,8 @@ module.exports = {
69
69
 
70
70
  create(context) {
71
71
 
72
+ const sourceCode = context.getSourceCode();
73
+
72
74
  /**
73
75
  * Returns the config option for the given node.
74
76
  * @param {ASTNode} node - A node to get the config for.
@@ -130,6 +132,7 @@ module.exports = {
130
132
  context.report({
131
133
  node,
132
134
  messageId: "unnamed",
135
+ loc: astUtils.getFunctionHeadLoc(node, sourceCode),
133
136
  data: { name: astUtils.getFunctionNameWithKind(node) }
134
137
  });
135
138
  }
@@ -143,6 +146,7 @@ module.exports = {
143
146
  context.report({
144
147
  node,
145
148
  messageId: "named",
149
+ loc: astUtils.getFunctionHeadLoc(node, sourceCode),
146
150
  data: { name: astUtils.getFunctionNameWithKind(node) }
147
151
  });
148
152
  }
@@ -0,0 +1,120 @@
1
+ /**
2
+ * @fileoverview Rule to enforce line breaks between arguments of a function call
3
+ * @author Alexey Gonchar <https://github.com/finico>
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //------------------------------------------------------------------------------
9
+ // Rule Definition
10
+ //------------------------------------------------------------------------------
11
+
12
+ module.exports = {
13
+ meta: {
14
+ type: "layout",
15
+
16
+ docs: {
17
+ description: "enforce line breaks between arguments of a function call",
18
+ category: "Stylistic Issues",
19
+ recommended: false,
20
+ url: "https://eslint.org/docs/rules/function-call-argument-newline"
21
+ },
22
+
23
+ fixable: "whitespace",
24
+
25
+ schema: [
26
+ {
27
+ enum: ["always", "never", "consistent"]
28
+ }
29
+ ],
30
+
31
+ messages: {
32
+ unexpectedLineBreak: "There should be no line break here.",
33
+ missingLineBreak: "There should be a line break after this argument."
34
+ }
35
+ },
36
+
37
+ create(context) {
38
+ const sourceCode = context.getSourceCode();
39
+
40
+ const checkers = {
41
+ unexpected: {
42
+ messageId: "unexpectedLineBreak",
43
+ check: (prevToken, currentToken) => prevToken.loc.start.line !== currentToken.loc.start.line,
44
+ createFix: (token, tokenBefore) => fixer =>
45
+ fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " ")
46
+ },
47
+ missing: {
48
+ messageId: "missingLineBreak",
49
+ check: (prevToken, currentToken) => prevToken.loc.start.line === currentToken.loc.start.line,
50
+ createFix: (token, tokenBefore) => fixer =>
51
+ fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n")
52
+ }
53
+ };
54
+
55
+ /**
56
+ * Check all arguments for line breaks in the CallExpression
57
+ * @param {CallExpression} node node to evaluate
58
+ * @param {{ messageId: string, check: Function }} checker selected checker
59
+ * @returns {void}
60
+ * @private
61
+ */
62
+ function checkArguments(node, checker) {
63
+ for (let i = 1; i < node.arguments.length; i++) {
64
+ const prevArgToken = sourceCode.getFirstToken(node.arguments[i - 1]);
65
+ const currentArgToken = sourceCode.getFirstToken(node.arguments[i]);
66
+
67
+ if (checker.check(prevArgToken, currentArgToken)) {
68
+ const tokenBefore = sourceCode.getTokenBefore(
69
+ currentArgToken,
70
+ { includeComments: true }
71
+ );
72
+
73
+ context.report({
74
+ node,
75
+ loc: {
76
+ start: tokenBefore.loc.end,
77
+ end: currentArgToken.loc.start
78
+ },
79
+ messageId: checker.messageId,
80
+ fix: checker.createFix(currentArgToken, tokenBefore)
81
+ });
82
+ }
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Check if open space is present in a function name
88
+ * @param {CallExpression} node node to evaluate
89
+ * @returns {void}
90
+ * @private
91
+ */
92
+ function check(node) {
93
+ if (node.arguments.length < 2) {
94
+ return;
95
+ }
96
+
97
+ const option = context.options[0] || "always";
98
+
99
+ if (option === "never") {
100
+ checkArguments(node, checkers.unexpected);
101
+ } else if (option === "always") {
102
+ checkArguments(node, checkers.missing);
103
+ } else if (option === "consistent") {
104
+ const firstArgToken = sourceCode.getFirstToken(node.arguments[0]);
105
+ const secondArgToken = sourceCode.getFirstToken(node.arguments[1]);
106
+
107
+ if (firstArgToken.loc.start.line === secondArgToken.loc.start.line) {
108
+ checkArguments(node, checkers.unexpected);
109
+ } else {
110
+ checkArguments(node, checkers.missing);
111
+ }
112
+ }
113
+ }
114
+
115
+ return {
116
+ CallExpression: check,
117
+ NewExpression: check
118
+ };
119
+ }
120
+ };
@@ -232,25 +232,15 @@ module.exports = {
232
232
  };
233
233
  }
234
234
 
235
- default:
236
- throw new TypeError(`unexpected node with type ${node.type}`);
237
- }
238
- }
239
-
240
- /**
241
- * Validates the parentheses for a node
242
- * @param {ASTNode} node The node with parens
243
- * @returns {void}
244
- */
245
- function validateNode(node) {
246
- const parens = getParenTokens(node);
247
-
248
- if (parens) {
249
- validateParens(parens, astUtils.isFunction(node) ? node.params : node.arguments);
235
+ case "ImportExpression": {
236
+ const leftParen = sourceCode.getFirstToken(node, 1);
237
+ const rightParen = sourceCode.getLastToken(node);
250
238
 
251
- if (multilineArgumentsOption) {
252
- validateArguments(parens, astUtils.isFunction(node) ? node.params : node.arguments);
239
+ return { leftParen, rightParen };
253
240
  }
241
+
242
+ default:
243
+ throw new TypeError(`unexpected node with type ${node.type}`);
254
244
  }
255
245
  }
256
246
 
@@ -259,11 +249,33 @@ module.exports = {
259
249
  //----------------------------------------------------------------------
260
250
 
261
251
  return {
262
- ArrowFunctionExpression: validateNode,
263
- CallExpression: validateNode,
264
- FunctionDeclaration: validateNode,
265
- FunctionExpression: validateNode,
266
- NewExpression: validateNode
252
+ [[
253
+ "ArrowFunctionExpression",
254
+ "CallExpression",
255
+ "FunctionDeclaration",
256
+ "FunctionExpression",
257
+ "ImportExpression",
258
+ "NewExpression"
259
+ ]](node) {
260
+ const parens = getParenTokens(node);
261
+ let params;
262
+
263
+ if (node.type === "ImportExpression") {
264
+ params = [node.source];
265
+ } else if (astUtils.isFunction(node)) {
266
+ params = node.params;
267
+ } else {
268
+ params = node.arguments;
269
+ }
270
+
271
+ if (parens) {
272
+ validateParens(parens, params);
273
+
274
+ if (multilineArgumentsOption) {
275
+ validateArguments(parens, params);
276
+ }
277
+ }
278
+ }
267
279
  };
268
280
  }
269
281
  };
@@ -99,7 +99,8 @@ const KNOWN_NODES = new Set([
99
99
  "ImportDeclaration",
100
100
  "ImportSpecifier",
101
101
  "ImportDefaultSpecifier",
102
- "ImportNamespaceSpecifier"
102
+ "ImportNamespaceSpecifier",
103
+ "ImportExpression"
103
104
  ]);
104
105
 
105
106
  /*
@@ -1109,7 +1110,6 @@ module.exports = {
1109
1110
 
1110
1111
  CallExpression: addFunctionCallIndent,
1111
1112
 
1112
-
1113
1113
  "ClassDeclaration[superClass], ClassExpression[superClass]"(node) {
1114
1114
  const classToken = sourceCode.getFirstToken(node);
1115
1115
  const extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken);
@@ -1236,6 +1236,17 @@ module.exports = {
1236
1236
  }
1237
1237
  },
1238
1238
 
1239
+ ImportExpression(node) {
1240
+ const openingParen = sourceCode.getFirstToken(node, 1);
1241
+ const closingParen = sourceCode.getLastToken(node);
1242
+
1243
+ parameterParens.add(openingParen);
1244
+ parameterParens.add(closingParen);
1245
+ offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);
1246
+
1247
+ addElementListIndent([node.source], openingParen, closingParen, options.CallExpression.arguments);
1248
+ },
1249
+
1239
1250
  "MemberExpression, JSXMemberExpression, MetaProperty"(node) {
1240
1251
  const object = node.type === "MetaProperty" ? node.meta : node.object;
1241
1252
  const firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken);
@@ -46,6 +46,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
46
46
  "func-name-matching": () => require("./func-name-matching"),
47
47
  "func-names": () => require("./func-names"),
48
48
  "func-style": () => require("./func-style"),
49
+ "function-call-argument-newline": () => require("./function-call-argument-newline"),
49
50
  "function-paren-newline": () => require("./function-paren-newline"),
50
51
  "generator-star-spacing": () => require("./generator-star-spacing"),
51
52
  "getter-return": () => require("./getter-return"),
@@ -23,7 +23,8 @@ const CAPS_ALLOWED = [
23
23
  "Object",
24
24
  "RegExp",
25
25
  "String",
26
- "Symbol"
26
+ "Symbol",
27
+ "BigInt"
27
28
  ];
28
29
 
29
30
  /**
@@ -120,7 +120,7 @@ module.exports = {
120
120
  }
121
121
 
122
122
  // Skip if the name is not static.
123
- if (!name) {
123
+ if (name === null) {
124
124
  return;
125
125
  }
126
126
 
@@ -33,17 +33,19 @@ module.exports = {
33
33
 
34
34
  return {
35
35
  SwitchStatement(node) {
36
- const mapping = {};
36
+ const previousKeys = new Set();
37
37
 
38
- node.cases.forEach(switchCase => {
39
- const key = sourceCode.getText(switchCase.test);
38
+ for (const switchCase of node.cases) {
39
+ if (switchCase.test) {
40
+ const key = sourceCode.getText(switchCase.test);
40
41
 
41
- if (mapping[key]) {
42
- context.report({ node: switchCase, messageId: "unexpected" });
43
- } else {
44
- mapping[key] = switchCase;
42
+ if (previousKeys.has(key)) {
43
+ context.report({ node: switchCase, messageId: "unexpected" });
44
+ } else {
45
+ previousKeys.add(key);
46
+ }
45
47
  }
46
- });
48
+ }
47
49
  }
48
50
  };
49
51
  }
@@ -98,6 +98,7 @@ module.exports = {
98
98
  grandparent.type === "CallExpression" &&
99
99
  grandparent.callee === parent &&
100
100
  grandparent.arguments.length === 1 &&
101
+ grandparent.arguments[0].type !== "SpreadElement" &&
101
102
  parent.type === "MemberExpression" &&
102
103
  parent.object === node &&
103
104
  astUtils.getStaticPropertyName(parent) === "bind"
@@ -50,8 +50,8 @@ module.exports = {
50
50
  /**
51
51
  * Check if a node is in a context where its value would be coerced to a boolean at runtime.
52
52
  *
53
- * @param {Object} node The node
54
- * @param {Object} parent Its parent
53
+ * @param {ASTNode} node The node
54
+ * @param {ASTNode} parent Its parent
55
55
  * @returns {boolean} If it is in a boolean context
56
56
  */
57
57
  function isInBooleanContext(node, parent) {
@@ -65,6 +65,15 @@ module.exports = {
65
65
  );
66
66
  }
67
67
 
68
+ /**
69
+ * Check if a node has comments inside.
70
+ *
71
+ * @param {ASTNode} node The node to check.
72
+ * @returns {boolean} `true` if it has comments inside.
73
+ */
74
+ function hasCommentsInside(node) {
75
+ return Boolean(sourceCode.getCommentsInside(node).length);
76
+ }
68
77
 
69
78
  return {
70
79
  UnaryExpression(node) {
@@ -89,7 +98,12 @@ module.exports = {
89
98
  context.report({
90
99
  node,
91
100
  messageId: "unexpectedNegation",
92
- fix: fixer => fixer.replaceText(parent, sourceCode.getText(node.argument))
101
+ fix: fixer => {
102
+ if (hasCommentsInside(parent)) {
103
+ return null;
104
+ }
105
+ return fixer.replaceText(parent, sourceCode.getText(node.argument));
106
+ }
93
107
  });
94
108
  }
95
109
  },
@@ -106,10 +120,35 @@ module.exports = {
106
120
  messageId: "unexpectedCall",
107
121
  fix: fixer => {
108
122
  if (!node.arguments.length) {
109
- return fixer.replaceText(parent, "true");
123
+ if (parent.type === "UnaryExpression" && parent.operator === "!") {
124
+
125
+ // !Boolean() -> true
126
+
127
+ if (hasCommentsInside(parent)) {
128
+ return null;
129
+ }
130
+
131
+ const replacement = "true";
132
+ let prefix = "";
133
+ const tokenBefore = sourceCode.getTokenBefore(parent);
134
+
135
+ if (tokenBefore && tokenBefore.range[1] === parent.range[0] &&
136
+ !astUtils.canTokensBeAdjacent(tokenBefore, replacement)) {
137
+ prefix = " ";
138
+ }
139
+
140
+ return fixer.replaceText(parent, prefix + replacement);
141
+ }
142
+
143
+ // Boolean() -> false
144
+ if (hasCommentsInside(node)) {
145
+ return null;
146
+ }
147
+ return fixer.replaceText(node, "false");
110
148
  }
111
149
 
112
- if (node.arguments.length > 1 || node.arguments[0].type === "SpreadElement") {
150
+ if (node.arguments.length > 1 || node.arguments[0].type === "SpreadElement" ||
151
+ hasCommentsInside(node)) {
113
152
  return null;
114
153
  }
115
154