Skip to content

Commit

Permalink
Merge pull request #66 from silvermine/esmith/fluent-chaining-tail
Browse files Browse the repository at this point in the history
fix: lint chained expressions after class or awaited expression (#62)
  • Loading branch information
onebytegone authored Dec 10, 2024
2 parents f2c5000 + 7f69d18 commit 7eef6b7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 10 deletions.
11 changes: 7 additions & 4 deletions lib/rules/fluent-chaining.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ module.exports = {
});
}


if (node.object.type === 'CallExpression' && !sameLine && lastLineIndent !== propertyLineIndent) {
if (!sameLine && lastLineIndent !== propertyLineIndent) {
context.report({
node: node.object,
message: 'Call expression should be on a new line and indented',
node: node.property,
message: 'Expected identifier "{{ identifier }}" to align with preceding {{ object }}',
data: {
identifier: node.property.name,
object: node.object.type,
},
});
}
}
Expand Down
96 changes: 90 additions & 6 deletions tests/lib/rules/fluent-chaining.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var fluentChaining = require('../../../lib/rules/fluent-chaining'),
RuleTester = require('eslint').RuleTester,
fs = require('fs'),
path = require('path'),
ruleTester = new RuleTester();
ruleTester = new RuleTester(),
es6RuleTester = require('../../ruleTesters').es6();

// ------------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -210,10 +211,26 @@ function checkSpacingErrorWhenNested() {
};
}

function chainingIndentationMatchErrorMessage() {
function checkSpacingErrorWhenFunctionIsAwaited() {
return {
message: 'Call expression should be on a new line and indented',
type: 'CallExpression',
code: formatCode(
'(async () => {',
' (await doSomething({',
' prop: true',
' }))',
' .exec();',
'})'
),
errors: [
chainingIndentationMatchErrorMessage('exec', 'AwaitExpression'),
],
};
}

function chainingIndentationMatchErrorMessage(identifier, objectType) {
return {
message: `Expected identifier "${identifier}" to align with preceding ${objectType}`,
type: 'Identifier',
};
}

Expand All @@ -232,7 +249,7 @@ function checkChainingIndentationError1() {
' .done();'
),
errors: [
chainingIndentationMatchErrorMessage(),
chainingIndentationMatchErrorMessage('spread', 'CallExpression'),
],
};
}
Expand All @@ -249,7 +266,54 @@ function checkChainingIndentationError2() {
' });'
),
errors: [
chainingIndentationMatchErrorMessage(),
chainingIndentationMatchErrorMessage('then', 'CallExpression'),
],
};
}

function checkChainingIndentationError3() {
return {
code: formatCode(
'new Class({',
' prop: true',
'})',
' .exec();'
),
errors: [
chainingIndentationMatchErrorMessage('exec', 'NewExpression'),
],
};
}

function checkChainingIndentationError4() {
return {
code: formatCode(
'new Class({',
' items: [',
' 1,',
' 2,',
' ]',
' .filter(isNotNullOrUndefined),',
'});'
),
errors: [
chainingIndentationMatchErrorMessage('filter', 'ArrayExpression'),
],
};
}


function checkChainingIndentationError5() {
return {
code: formatCode(
'var arr = [',
' 1,',
' 2,',
']',
' .filter(isNotNullOrUndefined);'
),
errors: [
chainingIndentationMatchErrorMessage('filter', 'ArrayExpression'),
],
};
}
Expand Down Expand Up @@ -315,6 +379,11 @@ ruleTester.run('fluent-chaining - checkSpacingErrorWhenNested', fluentChaining,
invalid: [ checkSpacingErrorWhenNested() ],
});

es6RuleTester.run('fluent-chaining - checkSpacingErrorWhenFunctionIsAwaited', fluentChaining, {
valid: [],
invalid: [ checkSpacingErrorWhenFunctionIsAwaited() ],
});

ruleTester.run('fluent-chaining - checkChainingIndentationError1', fluentChaining, {
valid: [],
invalid: [ checkChainingIndentationError1() ],
Expand All @@ -324,3 +393,18 @@ ruleTester.run('fluent-chaining - checkChainingIndentationError2', fluentChainin
valid: [],
invalid: [ checkChainingIndentationError2() ],
});

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

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

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

0 comments on commit 7eef6b7

Please sign in to comment.