Skip to content

Commit

Permalink
Chore: refactor with new API
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jan 18, 2017
1 parent 3ba56c3 commit 8f85577
Show file tree
Hide file tree
Showing 30 changed files with 412 additions and 226 deletions.
247 changes: 229 additions & 18 deletions lib/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,213 @@ function isParenthesised(sourceCode, node) {
}

/**
* Gets the `=>` token of the given arrow function node.
* Checks if the given token is an arrow token or not.
*
* @param {ASTNode} node - The arrow function node to get.
* @param {SourceCode} sourceCode - The source code object to get tokens.
* @returns {Token} `=>` token.
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is an arrow token.
*/
function getArrowToken(node, sourceCode) {
let token = sourceCode.getTokenBefore(node.body);
function isArrowToken(token) {
return token.value === "=>" && token.type === "Punctuator";
}

while (token.value !== "=>") {
token = sourceCode.getTokenBefore(token);
}
/**
* Checks if the given token is a comma token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is a comma token.
*/
function isCommaToken(token) {
return token.value === "," && token.type === "Punctuator";
}

/**
* Checks if the given token is a comma token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is a comma token.
*/
function isNotCommaToken(token) {
return token.value !== "," || token.type !== "Punctuator";
}

/**
* Checks if the given token is a semicolon token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is a semicolon token.
*/
function isSemicolonToken(token) {
return token.value === ";" && token.type === "Punctuator";
}

/**
* Checks if the given token is a semicolon token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is a semicolon token.
*/
function isNotSemicolonToken(token) {
return token.value !== ";" || token.type !== "Punctuator";
}

/**
* Checks if the given token is a colon token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is a colon token.
*/
function isColonToken(token) {
return token.value === ":" && token.type === "Punctuator";
}

/**
* Checks if the given token is a colon token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is a colon token.
*/
function isNotColonToken(token) {
return token.value !== ":" || token.type !== "Punctuator";
}

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

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

/**
* Checks if the given token is an opening parenthesis token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is an opening parenthesis token.
*/
function isNotOpeningParenToken(token) {
return token.value !== "(" || token.type !== "Punctuator";
}

/**
* Checks if the given token is a closing parenthesis token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is a closing parenthesis token.
*/
function isNotClosingParenToken(token) {
return token.value !== ")" || token.type !== "Punctuator";
}

return token;
/**
* Checks if the given token is an opening square bracket token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is an opening square bracket token.
*/
function isOpeningBracketToken(token) {
return token.value === "[" && token.type === "Punctuator";
}

/**
* Checks if the given token is a closing square bracket token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is a closing square bracket token.
*/
function isClosingBracketToken(token) {
return token.value === "]" && token.type === "Punctuator";
}

/**
* Checks if the given token is an opening square bracket token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is an opening square bracket token.
*/
function isNotOpeningBracketToken(token) {
return token.value !== "[" || token.type !== "Punctuator";
}

/**
* Checks if the given token is a closing square bracket token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is a closing square bracket token.
*/
function isNotClosingBracketToken(token) {
return token.value !== "]" || token.type !== "Punctuator";
}

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

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

/**
* Checks if the given token is an opening brace token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is an opening brace token.
*/
function isNotOpeningBraceToken(token) {
return token.value !== "{" || token.type !== "Punctuator";
}

/**
* Checks if the given token is a closing brace token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `false` if the token is a closing brace token.
*/
function isNotClosingBraceToken(token) {
return token.value !== "}" || token.type !== "Punctuator";
}

/**
* Checks if the given token is a comment token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is a comment token.
*/
function isCommentToken(token) {
return token.type === "Line" || token.type === "Block";
}

/**
* Checks if the given token is a keyword token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is a keyword token.
*/
function isKeywordToken(token) {
return token.type === "Keyword";
}

/**
Expand All @@ -271,13 +464,9 @@ function getArrowToken(node, sourceCode) {
* @returns {Token} `(` token.
*/
function getOpeningParenOfParams(node, sourceCode) {
let token = node.id ? sourceCode.getTokenAfter(node.id) : sourceCode.getFirstToken(node);

while (token.value !== "(") {
token = sourceCode.getTokenAfter(token);
}

return token;
return node.id
? sourceCode.getTokenAfter(node.id, isOpeningParenToken)
: sourceCode.getFirstToken(node, isOpeningParenToken);
}

const lineIndexCache = new WeakMap();
Expand Down Expand Up @@ -341,6 +530,28 @@ module.exports = {
isArrayFromMethod,
isParenthesised,

isArrowToken,
isClosingBraceToken,
isClosingBracketToken,
isClosingParenToken,
isColonToken,
isCommaToken,
isCommentToken,
isKeywordToken,
isNotCommaToken,
isNotClosingBraceToken,
isNotClosingBracketToken,
isNotClosingParenToken,
isNotColonToken,
isNotOpeningBraceToken,
isNotOpeningBracketToken,
isNotOpeningParenToken,
isNotSemicolonToken,
isOpeningBraceToken,
isOpeningBracketToken,
isOpeningParenToken,
isSemicolonToken,

/**
* Checks whether or not a given node is a string literal.
* @param {ASTNode} node - A node to check.
Expand Down Expand Up @@ -1038,7 +1249,7 @@ module.exports = {
let end = null;

if (node.type === "ArrowFunctionExpression") {
const arrowToken = getArrowToken(node, sourceCode);
const arrowToken = sourceCode.getTokenBefore(node.body, isArrowToken);

start = arrowToken.loc.start;
end = arrowToken.loc.end;
Expand Down
11 changes: 7 additions & 4 deletions lib/rules/arrow-body-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

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

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -136,10 +142,7 @@ module.exports = {
loc: arrowBody.loc.start,
message: "Expected block statement surrounding arrow body.",
fix(fixer) {
const lastTokenBeforeBody = sourceCode.getTokensBetween(sourceCode.getFirstToken(node), arrowBody)
.reverse()
.find(token => token.value !== "(");

const lastTokenBeforeBody = sourceCode.getLastTokenBetween(sourceCode.getFirstToken(node), arrowBody, astUtils.isNotOpeningParenToken);
const firstBodyToken = sourceCode.getTokenAfter(lastTokenBeforeBody);

return fixer.replaceTextRange(
Expand Down
13 changes: 7 additions & 6 deletions lib/rules/arrow-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

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

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

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -51,12 +57,7 @@ module.exports = {
* @returns {Object} Tokens of arrow and before/after arrow.
*/
function getTokens(node) {
let arrow = sourceCode.getTokenBefore(node.body);

// skip '(' tokens.
while (arrow.value !== "=>") {
arrow = sourceCode.getTokenBefore(arrow);
}
const arrow = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken);

return {
before: sourceCode.getTokenBefore(arrow),
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/consistent-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module.exports = {
} else if (node.type === "ArrowFunctionExpression") {

// `=>` token
loc = context.getSourceCode().getTokenBefore(node.body).loc.start;
loc = context.getSourceCode().getTokenBefore(node.body, astUtils.isArrowToken).loc.start;
type = "function";
} else if (
node.parent.type === "MethodDefinition" ||
Expand Down
18 changes: 11 additions & 7 deletions lib/rules/curly.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,23 @@ module.exports = {
return first.loc.start.line === last.loc.end.line;
}

/**
* Checks if the given token is an `else` token or not.
*
* @param {Token} token - The token to be check.
* @returns {boolean} `true` if the token is an `else` token.
*/
function isElseKeywordToken(token) {
return token.value === "else" && token.type === "Keyword";
}

/**
* Gets the `else` keyword token of a given `IfStatement` node.
* @param {ASTNode} node - A `IfStatement` node to get.
* @returns {Token} The `else` keyword token.
*/
function getElseKeyword(node) {
let token = sourceCode.getTokenAfter(node.consequent);

while (token.type !== "Keyword" || token.value !== "else") {
token = sourceCode.getTokenAfter(token);
}

return token;
return node.alternate && sourceCode.getFirstTokenBetween(node.consequent, node.alternate, isElseKeywordToken);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/rules/eqeqeq.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ module.exports = {

// If the comparison is a `typeof` comparison or both sides are literals with the same type, then it's safe to fix.
if (isTypeOfBinary(node) || areLiteralsAndSameType(node)) {
const operatorToken = sourceCode.getTokensBetween(node.left, node.right).find(token => token.value === node.operator);
const operatorToken = sourceCode.getFirstTokenBetween(
node.left,
node.right,
token => token.value === node.operator
);

return fixer.replaceText(operatorToken, expectedOperator);
}
Expand Down

0 comments on commit 8f85577

Please sign in to comment.