@ianvs/prettier-plugin-sort-imports
Advanced tools
Comparing version 4.0.0-alpha.1 to 4.0.0-alpha.2
@@ -18,8 +18,2 @@ "use strict"; | ||
}, | ||
importOrderCaseInsensitive: { | ||
type: 'boolean', | ||
category: 'Global', | ||
default: false, | ||
description: 'Provide a case sensitivity boolean flag', | ||
}, | ||
importOrderParserPlugins: { | ||
@@ -34,14 +28,2 @@ type: 'path', | ||
}, | ||
importOrderGroupNamespaceSpecifiers: { | ||
type: 'boolean', | ||
category: 'Global', | ||
default: false, | ||
description: 'Should namespace specifiers be grouped at the top of their group?', | ||
}, | ||
importOrderSortSpecifiers: { | ||
type: 'boolean', | ||
category: 'Global', | ||
default: false, | ||
description: 'Should specifiers be sorted?', | ||
}, | ||
importOrderMergeDuplicateImports: { | ||
@@ -48,0 +30,0 @@ type: 'boolean', |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.naturalSort = void 0; | ||
const javascript_natural_sort_1 = __importDefault(require("javascript-natural-sort")); | ||
exports.naturalSort = javascript_natural_sort_1.default; | ||
function naturalSort(a, b) { | ||
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 = { | ||
sensitivity: 'base', | ||
numeric: true, | ||
caseFirst: 'lower', | ||
}; | ||
return left.localeCompare(b, 'en', sortOptions); | ||
} | ||
exports.naturalSort = naturalSort; |
@@ -15,3 +15,3 @@ "use strict"; | ||
function preprocessor(code, options) { | ||
const { importOrderParserPlugins, importOrder, importOrderCaseInsensitive, importOrderGroupNamespaceSpecifiers, importOrderMergeDuplicateImports, importOrderSortSpecifiers, } = options; | ||
const { importOrderParserPlugins, importOrder, importOrderMergeDuplicateImports, } = options; | ||
let { importOrderCombineTypeAndValueImports } = options; | ||
@@ -49,7 +49,4 @@ if (importOrderCombineTypeAndValueImports && | ||
importOrder, | ||
importOrderCaseInsensitive, | ||
importOrderGroupNamespaceSpecifiers, | ||
importOrderMergeDuplicateImports, | ||
importOrderCombineTypeAndValueImports, | ||
importOrderSortSpecifiers, | ||
}); | ||
@@ -56,0 +53,0 @@ return (0, get_code_from_ast_1.getCodeFromAst)({ |
@@ -9,3 +9,2 @@ "use strict"; | ||
const constants_1 = require("../constants"); | ||
const natural_sort_1 = require("../natural-sort"); | ||
const get_import_nodes_matched_group_1 = require("./get-import-nodes-matched-group"); | ||
@@ -22,5 +21,3 @@ const get_sorted_import_specifiers_1 = require("./get-sorted-import-specifiers"); | ||
const getSortedNodesByImportOrder = (nodes, options) => { | ||
natural_sort_1.naturalSort.insensitive = options.importOrderCaseInsensitive; | ||
let { importOrder } = options; | ||
const { importOrderSortSpecifiers, importOrderGroupNamespaceSpecifiers } = options; | ||
const originalNodes = nodes.map(lodash_clone_1.default); | ||
@@ -66,9 +63,5 @@ const finalNodes = []; | ||
continue; | ||
const sortedInsideGroup = (0, get_sorted_nodes_group_1.getSortedNodesGroup)(groupNodes, { | ||
importOrderGroupNamespaceSpecifiers, | ||
}); | ||
const sortedInsideGroup = (0, get_sorted_nodes_group_1.getSortedNodesGroup)(groupNodes); | ||
// Sort the import specifiers | ||
if (importOrderSortSpecifiers) { | ||
sortedInsideGroup.forEach((node) => (0, get_sorted_import_specifiers_1.getSortedImportSpecifiers)(node)); | ||
} | ||
sortedInsideGroup.forEach((node) => (0, get_sorted_import_specifiers_1.getSortedImportSpecifiers)(node)); | ||
finalNodes.push(...sortedInsideGroup); | ||
@@ -75,0 +68,0 @@ } |
@@ -5,21 +5,5 @@ "use strict"; | ||
const natural_sort_1 = require("../natural-sort"); | ||
const getSortedNodesGroup = (imports, options) => { | ||
return imports.sort((a, b) => { | ||
if (options.importOrderGroupNamespaceSpecifiers) { | ||
const diff = namespaceSpecifierSort(a, b); | ||
if (diff !== 0) | ||
return diff; | ||
} | ||
return (0, natural_sort_1.naturalSort)(a.source.value, b.source.value); | ||
}); | ||
const getSortedNodesGroup = (imports) => { | ||
return imports.sort((a, b) => (0, natural_sort_1.naturalSort)(a.source.value, b.source.value)); | ||
}; | ||
exports.getSortedNodesGroup = getSortedNodesGroup; | ||
function namespaceSpecifierSort(a, b) { | ||
const aFirstSpecifier = a.specifiers.find((s) => s.type === 'ImportNamespaceSpecifier') | ||
? 1 | ||
: 0; | ||
const bFirstSpecifier = b.specifiers.find((s) => s.type === 'ImportNamespaceSpecifier') | ||
? 1 | ||
: 0; | ||
return bFirstSpecifier - aFirstSpecifier; | ||
} |
{ | ||
"name": "@ianvs/prettier-plugin-sort-imports", | ||
"private": false, | ||
"version": "4.0.0-alpha.1", | ||
"version": "4.0.0-alpha.2", | ||
"description": "A prettier plugins to sort imports in provided RegEx order", | ||
@@ -60,3 +60,2 @@ "main": "lib/src/index.js", | ||
"@babel/types": "^7.17.0", | ||
"javascript-natural-sort": "0.7.1", | ||
"lodash.clone": "^4.5.0", | ||
@@ -63,0 +62,0 @@ "lodash.isequal": "^4.5.0" |
@@ -34,5 +34,2 @@ NOTE: These docs are for the upcoming version 4.0. View the [3.X readme](https://github.com/IanVS/prettier-plugin-sort-imports/tree/main#readme) for the latest stable release, or the [migration guide](https://github.com/IanVS/prettier-plugin-sort-imports/blob/next/docs/MIGRATION.md#migrating-from-v3xx-to-v4xx) for the changes so far. | ||
- [`importOrder`](#importorder) | ||
- [`importOrderSortSpecifiers`](#importordersortspecifiers) | ||
- [`importOrderGroupNamespaceSpecifiers`](#importordergroupnamespacespecifiers) | ||
- [`importOrderCaseInsensitive`](#importordercaseinsensitive) | ||
- [`importOrderMergeDuplicateImports`](#importordermergeduplicateimports) | ||
@@ -136,7 +133,5 @@ - [`importOrderCombineTypeAndValueImports`](#importordercombinetypeandvalueimports) | ||
importOrder: ['^@core/(.*)$', '', '^@server/(.*)$', '', '^@ui/(.*)$', '', '^[./]'], | ||
importOrderCaseInsensitive: true, | ||
importOrderParserPlugins: ['typescript', 'jsx', 'decorators-legacy'], | ||
importOrderMergeDuplicateImports: true, | ||
importOrderCombineTypeAndValueImports: true, | ||
importOrderSortSpecifiers: true, | ||
}; | ||
@@ -229,53 +224,2 @@ ``` | ||
#### `importOrderSortSpecifiers` | ||
**type**: `boolean` | ||
**default value:** `false` | ||
A boolean value to enable or disable sorting of the specifiers in an import declarations. If enabled, type imports will be sorted after value imports. | ||
Before: | ||
```ts | ||
import Default, {type Bravo, delta as echo, charlie, type Alpha} from 'source'; | ||
``` | ||
After: | ||
```ts | ||
import Default, {charlie, delta as echo, type Alpha, type Bravo} from 'source'; | ||
``` | ||
#### `importOrderGroupNamespaceSpecifiers` | ||
**type**: `boolean` | ||
**default value:** `false` | ||
A boolean value to enable or disable sorting the namespace specifiers to the top of the import group. | ||
#### `importOrderCaseInsensitive` | ||
**type**: `boolean` | ||
**default value**: `false` | ||
A boolean value to enable case-insensitivity in the sorting algorithm | ||
used to order imports within each match group. | ||
For example, when false (or not specified): | ||
```javascript | ||
import ExampleView from './ExampleView'; | ||
import ExamplesList from './ExamplesList'; | ||
``` | ||
compared with `"importOrderCaseInsensitive": true`: | ||
```javascript | ||
import ExamplesList from './ExamplesList'; | ||
import ExampleView from './ExampleView'; | ||
``` | ||
#### `importOrderMergeDuplicateImports` | ||
@@ -282,0 +226,0 @@ |
@@ -42,31 +42,2 @@ import { ParserPlugin } from '@babel/parser'; | ||
/** | ||
* A boolean value to enable case-insensitivity in the sorting algorithm | ||
* used to order imports within each match group. | ||
* | ||
* For example, when false (or not specified): | ||
* | ||
* ```js | ||
* import ExampleView from './ExampleView'; | ||
* import ExamplesList from './ExamplesList'; | ||
* ``` | ||
* | ||
* compared with `"importOrderCaseInsensitive": true`: | ||
* | ||
* ```js | ||
* import ExamplesList from './ExamplesList'; | ||
* import ExampleView from './ExampleView'; | ||
* ``` | ||
* | ||
* @default false | ||
*/ | ||
importOrderCaseInsensitive?: boolean; | ||
/** | ||
* A boolean value to enable or disable sorting the namespace specifiers to the top of the import group. | ||
* | ||
* @default false | ||
*/ | ||
importOrderGroupNamespaceSpecifiers?: boolean; | ||
/** | ||
* When `true`, multiple import statements from the same module will be combined into a single import. | ||
@@ -100,9 +71,2 @@ * | ||
/** | ||
* A boolean value to enable or disable sorting of the specifiers in an import declarations. | ||
* | ||
* @default false | ||
*/ | ||
importOrderSortSpecifiers?: boolean; | ||
/** | ||
* A collection of plugins for babel parser. The plugin passes this list to babel parser, so it can understand the syntaxes | ||
@@ -109,0 +73,0 @@ * used in the file being formatted. The plugin uses prettier itself to figure out the parser it needs to use but if that fails, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9
75693
1146
323
- Removedjavascript-natural-sort@0.7.1
- Removedjavascript-natural-sort@0.7.1(transitive)