Skip to content

Commit

Permalink
Chore: unify checks for statement list parents (#8048)
Browse files Browse the repository at this point in the history
There are currently three types of nodes that can contain a list of statements:

* `Program`
* `BlockStatement`
* `SwitchCase`

Several rules have to check for these node types, and sometimes they do it incorrectly (see #8047). This commit puts the node types in a constant in `ast-utils`.
  • Loading branch information
not-an-aardvark committed Feb 8, 2017
1 parent c596690 commit 329dcdc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
4 changes: 4 additions & 0 deletions lib/ast-utils.js
Expand Up @@ -24,6 +24,9 @@ const bindOrCallOrApplyPattern = /^(?:bind|call|apply)$/;
const breakableTypePattern = /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/;
const thisTagPattern = /^[\s*]*@this/m;

// A set of node types that can contain a list of statements
const STATEMENT_LIST_PARENTS = new Set(["Program", "BlockStatement", "SwitchCase"]);

/**
* Checks reference if is non initializer and writable.
* @param {Reference} reference - A reference to check.
Expand Down Expand Up @@ -319,6 +322,7 @@ function getLineIndices(sourceCode) {
//------------------------------------------------------------------------------

module.exports = {
STATEMENT_LIST_PARENTS,

/**
* Determines whether two adjacent tokens are on the same line.
Expand Down
6 changes: 1 addition & 5 deletions lib/rules/brace-style.js
Expand Up @@ -144,11 +144,7 @@ module.exports = {

return {
BlockStatement(node) {
if (
node.parent.type !== "BlockStatement" &&
node.parent.type !== "SwitchCase" &&
node.parent.type !== "Program"
) {
if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) {
validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node));
}
},
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/no-lone-blocks.js
Expand Up @@ -47,6 +47,10 @@ module.exports = {
function isLoneBlock() {
const parent = context.getAncestors().pop();

/*
* Note: astUtils.STATEMENT_LIST_PARENTS is not used here because blocks in SwitchCases are not checked.
* https://github.com/eslint/eslint/issues/8047
*/
return parent.type === "BlockStatement" || parent.type === "Program";
}

Expand Down
8 changes: 1 addition & 7 deletions lib/rules/no-useless-return.js
Expand Up @@ -45,13 +45,7 @@ function remove(array, element) {
* @returns {boolean} `true` if the node is removeable.
*/
function isRemovable(node) {
const parent = node.parent;

return (
parent.type === "Program" ||
parent.type === "BlockStatement" ||
parent.type === "SwitchCase"
);
return astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions lib/rules/no-var.js
Expand Up @@ -274,9 +274,7 @@ module.exports = {
if (
!isLoopAssignee(node) &&
!(node.parent.type === "ForStatement" && node.parent.init === node) &&
node.parent.type !== "BlockStatement" &&
node.parent.type !== "Program" &&
node.parent.type !== "SwitchCase"
!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)
) {

// If the declaration is not in a block, e.g. `if (foo) var bar = 1;`, then it can't be fixed.
Expand Down

0 comments on commit 329dcdc

Please sign in to comment.