eslint 3.13.1 → 3.16.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 (137) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/README.md +1 -1
  3. package/conf/{eslint.json → eslint-recommended.js} +88 -71
  4. package/lib/ast-utils.js +247 -28
  5. package/lib/cli.js +2 -2
  6. package/lib/code-path-analysis/code-path-state.js +2 -2
  7. package/lib/config/autoconfig.js +24 -20
  8. package/lib/config/config-file.js +31 -24
  9. package/lib/config/config-initializer.js +2 -2
  10. package/lib/config/config-rule.js +15 -16
  11. package/lib/config/config-validator.js +6 -6
  12. package/lib/config.js +3 -2
  13. package/lib/eslint.js +18 -18
  14. package/lib/formatters/checkstyle.js +2 -2
  15. package/lib/formatters/codeframe.js +1 -1
  16. package/lib/formatters/compact.js +2 -2
  17. package/lib/formatters/junit.js +2 -2
  18. package/lib/formatters/tap.js +2 -2
  19. package/lib/formatters/unix.js +2 -2
  20. package/lib/formatters/visualstudio.js +2 -2
  21. package/lib/internal-rules/internal-consistent-docs-description.js +1 -1
  22. package/lib/rule-context.js +2 -2
  23. package/lib/rules/arrow-body-style.js +7 -4
  24. package/lib/rules/arrow-spacing.js +7 -6
  25. package/lib/rules/block-spacing.js +2 -2
  26. package/lib/rules/brace-style.js +93 -202
  27. package/lib/rules/capitalized-comments.js +6 -6
  28. package/lib/rules/comma-dangle.js +6 -6
  29. package/lib/rules/comma-spacing.js +16 -16
  30. package/lib/rules/comma-style.js +1 -1
  31. package/lib/rules/consistent-return.js +1 -1
  32. package/lib/rules/constructor-super.js +3 -3
  33. package/lib/rules/curly.js +11 -7
  34. package/lib/rules/default-case.js +3 -3
  35. package/lib/rules/eqeqeq.js +15 -6
  36. package/lib/rules/func-call-spacing.js +12 -15
  37. package/lib/rules/func-name-matching.js +1 -1
  38. package/lib/rules/generator-star-spacing.js +18 -19
  39. package/lib/rules/global-require.js +2 -2
  40. package/lib/rules/id-blacklist.js +2 -2
  41. package/lib/rules/id-length.js +3 -3
  42. package/lib/rules/id-match.js +2 -2
  43. package/lib/rules/indent.js +21 -20
  44. package/lib/rules/key-spacing.js +20 -23
  45. package/lib/rules/keyword-spacing.js +2 -13
  46. package/lib/rules/line-comment-position.js +1 -1
  47. package/lib/rules/linebreak-style.js +7 -1
  48. package/lib/rules/lines-around-comment.js +4 -4
  49. package/lib/rules/lines-around-directive.js +4 -4
  50. package/lib/rules/max-lines.js +3 -3
  51. package/lib/rules/max-statements-per-line.js +8 -7
  52. package/lib/rules/new-cap.js +2 -2
  53. package/lib/rules/newline-after-var.js +7 -2
  54. package/lib/rules/newline-before-return.js +2 -2
  55. package/lib/rules/newline-per-chained-call.js +3 -1
  56. package/lib/rules/no-await-in-loop.js +5 -5
  57. package/lib/rules/no-cond-assign.js +3 -3
  58. package/lib/rules/no-dupe-keys.js +1 -1
  59. package/lib/rules/no-else-return.js +88 -25
  60. package/lib/rules/no-extend-native.js +3 -3
  61. package/lib/rules/no-extra-bind.js +3 -4
  62. package/lib/rules/no-extra-boolean-cast.js +22 -1
  63. package/lib/rules/no-extra-parens.js +57 -9
  64. package/lib/rules/no-inner-declarations.js +4 -4
  65. package/lib/rules/no-irregular-whitespace.js +7 -1
  66. package/lib/rules/no-lone-blocks.js +10 -10
  67. package/lib/rules/no-mixed-operators.js +1 -7
  68. package/lib/rules/no-mixed-requires.js +4 -4
  69. package/lib/rules/no-multi-assign.js +41 -0
  70. package/lib/rules/no-multi-spaces.js +4 -1
  71. package/lib/rules/no-multi-str.js +7 -3
  72. package/lib/rules/no-redeclare.js +7 -7
  73. package/lib/rules/no-return-assign.js +7 -14
  74. package/lib/rules/no-return-await.js +2 -2
  75. package/lib/rules/no-sequences.js +7 -6
  76. package/lib/rules/no-throw-literal.js +2 -39
  77. package/lib/rules/no-trailing-spaces.js +8 -2
  78. package/lib/rules/no-undefined.js +45 -6
  79. package/lib/rules/no-unexpected-multiline.js +9 -8
  80. package/lib/rules/no-unneeded-ternary.js +5 -1
  81. package/lib/rules/no-unused-labels.js +17 -2
  82. package/lib/rules/no-unused-vars.js +34 -19
  83. package/lib/rules/no-use-before-define.js +33 -29
  84. package/lib/rules/no-useless-computed-key.js +9 -4
  85. package/lib/rules/no-useless-concat.js +10 -7
  86. package/lib/rules/no-useless-escape.js +1 -1
  87. package/lib/rules/no-useless-return.js +5 -11
  88. package/lib/rules/no-var.js +69 -3
  89. package/lib/rules/no-whitespace-before-property.js +5 -16
  90. package/lib/rules/object-curly-newline.js +2 -2
  91. package/lib/rules/object-curly-spacing.js +7 -25
  92. package/lib/rules/object-property-newline.js +3 -3
  93. package/lib/rules/object-shorthand.js +10 -10
  94. package/lib/rules/operator-assignment.js +2 -2
  95. package/lib/rules/operator-linebreak.js +8 -10
  96. package/lib/rules/padded-blocks.js +4 -4
  97. package/lib/rules/prefer-promise-reject-errors.js +124 -0
  98. package/lib/rules/prefer-spread.js +1 -1
  99. package/lib/rules/prefer-template.js +1 -1
  100. package/lib/rules/quotes.js +11 -7
  101. package/lib/rules/require-await.js +1 -1
  102. package/lib/rules/semi-spacing.js +4 -0
  103. package/lib/rules/sort-imports.js +4 -4
  104. package/lib/rules/sort-keys.js +2 -2
  105. package/lib/rules/sort-vars.js +2 -2
  106. package/lib/rules/space-before-function-paren.js +9 -6
  107. package/lib/rules/space-in-parens.js +8 -8
  108. package/lib/rules/spaced-comment.js +10 -10
  109. package/lib/rules/strict.js +2 -2
  110. package/lib/rules/template-tag-spacing.js +77 -0
  111. package/lib/rules/unicode-bom.js +1 -1
  112. package/lib/rules/wrap-iife.js +5 -5
  113. package/lib/rules/yoda.js +2 -7
  114. package/lib/rules.js +2 -2
  115. package/lib/testers/rule-tester.js +28 -21
  116. package/lib/token-store/backward-token-comment-cursor.js +57 -0
  117. package/lib/token-store/backward-token-cursor.js +56 -0
  118. package/lib/token-store/cursor.js +76 -0
  119. package/lib/token-store/cursors.js +92 -0
  120. package/lib/token-store/decorative-cursor.js +39 -0
  121. package/lib/token-store/filter-cursor.js +43 -0
  122. package/lib/token-store/forward-token-comment-cursor.js +57 -0
  123. package/lib/token-store/forward-token-cursor.js +61 -0
  124. package/lib/token-store/index.js +604 -0
  125. package/lib/token-store/limit-cursor.js +40 -0
  126. package/lib/token-store/padded-token-cursor.js +38 -0
  127. package/lib/token-store/skip-cursor.js +42 -0
  128. package/lib/token-store/utils.js +100 -0
  129. package/lib/util/comment-event-generator.js +17 -16
  130. package/lib/util/glob-util.js +1 -1
  131. package/lib/util/glob.js +1 -1
  132. package/lib/util/rule-fixer.js +3 -8
  133. package/lib/util/source-code-fixer.js +41 -45
  134. package/lib/util/source-code.js +35 -19
  135. package/messages/extend-config-missing.txt +3 -0
  136. package/package.json +3 -3
  137. package/lib/token-store.js +0 -203
