Skip to content

Commit

Permalink
Extract isNodeFirstInLine to astUtil function
Browse files Browse the repository at this point in the history
  • Loading branch information
jseminck committed Nov 16, 2017
1 parent 6d50fb6 commit c51087c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 72 deletions.
31 changes: 4 additions & 27 deletions lib/rules/jsx-closing-tag-location.js
Expand Up @@ -4,6 +4,8 @@
*/
'use strict';

const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand All @@ -18,31 +20,6 @@ module.exports = {
},

create: function(context) {
const sourceCode = context.getSourceCode();

/**
* Checks if the node is the first in its line, excluding whitespace.
* @param {ASTNode} node The node to check
* @return {Boolean} true if its the first node in its line
*/
function isNodeFirstInLine(node) {
let token = node;
let lines;
do {
token = sourceCode.getTokenBefore(token);
lines = token.type === 'JSXText'
? token.value.split('\n')
: null;
} while (
token.type === 'JSXText' &&
/^\s*$/.test(lines[lines.length - 1])
);

const startLine = node.loc.start.line;
const endLine = token ? token.loc.end.line : -1;
return startLine !== endLine;
}

return {
JSXClosingElement: function(node) {
if (!node.parent) {
Expand All @@ -59,7 +36,7 @@ module.exports = {
}

let message;
if (!isNodeFirstInLine(node)) {
if (!astUtil.isNodeFirstInLine(context, node)) {
message = 'Closing tag of a multiline JSX expression must be on its own line.';
} else {
message = 'Expected closing tag to match indentation of opening.';
Expand All @@ -71,7 +48,7 @@ module.exports = {
message,
fix: function(fixer) {
const indent = Array(opening.loc.start.column + 1).join(' ');
if (isNodeFirstInLine(node)) {
if (astUtil.isNodeFirstInLine(context, node)) {
return fixer.replaceTextRange(
[node.range[0] - node.loc.start.column, node.range[0]],
indent
Expand Down
27 changes: 3 additions & 24 deletions lib/rules/jsx-indent.js
Expand Up @@ -29,6 +29,8 @@
*/
'use strict';

const astUtil = require('../util/ast');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -152,29 +154,6 @@ module.exports = {
return indent ? indent[0].length : 0;
}

/**
* Checks node is the first in its own start line. By default it looks by start line.
* @param {ASTNode} node The node to check
* @return {Boolean} true if its the first in the its start line
*/
function isNodeFirstInLine(node) {
let token = node;
let lines;
do {
token = sourceCode.getTokenBefore(token);
lines = token.type === 'JSXText'
? token.value.split('\n')
: null;
} while (
token.type === 'JSXText' &&
/^\s*$/.test(lines[lines.length - 1])
);

const startLine = node.loc.start.line;
const endLine = token ? token.loc.end.line : -1;
return startLine !== endLine;
}

/**
* Check if the node is the right member of a logical expression
* @param {ASTNode} node The node to check
Expand Down Expand Up @@ -216,7 +195,7 @@ module.exports = {
const isCorrectAlternateInCondExp = isAlternateInConditionalExp(node) && (nodeIndent - indent) === 0;
if (
nodeIndent !== indent &&
isNodeFirstInLine(node) &&
astUtil.isNodeFirstInLine(context, node) &&
!isCorrectRightInLogicalExp &&
!isCorrectAlternateInCondExp
) {
Expand Down
69 changes: 48 additions & 21 deletions lib/util/ast.js
Expand Up @@ -3,6 +3,30 @@
*/
'use strict';

/**
* Find a return statment in the current node
*
* @param {ASTNode} ASTnode The AST node being checked
*/
function findReturnStatement(node) {
if (
(!node.value || !node.value.body || !node.value.body.body) &&
(!node.body || !node.body.body)
) {
return false;
}

const bodyNodes = (node.value ? node.value.body.body : node.body.body);

let i = bodyNodes.length - 1;
for (; i >= 0; i--) {
if (bodyNodes[i].type === 'ReturnStatement') {
return bodyNodes[i];
}
}
return false;
}

/**
* Get properties name
* @param {Object} node - Property.
Expand Down Expand Up @@ -36,31 +60,34 @@ function getComponentProperties(node) {
}

/**
* Find a return statment in the current node
*
* @param {ASTNode} ASTnode The AST node being checked
*/
function findReturnStatement(node) {
if (
(!node.value || !node.value.body || !node.value.body.body) &&
(!node.body || !node.body.body)
) {
return false;
}

const bodyNodes = (node.value ? node.value.body.body : node.body.body);
* Checks if the node is the first in its line, excluding whitespace.
* @param {Object} context The node to check
* @param {ASTNode} node The node to check
* @return {Boolean} true if its the first node in its line
*/
function isNodeFirstInLine(context, node) {
const sourceCode = context.getSourceCode();
let token = node;
let lines;
do {
token = sourceCode.getTokenBefore(token);
lines = token.type === 'JSXText'
? token.value.split('\n')
: null;
} while (
token.type === 'JSXText' &&
/^\s*$/.test(lines[lines.length - 1])
);

let i = bodyNodes.length - 1;
for (; i >= 0; i--) {
if (bodyNodes[i].type === 'ReturnStatement') {
return bodyNodes[i];
}
}
return false;
const startLine = node.loc.start.line;
const endLine = token ? token.loc.end.line : -1;
return startLine !== endLine;
}


module.exports = {
findReturnStatement: findReturnStatement,
getPropertyName: getPropertyName,
getComponentProperties: getComponentProperties,
findReturnStatement: findReturnStatement
isNodeFirstInLine: isNodeFirstInLine
};

0 comments on commit c51087c

Please sign in to comment.