Skip to content

Commit

Permalink
Chore: use updated token iterator methods (#8103)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo authored and alberto committed Feb 19, 2017
1 parent daf6f26 commit f62a724
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/rules/block-spacing.js
Expand Up @@ -74,8 +74,8 @@ module.exports = {
// Gets braces and the first/last token of content.
const openBrace = getOpenBrace(node);
const closeBrace = sourceCode.getLastToken(node);
const firstToken = sourceCode.getTokenOrCommentAfter(openBrace);
const lastToken = sourceCode.getTokenOrCommentBefore(closeBrace);
const firstToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });
const lastToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });

// Skip if the node is invalid or empty.
if (openBrace.type !== "Punctuator" ||
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/capitalized-comments.js
Expand Up @@ -163,8 +163,8 @@ module.exports = {
* otherwise.
*/
function isInlineComment(comment) {
const previousToken = sourceCode.getTokenOrCommentBefore(comment),
nextToken = sourceCode.getTokenOrCommentAfter(comment);
const previousToken = sourceCode.getTokenBefore(comment, { includeComments: true }),
nextToken = sourceCode.getTokenAfter(comment, { includeComments: true });

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

return Boolean(
previousTokenOrComment &&
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/key-spacing.js
Expand Up @@ -413,8 +413,8 @@ module.exports = {
function report(property, side, whitespace, expected, mode) {
const diff = whitespace.length - expected,
nextColon = getNextColon(property.key),
tokenBeforeColon = sourceCode.getTokenOrCommentBefore(nextColon),
tokenAfterColon = sourceCode.getTokenOrCommentAfter(nextColon),
tokenBeforeColon = sourceCode.getTokenBefore(nextColon, { includeComments: true }),
tokenAfterColon = sourceCode.getTokenAfter(nextColon, { includeComments: true }),
isKeySide = side === "key",
locStart = isKeySide ? tokenBeforeColon.loc.start : tokenAfterColon.loc.start,
isExtra = diff > 0,
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/line-comment-position.js
Expand Up @@ -77,7 +77,7 @@ module.exports = {
return;
}

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

if (above) {
Expand Down
8 changes: 4 additions & 4 deletions lib/rules/lines-around-comment.js
Expand Up @@ -139,7 +139,7 @@ module.exports = {

token = node;
do {
token = sourceCode.getTokenOrCommentBefore(token);
token = sourceCode.getTokenBefore(token, { includeComments: true });
} while (isCommentNodeType(token));

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

token = node;
do {
token = sourceCode.getTokenOrCommentAfter(token);
token = sourceCode.getTokenAfter(token, { includeComments: true });
} while (isCommentNodeType(token));

if (token && astUtils.isTokenOnSameLine(node, token)) {
Expand Down Expand Up @@ -300,8 +300,8 @@ module.exports = {
return;
}

const previousTokenOrComment = sourceCode.getTokenOrCommentBefore(node);
const nextTokenOrComment = sourceCode.getTokenOrCommentAfter(node);
const previousTokenOrComment = sourceCode.getTokenBefore(node, { includeComments: true });
const nextTokenOrComment = sourceCode.getTokenAfter(node, { includeComments: true });

// check for newline before
if (!exceptionStartAllowed && before && !lodash.includes(commentAndEmptyLines, prevLineNum) &&
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/lines-around-directive.js
Expand Up @@ -57,7 +57,7 @@ module.exports = {
* @returns {boolean} Whether or not the passed in node is preceded by a blank newline.
*/
function hasNewlineBefore(node) {
const tokenBefore = sourceCode.getTokenOrCommentBefore(node);
const tokenBefore = sourceCode.getTokenBefore(node, { includeComments: true });
const tokenLineBefore = tokenBefore ? tokenBefore.loc.end.line : 0;

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

return tokenAfter.loc.start.line - lastToken.loc.end.line >= 2;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = {
}

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

// Only check before the first directive if it is preceded by a comment or if it is at the top of
// the file and expectLineBefore is set to "never". This is to not force a newline at the top of
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-lines.js
Expand Up @@ -90,7 +90,7 @@ module.exports = {

token = comment;
do {
token = sourceCode.getTokenOrCommentBefore(token);
token = sourceCode.getTokenBefore(token, { includeComments: true });
} while (isCommentNodeType(token));

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

token = comment;
do {
token = sourceCode.getTokenOrCommentAfter(token);
token = sourceCode.getTokenAfter(token, { includeComments: true });
} while (isCommentNodeType(token));

if (token && astUtils.isTokenOnSameLine(comment, token)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unused-labels.js
Expand Up @@ -59,7 +59,7 @@ module.exports = {
* Only perform a fix if there are no comments between the label and the body. This will be the case
* when there is exactly one token/comment (the ":") between the label and the body.
*/
if (sourceCode.getTokenOrCommentAfter(node.label) === sourceCode.getTokenOrCommentBefore(node.body)) {
if (sourceCode.getTokenAfter(node.label, { includeComments: true }) === sourceCode.getTokenBefore(node.body, { includeComments: true })) {
return fixer.removeRange([node.range[0], node.body.range[0]]);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/object-curly-newline.js
Expand Up @@ -128,8 +128,8 @@ module.exports = {
const options = normalizedOptions[node.type];
const openBrace = sourceCode.getFirstToken(node);
const closeBrace = sourceCode.getLastToken(node);
let first = sourceCode.getTokenOrCommentAfter(openBrace);
let last = sourceCode.getTokenOrCommentBefore(closeBrace);
let first = sourceCode.getTokenAfter(openBrace, { includeComments: true });
let last = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
const needsLinebreaks = (
node.properties.length >= options.minProperties ||
(
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/operator-linebreak.js
Expand Up @@ -87,7 +87,7 @@ module.exports = {
if (hasLinebreakBefore !== hasLinebreakAfter && desiredStyle !== "none") {

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

Expand Down
8 changes: 4 additions & 4 deletions lib/rules/padded-blocks.js
Expand Up @@ -101,7 +101,7 @@ module.exports = {
let first = token;

do {
first = sourceCode.getTokenOrCommentAfter(first);
first = sourceCode.getTokenAfter(first, { includeComments: true });
} while (isComment(first) && first.loc.start.line === tokenStartLine);

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

do {
last = sourceCode.getTokenOrCommentBefore(last);
last = sourceCode.getTokenBefore(last, { includeComments: true });
} while (isComment(last) && last.loc.end.line === blockEnd);

const lastLine = last.loc.end.line;
Expand Down Expand Up @@ -182,7 +182,7 @@ module.exports = {
}
} else {
if (blockHasTopPadding) {
const nextToken = sourceCode.getTokenOrCommentAfter(openBrace);
const nextToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });

context.report({
node,
Expand All @@ -195,7 +195,7 @@ module.exports = {
}

if (blockHasBottomPadding) {
const previousToken = sourceCode.getTokenOrCommentBefore(closeBrace);
const previousToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });

context.report({
node,
Expand Down

0 comments on commit f62a724

Please sign in to comment.