eslint 6.1.0 → 6.3.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 (41) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +8 -4
  3. package/conf/config-schema.js +2 -0
  4. package/conf/default-cli-options.js +1 -1
  5. package/conf/environments.js +72 -15
  6. package/lib/cli-engine/config-array/config-array.js +13 -0
  7. package/lib/cli-engine/config-array/extracted-config.js +22 -1
  8. package/lib/cli-engine/config-array-factory.js +4 -0
  9. package/lib/cli-engine/formatters/stylish.js +2 -1
  10. package/lib/init/config-initializer.js +29 -0
  11. package/lib/init/npm-utils.js +10 -10
  12. package/lib/linter/apply-disable-directives.js +17 -9
  13. package/lib/linter/code-path-analysis/code-path-analyzer.js +1 -0
  14. package/lib/linter/linter.js +70 -17
  15. package/lib/options.js +1 -1
  16. package/lib/rules/accessor-pairs.js +195 -35
  17. package/lib/rules/class-methods-use-this.js +10 -3
  18. package/lib/rules/dot-notation.js +6 -2
  19. package/lib/rules/func-call-spacing.js +30 -20
  20. package/lib/rules/func-name-matching.js +1 -0
  21. package/lib/rules/func-names.js +4 -0
  22. package/lib/rules/function-call-argument-newline.js +120 -0
  23. package/lib/rules/function-paren-newline.js +36 -24
  24. package/lib/rules/indent.js +13 -2
  25. package/lib/rules/index.js +1 -0
  26. package/lib/rules/new-cap.js +2 -1
  27. package/lib/rules/no-dupe-keys.js +1 -1
  28. package/lib/rules/no-duplicate-case.js +10 -8
  29. package/lib/rules/no-extra-bind.js +1 -0
  30. package/lib/rules/no-extra-boolean-cast.js +54 -5
  31. package/lib/rules/no-extra-parens.js +62 -23
  32. package/lib/rules/no-mixed-operators.js +48 -13
  33. package/lib/rules/no-restricted-syntax.js +2 -2
  34. package/lib/rules/no-self-assign.js +11 -1
  35. package/lib/rules/no-unused-vars.js +1 -1
  36. package/lib/rules/prefer-template.js +1 -10
  37. package/lib/rules/sort-keys.js +11 -3
  38. package/lib/rules/utils/ast-utils.js +19 -2
  39. package/lib/rules/yoda.js +12 -3
  40. package/lib/shared/types.js +4 -0
  41. package/package.json +5 -5
@@ -20,12 +20,14 @@ const BITWISE_OPERATORS = ["&", "|", "^", "~", "<<", ">>", ">>>"];
20
20
  const COMPARISON_OPERATORS = ["==", "!=", "===", "!==", ">", ">=", "<", "<="];
21
21
  const LOGICAL_OPERATORS = ["&&", "||"];
22
22
  const RELATIONAL_OPERATORS = ["in", "instanceof"];
23
+ const TERNARY_OPERATOR = ["?:"];
23
24
  const ALL_OPERATORS = [].concat(
24
25
  ARITHMETIC_OPERATORS,
25
26
  BITWISE_OPERATORS,
26
27
  COMPARISON_OPERATORS,
27
28
  LOGICAL_OPERATORS,
28
- RELATIONAL_OPERATORS
29
+ RELATIONAL_OPERATORS,
30
+ TERNARY_OPERATOR
29
31
  );
