diff --git a/docs/content/rules/sort-decorators.mdx b/docs/content/rules/sort-decorators.mdx index aff8f48a..08fb79fa 100644 --- a/docs/content/rules/sort-decorators.mdx +++ b/docs/content/rules/sort-decorators.mdx @@ -208,6 +208,11 @@ Specifies the sorting locales. See [String.prototype.localeCompare() - locales]( ### partitionByComment + + - type: + - `boolean | string | string[]` (`BaseType`) + - `{ block: BaseType; line: BaseType }` + default: `false` Allows you to use comments to separate class decorators into logical groups. @@ -216,6 +221,7 @@ Allows you to use comments to separate class decorators into logical groups. - `false` — Comments will not be used as delimiters. - `string` — A regexp pattern to specify which comments should act as delimiters. - `string[]` — An array of regexp patterns to specify which comments should act as delimiters. +- `{ block: BaseType; line: BaseType }` — Specify which block and line comments should act as delimiters. ### groups diff --git a/test/rules/sort-decorators.test.ts b/test/rules/sort-decorators.test.ts index 54ab7e90..f677c20e 100644 --- a/test/rules/sort-decorators.test.ts +++ b/test/rules/sort-decorators.test.ts @@ -1060,6 +1060,460 @@ describe(ruleName, () => { }, ) + describe(`${ruleName}(${type}): allows to use "partitionByComment.line"`, () => { + ruleTester.run(`${ruleName}(${type}): ignores block comments`, rule, { + invalid: [ + { + output: dedent` + /* Comment */ + @A + @B + class Class { + + /* Comment */ + @A + @B + property + + /* Comment */ + @A + @B + accessor field + + /* Comment */ + @A + @B + method( + /* Comment */ + @A + @B + parameter) {} + } + `, + code: dedent` + @B + /* Comment */ + @A + class Class { + + @B + /* Comment */ + @A + property + + @B + /* Comment */ + @A + accessor field + + @B + /* Comment */ + @A + method( + @B + /* Comment */ + @A + parameter) {} + } + `, + errors: duplicate5Times([ + { + data: { + right: 'A', + left: 'B', + }, + messageId: 'unexpectedDecoratorsOrder', + }, + ]), + options: [ + { + ...options, + partitionByComment: { + line: true, + }, + }, + ], + }, + ], + valid: [], + }) + + ruleTester.run( + `${ruleName}(${type}): allows to use all comments as parts`, + rule, + { + valid: [ + { + code: dedent` + @B + // Comment + @A + class Class { + + @B + // Comment + @A + property + + @B + // Comment + @A + accessor field + + @B + // Comment + @A + method( + @B + // Comment + @A + parameter) {} + } + `, + options: [ + { + ...options, + partitionByComment: { + line: true, + }, + }, + ], + }, + ], + invalid: [], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): allows to use multiple partition comments`, + rule, + { + valid: [ + { + code: dedent` + @C + // B + @B + // A + @A + class Class { + + @C + // B + @B + // A + @A + property + + @C + // B + @B + // A + @A + accessor field + + @C + // B + @B + // A + @A + method( + @C + // B + @B + // A + @A + parameter) {} + } + `, + options: [ + { + ...options, + partitionByComment: { + line: ['A', 'B'], + }, + }, + ], + }, + ], + invalid: [], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): allows to use regex for partition comments`, + rule, + { + valid: [ + { + code: dedent` + @B + // I am a partition comment because I don't have f o o + @A + class Class { + + @B + // I am a partition comment because I don't have f o o + @A + property + + @B + // I am a partition comment because I don't have f o o + @A + accessor field + + @B + // I am a partition comment because I don't have f o o + @A + method( + @B + // I am a partition comment because I don't have f o o + @A + parameter) {} + } + `, + options: [ + { + ...options, + partitionByComment: { + line: ['^(?!.*foo).*$'], + }, + }, + ], + }, + ], + invalid: [], + }, + ) + }) + + describe(`${ruleName}(${type}): allows to use "partitionByComment.block"`, () => { + ruleTester.run(`${ruleName}(${type}): ignores line comments`, rule, { + invalid: [ + { + output: dedent` + // Comment + @A + @B + class Class { + + // Comment + @A + @B + property + + // Comment + @A + @B + accessor field + + // Comment + @A + @B + method( + // Comment + @A + @B + parameter) {} + } + `, + code: dedent` + @B + // Comment + @A + class Class { + + @B + // Comment + @A + property + + @B + // Comment + @A + accessor field + + @B + // Comment + @A + method( + @B + // Comment + @A + parameter) {} + } + `, + errors: duplicate5Times([ + { + data: { + right: 'A', + left: 'B', + }, + messageId: 'unexpectedDecoratorsOrder', + }, + ]), + options: [ + { + ...options, + partitionByComment: { + block: true, + }, + }, + ], + }, + ], + valid: [], + }) + + ruleTester.run( + `${ruleName}(${type}): allows to use all comments as parts`, + rule, + { + valid: [ + { + code: dedent` + @B + /* Comment */ + @A + class Class { + + @B + /* Comment */ + @A + property + + @B + /* Comment */ + @A + accessor field + + @B + /* Comment */ + @A + method( + @B + /* Comment */ + @A + parameter) {} + } + `, + options: [ + { + ...options, + partitionByComment: { + block: true, + }, + }, + ], + }, + ], + invalid: [], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): allows to use multiple partition comments`, + rule, + { + valid: [ + { + code: dedent` + @C + /* B */ + @B + /* A */ + @A + class Class { + + @C + /* B */ + @B + /* A */ + @A + property + + @C + /* B */ + @B + /* A */ + @A + accessor field + + @C + /* B */ + @B + /* A */ + @A + method( + @C + /* B */ + @B + /* A */ + @A + parameter) {} + } + `, + options: [ + { + ...options, + partitionByComment: { + block: ['A', 'B'], + }, + }, + ], + }, + ], + invalid: [], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): allows to use regex for partition comments`, + rule, + { + valid: [ + { + code: dedent` + @B + /* I am a partition comment because I don't have f o o */ + @A + class Class { + + @B + /* I am a partition comment because I don't have f o o */ + @A + property + + @B + /* I am a partition comment because I don't have f o o */ + @A + accessor field + + @B + /* I am a partition comment because I don't have f o o */ + @A + method( + @B + /* I am a partition comment because I don't have f o o */ + @A + parameter) {} + } + `, + options: [ + { + ...options, + partitionByComment: { + block: ['^(?!.*foo).*$'], + }, + }, + ], + }, + ], + invalid: [], + }, + ) + }) + ruleTester.run( `${ruleName}(${type}): allows to trim special characters`, rule,