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

Re-add importOrderCaseSensitive option for case-sensitive sorting #184

Merged
merged 8 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This project is based on [@trivago/prettier-plugin-sort-imports](https://github.
- [7. Enable/disable plugin or use different order in certain folders or files](#7-enabledisable-plugin-or-use-different-order-in-certain-folders-or-files)
- [`importOrderTypeScriptVersion`](#importordertypescriptversion)
- [`importOrderParserPlugins`](#importorderparserplugins)
- [`importOrderCaseSensitive`](#importordercasesensitive)
- [Prevent imports from being sorted](#prevent-imports-from-being-sorted)
- [Comments](#comments)
- [FAQ / Troubleshooting](#faq--troubleshooting)
Expand Down Expand Up @@ -139,6 +140,7 @@ module.exports = {
importOrder: ['^@core/(.*)$', '', '^@server/(.*)$', '', '^@ui/(.*)$', '', '^[./]'],
importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'],
importOrderTypeScriptVersion: '5.0.0',
importOrderCaseSensitive: false,
};
```

Expand Down Expand Up @@ -393,6 +395,31 @@ with options as a JSON string of the plugin array:
"importOrderParserPlugins": []
```

#### `importOrderCaseSensitive`

**type**: `boolean`

**default value**: `false`

A boolean value to enable case-sensitivity in the sorting algorithm
used to order imports within each match group.

For example, when false (or not specified):

```javascript
import ExampleComponent from './ExampleComponent';
import ExamplesList from './ExamplesList';
import ExampleWidget from './ExampleWidget';
```

compared with `"importOrderCaseSensitive": true`:

```javascript
import ExampleComponent from './ExampleComponent';
import ExampleWidget from './ExampleWidget';
import ExamplesList from './ExamplesList';
```

acnebs marked this conversation as resolved.
Show resolved Hide resolved
### Prevent imports from being sorted

This plugin supports standard prettier ignore comments. By default, side-effect imports (like
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export const options: Record<
description:
'Version of TypeScript in use in the project. Determines some output syntax when using TypeScript.',
},
importOrderCaseSensitive: {
type: 'boolean',
category: 'Global',
default: false,
description: 'Provide a case sensitivity boolean flag',
},
};

export const parsers = {
Expand Down
129 changes: 109 additions & 20 deletions src/natural-sort/__tests__/natural-sort.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,115 @@
import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';

import { naturalSort } from '..';
import { naturalSort, naturalSortCaseSensitive } from '..';

test('should sort normal things alphabetically', () => {
expect(
['a', 'h', 'b', 'i', 'c', 'd', 'j', 'e', 'k', 'f', 'g'].sort((a, b) =>
naturalSort(a, b),
),
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']);
});
describe('naturalSort', () => {
test('should sort normal things alphabetically', () => {
expect(
['a', 'h', 'b', 'i', 'c', 'd', 'j', 'e', 'k', 'f', 'g'].sort(
(a, b) => naturalSort(a, b),
),
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']);
});

test('should ignore capitalization differences', () => {
expect(
['./ExampleComponent', './ExamplesList', './ExampleWidget'].sort(
(a, b) => naturalSort(a, b),
),
).toEqual(['./ExampleComponent', './ExamplesList', './ExampleWidget']);
});

test('should ignore capitalization differences', () => {
// We have no option to cause case-sensitive sorting, so this is the "default" case!
expect(
['./ExampleView', './ExamplesList'].sort((a, b) => naturalSort(a, b)),
).toEqual(['./ExamplesList', './ExampleView']);
test('should sort things numerically', () => {
expect(
[
'a2',
'a3',
'a10',
'a1',
'a11',
'a9',
'a1b',
'file000b',
'file000a',
'file00a',
'file00z',
].sort(naturalSort),
).toEqual([
'a1',
'a1b',
'a2',
'a3',
'a9',
'a10',
'a11',
'file000a',
'file00a',
'file000b',
'file00z',
]);
});
});

test('should sort things numerically', () => {
expect(
['a2', 'a3', 'a10', 'a1', 'a11', 'a9'].sort((a, b) =>
naturalSort(a, b),
),
).toEqual(['a1', 'a2', 'a3', 'a9', 'a10', 'a11']);
describe('naturalSortCaseSensitive', () => {
test('should not ignore capitalization differences', () => {
expect(
['./ExampleComponent', './ExamplesList', './ExampleWidget'].sort(
(a, b) => naturalSortCaseSensitive(a, b),
),
).toEqual(['./ExampleComponent', './ExampleWidget', './ExamplesList']);
});

test('should sort numerically and case-sensitively', () => {
expect(
[
'file1',
'File10',
'AbA',
'file10',
'files10',
'file1z',
'file10ab',
'file2s',
'a',
'Ab',
'file20',
'file22',
'file11',
'file2',
'File20',
'file000b',
'file000a',
'file00a',
'file00z',
'aaa',
'AAA',
'bBb',
'BBB',
].sort(naturalSortCaseSensitive),
).toEqual([
'AAA',
'Ab',
'AbA',
'BBB',
'File10',
'File20',
'a',
'aaa',
'bBb',
'file000a',
'file00a',
'file000b',
'file00z',
'file1',
'file1z',
'file2',
'file2s',
'file10',
'file10ab',
'file11',
'file20',
'file22',
'files10',
]);
});
});
39 changes: 37 additions & 2 deletions src/natural-sort/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,47 @@
export function naturalSort(a: string, b: string): number {
const left = typeof a === 'string' ? a : String(a);

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#syntax
const sortOptions: Intl.CollatorOptions = {
sensitivity: 'base',
numeric: true,
caseFirst: 'lower',
};

return left.localeCompare(b, 'en', sortOptions);
}

/**
* Using a custom comparison function here, as `String.localeCompare` does not
* support sorting characters with all uppercase letters before lowercase
* letters, which is the desired behavior for a case-sensitive import sort. When
* `sensitivity` is set to `base`, `String.localeCompare` sorts alphabetically
* and then by case, but we want to sort by case first (then alphabetical).
*/
const numericRegex = /^\d+/;
export function naturalSortCaseSensitive(a: string, b: string) {
let aIndex = 0;
let bIndex = 0;
while (aIndex < Math.max(a.length, b.length)) {
// check if we've encountered a number and compare appropriately if so
const aNumericMatch = a.slice(aIndex).match(numericRegex);
const bNumericMatch = b.slice(bIndex).match(numericRegex);
if (aNumericMatch && !bNumericMatch) return -1;
if (!aNumericMatch && bNumericMatch) return 1;
if (aNumericMatch && bNumericMatch) {
const aNumber = parseInt(aNumericMatch[0]);
const bNumber = parseInt(bNumericMatch[0]);
if (aNumber > bNumber) return 1;
if (aNumber < bNumber) return -1;
aIndex += aNumericMatch[0].length;
bIndex += bNumericMatch[0].length;
Copy link
Owner

Choose a reason for hiding this comment

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

One minor edge case here is if there are different-length strings that evaluate to the same number, like multiple zeroes for instance. We can get this kind of sorting:

"file000a",
"file00a",
"file000b",
"file00z",

On the other hand, that's also how naturalSort sorts them, so I don't think it's a problem.

}
// otherwise just compare characters directly
const aChar = a[aIndex];
const bChar = b[bIndex];
if (aChar && !bChar) return 1;
if (!aChar && bChar) return -1;
if (aChar !== bChar) return aChar.charCodeAt(0) - bChar.charCodeAt(0);
aIndex++;
bIndex++;
}
return 0;
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ export type GetSortedNodes = (

export type GetSortedNodesByImportOrder = (
nodes: ImportDeclaration[],
options: Pick<ExtendedOptions, 'importOrder'>,
options: Pick<ExtendedOptions, 'importOrder'> & {
importOrderCaseSensitive?: boolean;
},
) => ImportOrLine[];

export type GetChunkTypeOfNode = (node: ImportDeclaration) => ChunkType;
Expand Down
17 changes: 17 additions & 0 deletions src/utils/__tests__/get-sorted-import-specifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,20 @@ test('should group type imports after value imports - flow', () => {
'TypeB',
]);
});

test('should sort case-sensitively', () => {
const code = `import { ExampleComponent, ExamplesList, ExampleWidget } from '@components/e';`;
const [importNode] = getImportNodes(code);
const sortedImportSpecifiers = getSortedImportSpecifiers(importNode, {
importOrderCaseSensitive: true,
});
const specifiersList = getSortedNodesModulesNames(
sortedImportSpecifiers.specifiers,
);

expect(specifiersList).toEqual([
'ExampleComponent',
'ExampleWidget',
'ExamplesList',
]);
});
25 changes: 25 additions & 0 deletions src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,28 @@ test('it does not add multiple custom import separators', () => {
'./local',
]);
});

test('it should sort nodes case-sensitively', () => {
const result = getImportNodes(code);
const sorted = getSortedNodesByImportOrder(result, {
importOrder: testingOnly.normalizeImportOrderOption(['^[./]']),
importOrderCaseSensitive: true,
}) as ImportDeclaration[];
expect(getSortedNodesNamesAndNewlines(sorted)).toEqual([
'node:fs/promises',
'node:url',
'path',
'BY',
'Ba',
'XY',
'Xa',
'a',
'c',
'g',
'k',
't',
'x',
'z',
'./local',
]);
});
15 changes: 11 additions & 4 deletions src/utils/get-sorted-import-specifiers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type ImportDeclaration } from '@babel/types';

import { naturalSort } from '../natural-sort';
import type { PluginConfig } from '../../types';
import { naturalSort, naturalSortCaseSensitive } from '../natural-sort';

/**
* This function returns import nodes with alphabetically sorted module
Expand All @@ -12,7 +13,11 @@ import { naturalSort } from '../natural-sort';
*
* @param node Import declaration node
*/
export const getSortedImportSpecifiers = (node: ImportDeclaration) => {
export const getSortedImportSpecifiers = (
node: ImportDeclaration,
options?: Pick<PluginConfig, 'importOrderCaseSensitive'>,
) => {
const { importOrderCaseSensitive } = options || {};
node.specifiers.sort((a, b) => {
if (a.type !== b.type) {
return a.type === 'ImportDefaultSpecifier' ? -1 : 1;
Expand All @@ -25,8 +30,10 @@ export const getSortedImportSpecifiers = (node: ImportDeclaration) => {
// flow uses null for value import specifiers
return a.importKind === 'value' || a.importKind == null ? -1 : 1;
}

return naturalSort(a.local.name, b.local.name);
const sortFn = importOrderCaseSensitive
? naturalSortCaseSensitive
: naturalSort;
return sortFn(a.local.name, b.local.name);
});
return node;
};
10 changes: 7 additions & 3 deletions src/utils/get-sorted-nodes-by-import-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
*/
export const getSortedNodesByImportOrder: GetSortedNodesByImportOrder = (
originalNodes,
{ importOrder },
{ importOrder, importOrderCaseSensitive },
) => {
if (
process.env.NODE_ENV === 'test' &&
Expand Down Expand Up @@ -83,10 +83,14 @@ export const getSortedNodesByImportOrder: GetSortedNodesByImportOrder = (

if (groupNodes.length === 0) continue;

const sortedInsideGroup = getSortedNodesGroup(groupNodes);
const sortedInsideGroup = getSortedNodesGroup(groupNodes, {
importOrderCaseSensitive,
});

// Sort the import specifiers
sortedInsideGroup.forEach((node) => getSortedImportSpecifiers(node));
sortedInsideGroup.forEach((node) =>
getSortedImportSpecifiers(node, { importOrderCaseSensitive }),
);

finalNodes.push(...sortedInsideGroup);
}
Expand Down
14 changes: 11 additions & 3 deletions src/utils/get-sorted-nodes-group.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import type { ImportDeclaration } from '@babel/types';

import { naturalSort } from '../natural-sort';
import type { PluginConfig } from '../../types';
import { naturalSort, naturalSortCaseSensitive } from '../natural-sort';

export const getSortedNodesGroup = (imports: ImportDeclaration[]) => {
return imports.sort((a, b) => naturalSort(a.source.value, b.source.value));
export const getSortedNodesGroup = (
imports: ImportDeclaration[],
options?: Pick<PluginConfig, 'importOrderCaseSensitive'>,
) => {
const { importOrderCaseSensitive } = options || {};
const sortFn = importOrderCaseSensitive
? naturalSortCaseSensitive
: naturalSort;
return imports.sort((a, b) => sortFn(a.source.value, b.source.value));
};
Loading
Loading