Skip to content

Commit f62a724

Browse files
kaicataldoalberto
authored andcommitted
Chore: use updated token iterator methods (#8103)
1 parent daf6f26 commit f62a724

11 files changed

+25
-25
lines changed

lib/rules/block-spacing.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ module.exports = {
7474
// Gets braces and the first/last token of content.
7575
const openBrace = getOpenBrace(node);
7676
const closeBrace = sourceCode.getLastToken(node);
77-
const firstToken = sourceCode.getTokenOrCommentAfter(openBrace);
78-
const lastToken = sourceCode.getTokenOrCommentBefore(closeBrace);
77+
const firstToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });
78+
const lastToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
7979

8080
// Skip if the node is invalid or empty.
8181
if (openBrace.type !== "Punctuator" ||

lib/rules/capitalized-comments.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ module.exports = {
163163
* otherwise.
164164
*/
165165
function isInlineComment(comment) {
166-
const previousToken = sourceCode.getTokenOrCommentBefore(comment),
167-
nextToken = sourceCode.getTokenOrCommentAfter(comment);
166+
const previousToken = sourceCode.getTokenBefore(comment, { includeComments: true }),
167+
nextToken = sourceCode.getTokenAfter(comment, { includeComments: true });
168168

169169
return Boolean(
170170
previousToken &&
@@ -181,7 +181,7 @@ module.exports = {
181181
* @returns {boolean} True if the comment follows a valid comment.
182182
*/
183183
function isConsecutiveComment(comment) {
184-
const previousTokenOrComment = sourceCode.getTokenOrCommentBefore(comment);
184+
const previousTokenOrComment = sourceCode.getTokenBefore(comment, { includeComments: true });
185185

186186
return Boolean(
187187
previousTokenOrComment &&

lib/rules/key-spacing.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,8 @@ module.exports = {
413413
function report(property, side, whitespace, expected, mode) {
414414
const diff = whitespace.length - expected,
415415
nextColon = getNextColon(property.key),
416-
tokenBeforeColon = sourceCode.getTokenOrCommentBefore(nextColon),
417-
tokenAfterColon = sourceCode.getTokenOrCommentAfter(nextColon),
416+
tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
417+
tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
418418
isKeySide = side === "key",
419419
locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,
420420
isExtra = diff > 0,

lib/rules/line-comment-position.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ module.exports = {
7777
return;
7878
}
7979

80-
const previous = sourceCode.getTokenOrCommentBefore(node);
80+
const previous = sourceCode.getTokenBefore(node, { includeComments: true });
8181
const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line;
8282

8383
if (above) {

lib/rules/lines-around-comment.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ module.exports = {
139139

140140
token = node;
141141
do {
142-
token = sourceCode.getTokenOrCommentBefore(token);
142+
token = sourceCode.getTokenBefore(token, { includeComments: true });
143143
} while (isCommentNodeType(token));
144144

145145
if (token && astUtils.isTokenOnSameLine(token, node)) {
@@ -148,7 +148,7 @@ module.exports = {
148148

149149
token = node;
150150
do {
151-
token = sourceCode.getTokenOrCommentAfter(token);
151+
token = sourceCode.getTokenAfter(token, { includeComments: true });
152152
} while (isCommentNodeType(token));
153153

154154
if (token && astUtils.isTokenOnSameLine(node, token)) {
@@ -300,8 +300,8 @@ module.exports = {
300300
return;
301301
}
302302

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 });
305305

306306
// check for newline before
307307
if (!exceptionStartAllowed && before && !lodash.includes(commentAndEmptyLines, prevLineNum) &&

lib/rules/lines-around-directive.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = {
5757
* @returns {boolean} Whether or not the passed in node is preceded by a blank newline.
5858
*/
5959
function hasNewlineBefore(node) {
60-
const tokenBefore = sourceCode.getTokenOrCommentBefore(node);
60+
const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true });
6161
const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0;
6262

6363
return node.loc.start.line - tokenLineBefore >= 2;
@@ -86,7 +86,7 @@ module.exports = {
8686
*/
8787
function hasNewlineAfter(node) {
8888
const lastToken = getLastTokenOnLine(node);
89-
const tokenAfter = sourceCode.getTokenOrCommentAfter(lastToken);
89+
const tokenAfter = sourceCode.getTokenAfter(lastToken, { includeComments: true });
9090

9191
return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2;
9292
}
@@ -131,7 +131,7 @@ module.exports = {
131131
}
132132

133133
const firstDirective = directives[0];
134-
const hasTokenOrCommentBefore = !!sourceCode.getTokenOrCommentBefore(firstDirective);
134+
const hasTokenOrCommentBefore = !!sourceCode.getTokenBefore(firstDirective, { includeComments: true });
135135

136136
// Only check before the first directive if it is preceded by a comment or if it is at the top of
137137
// the file and expectLineBefore is set to "never". This is to not force a newline at the top of

lib/rules/max-lines.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = {
9090

9191
token = comment;
9292
do {
93-
token = sourceCode.getTokenOrCommentBefore(token);
93+
token = sourceCode.getTokenBefore(token, { includeComments: true });
9494
} while (isCommentNodeType(token));
9595

9696
if (token && astUtils.isTokenOnSameLine(token, comment)) {
@@ -99,7 +99,7 @@ module.exports = {
9999

100100
token = comment;
101101
do {
102-
token = sourceCode.getTokenOrCommentAfter(token);
102+
token = sourceCode.getTokenAfter(token, { includeComments: true });
103103
} while (isCommentNodeType(token));
104104

105105
if (token && astUtils.isTokenOnSameLine(comment, token)) {

lib/rules/no-unused-labels.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = {
5959
* Only perform a fix if there are no comments between the label and the body. This will be the case
6060
* when there is exactly one token/comment (the ":") between the label and the body.
6161
*/
62-
if (sourceCode.getTokenOrCommentAfter(node.label) === sourceCode.getTokenOrCommentBefore(node.body)) {
62+
if (sourceCode.getTokenAfter(node.label, { includeComments: true }) === sourceCode.getTokenBefore(node.body, { includeComments: true })) {
6363
return fixer.removeRange([node.range[0], node.body.range[0]]);
6464
}
6565

lib/rules/object-curly-newline.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ module.exports = {
128128
const options = normalizedOptions[node.type];
129129
const openBrace = sourceCode.getFirstToken(node);
130130
const closeBrace = sourceCode.getLastToken(node);
131-
let first = sourceCode.getTokenOrCommentAfter(openBrace);
132-
let last = sourceCode.getTokenOrCommentBefore(closeBrace);
131+
let first = sourceCode.getTokenAfter(openBrace, { includeComments: true });
132+
let last = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
133133
const needsLinebreaks = (
134134
node.properties.length >= options.minProperties ||
135135
(

lib/rules/operator-linebreak.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ module.exports = {
8787
if (hasLinebreakBefore !== hasLinebreakAfter && desiredStyle !== "none") {
8888

8989
// If there is a comment before and after the operator, don't do a fix.
90-
if (sourceCode.getTokenOrCommentBefore(operatorToken) !== tokenBefore && sourceCode.getTokenOrCommentAfter(operatorToken) !== tokenAfter) {
90+
if (sourceCode.getTokenBefore(operatorToken, { includeComments: true }) !== tokenBefore && sourceCode.getTokenAfter(operatorToken, { includeComments: true }) !== tokenAfter) {
9191
return null;
9292
}
9393

lib/rules/padded-blocks.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = {
101101
let first = token;
102102

103103
do {
104-
first = sourceCode.getTokenOrCommentAfter(first);
104+
first = sourceCode.getTokenAfter(first, { includeComments: true });
105105
} while (isComment(first) && first.loc.start.line === tokenStartLine);
106106

107107
const firstLine = first.loc.start.line;
@@ -120,7 +120,7 @@ module.exports = {
120120
let last = token;
121121

122122
do {
123-
last = sourceCode.getTokenOrCommentBefore(last);
123+
last = sourceCode.getTokenBefore(last, { includeComments: true });
124124
} while (isComment(last) && last.loc.end.line === blockEnd);
125125

126126
const lastLine = last.loc.end.line;
@@ -182,7 +182,7 @@ module.exports = {
182182
}
183183
} else {
184184
if (blockHasTopPadding) {
185-
const nextToken = sourceCode.getTokenOrCommentAfter(openBrace);
185+
const nextToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });
186186

187187
context.report({
188188
node,
@@ -195,7 +195,7 @@ module.exports = {
195195
}
196196

197197
if (blockHasBottomPadding) {
198-
const previousToken = sourceCode.getTokenOrCommentBefore(closeBrace);
198+
const previousToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
199199

200200
context.report({
201201
node,

0 commit comments

Comments
 (0)