eslint 8.28.0 → 8.30.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.
@@ -0,0 +1,46 @@
1
+ [
2
+ {
3
+ "name": "checkstyle",
4
+ "description": "Outputs results to the [Checkstyle](https://checkstyle.sourceforge.io/) format."
5
+ },
6
+ {
7
+ "name": "compact",
8
+ "description": "Human-readable output format. Mimics the default output of JSHint."
9
+ },
10
+ {
11
+ "name": "html",
12
+ "description": "Outputs results to HTML. The `html` formatter is useful for visual presentation in the browser."
13
+ },
14
+ {
15
+ "name": "jslint-xml",
16
+ "description": "Outputs results to format compatible with the [JSLint Jenkins plugin](https://plugins.jenkins.io/jslint/)."
17
+ },
18
+ {
19
+ "name": "json-with-metadata",
20
+ "description": "Outputs JSON-serialized results. The `json-with-metadata` provides the same linting results as the [`json`](#json) formatter with additional metadata about the rules applied. The linting results are included in the `results` property and the rules metadata is included in the `metadata` property.\n\nAlternatively, you can use the [ESLint Node.js API](../../developer-guide/nodejs-api) to programmatically use ESLint."
21
+ },
22
+ {
23
+ "name": "json",
24
+ "description": "Outputs JSON-serialized results. The `json` formatter is useful when you want to programmatically work with the CLI's linting results.\n\nAlternatively, you can use the [ESLint Node.js API](../../developer-guide/nodejs-api) to programmatically use ESLint."
25
+ },
26
+ {
27
+ "name": "junit",
28
+ "description": "Outputs results to format compatible with the [JUnit Jenkins plugin](https://plugins.jenkins.io/junit/)."
29
+ },
30
+ {
31
+ "name": "stylish",
32
+ "description": "Human-readable output format. This is the default formatter."
33
+ },
34
+ {
35
+ "name": "tap",
36
+ "description": "Outputs results to the [Test Anything Protocol (TAP)](https://testanything.org/) specification format."
37
+ },
38
+ {
39
+ "name": "unix",
40
+ "description": "Outputs results to a format similar to many commands in UNIX-like systems. Parsable with tools such as [grep](https://www.gnu.org/software/grep/manual/grep.html), [sed](https://www.gnu.org/software/sed/manual/sed.html), and [awk](https://www.gnu.org/software/gawk/manual/gawk.html)."
41
+ },
42
+ {
43
+ "name": "visualstudio",
44
+ "description": "Outputs results to format compatible with the integrated terminal of the [Visual Studio](https://visualstudio.microsoft.com/) IDE. When using Visual Studio, you can click on the linting results in the integrated terminal to go to the issue in the source code."
45
+ }
46
+ ]
@@ -51,7 +51,7 @@ exports.defaultConfig = [
51
51
  // default ignores are listed here
52
52
  {
53
53
  ignores: [
54
- "**/node_modules/**",
54
+ "**/node_modules/*",
55
55
  ".git/"
56
56
  ]
57
57
  },
package/lib/options.js CHANGED
@@ -177,7 +177,7 @@ module.exports = function(usingFlatConfig) {
177
177
  },
178
178
  resolvePluginsFlag,
179
179
  {
180
- heading: "Specifying rules and plugins"
180
+ heading: "Specify Rules and Plugins"
181
181
  },
182
182
  {
183
183
  option: "plugin",
@@ -191,7 +191,7 @@ module.exports = function(usingFlatConfig) {
191
191
  },
192
192
  rulesDirFlag,
193
193
  {
194
- heading: "Fixing problems"
194
+ heading: "Fix Problems"
195
195
  },
196
196
  {
197
197
  option: "fix",
@@ -211,7 +211,7 @@ module.exports = function(usingFlatConfig) {
211
211
  description: "Specify the types of fixes to apply (directive, problem, suggestion, layout)"
212
212
  },
213
213
  {
214
- heading: "Ignoring files"
214
+ heading: "Ignore Files"
215
215
  },
216
216
  ignorePathFlag,
217
217
  {
@@ -229,7 +229,7 @@ module.exports = function(usingFlatConfig) {
229
229
  }]
230
230
  },
231
231
  {
232
- heading: "Using stdin"
232
+ heading: "Use stdin"
233
233
  },
234
234
  {
235
235
  option: "stdin",
@@ -243,7 +243,7 @@ module.exports = function(usingFlatConfig) {
243
243
  description: "Specify filename to process STDIN as"
244
244
  },
245
245
  {
246
- heading: "Handling warnings"
246
+ heading: "Handle Warnings"
247
247
  },
248
248
  {
249
249
  option: "quiet",
@@ -619,15 +619,17 @@ class FlatRuleTester {
619
619
  plugins: {
620
620
  "rule-tester": {
621
621
  rules: {
622
- "validate-ast"() {
623
- return {
624
- Program(node) {
625
- beforeAST = cloneDeeplyExcludesParent(node);
626
- },
627
- "Program:exit"(node) {
628
- afterAST = node;
629
- }
630
- };
622
+ "validate-ast": {
623
+ create() {
624
+ return {
625
+ Program(node) {
626
+ beforeAST = cloneDeeplyExcludesParent(node);
627
+ },
628
+ "Program:exit"(node) {
629
+ afterAST = node;
630
+ }
631
+ };
632
+ }
631
633
  }
632
634
  }
633
635
  }
@@ -632,14 +632,18 @@ class RuleTester {
632
632
  * The goal is to check whether or not AST was modified when
633
633
  * running the rule under test.
634
634
  */
635
- linter.defineRule("rule-tester/validate-ast", () => ({
636
- Program(node) {
637
- beforeAST = cloneDeeplyExcludesParent(node);
638
- },
639
- "Program:exit"(node) {
640
- afterAST = node;
635
+ linter.defineRule("rule-tester/validate-ast", {
636
+ create() {
637
+ return {
638
+ Program(node) {
639
+ beforeAST = cloneDeeplyExcludesParent(node);
640
+ },
641
+ "Program:exit"(node) {
642
+ afterAST = node;
643
+ }
644
+ };
641
645
  }
642
- }));
646
+ });
643
647
 
644
648
  if (typeof config.parser === "string") {
645
649
  assert(path.isAbsolute(config.parser), "Parsers provided as strings to RuleTester must be absolute paths");
@@ -346,7 +346,7 @@ module.exports = {
346
346
  "always-multiline": forceTrailingCommaIfMultiline,
347
347
  "only-multiline": allowTrailingCommaIfMultiline,
348
348
  never: forbidTrailingComma,
349
- ignore: () => {}
349
+ ignore() {}
350
350
  };
351
351
 
352
352
  return {
@@ -73,7 +73,7 @@ module.exports = {
73
73
  end: lastTokenOfTest.loc.end
74
74
  },
75
75
  messageId: "unexpectedTestCons",
76
- fix: fixer => {
76
+ fix(fixer) {
77
77
  if (hasComments) {
78
78
  return null;
79
79
  }
@@ -101,7 +101,7 @@ module.exports = {
101
101
  end: lastTokenOfConsequent.loc.end
102
102
  },
103
103
  messageId: "unexpectedConsAlt",
104
- fix: fixer => {
104
+ fix(fixer) {
105
105
  if (hasComments) {
106
106
  return null;
107
107
  }
@@ -178,7 +178,7 @@ module.exports = {
178
178
  context.report({
179
179
  node,
180
180
  messageId: "unexpected",
181
- fix: fixer => {
181
+ fix(fixer) {
182
182
 
183
183
  if (!isSafeFromNameCollisions(node, currentScope)) {
184
184
  return null;
@@ -52,7 +52,8 @@ module.exports = {
52
52
  enforceForArrowConditionals: { type: "boolean" },
53
53
  enforceForSequenceExpressions: { type: "boolean" },
54
54
  enforceForNewInMemberExpressions: { type: "boolean" },
55
- enforceForFunctionPrototypeMethods: { type: "boolean" }
55
+ enforceForFunctionPrototypeMethods: { type: "boolean" },
56
+ allowParensAfterCommentPattern: { type: "string" }
56
57
  },
57
58
  additionalProperties: false
58
59
  }
@@ -86,6 +87,7 @@ module.exports = {
86
87
  context.options[1].enforceForNewInMemberExpressions === false;
87
88
  const IGNORE_FUNCTION_PROTOTYPE_METHODS = ALL_NODES && context.options[1] &&
88
89
  context.options[1].enforceForFunctionPrototypeMethods === false;
90
+ const ALLOW_PARENS_AFTER_COMMENT_PATTERN = ALL_NODES && context.options[1] && context.options[1].allowParensAfterCommentPattern;
89
91
 
90
92
  const PRECEDENCE_OF_ASSIGNMENT_EXPR = precedence({ type: "AssignmentExpression" });
91
93
  const PRECEDENCE_OF_UPDATE_EXPR = precedence({ type: "UpdateExpression" });
@@ -402,6 +404,19 @@ module.exports = {
402
404
  if (isIIFE(node) && !isParenthesised(node.callee)) {
403
405
  return;
404
406
  }
407
+
408
+ if (ALLOW_PARENS_AFTER_COMMENT_PATTERN) {
409
+ const commentsBeforeLeftParenToken = sourceCode.getCommentsBefore(leftParenToken);
410
+ const totalCommentsBeforeLeftParenTokenCount = commentsBeforeLeftParenToken.length;
411
+ const ignorePattern = new RegExp(ALLOW_PARENS_AFTER_COMMENT_PATTERN, "u");
412
+
413
+ if (
414
+ totalCommentsBeforeLeftParenTokenCount > 0 &&
415
+ ignorePattern.test(commentsBeforeLeftParenToken[totalCommentsBeforeLeftParenTokenCount - 1].value)
416
+ ) {
417
+ return;
418
+ }
419
+ }
405
420
  }
406
421
 
407
422
  /**
@@ -59,6 +59,20 @@ module.exports = {
59
59
  }
60
60
  }
61
61
 
62
+ /**
63
+ * Reports error with the provided message.
64
+ * @param {ASTNode} node The node holding the invalid RegExp
65
+ * @param {string} message The message to report.
66
+ * @returns {void}
67
+ */
68
+ function report(node, message) {
69
+ context.report({
70
+ node,
71
+ messageId: "regexMessage",
72
+ data: { message }
73
+ });
74
+ }
75
+
62
76
  /**
63
77
  * Check if node is a string
64
78
  * @param {ASTNode} node node to evaluate
@@ -108,10 +122,13 @@ module.exports = {
108
122
 
109
123
  /**
110
124
  * Check syntax error in a given flags.
111
- * @param {string} flags The RegExp flags to validate.
125
+ * @param {string|null} flags The RegExp flags to validate.
112
126
  * @returns {string|null} The syntax error.
113
127
  */
114
128
  function validateRegExpFlags(flags) {
129
+ if (!flags) {
130
+ return null;
131
+ }
115
132
  try {
116
133
  validator.validateFlags(flags);
117
134
  return null;
@@ -122,34 +139,39 @@ module.exports = {
122
139
 
123
140
  return {
124
141
  "CallExpression, NewExpression"(node) {
125
- if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp" || !isString(node.arguments[0])) {
142
+ if (node.callee.type !== "Identifier" || node.callee.name !== "RegExp") {
126
143
  return;
127
144
  }
128
- const pattern = node.arguments[0].value;
145
+
129
146
  let flags = getFlags(node);
130
147
 
131
148
  if (flags && allowedFlags) {
132
149
  flags = flags.replace(allowedFlags, "");
133
150
  }
134
151
 
135
- const message =
136
- (
137
- flags && validateRegExpFlags(flags)
138
- ) ||
139
- (
152
+ let message = validateRegExpFlags(flags);
153
+
154
+ if (message) {
155
+ report(node, message);
156
+ return;
157
+ }
158
+
159
+ if (!isString(node.arguments[0])) {
160
+ return;
161
+ }
162
+
163
+ const pattern = node.arguments[0].value;
164
+
165
+ message = (
140
166
 
141
- // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
142
- flags === null
143
- ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
144
- : validateRegExpPattern(pattern, flags.includes("u"))
145
- );
167
+ // If flags are unknown, report the regex only if its pattern is invalid both with and without the "u" flag
168
+ flags === null
169
+ ? validateRegExpPattern(pattern, true) && validateRegExpPattern(pattern, false)
170
+ : validateRegExpPattern(pattern, flags.includes("u"))
171
+ );
146
172
 
147
173
  if (message) {
148
- context.report({
149
- node,
150
- messageId: "regexMessage",
151
- data: { message }
152
- });
174
+ report(node, message);
153
175
  }
154
176
  }
155
177
  };
@@ -13,6 +13,7 @@ const astUtils = require("./utils/ast-utils");
13
13
  /** @type {import('../shared/types').Rule} */
14
14
  module.exports = {
15
15
  meta: {
16
+ hasSuggestions: true,
16
17
  type: "suggestion",
17
18
 
18
19
  docs: {
@@ -29,6 +30,7 @@ module.exports = {
29
30
  ],
30
31
 
31
32
  messages: {
33
+ removeAwait: "Remove redundant `await`.",
32
34
  redundantUseOfAwait: "Redundant use of `await` on a return value."
33
35
  }
34
36
  },
@@ -44,7 +46,32 @@ module.exports = {
44
46
  context.report({
45
47
  node: context.getSourceCode().getFirstToken(node),
46
48
  loc: node.loc,
47
- messageId: "redundantUseOfAwait"
49
+ messageId: "redundantUseOfAwait",
50
+ suggest: [
51
+ {
52
+ messageId: "removeAwait",
53
+ fix(fixer) {
54
+ const sourceCode = context.getSourceCode();
55
+ const [awaitToken, tokenAfterAwait] = sourceCode.getFirstTokens(node, 2);
56
+
57
+ const areAwaitAndAwaitedExpressionOnTheSameLine = awaitToken.loc.start.line === tokenAfterAwait.loc.start.line;
58
+
59
+ if (!areAwaitAndAwaitedExpressionOnTheSameLine) {
60
+ return null;
61
+ }
62
+
63
+ const [startOfAwait, endOfAwait] = awaitToken.range;
64
+
65
+ const characterAfterAwait = sourceCode.text[endOfAwait];
66
+ const trimLength = characterAfterAwait === " " ? 1 : 0;
67
+
68
+ const range = [startOfAwait, endOfAwait + trimLength];
69
+
70
+ return fixer.removeRange(range);
71
+ }
72
+ }
73
+ ]
74
+
48
75
  });
49
76
  }
50
77
 
@@ -144,7 +144,7 @@ module.exports = {
144
144
  context.report({
145
145
  node,
146
146
  messageId: "unnecessaryConditionalAssignment",
147
- fix: fixer => {
147
+ fix(fixer) {
148
148
  const shouldParenthesizeAlternate =
149
149
  (
150
150
  astUtils.getPrecedence(node.alternate) < OR_PRECEDENCE ||
@@ -159,7 +159,7 @@ function hasReferenceInTDZ(node) {
159
159
  return !reference.init && (
160
160
  start < idStart ||
161
161
  (defaultValue !== null && start >= defaultStart && end <= defaultEnd) ||
162
- (start >= initStart && end <= initEnd)
162
+ (!astUtils.isFunction(node) && start >= initStart && end <= initEnd)
163
163
  );
164
164
  });
165
165
  };
@@ -23,6 +23,61 @@ const regexpp = require("regexpp");
23
23
 
24
24
  const parser = new regexpp.RegExpParser();
25
25
 
26
+ /**
27
+ * Creates fixer suggestions for the regex, if statically determinable.
28
+ * @param {number} groupStart Starting index of the regex group.
29
+ * @param {string} pattern The regular expression pattern to be checked.
30
+ * @param {string} rawText Source text of the regexNode.
31
+ * @param {ASTNode} regexNode AST node which contains the regular expression.
32
+ * @returns {Array<SuggestionResult>} Fixer suggestions for the regex, if statically determinable.
33
+ */
34
+ function suggestIfPossible(groupStart, pattern, rawText, regexNode) {
35
+ switch (regexNode.type) {
36
+ case "Literal":
37
+ if (typeof regexNode.value === "string" && rawText.includes("\\")) {
38
+ return null;
39
+ }
40
+ break;
41
+ case "TemplateLiteral":
42
+ if (regexNode.expressions.length || rawText.slice(1, -1) !== pattern) {
43
+ return null;
44
+ }
45
+ break;
46
+ default:
47
+ return null;
48
+ }
49
+
50
+ const start = regexNode.range[0] + groupStart + 2;
51
+
52
+ return [
53
+ {
54
+ fix(fixer) {
55
+ const existingTemps = pattern.match(/temp\d+/gu) || [];
56
+ const highestTempCount = existingTemps.reduce(
57
+ (previous, next) =>
58
+ Math.max(previous, Number(next.slice("temp".length))),
59
+ 0
60
+ );
61
+
62
+ return fixer.insertTextBeforeRange(
63
+ [start, start],
64
+ `?<temp${highestTempCount + 1}>`
65
+ );
66
+ },
67
+ messageId: "addGroupName"
68
+ },
69
+ {
70
+ fix(fixer) {
71
+ return fixer.insertTextBeforeRange(
72
+ [start, start],
73
+ "?:"
74
+ );
75
+ },
76
+ messageId: "addNonCapture"
77
+ }
78
+ ];
79
+ }
80
+
26
81
  //------------------------------------------------------------------------------
27
82
  // Rule Definition
28
83
  //------------------------------------------------------------------------------
@@ -38,23 +93,29 @@ module.exports = {
38
93
  url: "https://eslint.org/docs/rules/prefer-named-capture-group"
39
94
  },
40
95
 
96
+ hasSuggestions: true,
97
+
41
98
  schema: [],
42
99
 
43
100
  messages: {
101
+ addGroupName: "Add name to capture group.",
102
+ addNonCapture: "Convert group to non-capturing.",
44
103
  required: "Capture group '{{group}}' should be converted to a named or non-capturing group."
45
104
  }
46
105
  },
47
106
 
48
107
  create(context) {
108
+ const sourceCode = context.getSourceCode();
49
109
 
50
110
  /**
51
111
  * Function to check regular expression.
52
- * @param {string} pattern The regular expression pattern to be check.
53
- * @param {ASTNode} node AST node which contains regular expression.
112
+ * @param {string} pattern The regular expression pattern to be checked.
113
+ * @param {ASTNode} node AST node which contains the regular expression or a call/new expression.
114
+ * @param {ASTNode} regexNode AST node which contains the regular expression.
54
115
  * @param {boolean} uFlag Flag indicates whether unicode mode is enabled or not.
55
116
  * @returns {void}
56
117
  */
57
- function checkRegex(pattern, node, uFlag) {
118
+ function checkRegex(pattern, node, regexNode, uFlag) {
58
119
  let ast;
59
120
 
60
121
  try {
@@ -68,12 +129,16 @@ module.exports = {
68
129
  regexpp.visitRegExpAST(ast, {
69
130
  onCapturingGroupEnter(group) {
70
131
  if (!group.name) {
132
+ const rawText = sourceCode.getText(regexNode);
133
+ const suggest = suggestIfPossible(group.start, pattern, rawText, regexNode);
134
+
71
135
  context.report({
72
136
  node,
73
137
  messageId: "required",
74
138
  data: {
75
139
  group: group.raw
76
- }
140
+ },
141
+ suggest
77
142
  });
78
143
  }
79
144
  }
@@ -83,7 +148,7 @@ module.exports = {
83
148
  return {
84
149
  Literal(node) {
85
150
  if (node.regex) {
86
- checkRegex(node.regex.pattern, node, node.regex.flags.includes("u"));
151
+ checkRegex(node.regex.pattern, node, node, node.regex.flags.includes("u"));
87
152
  }
88
153
  },
89
154
  Program() {
@@ -101,7 +166,7 @@ module.exports = {
101
166
  const flags = getStringIfConstant(node.arguments[1]);
102
167
 
103
168
  if (regex) {
104
- checkRegex(regex, node, flags && flags.includes("u"));
169
+ checkRegex(regex, node, node.arguments[0], flags && flags.includes("u"));
105
170
  }
106
171
  }
107
172
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint",
3
- "version": "8.28.0",
3
+ "version": "8.30.0",
4
4
  "author": "Nicholas C. Zakas <nicholas+npm@nczconsulting.com>",
5
5
  "description": "An AST-based pattern checker for JavaScript.",
6
6
  "bin": {
@@ -13,22 +13,22 @@
13
13
  "./use-at-your-own-risk": "./lib/unsupported-api.js"
14
14
  },
15
15
  "scripts": {
16
+ "build:docs:update-links": "node tools/fetch-docs-links.js",
17
+ "release:generate:latest": "node Makefile.js generateRelease",
18
+ "release:generate:alpha": "node Makefile.js generatePrerelease -- alpha",
19
+ "release:generate:beta": "node Makefile.js generatePrerelease -- beta",
20
+ "release:publish": "node Makefile.js publishRelease",
21
+ "release:generate:rc": "node Makefile.js generatePrerelease -- rc",
22
+ "build:site": "node Makefile.js gensite",
23
+ "build:webpack": "node Makefile.js webpack",
24
+ "lint": "node Makefile.js lint",
25
+ "lint:fix": "node Makefile.js lint -- fix",
26
+ "lint:docs:js": "node Makefile.js lintDocsJS",
27
+ "lint:fix:docs:js": "node Makefile.js lintDocsJS -- fix",
16
28
  "test": "node Makefile.js test",
17
29
  "test:cli": "mocha",
18
- "lint": "node Makefile.js lint",
19
- "lint:docsjs": "node Makefile.js lintDocsJS",
20
- "fix": "node Makefile.js lint -- fix",
21
- "fix:docsjs": "node Makefile.js lintDocsJS -- fix",
22
- "fuzz": "node Makefile.js fuzz",
23
- "generate-release": "node Makefile.js generateRelease",
24
- "generate-alpharelease": "node Makefile.js generatePrerelease -- alpha",
25
- "generate-betarelease": "node Makefile.js generatePrerelease -- beta",
26
- "generate-rcrelease": "node Makefile.js generatePrerelease -- rc",
27
- "publish-release": "node Makefile.js publishRelease",
28
- "gensite": "node Makefile.js gensite",
29
- "webpack": "node Makefile.js webpack",
30
- "perf": "node Makefile.js perf",
31
- "docs:update-links": "node tools/fetch-docs-links.js"
30
+ "test:fuzz": "node Makefile.js fuzz",
31
+ "test:performance": "node Makefile.js perf"
32
32
  },
33
33
  "gitHooks": {
34
34
  "pre-commit": "lint-staged"
@@ -55,8 +55,8 @@
55
55
  "homepage": "https://eslint.org",
56
56
  "bugs": "https://github.com/eslint/eslint/issues/",
57
57
  "dependencies": {
58
- "@eslint/eslintrc": "^1.3.3",
59
- "@humanwhocodes/config-array": "^0.11.6",
58
+ "@eslint/eslintrc": "^1.4.0",
59
+ "@humanwhocodes/config-array": "^0.11.8",
60
60
  "@humanwhocodes/module-importer": "^1.0.1",
61
61
  "@nodelib/fs.walk": "^1.2.8",
62
62
  "ajv": "^6.10.0",
@@ -75,7 +75,7 @@
75
75
  "file-entry-cache": "^6.0.1",
76
76
  "find-up": "^5.0.0",
77
77
  "glob-parent": "^6.0.2",
78
- "globals": "^13.15.0",
78
+ "globals": "^13.19.0",
79
79
  "grapheme-splitter": "^1.0.4",
80
80
  "ignore": "^5.2.0",
81
81
  "import-fresh": "^3.0.0",