Skip to content

Commit

Permalink
Merge pull request #65 from pbredenberg/pb/fluent-chaining-array-acce…
Browse files Browse the repository at this point in the history
…ssors

feat: allow array access in fluent chaining (#54)
  • Loading branch information
onebytegone authored Dec 1, 2024
2 parents c62e2ca + c112091 commit f2c5000
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
26 changes: 22 additions & 4 deletions lib/rules/fluent-chaining.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,31 @@ module.exports = {
}

function checkMemberExpression(node) {
var numberOfLines = node.property.loc.start.line - node.object.loc.end.line + 1,
objectComments = helper.sourceCode.getCommentsAfter(node.object);
var objectComments,
numberOfLines;

numberOfLines = node.property && node.object
? node.property.loc.start.line - node.object.loc.end.line + 1
: null;

// If this is an array accessor, pass if it's on the same line as its
// parent member expression's EOL.
if (node.property && node.type === 'MemberExpression' && node.computed && node.property.type === 'Literal') {
// This is an array accessor, check the previous node.
let parentNode = node.parent;

if (parentNode && parentNode.loc.end.line === node.property.loc.start.line) {
// It's okay for array accessors to be on the same line.
return;
}
}

if (node.object.type === 'FunctionExpression') {
if (node.object && node.object.type === 'FunctionExpression' || numberOfLines === null) {
return;
}

objectComments = helper.sourceCode.getCommentsAfter(node.object);

// if there are leading comments, we need to subtract their lines from the number
// of lines of the expression or we will not be able to put comments between
// calls. Note: trailing comments of the `node.object` are leading comments for
Expand Down Expand Up @@ -118,7 +136,7 @@ module.exports = {
// --------------------------------------------------------------------------

return {
'MemberExpression': checkMemberExpression,
'CallExpression, MemberExpression': checkMemberExpression,
};

},
Expand Down
21 changes: 21 additions & 0 deletions tests/lib/rules/fluent-chaining.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ function checkNewLineError() {
};
}

function checkAllowMemberExpressionWithArrayAccess() {
return {
code: formatCode(
'"https://google.com/#home?greeting=hello"',
' .split()[0];'
),
errors: [],
output: [
formatCode(
'"https://google.com/#home?greeting=hello"',
' .split()[0];'
),
],
};
}

function checkNewLineErrorWithTabOption() {
return {
code: formatCode(
Expand Down Expand Up @@ -259,6 +275,11 @@ ruleTester.run('fluent-chaining - checkManyLinesError', fluentChaining, {
invalid: [ checkManyLinesCommentError() ],
});

ruleTester.run('fluent-chaning - checkAllowMemberExpressionWithArrayAccess', fluentChaining, {
valid: [ checkAllowMemberExpressionWithArrayAccess() ],
invalid: [],
});

ruleTester.run('fluent-chaining - checkNewLineError', fluentChaining, {
valid: [],
invalid: [ checkNewLineError() ],
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/fluent-chaining.valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,10 @@ aPromise
.catch(function(err) {
return err;
});

// VALID - array accessors used on call expressions are an accepted pattern
'https://google.com/#home?greeting=hello'
// Remove any query string:
.split('?')[0]
// Remove any hash:
.split('#')[0];

0 comments on commit f2c5000

Please sign in to comment.