eslint 8.27.0 → 8.28.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.
@@ -26,6 +26,7 @@ const isPathInside = require("is-path-inside");
26
26
 
27
27
  const doFsWalk = util.promisify(fswalk.walk);
28
28
  const Minimatch = minimatch.Minimatch;
29
+ const MINIMATCH_OPTIONS = { dot: true };
29
30
 
30
31
  //-----------------------------------------------------------------------------
31
32
  // Types
@@ -76,7 +77,7 @@ class UnmatchedSearchPatternsError extends Error {
76
77
  constructor({ basePath, unmatchedPatterns, patterns, rawPatterns }) {
77
78
  super(`No files matching '${rawPatterns}' in '${basePath}' were found.`);
78
79
  this.basePath = basePath;
79
- this.patternsToCheck = unmatchedPatterns;
80
+ this.unmatchedPatterns = unmatchedPatterns;
80
81
  this.patterns = patterns;
81
82
  this.rawPatterns = rawPatterns;
82
83
  }
@@ -158,7 +159,7 @@ function globMatch({ basePath, pattern }) {
158
159
  ? normalizeToPosix(path.relative(basePath, pattern))
159
160
  : pattern;
160
161
 
161
- const matcher = new Minimatch(patternToUse);
162
+ const matcher = new Minimatch(patternToUse, MINIMATCH_OPTIONS);
162
163
 
163
164
  const fsWalkSettings = {
164
165
 
@@ -257,7 +258,7 @@ async function globSearch({
257
258
 
258
259
  relativeToPatterns.set(patternToUse, patterns[i]);
259
260
 
260
- return new minimatch.Minimatch(patternToUse);
261
+ return new Minimatch(patternToUse, MINIMATCH_OPTIONS);
261
262
  });
262
263
 
263
264
  /*
@@ -337,49 +338,43 @@ async function globSearch({
337
338
  }
338
339
 
339
340
  /**
340
- * Checks to see if there are any ignored results for a given search. This
341
- * happens either when there are unmatched patterns during a search or if
342
- * a search returns no results.
341
+ * Throws an error for unmatched patterns. The error will only contain information about the first one.
342
+ * Checks to see if there are any ignored results for a given search.
343
343
  * @param {Object} options The options for this function.
344
344
  * @param {string} options.basePath The directory to search.
345
345
  * @param {Array<string>} options.patterns An array of glob patterns
346
346
  * that were used in the original search.
347
347
  * @param {Array<string>} options.rawPatterns An array of glob patterns
348
348
  * as the user inputted them. Used for errors.
349
- * @param {Array<string>} options.patternsToCheck An array of glob patterns
350
- * to use for this check.
351
- * @returns {void}
352
- * @throws {NoFilesFoundError} If there is a pattern that doesn't match
353
- * any files and `errorOnUnmatchedPattern` is true.
354
- * @throws {AllFilesIgnoredError} If there is a pattern that matches files
355
- * when there are no ignores.
349
+ * @param {Array<string>} options.unmatchedPatterns A non-empty array of glob patterns
350
+ * that were unmatched in the original search.
351
+ * @returns {void} Always throws an error.
352
+ * @throws {NoFilesFoundError} If the first unmatched pattern
353
+ * doesn't match any files even when there are no ignores.
354
+ * @throws {AllFilesIgnoredError} If the first unmatched pattern
355
+ * matches some files when there are no ignores.
356
356
  */
357
- async function checkForIgnoredResults({
357
+ async function throwErrorForUnmatchedPatterns({
358
358
  basePath,
359
359
  patterns,
360
360
  rawPatterns,
361
- patternsToCheck = patterns
361
+ unmatchedPatterns
362
362
  }) {
363
363
 
364
- for (const pattern of patternsToCheck) {
364
+ const pattern = unmatchedPatterns[0];
365
+ const rawPattern = rawPatterns[patterns.indexOf(pattern)];
365
366
 
366
- const patternHasMatch = await globMatch({
367
- basePath,
368
- pattern
369
- });
367
+ const patternHasMatch = await globMatch({
368
+ basePath,
369
+ pattern
370
+ });
370
371
 
371
- if (patternHasMatch) {
372
- throw new AllFilesIgnoredError(
373
- rawPatterns[patterns.indexOf(pattern)]
374
- );
375
- }
372
+ if (patternHasMatch) {
373
+ throw new AllFilesIgnoredError(rawPattern);
376
374
  }
377
375
 
378
376
  // if we get here there are truly no matches
379
- throw new NoFilesFoundError(
380
- rawPatterns[patterns.indexOf(patternsToCheck[0])],
381
- true
382
- );
377
+ throw new NoFilesFoundError(rawPattern, true);
383
378
  }
384
379
 
385
380
  /**
@@ -446,9 +441,9 @@ async function globMultiSearch({ searches, configs, errorOnUnmatchedPattern }) {
446
441
 
447
442
  if (errorOnUnmatchedPattern) {
448
443
 
449
- await checkForIgnoredResults({
444
+ await throwErrorForUnmatchedPatterns({
450
445
  ...currentSearch,
451
- patternsToCheck: error.patternsToCheck
446
+ unmatchedPatterns: error.unmatchedPatterns
452
447
  });
453
448
 
454
449
  }
@@ -15,7 +15,7 @@ module.exports = {
15
15
  type: "problem",
16
16
 
17
17
  docs: {
18
- description: "Enforce \"for\" loop update clause moving the counter in the right direction.",
18
+ description: "Enforce \"for\" loop update clause moving the counter in the right direction",
19
19
  recommended: true,
20
20
  url: "https://eslint.org/docs/rules/for-direction"
21
21
  },
@@ -334,6 +334,19 @@ module.exports = {
334
334
 
335
335
  const sourceCode = context.getSourceCode();
336
336
 
337
+ /**
338
+ * Determines if the given property is key-value property.
339
+ * @param {ASTNode} property Property node to check.
340
+ * @returns {boolean} Whether the property is a key-value property.
341
+ */
342
+ function isKeyValueProperty(property) {
343
+ return !(
344
+ (property.method ||
345
+ property.shorthand ||
346
+ property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"
347
+ );
348
+ }
349
+
337
350
  /**
338
351
  * Checks whether a property is a member of the property group it follows.
339
352
  * @param {ASTNode} lastMember The last Property known to be in the group.
@@ -342,9 +355,9 @@ module.exports = {
342
355
  */
343
356
  function continuesPropertyGroup(lastMember, candidate) {
344
357
  const groupEndLine = lastMember.loc.start.line,
345
- candidateStartLine = candidate.loc.start.line;
358
+ candidateValueStartLine = (isKeyValueProperty(candidate) ? candidate.value : candidate).loc.start.line;
346
359
 
347
- if (candidateStartLine - groupEndLine <= 1) {
360
+ if (candidateValueStartLine - groupEndLine <= 1) {
348
361
  return true;
349
362
  }
350
363
 
@@ -358,7 +371,7 @@ module.exports = {
358
371
  if (
359
372
  leadingComments.length &&
360
373
  leadingComments[0].loc.start.line - groupEndLine <= 1 &&
361
- candidateStartLine - last(leadingComments).loc.end.line <= 1
374
+ candidateValueStartLine - last(leadingComments).loc.end.line <= 1
362
375
  ) {
363
376
  for (let i = 1; i < leadingComments.length; i++) {
364
377
  if (leadingComments[i].loc.start.line - leadingComments[i - 1].loc.end.line > 1) {
@@ -371,19 +384,6 @@ module.exports = {
371
384
  return false;
372
385
  }
373
386
 
374
- /**
375
- * Determines if the given property is key-value property.
376
- * @param {ASTNode} property Property node to check.
377
- * @returns {boolean} Whether the property is a key-value property.
378
- */
379
- function isKeyValueProperty(property) {
380
- return !(
381
- (property.method ||
382
- property.shorthand ||
383
- property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"
384
- );
385
- }
386
-
387
387
  /**
388
388
  * Starting from the given a node (a property.key node here) looks forward
389
389
  * until it finds the last token before a colon punctuator and returns it.
@@ -71,6 +71,24 @@ function isMultiplyByOne(node) {
71
71
  );
72
72
  }
73
73
 
74
+ /**
75
+ * Checks whether the given node logically represents multiplication by a fraction of `1`.
76
+ * For example, `a * 1` in `a * 1 / b` is technically multiplication by `1`, but the
77
+ * whole expression can be logically interpreted as `a * (1 / b)` rather than `(a * 1) / b`.
78
+ * @param {BinaryExpression} node A BinaryExpression node to check.
79
+ * @param {SourceCode} sourceCode The source code object.
80
+ * @returns {boolean} Whether or not the node is a multiplying by a fraction of `1`.
81
+ */
82
+ function isMultiplyByFractionOfOne(node, sourceCode) {
83
+ return node.type === "BinaryExpression" &&
84
+ node.operator === "*" &&
85
+ (node.right.type === "Literal" && node.right.value === 1) &&
86
+ node.parent.type === "BinaryExpression" &&
87
+ node.parent.operator === "/" &&
88
+ node.parent.left === node &&
89
+ !astUtils.isParenthesised(sourceCode, node);
90
+ }
91
+
74
92
  /**
75
93
  * Checks whether the result of a node is numeric or not
76
94
  * @param {ASTNode} node The node to test
@@ -290,7 +308,8 @@ module.exports = {
290
308
 
291
309
  // 1 * foo
292
310
  operatorAllowed = options.allow.includes("*");
293
- const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && getNonNumericOperand(node);
311
+ const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && !isMultiplyByFractionOfOne(node, sourceCode) &&
312
+ getNonNumericOperand(node);
294
313
 
295
314
  if (nonNumericOperand) {
296
315
  const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`;
@@ -65,6 +65,10 @@ module.exports = {
65
65
  ignoreDefaultValues: {
66
66
  type: "boolean",
67
67
  default: false
68
+ },
69
+ ignoreClassFieldInitialValues: {
70
+ type: "boolean",
71
+ default: false
68
72
  }
69
73
  },
70
74
  additionalProperties: false
@@ -82,7 +86,8 @@ module.exports = {
82
86
  enforceConst = !!config.enforceConst,
83
87
  ignore = new Set((config.ignore || []).map(normalizeIgnoreValue)),
84
88
  ignoreArrayIndexes = !!config.ignoreArrayIndexes,
85
- ignoreDefaultValues = !!config.ignoreDefaultValues;
89
+ ignoreDefaultValues = !!config.ignoreDefaultValues,
90
+ ignoreClassFieldInitialValues = !!config.ignoreClassFieldInitialValues;
86
91
 
87
92
  const okTypes = detectObjects ? [] : ["ObjectExpression", "Property", "AssignmentExpression"];
88
93
 
@@ -106,6 +111,17 @@ module.exports = {
106
111
  return parent.type === "AssignmentPattern" && parent.right === fullNumberNode;
107
112
  }
108
113
 
114
+ /**
115
+ * Returns whether the number is the initial value of a class field.
116
+ * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
117
+ * @returns {boolean} true if the number is the initial value of a class field.
118
+ */
119
+ function isClassFieldInitialValue(fullNumberNode) {
120
+ const parent = fullNumberNode.parent;
121
+
122
+ return parent.type === "PropertyDefinition" && parent.value === fullNumberNode;
123
+ }
124
+
109
125
  /**
110
126
  * Returns whether the given node is used as a radix within parseInt() or Number.parseInt()
111
127
  * @param {ASTNode} fullNumberNode `Literal` or `UnaryExpression` full number node
@@ -194,6 +210,7 @@ module.exports = {
194
210
  if (
195
211
  isIgnoredValue(value) ||
196
212
  (ignoreDefaultValues && isDefaultValue(fullNumberNode)) ||
213
+ (ignoreClassFieldInitialValues && isClassFieldInitialValue(fullNumberNode)) ||
197
214
  isParseIntRadix(fullNumberNode) ||
198
215
  isJSXNumber(fullNumberNode) ||
199
216
  (ignoreArrayIndexes && isArrayIndex(fullNumberNode, value))
@@ -16,7 +16,7 @@ const getPropertyName = require("./utils/ast-utils").getStaticPropertyName;
16
16
  // Helpers
17
17
  //------------------------------------------------------------------------------
18
18
 
19
- const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect"];
19
+ const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect", "Intl"];
20
20
 
21
21
  /**
22
22
  * Returns the name of the node to report
@@ -247,7 +247,7 @@ module.exports = {
247
247
 
248
248
  docs: {
249
249
  description:
250
- "Disallow using Object.assign with an object literal as the first argument and prefer the use of object spread instead.",
250
+ "Disallow using Object.assign with an object literal as the first argument and prefer the use of object spread instead",
251
251
  recommended: false,
252
252
  url: "https://eslint.org/docs/rules/prefer-object-spread"
253
253
  },
@@ -97,7 +97,7 @@ function environment() {
97
97
  */
98
98
  function getNpmPackageVersion(pkg, { global = false } = {}) {
99
99
  const npmBinArgs = ["bin", "-g"];
100
- const npmLsArgs = ["ls", "--depth=0", "--json", "eslint"];
100
+ const npmLsArgs = ["ls", "--depth=0", "--json", pkg];
101
101
 
102
102
  if (global) {
103
103
  npmLsArgs.push("-g");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint",
3
- "version": "8.27.0",
3
+ "version": "8.28.0",
4
4
  "author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
5
5
  "description": "An AST-based pattern checker for JavaScript.",
6
6
  "bin": {