30
32
  const DEFAULT_GROUPS = [
31
33
  ARITHMETIC_OPERATORS,
@@ -34,7 +36,7 @@ const DEFAULT_GROUPS = [
34
36
  LOGICAL_OPERATORS,
35
37
  RELATIONAL_OPERATORS
36
38
  ];
37
- const TARGET_NODE_TYPE = /^(?:Binary|Logical)Expression$/u;
39
+ const TARGET_NODE_TYPE = /^(?:Binary|Logical|Conditional)Expression$/u;
38
40
 
39
41
  /**
40
42
  * Normalizes options.
@@ -65,6 +67,18 @@ function includesBothInAGroup(groups, left, right) {
65
67
  return groups.some(group => group.indexOf(left) !== -1 && group.indexOf(right) !== -1);
66
68
  }
67
69
 
70
+ /**
71
+ * Checks whether the given node is a conditional expression and returns the test node else the left node.
72
+ *
73
+ * @param {ASTNode} node - A node which can be a BinaryExpression or a LogicalExpression node.
74
+ * This parent node can be BinaryExpression, LogicalExpression
75
+ * , or a ConditionalExpression node
76
+ * @returns {ASTNode} node the appropriate node(left or test).
77
+ */
78
+ function getChildNode(node) {
79
+ return node.type === "ConditionalExpression" ? node.test : node.left;
80
+ }
81
+
68
82
  //------------------------------------------------------------------------------
69
83
  // Rule Definition
70
84
  //------------------------------------------------------------------------------
@@ -121,7 +135,7 @@ module.exports = {
121
135
  const b = node.parent;
122
136
 
123
137
  return (
124
- !includesBothInAGroup(options.groups, a.operator, b.operator) ||
138
+ !includesBothInAGroup(options.groups, a.operator, b.type === "ConditionalExpression" ? "?:" : b.operator) ||
125
139
  (
126
140
  options.allowSamePrecedence &&
127
141
  astUtils.getPrecedence(a) === astUtils.getPrecedence(b)
@@ -139,12 +153,25 @@ module.exports = {
139
153
  * @returns {boolean} `true` if the node was mixed.
140
154
  */
141
155
  function isMixedWithParent(node) {
156
+
142
157
  return (
143
158
  node.operator !== node.parent.operator &&
144
159
  !astUtils.isParenthesised(sourceCode, node)
145
160
  );
146
161
  }
147
162
 
163
+ /**
164
+ * Checks whether the operator of a given node is mixed with a
165
+ * conditional expression.
166
+ *
167
+ * @param {ASTNode} node - A node to check. This is a conditional
168
+ * expression node
169
+ * @returns {boolean} `true` if the node was mixed.
170
+ */
171
+ function isMixedWithConditionalParent(node) {
172
+ return !astUtils.isParenthesised(sourceCode, node) && !astUtils.isParenthesised(sourceCode, node.test);
173
+ }
174
+
148
175
  /**
149
176
  * Gets the operator token of a given node.
150
177
  *
@@ -153,7 +180,7 @@ module.exports = {
153
180
  * @returns {Token} The operator token of the node.
154
181
  */
155
182
  function getOperatorToken(node) {
156
- return sourceCode.getTokenAfter(node.left, astUtils.isNotClosingParenToken);
183
+ return sourceCode.getTokenAfter(getChildNode(node), astUtils.isNotClosingParenToken);
157
184
  }
158
185
 
159
186
  /**
@@ -167,13 +194,13 @@ module.exports = {
167
194
  */
168
195
  function reportBothOperators(node) {
169
196
  const parent = node.parent;
170
- const left = (parent.left === node) ? node : parent;
171
- const right = (parent.left !== node) ? node : parent;
197
+ const left = (getChildNode(parent) === node) ? node : parent;
198
+ const right = (getChildNode(parent) !== node) ? node : parent;
172
199
  const message =
173
200
  "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'.";
174
201
  const data = {
175
- leftOperator: left.operator,
176
- rightOperator: right.operator
202
+ leftOperator: left.operator || "?:",
203
+ rightOperator: right.operator || "?:"
177
204
  };
178
205
 
179
206
  context.report({
@@ -198,17 +225,25 @@ module.exports = {
198
225
  * @returns {void}
199
226
  */
200
227
  function check(node) {
201
- if (TARGET_NODE_TYPE.test(node.parent.type) &&
202
- isMixedWithParent(node) &&
203
- !shouldIgnore(node)
204
- ) {
205
- reportBothOperators(node);
228
+ if (TARGET_NODE_TYPE.test(node.parent.type)) {
229
+ if (node.parent.type === "ConditionalExpression" && !shouldIgnore(node) && isMixedWithConditionalParent(node.parent)) {
230
+ reportBothOperators(node);
231
+ } else {
232
+ if (TARGET_NODE_TYPE.test(node.parent.type) &&
233
+ isMixedWithParent(node) &&
234
+ !shouldIgnore(node)
235
+ ) {
236
+ reportBothOperators(node);
237
+ }
238
+ }
206
239
  }
240
+
207
241
  }
208
242
 
209
243
  return {
210
244
  BinaryExpression: check,
211
245
  LogicalExpression: check
246
+
212
247
  };
213
248
  }
214
249
  };
@@ -21,7 +21,7 @@ module.exports = {
21
21
 
22
22
  schema: {
23
23
  type: "array",
24
- items: [{
24
+ items: {
25
25
  oneOf: [
26
26
  {
27
27
  type: "string"
@@ -36,7 +36,7 @@ module.exports = {
36
36
  additionalProperties: false
37
37
  }
38
38
  ]
39
- }],
39
+ },
40
40
  uniqueItems: true,
41
41
  minItems: 0
42
42
  }
@@ -94,9 +94,19 @@ function eachSelfAssignment(left, right, props, report) {
94
94
  const end = Math.min(left.elements.length, right.elements.length);
95
95
 
96
96
  for (let i = 0; i < end; ++i) {
97
+ const leftElement = left.elements[i];
97
98
  const rightElement = right.elements[i];
98
99
 
99
- eachSelfAssignment(left.elements[i], rightElement, props, report);
100
+ // Avoid cases such as [...a] = [...a, 1]
101
+ if (
102
+ leftElement &&
103
+ leftElement.type === "RestElement" &&
104
+ i < right.elements.length - 1
105
+ ) {
106
+ break;
107
+ }
108
+
109
+ eachSelfAssignment(leftElement, rightElement, props, report);
100
110
 
101
111
  // After a spread element, those indices are unknown.
102
112
  if (rightElement && rightElement.type === "SpreadElement") {
@@ -507,7 +507,7 @@ module.exports = {
507
507
  const childScopes = scope.childScopes;
508
508
  let i, l;
509
509
 
510
- if (scope.type !== "TDZ" && (scope.type !== "global" || config.vars === "all")) {
510
+ if (scope.type !== "global" || config.vars === "all") {
511
511
  for (i = 0, l = variables.length; i < l; ++i) {
512
512
  const variable = variables[i];
513
513
 
@@ -52,16 +52,7 @@ function isOctalEscapeSequence(node) {
52
52
  return false;
53
53
  }
54
54
 
55
- const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-7]{1,3})/u);
56
-
57
- if (match) {
58
-
59
- // \0 is actually not considered an octal
60
- if (match[2] !== "0" || typeof match[3] !== "undefined") {
61
- return true;
62
- }
63
- }
64
- return false;
55
+ return astUtils.hasOctalEscapeSequence(node.raw);
65
56
  }
66
57
 
67
58
  /**
@@ -29,7 +29,13 @@ const astUtils = require("./utils/ast-utils"),
29
29
  * @private
30
30
  */
31
31
  function getPropertyName(node) {
32
- return astUtils.getStaticPropertyName(node) || node.key.name || null;
32
+ const staticName = astUtils.getStaticPropertyName(node);
33
+
34
+ if (staticName !== null) {
35
+ return staticName;
36
+ }
37
+
38
+ return node.key.name || null;
33
39
  }
34
40
 
35
41
  /**
@@ -151,9 +157,11 @@ module.exports = {
151
157
  const numKeys = stack.numKeys;
152
158
  const thisName = getPropertyName(node);
153
159
 
154
- stack.prevName = thisName || prevName;
160
+ if (thisName !== null) {
161
+ stack.prevName = thisName;
162
+ }
155
163
 
156
- if (!prevName || !thisName || numKeys < minKeys) {
164
+ if (prevName === null || thisName === null || numKeys < minKeys) {
157
165
  return;
158
166
  }
159
167
 
@@ -38,6 +38,7 @@ const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);
38
38
  const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "SwitchCase"]);
39
39
 
40
40
  const DECIMAL_INTEGER_PATTERN = /^(0|[1-9]\d*)$/u;
41
+ const OCTAL_ESCAPE_PATTERN = /^(?:[^\\]|\\[^0-7]|\\0(?![0-9]))*\\(?:[1-7]|0[0-9])/u;
41
42
 
42
43
  /**
43
44
  * Checks reference if is non initializer and writable.
@@ -847,6 +848,7 @@ module.exports = {
847
848
  return 17;
848
849
 
849
850
  case "CallExpression":
851
+ case "ImportExpression":
850
852
  return 18;
851
853
 
852
854
  case "NewExpression":
@@ -1101,7 +1103,7 @@ module.exports = {
1101
1103
  } else {
1102
1104
  const name = module.exports.getStaticPropertyName(parent);
1103
1105
 
1104
- if (name) {
1106
+ if (name !== null) {
1105
1107
  tokens.push(`'${name}'`);
1106
1108
  }
1107
1109
  }
@@ -1301,7 +1303,7 @@ module.exports = {
1301
1303
  * set `node.value` to a unicode regex. To make sure a literal is actually `null`, check
1302
1304
  * `node.regex` instead. Also see: https://github.com/eslint/eslint/issues/8020
1303
1305
  */
1304
- return node.type === "Literal" && node.value === null && !node.regex;
1306
+ return node.type === "Literal" && node.value === null && !node.regex && !node.bigint;
1305
1307
  },
1306
1308
 
1307
1309
  /**
@@ -1373,5 +1375,20 @@ module.exports = {
1373
1375
  "/*".length +
1374
1376
  (match ? match.index + 1 : 0)
1375
1377
  );
1378
+ },
1379
+
1380
+ /**
1381
+ * Determines whether the given raw string contains an octal escape sequence.
1382
+ *
1383
+ * "\1", "\2" ... "\7"
1384
+ * "\00", "\01" ... "\09"
1385
+ *
1386
+ * "\0", when not followed by a digit, is not an octal escape sequence.
1387
+ *
1388
+ * @param {string} rawString A string in its raw representation.
1389
+ * @returns {boolean} `true` if the string contains at least one octal escape sequence.
1390
+ */
1391
+ hasOctalEscapeSequence(rawString) {
1392
+ return OCTAL_ESCAPE_PATTERN.test(rawString);
1376
1393
  }
1377
1394
  };
package/lib/rules/yoda.js CHANGED
@@ -119,7 +119,7 @@ function same(a, b) {
119
119
  const nameA = astUtils.getStaticPropertyName(a);
120
120
 
121
121
  // x.y = x["y"]
122
- if (nameA) {
122
+ if (nameA !== null) {
123
123
  return (
124
124
  same(a.object, b.object) &&
125
125
  nameA === astUtils.getStaticPropertyName(b)
@@ -274,13 +274,22 @@ module.exports = {
274
274
  * @returns {string} A string representation of the node with the sides and operator flipped
275
275
  */
276
276
  function getFlippedString(node) {
277
+ const tokenBefore = sourceCode.getTokenBefore(node);
277
278
  const operatorToken = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
278
279
  const textBeforeOperator = sourceCode.getText().slice(sourceCode.getTokenBefore(operatorToken).range[1], operatorToken.range[0]);
279
280
  const textAfterOperator = sourceCode.getText().slice(operatorToken.range[1], sourceCode.getTokenAfter(operatorToken).range[0]);
280
281
  const leftText = sourceCode.getText().slice(node.range[0], sourceCode.getTokenBefore(operatorToken).range[1]);
281
- const rightText = sourceCode.getText().slice(sourceCode.getTokenAfter(operatorToken).range[0], node.range[1]);
282
+ const firstRightToken = sourceCode.getTokenAfter(operatorToken);
283
+ const rightText = sourceCode.getText().slice(firstRightToken.range[0], node.range[1]);
282
284
 
283
- return rightText + textBeforeOperator + OPERATOR_FLIP_MAP[operatorToken.value] + textAfterOperator + leftText;
285
+ let prefix = "";
286
+
287
+ if (tokenBefore && tokenBefore.range[1] === node.range[0] &&
288
+ !astUtils.canTokensBeAdjacent(tokenBefore, firstRightToken)) {
289
+ prefix = " ";
290
+ }
291
+
292
+ return prefix + rightText + textBeforeOperator + OPERATOR_FLIP_MAP[operatorToken.value] + textAfterOperator + leftText;
284
293
  }
285
294
 
286
295
  //--------------------------------------------------------------------------
@@ -30,11 +30,13 @@ module.exports = {};
30
30
  * @property {Record<string, boolean>} [env] The environment settings.
31
31
  * @property {string | string[]} [extends] The path to other config files or the package name of shareable configs.
32
32
  * @property {Record<string, GlobalConf>} [globals] The global variable settings.
33
+ * @property {boolean} [noInlineConfig] The flag that disables directive comments.
33
34
  * @property {OverrideConfigData[]} [overrides] The override settings per kind of files.
34
35
  * @property {string} [parser] The path to a parser or the package name of a parser.
35
36
  * @property {ParserOptions} [parserOptions] The parser options.
36
37
  * @property {string[]} [plugins] The plugin specifiers.
37
38
  * @property {string} [processor] The processor specifier.
39
+ * @property {boolean|undefined} reportUnusedDisableDirectives The flag to report unused `eslint-disable` comments.
38
40
  * @property {boolean} [root] The root flag.
39
41
  * @property {Record<string, RuleConf>} [rules] The rule settings.
40
42
  * @property {Object} [settings] The shared settings.
@@ -47,11 +49,13 @@ module.exports = {};
47
49
  * @property {string | string[]} [extends] The path to other config files or the package name of shareable configs.
48
50
  * @property {string | string[]} files The glob pattarns for target files.
49
51
  * @property {Record<string, GlobalConf>} [globals] The global variable settings.
52
+ * @property {boolean} [noInlineConfig] The flag that disables directive comments.
50
53
  * @property {OverrideConfigData[]} [overrides] The override settings per kind of files.
51
54
  * @property {string} [parser] The path to a parser or the package name of a parser.
52
55
  * @property {ParserOptions} [parserOptions] The parser options.
53
56
  * @property {string[]} [plugins] The plugin specifiers.
54
57
  * @property {string} [processor] The processor specifier.
58
+ * @property {boolean|undefined} reportUnusedDisableDirectives The flag to report unused `eslint-disable` comments.
55
59
  * @property {Record<string, RuleConf>} [rules] The rule settings.
56
60
  * @property {Object} [settings] The shared settings.
57
61
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint",
3
- "version": "6.1.0",
3
+ "version": "6.3.0",
4
4
  "author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
5
5
  "description": "An AST-based pattern checker for JavaScript.",
6
6
  "bin": {
@@ -50,9 +50,9 @@
50
50
  "debug": "^4.0.1",
51
51
  "doctrine": "^3.0.0",
52
52
  "eslint-scope": "^5.0.0",
53
- "eslint-utils": "^1.3.1",
54
- "eslint-visitor-keys": "^1.0.0",
55
- "espree": "^6.0.0",
53
+ "eslint-utils": "^1.4.2",
54
+ "eslint-visitor-keys": "^1.1.0",
55
+ "espree": "^6.1.1",
56
56
  "esquery": "^1.0.1",
57
57
  "esutils": "^2.0.2",
58
58
  "file-entry-cache": "^5.0.1",
@@ -84,7 +84,7 @@
84
84
  "devDependencies": {
85
85
  "@babel/core": "^7.4.3",
86
86
  "@babel/preset-env": "^7.4.3",
87
- "acorn": "^6.1.1",
87
+ "acorn": "^7.0.0",
88
88
  "babel-loader": "^8.0.5",
89
89
  "chai": "^4.0.1",
90
90
  "cheerio": "^0.22.0",