Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lint chained expressions after class or awaited expression (#62) #66

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() ],
});
Loading