@@ -55,21 +55,26 @@ module.exports = {
55
55
  const sourceCode = context.getSourceCode();
56
56
 
57
57
  /**
58
- * Gets `*` token from a given node.
58
+ * Checks if the given token is a star token or not.
59
59
  *
60
- * @param {ASTNode} node - A node to get `*` token. This is one of
61
- * FunctionDeclaration, FunctionExpression, Property, and
62
- * MethodDefinition.
63
- * @returns {Token} `*` token.
60
+ * @param {Token} token - The token to check.
61
+ * @returns {boolean} `true` if the token is a star token.
64
62
  */
65
- function getStarToken(node) {
66
- let token = sourceCode.getFirstToken(node);
67
-
68
- while (token.value !== "*") {
69
- token = sourceCode.getTokenAfter(token);
70
- }
63
+ function isStarToken(token) {
64
+ return token.value === "*" && token.type === "Punctuator";
65
+ }
71
66
 
72
- return token;
67
+ /**
68
+ * Gets the generator star token of the given function node.
69
+ *
70
+ * @param {ASTNode} node - The function node to get.
71
+ * @returns {Token} Found star token.
72
+ */
73
+ function getStarToken(node) {
74
+ return sourceCode.getFirstToken(
75
+ (node.parent.method || node.parent.type === "MethodDefinition") ? node.parent : node,
76
+ isStarToken
77
+ );
73
78
  }
74
79
 
75
80
  /**
@@ -116,17 +121,11 @@ module.exports = {
116
121
  * @returns {void}
117
122
  */
118
123
  function checkFunction(node) {
119
- let starToken;
120
-
121
124
  if (!node.generator) {
122
125
  return;
123
126
  }
124
127
 
125
- if (node.parent.method || node.parent.type === "MethodDefinition") {
126
- starToken = getStarToken(node.parent);
127
- } else {
128
- starToken = getStarToken(node);
129
- }
128
+ const starToken = getStarToken(node);
130
129
 
131
130
  // Only check before when preceded by `function`|`static` keyword
132
131
  const prevToken = sourceCode.getTokenBefore(starToken);
@@ -29,9 +29,9 @@ function findReference(scope, node) {
29
29
  /* istanbul ignore else: correctly returns null */
30
30
  if (references.length === 1) {
31
31
  return references[0];
32
- } else {
33
- return null;
34
32
  }
33
+ return null;
34
+
35
35
  }
36
36
 
37
37
  /**
@@ -55,8 +55,8 @@ module.exports = {
55
55
  * @returns {boolean} whether an error should be reported or not
56
56
  */
57
57
  function shouldReport(effectiveParent, name) {
58
- return effectiveParent.type !== "CallExpression"
59
- && effectiveParent.type !== "NewExpression" &&
58
+ return effectiveParent.type !== "CallExpression" &&
59
+ effectiveParent.type !== "NewExpression" &&
60
60
  isInvalid(name);
61
61
  }
62
62
 
@@ -104,9 +104,9 @@ module.exports = {
104
104
  if (isValidExpression && (isValidExpression === true || isValidExpression(parent, node))) {
105
105
  context.report({
106
106
  node,
107
- message: isShort ?
108
- "Identifier name '{{name}}' is too short (< {{min}})." :
109
- "Identifier name '{{name}}' is too long (> {{max}}).",
107
+ message: isShort
108
+ ? "Identifier name '{{name}}' is too short (< {{min}})."
109
+ : "Identifier name '{{name}}' is too long (> {{max}}).",
110
110
  data: { name, min: minLength, max: maxLength }
111
111
  });
112
112
  }
@@ -63,8 +63,8 @@ module.exports = {
63
63
  * @returns {boolean} whether an error should be reported or not
64
64
  */
65
65
  function shouldReport(effectiveParent, name) {
66
- return effectiveParent.type !== "CallExpression"
67
- && effectiveParent.type !== "NewExpression" &&
66
+ return effectiveParent.type !== "CallExpression" &&
67
+ effectiveParent.type !== "NewExpression" &&
68
68
  isInvalid(name);
69
69
  }
70
70
 
@@ -8,6 +8,12 @@
8
8
 
9
9
  "use strict";
10
10
 
11
+ //------------------------------------------------------------------------------
12
+ // Requirements
13
+ //------------------------------------------------------------------------------
14
+
15
+ const astUtils = require("../ast-utils");
16
+
11
17
  //------------------------------------------------------------------------------
12
18
  // Rule Definition
13
19
  //------------------------------------------------------------------------------
@@ -435,15 +441,10 @@ module.exports = {
435
441
  * @returns {void}
436
442
  */
437
443
  function checkLastReturnStatementLineIndent(node, firstLineIndent) {
438
- const nodeLastToken = sourceCode.getLastToken(node);
439
- let lastToken = nodeLastToken;
440
444
 
441
445
  // in case if return statement ends with ');' we have traverse back to ')'
442
446
  // otherwise we'll measure indent for ';' and replace ')'
443
- while (lastToken.value !== ")") {
444
- lastToken = sourceCode.getTokenBefore(lastToken);
445
- }
446
-
447
+ const lastToken = sourceCode.getLastToken(node, astUtils.isClosingParenToken);
447
448
  const textBeforeClosingParenthesis = sourceCode.getText(lastToken, lastToken.loc.start.column).slice(0, -1);
448
449
 
449
450
  if (textBeforeClosingParenthesis.trim()) {
@@ -691,9 +692,9 @@ module.exports = {
691
692
  function isFirstArrayElementOnSameLine(node) {
692
693
  if (node.type === "ArrayExpression" && node.elements[0]) {
693
694
  return node.elements[0].loc.start.line === node.loc.start.line && node.elements[0].type === "ObjectExpression";
694
- } else {
695
- return false;
696
695
  }
696
+ return false;
697
+
697
698
  }
698
699
 
699
700
  /**
@@ -729,7 +730,7 @@ module.exports = {
729
730
  } else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") {
730
731
  const parentElements = node.parent.type === "ObjectExpression" ? node.parent.properties : node.parent.elements;
731
732
 
732
- if (parentElements[0].loc.start.line === parent.loc.start.line && parentElements[0].loc.end.line !== parent.loc.start.line) {
733
+ if (parentElements[0] && parentElements[0].loc.start.line === parent.loc.start.line && parentElements[0].loc.end.line !== parent.loc.start.line) {
733
734
 
734
735
  /*
735
736
  * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest.
@@ -936,20 +937,20 @@ module.exports = {
936
937
 
937
938
  if (caseIndentStore[switchNode.loc.start.line]) {
938
939
  return caseIndentStore[switchNode.loc.start.line];
940
+ }
941
+ if (typeof switchIndent === "undefined") {
942
+ switchIndent = getNodeIndent(switchNode).goodChar;
943
+ }
944
+
945
+ if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
946
+ caseIndent = switchIndent;
939
947
  } else {
940
- if (typeof switchIndent === "undefined") {
941
- switchIndent = getNodeIndent(switchNode).goodChar;
942
- }
948
+ caseIndent = switchIndent + (indentSize * options.SwitchCase);
949
+ }
943
950
 
944
- if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
945
- caseIndent = switchIndent;
946
- } else {
947
- caseIndent = switchIndent + (indentSize * options.SwitchCase);
948
- }
951
+ caseIndentStore[switchNode.loc.start.line] = caseIndent;
952
+ return caseIndent;
949
953
 
950
- caseIndentStore[switchNode.loc.start.line] = caseIndent;
951
- return caseIndent;
952
- }
953
954
  }
954
955
 
955
956
  /**
@@ -4,6 +4,12 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
+ //------------------------------------------------------------------------------
8
+ // Requirements
9
+ //------------------------------------------------------------------------------
10
+
11
+ const astUtils = require("../ast-utils");
12
+
7
13
  //------------------------------------------------------------------------------
8
14
  // Helpers
9
15
  //------------------------------------------------------------------------------
@@ -15,7 +21,7 @@
15
21
  * @returns {boolean} True if str contains a line terminator.
16
22
  */
17
23
  function containsLineTerminator(str) {
18
- return /[\n\r\u2028\u2029]/.test(str);
24
+ return astUtils.LINEBREAK_MATCHER.test(str);
19
25
  }
20
26
 
21
27
  /**
@@ -364,14 +370,9 @@ module.exports = {
364
370
  * @returns {ASTNode} The last token before a colon punctuator.
365
371
  */
366
372
  function getLastTokenBeforeColon(node) {
367
- let prevNode;
373
+ const colonToken = sourceCode.getTokenAfter(node, astUtils.isColonToken);
368
374
 
369
- while (node && (node.type !== "Punctuator" || node.value !== ":")) {
370
- prevNode = node;
371
- node = sourceCode.getTokenAfter(node);
372
- }
373
-
374
- return prevNode;
375
+ return sourceCode.getTokenBefore(colonToken);
375
376
  }
376
377
 
377
378
  /**
@@ -381,12 +382,7 @@ module.exports = {
381
382
  * @returns {ASTNode} The colon punctuator.
382
383
  */
383
384
  function getNextColon(node) {
384
-
385
- while (node && (node.type !== "Punctuator" || node.value !== ":")) {
386
- node = sourceCode.getTokenAfter(node);
387
- }
388
-
389
- return node;
385
+ return sourceCode.getTokenAfter(node, astUtils.isColonToken);
390
386
  }
391
387
 
392
388
  /**
@@ -417,8 +413,8 @@ module.exports = {
417
413
  function report(property, side, whitespace, expected, mode) {
418
414
  const diff = whitespace.length - expected,
419
415
  nextColon = getNextColon(property.key),
420
- tokenBeforeColon = sourceCode.getTokenOrCommentBefore(nextColon),
421
- tokenAfterColon = sourceCode.getTokenOrCommentAfter(nextColon),
416
+ tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
417
+ tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
422
418
  isKeySide = side === "key",
423
419
  locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,
424
420
  isExtra = diff > 0,
@@ -628,15 +624,16 @@ module.exports = {
628
624
  }
629
625
  };
630
626
 
631
- } else { // Obey beforeColon and afterColon in each property as configured
627
+ }
628
+
629
+ // Obey beforeColon and afterColon in each property as configured
630
+ return {
631
+ Property(node) {
632
+ verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions);
633
+ }
634
+ };
632
635
 
633
- return {
634
- Property(node) {
635
- verifySpacing(node, isSingleLine(node.parent) ? singleLineOptions : multiLineOptions);
636
- }
637
- };
638
636
 
639
- }
640
637
 
641
638
  }
642
639
  };
@@ -342,11 +342,7 @@ module.exports = {
342
342
  */
343
343
  function checkSpacingAroundTokenBefore(node) {
344
344
  if (node) {
345
- let token = sourceCode.getTokenBefore(node);
346
-
347
- while (token.type !== "Keyword") {
348
- token = sourceCode.getTokenBefore(token);
349
- }
345
+ const token = sourceCode.getTokenBefore(node, astUtils.isKeywordToken);
350
346
 
351
347
  checkSpacingAround(token);
352
348
  }
@@ -439,14 +435,7 @@ module.exports = {
439
435
  */
440
436
  function checkSpacingForForOfStatement(node) {
441
437
  checkSpacingAroundFirstToken(node);
442
-
443
- // `of` is not a keyword token.
444
- let token = sourceCode.getTokenBefore(node.right);
445
-
446
- while (token.value !== "of") {
447
- token = sourceCode.getTokenBefore(token);
448
- }
449
- checkSpacingAround(token);
438
+ checkSpacingAround(sourceCode.getTokenBefore(node.right, astUtils.isNotOpeningParenToken));
450
439
  }
451
440
 
452
441
  /**
@@ -77,7 +77,7 @@ module.exports = {
77
77
  return;
78
78
  }
79
79
 
80
- const previous = sourceCode.getTokenOrCommentBefore(node);
80
+ const previous = sourceCode.getTokenBefore(node, { includeComments: true });
81
81
  const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line;
82
82
 
83
83
  if (above) {
@@ -5,6 +5,12 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const astUtils = require("../ast-utils");
13
+
8
14
  //------------------------------------------------------------------------------
9
15
  // Rule Definition
10
16
  //------------------------------------------------------------------------------
@@ -60,7 +66,7 @@ module.exports = {
60
66
  expectedLF = linebreakStyle === "unix",
61
67
  expectedLFChars = expectedLF ? "\n" : "\r\n",
62
68
  source = sourceCode.getText(),
63
- pattern = /\r\n|\r|\n|\u2028|\u2029/g;
69
+ pattern = astUtils.createGlobalLinebreakMatcher();
64
70
  let match;
65
71
 
66
72
  let i = 0;
@@ -139,7 +139,7 @@ module.exports = {
139
139
 
140
140
  token = node;
141
141
  do {
142
- token = sourceCode.getTokenOrCommentBefore(token);
142
+ token = sourceCode.getTokenBefore(token, { includeComments: true });
143
143
  } while (isCommentNodeType(token));
144
144
 
145
145
  if (token && astUtils.isTokenOnSameLine(token, node)) {
@@ -148,7 +148,7 @@ module.exports = {
148
148
 
149
149
  token = node;
150
150
  do {
151
- token = sourceCode.getTokenOrCommentAfter(token);
151
+ token = sourceCode.getTokenAfter(token, { includeComments: true });
152
152
  } while (isCommentNodeType(token));
153
153
 
154
154
  if (token && astUtils.isTokenOnSameLine(node, token)) {
@@ -300,8 +300,8 @@ module.exports = {
300
300
  return;
301
301
  }
302
302
 
303
- const previousTokenOrComment = sourceCode.getTokenOrCommentBefore(node);
304
- const nextTokenOrComment = sourceCode.getTokenOrCommentAfter(node);
303
+ const previousTokenOrComment = sourceCode.getTokenBefore(node, { includeComments: true });
304
+ const nextTokenOrComment = sourceCode.getTokenAfter(node, { includeComments: true });
305
305
 
306
306
  // check for newline before
307
307
  if (!exceptionStartAllowed && before && !lodash.includes(commentAndEmptyLines, prevLineNum) &&
@@ -31,7 +31,7 @@ module.exports = {
31
31
  },
32
32
  after: {
33
33
  enum: ["always", "never"]
34
- },
34
+ }
35
35
  },
36
36
  additionalProperties: false,
37
37
  minProperties: 2
@@ -57,7 +57,7 @@ module.exports = {
57
57
  * @returns {boolean} Whether or not the passed in node is preceded by a blank newline.
58
58
  */
59
59
  function hasNewlineBefore(node) {
60
- const tokenBefore = sourceCode.getTokenOrCommentBefore(node);
60
+ const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true });
61
61
  const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0;
62
62
 
63
63
  return node.loc.start.line - tokenLineBefore >= 2;
@@ -86,7 +86,7 @@ module.exports = {
86
86
  */
87
87
  function hasNewlineAfter(node) {
88
88
  const lastToken = getLastTokenOnLine(node);
89
- const tokenAfter = sourceCode.getTokenOrCommentAfter(lastToken);
89
+ const tokenAfter = sourceCode.getTokenAfter(lastToken, { includeComments: true });
90
90
 
91
91
  return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2;
92
92
  }
@@ -131,7 +131,7 @@ module.exports = {
131
131
  }
132
132
 
133
133
  const firstDirective = directives[0];
134
- const hasTokenOrCommentBefore = !!sourceCode.getTokenOrCommentBefore(firstDirective);
134
+ const hasTokenOrCommentBefore = !!sourceCode.getTokenBefore(firstDirective, { includeComments: true });
135
135
 
136
136
  // Only check before the first directive if it is preceded by a comment or if it is at the top of
137
137
  // the file and expectLineBefore is set to "never". This is to not force a newline at the top of
@@ -90,7 +90,7 @@ module.exports = {
90
90
 
91
91
  token = comment;
92
92
  do {
93
- token = sourceCode.getTokenOrCommentBefore(token);
93
+ token = sourceCode.getTokenBefore(token, { includeComments: true });
94
94
  } while (isCommentNodeType(token));
95
95
 
96
96
  if (token && astUtils.isTokenOnSameLine(token, comment)) {
@@ -99,7 +99,7 @@ module.exports = {
99
99
 
100
100
  token = comment;
101
101
  do {
102
- token = sourceCode.getTokenOrCommentAfter(token);
102
+ token = sourceCode.getTokenAfter(token, { includeComments: true });
103
103
  } while (isCommentNodeType(token));
104
104
 
105
105
  if (token && astUtils.isTokenOnSameLine(comment, token)) {
@@ -134,7 +134,7 @@ module.exports = {
134
134
  message: "File must be at most {{max}} lines long. It's {{actual}} lines long.",
135
135
  data: {
136
136
  max,
137
- actual: lines.length,
137
+ actual: lines.length
138
138
  }
139
139
  });
140
140
  }
@@ -4,6 +4,12 @@
4
4
  */
5
5
  "use strict";
6
6
 
7
+ //------------------------------------------------------------------------------
8
+ // Requirements
9
+ //------------------------------------------------------------------------------
10
+
11
+ const astUtils = require("../ast-utils");
12
+
7
13
  //------------------------------------------------------------------------------
8
14
  // Rule Definition
9
15
  //------------------------------------------------------------------------------
@@ -60,7 +66,7 @@ module.exports = {
60
66
  data: {
61
67
  numberOfStatementsOnThisLine,
62
68
  maxStatementsPerLine,
63
- statements: numberOfStatementsOnThisLine === 1 ? "statement" : "statements",
69
+ statements: numberOfStatementsOnThisLine === 1 ? "statement" : "statements"
64
70
  }
65
71
  });
66
72
  }
@@ -74,12 +80,7 @@ module.exports = {
74
80
  * @returns {Token} The actual last token.
75
81
  */
76
82
  function getActualLastToken(node) {
77
- let lastToken = sourceCode.getLastToken(node);
78
-
79
- if (lastToken.value === ";") {
80
- lastToken = sourceCode.getTokenBefore(lastToken);
81
- }
82
- return lastToken;
83
+ return sourceCode.getLastToken(node, astUtils.isNotSemicolonToken);
83
84
  }
84
85
 
85
86
  /**
@@ -180,9 +180,9 @@ module.exports = {
180
180
  return "non-alpha";
181
181
  } else if (firstChar === firstCharLower) {
182
182
  return "lower";
183
- } else {
184
- return "upper";
185
183
  }
184
+ return "upper";
185
+
186
186
  }
187
187
 
188
188
  /**
@@ -5,6 +5,12 @@
5
5
 
6
6
  "use strict";
7
7
 
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const astUtils = require("../ast-utils");
13
+
8
14
  //------------------------------------------------------------------------------
9
15
  // Rule Definition
10
16
  //------------------------------------------------------------------------------
@@ -201,8 +207,7 @@ module.exports = {
201
207
  message: NEVER_MESSAGE,
202
208
  data: { identifier: node.name },
203
209
  fix(fixer) {
204
- const NEWLINE_REGEX = /\r\n|\r|\n|\u2028|\u2029/;
205
- const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(NEWLINE_REGEX);
210
+ const linesBetween = sourceCode.getText().slice(lastToken.range[1], nextToken.range[0]).split(astUtils.LINEBREAK_MATCHER);
206
211
 
207
212
  return fixer.replaceTextRange([lastToken.range[1], nextToken.range[0]], `${linesBetween.slice(0, -1).join("")}\n${linesBetween[linesBetween.length - 1]}`);
208
213
  }
@@ -60,9 +60,9 @@ module.exports = {
60
60
  return isPrecededByTokens(node, ["do"]);
61
61
  } else if (parentType === "SwitchCase") {
62
62
  return isPrecededByTokens(node, [":"]);
63
- } else {
64
- return isPrecededByTokens(node, [")"]);
65
63
  }
64
+ return isPrecededByTokens(node, [")"]);
65
+
66
66
  }
67
67
 
68
68
  /**
@@ -6,6 +6,8 @@
6
6
 
7
7
  "use strict";
8
8
 
9
+ const astUtils = require("../ast-utils");
10
+
9
11
  //------------------------------------------------------------------------------
10
12
  // Rule Definition
11
13
  //------------------------------------------------------------------------------
@@ -47,7 +49,7 @@ module.exports = {
47
49
  */
48
50
  function getPropertyText(node) {
49
51
  const prefix = node.computed ? "[" : ".";
50
- const lines = sourceCode.getText(node.property).split(/\r\n|\r|\n/g);
52
+ const lines = sourceCode.getText(node.property).split(astUtils.LINEBREAK_MATCHER);
51
53
  const suffix = node.computed && lines.length === 1 ? "]" : "";
52
54
 
53
55
  return prefix + lines[0] + suffix;
@@ -10,7 +10,7 @@ const loopTypes = new Set([
10
10
  "ForOfStatement",
11
11
  "ForInStatement",
12
12
  "WhileStatement",
13
- "DoWhileStatement",
13
+ "DoWhileStatement"
14
14
  ]);
15
15
 
16
16
  // Node types at which we should stop looking for loops. For example, it is fine to declare an async
@@ -18,7 +18,7 @@ const loopTypes = new Set([
18
18
  const boundaryTypes = new Set([
19
19
  "FunctionDeclaration",
20
20
  "FunctionExpression",
21
- "ArrowFunctionExpression",
21
+ "ArrowFunctionExpression"
22
22
  ]);
23
23
 
24
24
  module.exports = {
@@ -26,9 +26,9 @@ module.exports = {
26
26
  docs: {
27
27
  description: "disallow `await` inside of loops",
28
28
  category: "Possible Errors",
29
- recommended: false,
29
+ recommended: false
30
30
  },
31
- schema: [],
31
+ schema: []
32
32
  },
33
33
  create(context) {
34
34
  return {
@@ -69,7 +69,7 @@ module.exports = {
69
69
  }
70
70
  }
71
71
  }
72
- },
72
+ }
73
73
  };
74
74
  }
75
75
  };
@@ -101,9 +101,9 @@ module.exports = {
101
101
  function testForAssign(node) {
102
102
  if (node.test &&
103
103
  (node.test.type === "AssignmentExpression") &&
104
- (node.type === "ForStatement" ?
105
- !isParenthesised(node.test) :
106
- !isParenthesisedTwice(node.test)
104
+ (node.type === "ForStatement"
105
+ ? !isParenthesised(node.test)
106
+ : !isParenthesisedTwice(node.test)
107
107
  )
108
108
  ) {
109
109
 
@@ -123,7 +123,7 @@ module.exports = {
123
123
  node: info.node,
124
124
  loc: node.key.loc,
125
125
  message: "Duplicate key '{{name}}'.",
126
- data: { name },
126
+ data: { name }
127
127
  });
128
128
  }
129
129