Skip to content

Commit

Permalink
Fix: support for MemberExpression with function body. (#7400)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstern6 authored and gyandeeps committed Oct 23, 2016
1 parent 2c8ed2d commit c710584
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
24 changes: 10 additions & 14 deletions lib/rules/indent.js
Expand Up @@ -379,12 +379,17 @@ module.exports = {
* if not present then return null
* @param {ASTNode} node node to examine
* @param {string} type type that is being looked for
* @param {string} stopAtList end points for the evaluating code
* @returns {ASTNode|void} if found then node otherwise null
*/
function getParentNodeByType(node, type) {
function getParentNodeByType(node, type, stopAtList) {
let parent = node.parent;

while (parent.type !== type && parent.type !== "Program") {
if (!stopAtList) {
stopAtList = ["Program"];
}

while (parent.type !== type && stopAtList.indexOf(parent.type) === -1 && parent.type !== "Program") {
parent = parent.parent;
}

Expand All @@ -401,16 +406,6 @@ module.exports = {
return getParentNodeByType(node, "VariableDeclarator");
}

/**
* Returns the ExpressionStatement based on the current node
* if not present then return null
* @param {ASTNode} node node to examine
* @returns {ASTNode|void} if found then node otherwise null
*/
function getAssignmentExpressionNode(node) {
return getParentNodeByType(node, "AssignmentExpression");
}

/**
* Check to see if the node is part of the multi-line variable declaration.
* Also if its on the same line as the varNode
Expand Down Expand Up @@ -876,6 +871,7 @@ module.exports = {
},

MemberExpression(node) {

if (typeof options.MemberExpression === "undefined") {
return;
}
Expand All @@ -888,11 +884,11 @@ module.exports = {
// alter the expectation of correct indentation. Skip them.
// TODO: Add appropriate configuration options for variable
// declarations and assignments.
if (getVariableDeclaratorNode(node)) {
if (getParentNodeByType(node, "VariableDeclarator", ["FunctionExpression", "ArrowFunctionExpression"])) {
return;
}

if (getAssignmentExpressionNode(node)) {
if (getParentNodeByType(node, "AssignmentExpression", ["FunctionExpression"])) {
return;
}

Expand Down
67 changes: 67 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -1929,6 +1929,73 @@ ruleTester.run("indent", rule, {
[7, 8, 0, "BreakStatement"]
])
},
{
code:
"var foo = function(){\n" +
" foo\n" +
" .bar\n" +
"}",
options: [4, {MemberExpression: 1}],
errors: expectedErrors(
[3, 8, 10, "Punctuator"]
)
},
{
code:
"var foo = function(){\n" +
" foo\n" +
" .bar\n" +
"}",
options: [4, {MemberExpression: 2}],
errors: expectedErrors(
[3, 12, 13, "Punctuator"]
)
},
{
code:
"var foo = () => {\n" +
" foo\n" +
" .bar\n" +
"}",
options: [4, {MemberExpression: 2}],
parserOptions: { ecmaVersion: 6 },
errors: expectedErrors(
[3, 12, 13, "Punctuator"]
)
},
{
code:
"JSON\n" +
" .stringify(\n" +
" {\n" +
" foo: bar\n" +
" }\n" +
" )\n",
options: [4],
errors: expectedErrors(
[
[3, 4, 8, "ObjectExpression"],
[4, 8, 12, "Property"],
[5, 4, 8, "ObjectExpression"]
]
)
},
{
code:
"TestClass.prototype.method = function () {\n" +
" return Promise.resolve(3)\n" +
" .then(function (x) {\n" +
" return x;\n" +
" });\n" +
"};",
options: [2, {MemberExpression: 1}],
parserOptions: { ecmaVersion: 6 },
errors: expectedErrors(
[
[3, 4, 6, "Punctuator"]
]
)
},
{
code:
"while (a) \n" +
Expand Down

0 comments on commit c710584

Please sign in to comment.