eslint 6.3.0 → 6.4.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.
@@ -79,7 +79,7 @@ module.exports = {
79
79
  }
80
80
  };
81
81
 
82
- // ES6: report blocks without block-level bindings
82
+ // ES6: report blocks without block-level bindings, or that's only child of another block
83
83
  if (context.parserOptions.ecmaVersion >= 6) {
84
84
  ruleDef = {
85
85
  BlockStatement(node) {
@@ -91,6 +91,11 @@ module.exports = {
91
91
  if (loneBlocks.length > 0 && loneBlocks[loneBlocks.length - 1] === node) {
92
92
  loneBlocks.pop();
93
93
  report(node);
94
+ } else if (
95
+ node.parent.type === "BlockStatement" &&
96
+ node.parent.body.length === 1
97
+ ) {
98
+ report(node);
94
99
  }
95
100
  }
96
101
  };
@@ -5,6 +5,18 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const { CALL, ReferenceTracker } = require("eslint-utils");
13
+
14
+ //------------------------------------------------------------------------------
15
+ // Helpers
16
+ //------------------------------------------------------------------------------
17
+
18
+ const nonCallableGlobals = ["Atomics", "JSON", "Math", "Reflect"];
19
+
8
20
  //------------------------------------------------------------------------------
9
21
  // Rule Definition
10
22
  //------------------------------------------------------------------------------
@@ -20,23 +32,31 @@ module.exports = {
20
32
  url: "https://eslint.org/docs/rules/no-obj-calls"
21
33
  },
22
34
 
23
- schema: []
35
+ schema: [],
36
+
37
+ messages: {
38
+ unexpectedCall: "'{{name}}' is not a function."
39
+ }
24
40
  },
25
41
 
26
42
  create(context) {
27
43
 
28
44
  return {
29
- CallExpression(node) {
30
-
31
- if (node.callee.type === "Identifier") {
32
- const name = node.callee.name;
45
+ Program() {
46
+ const scope = context.getScope();
47
+ const tracker = new ReferenceTracker(scope);
48
+ const traceMap = {};
49
+
50
+ for (const global of nonCallableGlobals) {
51
+ traceMap[global] = {
52
+ [CALL]: true
53
+ };
54
+ }
33
55
 
34
- if (name === "Math" || name === "JSON" || name === "Reflect") {
35
- context.report({ node, message: "'{{name}}' is not a function.", data: { name } });
36
- }
56
+ for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
57
+ context.report({ node, messageId: "unexpectedCall", data: { name: node.callee.name } });
37
58
  }
38
59
  }
39
60
  };
40
-
41
61
  }
42
62
  };
@@ -20,7 +20,11 @@ module.exports = {
20
20
  url: "https://eslint.org/docs/rules/no-octal-escape"
21
21
  },
22
22
 
23
- schema: []
23
+ schema: [],
24
+
25
+ messages: {
26
+ octalEscapeSequence: "Don't use octal: '\\{{sequence}}'. Use '\\u....' instead."
27
+ }
24
28
  },
25
29
 
26
30
  create(context) {
@@ -32,15 +36,17 @@ module.exports = {
32
36
  return;
33
37
  }
34
38
 
35
- const match = node.raw.match(/^([^\\]|\\[^0-7])*\\([0-3][0-7]{1,2}|[4-7][0-7]|[0-7])/u);
39
+ // \0 represents a valid NULL character if it isn't followed by a digit.
40
+ const match = node.raw.match(
41
+ /^(?:[^\\]|\\.)*?\\([0-3][0-7]{1,2}|[4-7][0-7]|[1-7])/u
42
+ );
36
43
 
37
44
  if (match) {
38
- const octalDigit = match[2];
39
-
40
- // \0 is actually not considered an octal
41
- if (match[2] !== "0" || typeof match[3] !== "undefined") {
42
- context.report({ node, message: "Don't use octal: '\\{{octalDigit}}'. Use '\\u....' instead.", data: { octalDigit } });
43
- }
45
+ context.report({
46
+ node,
47
+ messageId: "octalEscapeSequence",
48
+ data: { sequence: match[1] }
49
+ });
44
50
  }
45
51
  }
46
52
 
@@ -152,13 +152,14 @@ function eachSelfAssignment(left, right, props, report) {
152
152
  } else if (
153
153
  left.type === "Property" &&
154
154
  right.type === "Property" &&
155
- !left.computed &&
156
- !right.computed &&
157
155
  right.kind === "init" &&
158
- !right.method &&
159
- left.key.name === right.key.name
156
+ !right.method
160
157
  ) {
161
- eachSelfAssignment(left.value, right.value, props, report);
158
+ const leftName = astUtils.getStaticPropertyName(left);
159
+
160
+ if (leftName !== null && leftName === astUtils.getStaticPropertyName(right)) {
161
+ eachSelfAssignment(left.value, right.value, props, report);
162
+ }
162
163
  } else if (
163
164
  props &&
164
165
  left.type === "MemberExpression" &&
@@ -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
 
@@ -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
  }
@@ -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
  }
@@ -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({