From 9900388f51af1a0659c7d65e793c393f21e8dfff Mon Sep 17 00:00:00 2001 From: "Xunnamius (Romulus)" Date: Sun, 26 Jan 2025 05:52:50 -0800 Subject: [PATCH 1/3] fix: prohibit nonsensical configurations using the schema --- src/rules/order.js | 108 +++++++++++++++++++++++++++++++++------------ 1 file changed, 80 insertions(+), 28 deletions(-) diff --git a/src/rules/order.js b/src/rules/order.js index b34efd027..d84cf86c8 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -858,9 +858,12 @@ module.exports = { properties: { groups: { type: 'array', + // Verified manually in the convertGroupsToRanks function }, pathGroupsExcludedImportTypes: { type: 'array', + uniqueItems: true, + items: { enum: types }, }, distinctGroup: { type: 'boolean', @@ -918,27 +921,28 @@ module.exports = { }, named: { default: false, - oneOf: [{ - type: 'boolean', - }, { - type: 'object', - properties: { - enabled: { type: 'boolean' }, - import: { type: 'boolean' }, - export: { type: 'boolean' }, - require: { type: 'boolean' }, - cjsExports: { type: 'boolean' }, - types: { - type: 'string', - enum: [ - 'mixed', - 'types-first', - 'types-last', - ], + oneOf: [ + { type: 'boolean' }, + { + type: 'object', + properties: { + enabled: { type: 'boolean' }, + import: { type: 'boolean' }, + export: { type: 'boolean' }, + require: { type: 'boolean' }, + cjsExports: { type: 'boolean' }, + types: { + type: 'string', + enum: [ + 'mixed', + 'types-first', + 'types-last', + ], + }, }, + additionalProperties: false, }, - additionalProperties: false, - }], + ], }, alphabetize: { type: 'object', @@ -965,6 +969,38 @@ module.exports = { }, additionalProperties: false, dependencies: { + sortTypesGroup: { + oneOf: [ + { + // When sortTypesGroup is true, groups must NOT be an array that does not contain 'type' + properties: { + sortTypesGroup: { enum: [true] }, + groups: { + not: { + type: 'array', + uniqueItems: true, + items: { + oneOf: [ + { enum: types.filter((t) => t !== 'type') }, + { + type: 'array', + uniqueItems: true, + items: { enum: types.filter((t) => t !== 'type') }, + }, + ], + }, + }, + }, + }, + required: ['groups'], + }, + { + properties: { + sortTypesGroup: { enum: [false] }, + } + }, + ], + }, 'newlines-between-types': { properties: { sortTypesGroup: { enum: [true] }, @@ -972,17 +1008,33 @@ module.exports = { required: ['sortTypesGroup'], }, consolidateIslands: { - anyOf: [{ - properties: { - 'newlines-between': { enum: ['always-and-inside-groups'] }, + oneOf: [ + { + properties: { + consolidateIslands: { enum: ['inside-groups'] }, + }, + anyOf: [ + { + properties: { + 'newlines-between': { enum: ['always-and-inside-groups'] }, + }, + required: ['newlines-between'], + }, + { + properties: { + 'newlines-between-types': { enum: ['always-and-inside-groups'] }, + }, + required: ['newlines-between-types'], + }, + ], }, - required: ['newlines-between'], - }, { - properties: { - 'newlines-between-types': { enum: ['always-and-inside-groups'] }, + { + properties: { + consolidateIslands: { enum: ['never'] }, + }, }, - required: ['newlines-between-types'], - }] }, + ], + }, }, }, ], From a8ab6461544c3d908df3dafaccff823a29e683fe Mon Sep 17 00:00:00 2001 From: "Xunnamius (Romulus)" Date: Sun, 26 Jan 2025 05:54:00 -0800 Subject: [PATCH 2/3] test: fix erroneously invalid options in "valid" tests identified by stricter schema --- src/rules/order.js | 2 +- tests/src/rules/order.js | 36 ++---------------------------------- 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/src/rules/order.js b/src/rules/order.js index d84cf86c8..d52215cfd 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -997,7 +997,7 @@ module.exports = { { properties: { sortTypesGroup: { enum: [false] }, - } + }, }, ], }, diff --git a/tests/src/rules/order.js b/tests/src/rules/order.js index fa592c9b1..97f2528e3 100644 --- a/tests/src/rules/order.js +++ b/tests/src/rules/order.js @@ -2511,7 +2511,7 @@ ruleTester.run('order', rule, { { pattern: '@namespace', group: 'external', position: 'after' }, { pattern: '@namespace/**', group: 'external', position: 'after' }, ], - pathGroupsExcludedImportTypes: ['@namespace'], + pathGroupsExcludedImportTypes: [], }, ], errors: [ @@ -3554,38 +3554,6 @@ context('TypeScript', function () { }, ], }), - // Option sortTypesGroup: true and 'type' omitted from groups - test({ - code: ` - import c from 'Bar'; - import type { AA } from 'abc'; - import a from 'foo'; - import type { A } from 'foo'; - - import type { C } from 'dirA/Bar'; - import b from 'dirA/bar'; - import type { D } from 'dirA/bar'; - - import index from './'; - `, - ...parserConfig, - options: [ - { - alphabetize: { order: 'asc' }, - groups: ['external', 'internal', 'index'], - pathGroups: [ - { - pattern: 'dirA/**', - group: 'internal', - }, - ], - 'newlines-between': 'always', - pathGroupsExcludedImportTypes: [], - // Becomes a no-op without "type" in groups - sortTypesGroup: true, - }, - ], - }), test({ code: ` import c from 'Bar'; @@ -6877,7 +6845,7 @@ flowRuleTester.run('order', rule, { }, }, ], - pathGroupsExcludedImportTypes: ['react'], + pathGroupsExcludedImportTypes: [], alphabetize: { order: 'asc', }, From 30b4dcf9fe0d25f76b3a5c596a41862a83f18de8 Mon Sep 17 00:00:00 2001 From: "Xunnamius (Romulus)" Date: Fri, 31 Jan 2025 20:47:52 -0800 Subject: [PATCH 3/3] refactor(src): undo `pathGroupsExcludedImportTypes` schema tightening; undo cosmetic changes --- src/rules/order.js | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/rules/order.js b/src/rules/order.js index d52215cfd..4a17e204a 100644 --- a/src/rules/order.js +++ b/src/rules/order.js @@ -858,12 +858,9 @@ module.exports = { properties: { groups: { type: 'array', - // Verified manually in the convertGroupsToRanks function }, pathGroupsExcludedImportTypes: { type: 'array', - uniqueItems: true, - items: { enum: types }, }, distinctGroup: { type: 'boolean', @@ -921,28 +918,27 @@ module.exports = { }, named: { default: false, - oneOf: [ - { type: 'boolean' }, - { - type: 'object', - properties: { - enabled: { type: 'boolean' }, - import: { type: 'boolean' }, - export: { type: 'boolean' }, - require: { type: 'boolean' }, - cjsExports: { type: 'boolean' }, - types: { - type: 'string', - enum: [ - 'mixed', - 'types-first', - 'types-last', - ], - }, + oneOf: [{ + type: 'boolean', + }, { + type: 'object', + properties: { + enabled: { type: 'boolean' }, + import: { type: 'boolean' }, + export: { type: 'boolean' }, + require: { type: 'boolean' }, + cjsExports: { type: 'boolean' }, + types: { + type: 'string', + enum: [ + 'mixed', + 'types-first', + 'types-last', + ], }, - additionalProperties: false, }, - ], + additionalProperties: false, + }], }, alphabetize: { type: 'object',