From 7f69d18b797ab55223c91035fb28ff10848f6ec3 Mon Sep 17 00:00:00 2001 From: Ethan Smith Date: Sun, 1 Dec 2024 10:43:31 -0500 Subject: [PATCH] fix: lint chained expressions after class or awaited expression (#62) --- lib/rules/fluent-chaining.js | 11 +-- tests/lib/rules/fluent-chaining.test.js | 96 +++++++++++++++++++++++-- 2 files changed, 97 insertions(+), 10 deletions(-) diff --git a/lib/rules/fluent-chaining.js b/lib/rules/fluent-chaining.js index 632df5b..b08ec7d 100644 --- a/lib/rules/fluent-chaining.js +++ b/lib/rules/fluent-chaining.js @@ -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, + }, }); } } diff --git a/tests/lib/rules/fluent-chaining.test.js b/tests/lib/rules/fluent-chaining.test.js index 9e27f09..6c8ee14 100644 --- a/tests/lib/rules/fluent-chaining.test.js +++ b/tests/lib/rules/fluent-chaining.test.js @@ -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 @@ -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', }; } @@ -232,7 +249,7 @@ function checkChainingIndentationError1() { ' .done();' ), errors: [ - chainingIndentationMatchErrorMessage(), + chainingIndentationMatchErrorMessage('spread', 'CallExpression'), ], }; } @@ -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'), ], }; } @@ -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() ], @@ -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() ], +});