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] order: codify invariants from docs into config schema #3152

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
108 changes: 80 additions & 28 deletions src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -858,9 +858,12 @@ module.exports = {
properties: {
groups: {
type: 'array',
// Verified manually in the convertGroupsToRanks function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for the comment. but this can be verified to have numbers in it, and no duplicates, yes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The secondary validation has its own error messages. I believe you're saying to move the duplicates/existence validation logic up to the schema? That would change the error messages and perhaps other behavior. For instance, there are currently unit tests that check for the current error messages.

Would that count as tightening the schema of a released option?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed error messages are fine; test cases that always error, changing to "removed test cases because it's now unreachable" is also fine, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, I'll take a look

},
pathGroupsExcludedImportTypes: {
type: 'array',
uniqueItems: true,
items: { enum: types },
},
distinctGroup: {
type: 'boolean',
Expand Down Expand Up @@ -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' },
{
Xunnamius marked this conversation as resolved.
Show resolved Hide resolved
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',
Expand All @@ -965,24 +969,72 @@ 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] },
},
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'],
}] },
],
},
},
},
],
Expand Down
36 changes: 2 additions & 34 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -2511,7 +2511,7 @@ ruleTester.run('order', rule, {
{ pattern: '@namespace', group: 'external', position: 'after' },
{ pattern: '@namespace/**', group: 'external', position: 'after' },
],
pathGroupsExcludedImportTypes: ['@namespace'],
pathGroupsExcludedImportTypes: [],
},
],
errors: [
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -6877,7 +6845,7 @@ flowRuleTester.run('order', rule, {
},
},
],
pathGroupsExcludedImportTypes: ['react'],
pathGroupsExcludedImportTypes: [],
alphabetize: {
order: 'asc',
},
Expand Down