Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
tests/
lib/
examples/
.github/
docs/
*.md
24 changes: 12 additions & 12 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ orientation.
Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
- Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities
Expand Down Expand Up @@ -71,4 +71,4 @@ This Code of Conduct is adapted from the [Contributor Covenant][homepage], versi
available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
[version]: http://contributor-covenant.org/version/1/4/
157 changes: 79 additions & 78 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,84 +1,85 @@
{
"name": "@trivago/prettier-plugin-sort-imports",
"version": "6.0.0-0",
"description": "A prettier plugins to sort imports in provided RegEx order",
"main": "lib/src/index.js",
"type": "module",
"types": "types/index.d.ts",
"repository": {
"url": "https://github.com/trivago/prettier-plugin-sort-imports",
"type": "git"
},
"homepage": "https://github.com/trivago/prettier-plugin-sort-imports#readme",
"scripts": {
"prepare": "yarn run compile",
"compile": "tsc",
"preexample": "yarn run compile",
"example": "prettier --config ./examples/.prettierrc",
"test": "vitest run",
"type-check": "tsc --noEmit",
"prepublishOnly": "npm run compile && npm run test"
},
"keywords": [
"prettier",
"plugin",
"sort",
"import",
"typescript",
"javascript"
],
"author": {
"name": "Ayush Sharma",
"email": "[email protected]",
"url": "https://github.com/ayusharma"
},
"license": "Apache-2.0",
"dependencies": {
"@babel/generator": "^7.28.0",
"@babel/parser": "^7.28.0",
"@babel/traverse": "^7.28.0",
"@babel/types": "^7.28.0",
"javascript-natural-sort": "^0.7.1",
"lodash-es": "^4.17.21",
"parse-imports-exports": "^0.2.4"
},
"devDependencies": {
"@babel/core": "^7.26.7",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.27.0",
"@types/babel__traverse": "^7.20.7",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.10.10",
"@vue/compiler-sfc": "^3.5.13",
"prettier": "^3.4.2",
"prettier-plugin-ember-template-tag": "^2.1.0",
"prettier-plugin-svelte": "^3.3.3",
"svelte": "^4.2.19",
"typescript": "^5.7.3",
"vitest": "^3.2.4"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
"prettier": "2.x - 3.x",
"prettier-plugin-ember-template-tag": ">= 2.0.0",
"prettier-plugin-svelte": "3.x",
"svelte": "4.x || 5.x"
},
"engines": {
"node": ">= 20"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
"name": "@trivago/prettier-plugin-sort-imports",
"version": "6.0.0-0",
"description": "A prettier plugins to sort imports in provided RegEx order",
"main": "lib/src/index.js",
"type": "module",
"types": "types/index.d.ts",
"repository": {
"url": "https://github.com/trivago/prettier-plugin-sort-imports",
"type": "git"
},
"prettier-plugin-ember-template-tag": {
"optional": true
"homepage": "https://github.com/trivago/prettier-plugin-sort-imports#readme",
"scripts": {
"prepare": "yarn run compile",
"compile": "tsc",
"preexample": "yarn run compile",
"example": "prettier --config ./examples/.prettierrc",
"test": "vitest run",
"type-check": "tsc --noEmit",
"prepublishOnly": "npm run compile && npm run test",
"prettify": "yarn prettier --write ."
},
"prettier-plugin-svelte": {
"optional": true
"keywords": [
"prettier",
"plugin",
"sort",
"import",
"typescript",
"javascript"
],
"author": {
"name": "Ayush Sharma",
"email": "[email protected]",
"url": "https://github.com/ayusharma"
},
"svelte": {
"optional": true
"license": "Apache-2.0",
"dependencies": {
"@babel/generator": "^7.28.0",
"@babel/parser": "^7.28.0",
"@babel/traverse": "^7.28.0",
"@babel/types": "^7.28.0",
"javascript-natural-sort": "^0.7.1",
"lodash-es": "^4.17.21",
"parse-imports-exports": "^0.2.4"
},
"devDependencies": {
"@babel/core": "^7.26.7",
"@types/babel__core": "^7.20.5",
"@types/babel__generator": "^7.27.0",
"@types/babel__traverse": "^7.20.7",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.10.10",
"@vue/compiler-sfc": "^3.5.13",
"prettier": "^3.4.2",
"prettier-plugin-ember-template-tag": "^2.1.0",
"prettier-plugin-svelte": "^3.3.3",
"svelte": "^4.2.19",
"typescript": "^5.7.3",
"vitest": "^3.2.4"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
"prettier": "2.x - 3.x",
"prettier-plugin-ember-template-tag": ">= 2.0.0",
"prettier-plugin-svelte": "3.x",
"svelte": "4.x || 5.x"
},
"engines": {
"node": ">= 20"
},
"peerDependenciesMeta": {
"@vue/compiler-sfc": {
"optional": true
},
"prettier-plugin-ember-template-tag": {
"optional": true
},
"prettier-plugin-svelte": {
"optional": true
},
"svelte": {
"optional": true
}
}
}
}
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Options } from 'prettier';
import { parsers as babelParsers } from 'prettier/plugins/babel';
import { parsers as flowParsers } from 'prettier/plugins/flow';
import { parsers as htmlParsers } from 'prettier/plugins/html';
Expand All @@ -7,7 +8,6 @@ import { defaultPreprocessor } from './preprocessors/default-processor.js';
import { emberPreprocessor } from './preprocessors/ember-preprocessor.js';
import { sveltePreprocessor } from './preprocessors/svelte-preprocessor.js';
import { vuePreprocessor } from './preprocessors/vue-preprocessor.js';
import type { Options } from 'prettier';
import { createEmberParsers } from './utils/create-ember-parsers.js';
import { createSvelteParsers } from './utils/create-svelte-parsers.js';

Expand Down Expand Up @@ -61,11 +61,17 @@ const options: Options = {
category: 'Global',
default: null,
choices: [
{value: 'asc', description: 'will sort from shortest to longest'},
{value: 'desc', description: 'will sort from longest to shortest'},
{value: null, description: 'will disable sorting based on length'}
{ value: 'asc', description: 'will sort from shortest to longest' },
{
value: 'desc',
description: 'will sort from longest to shortest',
},
{
value: null,
description: 'will disable sorting based on length',
},
],
description: 'Should imports be sorted by their string length'
description: 'Should imports be sorted by their string length',
},
importOrderSideEffects: {
type: 'boolean',
Expand All @@ -78,7 +84,7 @@ const options: Options = {
category: 'Global',
default: 'with',
description: 'Provide a keyword for import attributes',
}
},
};

export default {
Expand Down
4 changes: 2 additions & 2 deletions src/preprocessors/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderSortSpecifiers,
importOrderSortByLength,
importOrderSideEffects,
importOrderImportAttributesKeyword
importOrderImportAttributesKeyword,
} = options;

const parserOptions: ParserOptions = {
Expand Down Expand Up @@ -46,7 +46,7 @@ export function preprocessor(code: string, options: PrettierOptions) {
importOrderGroupNamespaceSpecifiers,
importOrderSortSpecifiers,
importOrderSortByLength,
importOrderSideEffects
importOrderSideEffects,
});

return getCodeFromAst(allImports, directives, code, interpreter, {
Expand Down
14 changes: 7 additions & 7 deletions src/preprocessors/svelte-preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor.js';

let prettierPluginSvelte: typeof import('prettier-plugin-svelte') | undefined;
let svelteCompiler: typeof import('svelte/compiler') | undefined;

try {
prettierPluginSvelte = await import('prettier-plugin-svelte')
prettierPluginSvelte = await import('prettier-plugin-svelte');
svelteCompiler = await import('svelte/compiler');
} catch {
// Do not error because the dependency is optional.
}

import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor.js';

const booleanGuard = <T>(value: T | undefined): value is T => Boolean(value);

const sortImports = (code: string, options: PrettierOptions) => {
if (!svelteCompiler) {
throw new Error(
"Missing peer dependency 'svelte/compiler'. Please install it to use the svelte parser.",
"Missing peer dependency 'svelte/compiler'. Please install it to use the svelte parser.",
);
}

const { parse } = svelteCompiler;
const { instance, module } = parse(code);
const sources = [instance, module].filter(booleanGuard);
Expand All @@ -38,7 +38,7 @@ export function sveltePreprocessor(code: string, options: PrettierOptions) {

if (!prettierPluginSvelte) {
throw new Error(
"Missing peer dependency 'prettier-plugin-svelte'. Please install it to use the svelte parser.",
"Missing peer dependency 'prettier-plugin-svelte'. Please install it to use the svelte parser.",
);
}
// @ts-expect-error TODO: Fix this type error
Expand Down
8 changes: 4 additions & 4 deletions src/preprocessors/vue-preprocessor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor.js';

let vueCompilerSfc: typeof import('@vue/compiler-sfc') | undefined;

try {
Expand All @@ -6,13 +9,10 @@ try {
// Do not error because the dependency is optional.
}

import { PrettierOptions } from '../types';
import { preprocessor } from './preprocessor.js';

export function vuePreprocessor(code: string, options: PrettierOptions) {
if (!vueCompilerSfc) {
throw new Error(
"Missing peer dependency '@vue/compiler-sfc'. Please install it to use the vue parser.",
"Missing peer dependency '@vue/compiler-sfc'. Please install it to use the vue parser.",
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest'
import { expect, test } from 'vitest';

import { adjustCommentsOnSortedNodes } from '../adjust-comments-on-sorted-nodes';
import { getImportNodes } from '../get-import-nodes';
Expand Down
4 changes: 2 additions & 2 deletions src/utils/__tests__/get-all-comments-from-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ParserOptions } from '@babel/parser';
import { CommentBlock, CommentLine, ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest'
import { expect, test } from 'vitest';

import { getAllCommentsFromNodes } from '../get-all-comments-from-nodes';
import { getImportNodes } from '../get-import-nodes';
Expand All @@ -16,7 +16,7 @@ const getSortedImportNodes = (code: string, options?: ParserOptions) => {
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSideEffects: true
importOrderSideEffects: true,
});
};

Expand Down
6 changes: 3 additions & 3 deletions src/utils/__tests__/get-code-from-ast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse as babelParser, ParserOptions } from '@babel/parser';
import { ParserOptions, parse as babelParser } from '@babel/parser';
import { format } from 'prettier';
import { expect, test } from 'vitest'
import { expect, test } from 'vitest';

import { extractASTNodes } from '../extract-ast-nodes';
import { getCodeFromAst } from '../get-code-from-ast';
Expand All @@ -26,7 +26,7 @@ import a from 'a';
importOrderGroupNamespaceSpecifiers: false,
importOrderSortSpecifiers: false,
importOrderSortByLength: null,
importOrderSideEffects: true
importOrderSideEffects: true,
});
const formatted = getCodeFromAst(sortedNodes, [], code, null);
expect(await format(formatted, { parser: 'babel' })).toEqual(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from 'vitest'
import { expect, test } from 'vitest';

import { getExperimentalParserPlugins } from '../get-experimental-parser-plugins';

Expand Down
Loading