Skip to content

Commit

Permalink
Chore: update token logic in rules to use ast-utils (#8288)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Mar 20, 2017
1 parent 9b509ce commit 7521cd5
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 119 deletions.
4 changes: 2 additions & 2 deletions lib/rules/arrow-body-style.js
Expand Up @@ -100,7 +100,7 @@ module.exports = {
const firstValueToken = sourceCode.getTokenAfter(returnKeyword);
let lastValueToken = sourceCode.getLastToken(blockBody[0]);

if (lastValueToken.type === "Punctuator" && lastValueToken.value === ";") {
if (astUtils.isSemicolonToken(lastValueToken)) {

/* The last token of the returned value is the last token of the ReturnExpression (if
* the ReturnExpression has no semicolon), or the second-to-last token (if the ReturnExpression
Expand All @@ -120,7 +120,7 @@ module.exports = {
const textBeforeReturn = sourceText.slice(arrowBody.range[0] + 1, returnKeyword.range[0]);
const textBetweenReturnAndValue = sourceText.slice(returnKeyword.range[1], firstValueToken.range[0]);
const rawReturnValueText = sourceText.slice(firstValueToken.range[0], lastValueToken.range[1]);
const returnValueText = firstValueToken.value === "{" ? `(${rawReturnValueText})` : rawReturnValueText;
const returnValueText = astUtils.isOpeningBraceToken(firstValueToken) ? `(${rawReturnValueText})` : rawReturnValueText;
const textAfterValue = sourceText.slice(lastValueToken.range[1], blockBody[0].range[1] - 1);
const textAfterReturnStatement = sourceText.slice(blockBody[0].range[1], arrowBody.range[1] - 1);

Expand Down
12 changes: 9 additions & 3 deletions lib/rules/arrow-parens.js
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -62,7 +68,7 @@ module.exports = {
node.body.type !== "BlockStatement" &&
!node.returnType
) {
if (token.type === "Punctuator" && token.value === "(") {
if (astUtils.isOpeningParenToken(token)) {
context.report({
node,
message: requireForBlockBodyMessage,
Expand All @@ -84,7 +90,7 @@ module.exports = {
requireForBlockBody &&
node.body.type === "BlockStatement"
) {
if (token.type !== "Punctuator" || token.value !== "(") {
if (!astUtils.isOpeningParenToken(token)) {
context.report({
node,
message: requireForBlockBodyNoParensMessage,
Expand All @@ -103,7 +109,7 @@ module.exports = {
!node.params[0].typeAnnotation &&
!node.returnType
) {
if (token.type === "Punctuator" && token.value === "(") {
if (astUtils.isOpeningParenToken(token)) {
context.report({
node,
message: asNeededMessage,
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/comma-dangle.js
Expand Up @@ -10,6 +10,7 @@
//------------------------------------------------------------------------------

const lodash = require("lodash");
const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -178,7 +179,7 @@ module.exports = {
default: {
const nextToken = sourceCode.getTokenAfter(lastItem);

if (nextToken.value === ",") {
if (astUtils.isCommaToken(nextToken)) {
return nextToken;
}
return sourceCode.getLastToken(lastItem);
Expand Down Expand Up @@ -224,7 +225,7 @@ module.exports = {

const trailingToken = getTrailingToken(node, lastItem);

if (trailingToken.value === ",") {
if (astUtils.isCommaToken(trailingToken)) {
context.report({
node: lastItem,
loc: trailingToken.loc.start,
Expand Down
18 changes: 4 additions & 14 deletions lib/rules/comma-spacing.js
Expand Up @@ -53,16 +53,6 @@ module.exports = {
// list of comma tokens to ignore for the check of leading whitespace
const commaTokensToIgnore = [];

/**
* Determines if a given token is a comma operator.
* @param {ASTNode} token The token to check.
* @returns {boolean} True if the token is a comma, false if not.
* @private
*/
function isComma(token) {
return !!token && (token.type === "Punctuator") && (token.value === ",");
}

/**
* Reports a spacing error with an appropriate message.
* @param {ASTNode} node The binary expression node to report.
Expand Down Expand Up @@ -147,7 +137,7 @@ module.exports = {
if (element === null) {
token = sourceCode.getTokenAfter(previousToken);

if (isComma(token)) {
if (astUtils.isCommaToken(token)) {
commaTokensToIgnore.push(token);
}
} else {
Expand All @@ -166,7 +156,7 @@ module.exports = {
"Program:exit"() {
tokensAndComments.forEach((token, i) => {

if (!isComma(token)) {
if (!astUtils.isCommaToken(token)) {
return;
}

Expand All @@ -179,8 +169,8 @@ module.exports = {

validateCommaItemSpacing({
comma: token,
left: isComma(previousToken) || commaTokensToIgnore.indexOf(token) > -1 ? null : previousToken,
right: isComma(nextToken) ? null : nextToken
left: astUtils.isCommaToken(previousToken) || commaTokensToIgnore.indexOf(token) > -1 ? null : previousToken,
right: astUtils.isCommaToken(nextToken) ? null : nextToken
}, token);
});
},
Expand Down
16 changes: 3 additions & 13 deletions lib/rules/comma-style.js
Expand Up @@ -63,16 +63,6 @@ module.exports = {
// Helpers
//--------------------------------------------------------------------------

/**
* Determines if a given token is a comma operator.
* @param {ASTNode} token The token to check.
* @returns {boolean} True if the token is a comma, false if not.
* @private
*/
function isComma(token) {
return !!token && (token.type === "Punctuator") && (token.value === ",");
}

/**
* Modified text based on the style
* @param {string} styleType Style type
Expand Down Expand Up @@ -192,7 +182,7 @@ module.exports = {
tokenBeforeComma = sourceCode.getTokenBefore(commaToken);

// Check if previous token is wrapped in parentheses
if (tokenBeforeComma && tokenBeforeComma.value === ")") {
if (tokenBeforeComma && astUtils.isClosingParenToken(tokenBeforeComma)) {
previousItemToken = tokenBeforeComma;
}

Expand All @@ -210,7 +200,7 @@ module.exports = {
* All comparisons are done based on these tokens directly, so
* they are always valid regardless of an undefined item.
*/
if (isComma(commaToken)) {
if (astUtils.isCommaToken(commaToken)) {
validateCommaItemSpacing(previousItemToken, commaToken,
currentItemToken, reportItem);
}
Expand All @@ -229,7 +219,7 @@ module.exports = {
const lastToken = sourceCode.getLastToken(node),
nextToLastToken = sourceCode.getTokenBefore(lastToken);

if (isComma(nextToLastToken)) {
if (astUtils.isCommaToken(nextToLastToken)) {
validateCommaItemSpacing(
sourceCode.getTokenBefore(nextToLastToken),
nextToLastToken,
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/curly.js
Expand Up @@ -76,7 +76,7 @@ module.exports = {
function isCollapsedOneLiner(node) {
const before = sourceCode.getTokenBefore(node);
const last = sourceCode.getLastToken(node);
const lastExcludingSemicolon = last.type === "Punctuator" && last.value === ";" ? sourceCode.getTokenBefore(last) : last;
const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last;

return before.loc.start.line === lastExcludingSemicolon.loc.end.line;
}
Expand Down Expand Up @@ -174,7 +174,7 @@ module.exports = {
const tokenAfter = sourceCode.getTokenAfter(closingBracket);
const lastBlockNode = sourceCode.getNodeByRangeIndex(tokenBefore.range[0]);

if (tokenBefore.value === ";") {
if (astUtils.isSemicolonToken(tokenBefore)) {

// If the last statement already has a semicolon, don't add another one.
return false;
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/lines-around-directive.js
Expand Up @@ -74,7 +74,7 @@ module.exports = {
const lastToken = sourceCode.getLastToken(node);
const secondToLastToken = sourceCode.getTokenBefore(lastToken);

return lastToken.type === "Punctuator" && lastToken.value === ";" && lastToken.loc.start.line > secondToLastToken.loc.end.line
return astUtils.isSemicolonToken(lastToken) && lastToken.loc.start.line > secondToLastToken.loc.end.line
? secondToLastToken
: lastToken;
}
Expand Down
28 changes: 7 additions & 21 deletions lib/rules/new-parens.js
Expand Up @@ -6,28 +6,14 @@
"use strict";

//------------------------------------------------------------------------------
// Helpers
// Requirements
//------------------------------------------------------------------------------

/**
* Checks whether the given token is an opening parenthesis or not.
*
* @param {Token} token - The token to check.
* @returns {boolean} `true` if the token is an opening parenthesis.
*/
function isOpeningParen(token) {
return token.type === "Punctuator" && token.value === "(";
}
const astUtils = require("../ast-utils");

/**
* Checks whether the given token is an closing parenthesis or not.
*
* @param {Token} token - The token to check.
* @returns {boolean} `true` if the token is an closing parenthesis.
*/
function isClosingParen(token) {
return token.type === "Punctuator" && token.value === ")";
}
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -56,8 +42,8 @@ module.exports = {
}

const lastToken = sourceCode.getLastToken(node);
const hasLastParen = lastToken && isClosingParen(lastToken);
const hasParens = hasLastParen && isOpeningParen(sourceCode.getTokenBefore(lastToken));
const hasLastParen = lastToken && astUtils.isClosingParenToken(lastToken);
const hasParens = hasLastParen && astUtils.isOpeningParenToken(sourceCode.getTokenBefore(lastToken));

if (!hasParens) {
context.report({
Expand Down
21 changes: 4 additions & 17 deletions lib/rules/no-cond-assign.js
Expand Up @@ -66,19 +66,6 @@ module.exports = {
return null;
}

/**
* Check whether the code represented by an AST node is enclosed in parentheses.
* @param {!Object} node The node to test.
* @returns {boolean} `true` if the code is enclosed in parentheses; otherwise, `false`.
*/
function isParenthesised(node) {
const previousToken = sourceCode.getTokenBefore(node),
nextToken = sourceCode.getTokenAfter(node);

return previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
nextToken.value === ")" && nextToken.range[0] >= node.range[1];
}

/**
* Check whether the code represented by an AST node is enclosed in two sets of parentheses.
* @param {!Object} node The node to test.
Expand All @@ -88,9 +75,9 @@ module.exports = {
const previousToken = sourceCode.getTokenBefore(node, 1),
nextToken = sourceCode.getTokenAfter(node, 1);

return isParenthesised(node) &&
previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
nextToken.value === ")" && nextToken.range[0] >= node.range[1];
return astUtils.isParenthesised(sourceCode, node) &&
astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
}

/**
Expand All @@ -102,7 +89,7 @@ module.exports = {
if (node.test &&
(node.test.type === "AssignmentExpression") &&
(node.type === "ForStatement"
? !isParenthesised(node.test)
? !astUtils.isParenthesised(sourceCode, node.test)
: !isParenthesisedTwice(node.test)
)
) {
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-extra-parens.js
Expand Up @@ -116,8 +116,8 @@ module.exports = {
nextToken = sourceCode.getTokenAfter(node, 1);

return isParenthesised(node) && previousToken && nextToken &&
previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
nextToken.value === ")" && nextToken.range[0] >= node.range[1];
astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
}

/**
Expand Down Expand Up @@ -179,7 +179,7 @@ module.exports = {
const lastToken = sourceCode.getLastToken(newExpression);
const penultimateToken = sourceCode.getTokenBefore(lastToken);

return newExpression.arguments.length > 0 || penultimateToken.value === "(" && lastToken.value === ")";
return newExpression.arguments.length > 0 || astUtils.isOpeningParenToken(penultimateToken) && astUtils.isClosingParenToken(lastToken);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-extra-semi.js
Expand Up @@ -10,6 +10,7 @@
//------------------------------------------------------------------------------

const FixTracker = require("../util/fix-tracker");
const astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -60,10 +61,10 @@ module.exports = {
*/
function checkForPartOfClassBody(firstToken) {
for (let token = firstToken;
token.type === "Punctuator" && token.value !== "}";
token.type === "Punctuator" && !astUtils.isClosingBraceToken(token);
token = sourceCode.getTokenAfter(token)
) {
if (token.value === ";") {
if (astUtils.isSemicolonToken(token)) {
report(token);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-sequences.js
Expand Up @@ -76,8 +76,8 @@ module.exports = {
nextToken = sourceCode.getTokenAfter(node, 1);

return isParenthesised(node) && previousToken && nextToken &&
previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
nextToken.value === ")" && nextToken.range[0] >= node.range[1];
astUtils.isOpeningParenToken(previousToken) && previousToken.range[1] <= node.range[0] &&
astUtils.isClosingParenToken(nextToken) && nextToken.range[0] >= node.range[1];
}

return {
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/object-curly-spacing.js
Expand Up @@ -171,8 +171,8 @@ module.exports = {

if (astUtils.isTokenOnSameLine(penultimate, last)) {
const shouldCheckPenultimate = (
options.arraysInObjectsException && penultimate.value === "]" ||
options.objectsInObjectsException && penultimate.value === "}"
options.arraysInObjectsException && astUtils.isClosingBracketToken(penultimate) ||
options.objectsInObjectsException && astUtils.isClosingBraceToken(penultimate)
);
const penultimateType = shouldCheckPenultimate && sourceCode.getNodeByRangeIndex(penultimate.start).type;

Expand Down
17 changes: 2 additions & 15 deletions lib/rules/semi-spacing.js
Expand Up @@ -105,20 +105,7 @@ module.exports = {
function isBeforeClosingParen(token) {
const nextToken = sourceCode.getTokenAfter(token);

return (
nextToken &&
nextToken.type === "Punctuator" &&
(nextToken.value === "}" || nextToken.value === ")")
);
}

/**
* Checks if the given token is a semicolon.
* @param {Token} token The token to check.
* @returns {boolean} Whether or not the given token is a semicolon.
*/
function isSemicolon(token) {
return token.type === "Punctuator" && token.value === ";";
return (nextToken && astUtils.isClosingBraceToken(nextToken) || astUtils.isClosingParenToken(nextToken));
}

/**
Expand All @@ -128,7 +115,7 @@ module.exports = {
* @returns {void}
*/
function checkSemicolonSpacing(token, node) {
if (isSemicolon(token)) {
if (astUtils.isSemicolonToken(token)) {
const location = token.loc.start;

if (hasLeadingSpace(token)) {
Expand Down

0 comments on commit 7521cd5

Please sign in to comment.