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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import React, {
KeyboardEvent,
} from 'react';
import { logger } from '@core/logger';
import { reduce, debounce } from 'lodash';
import { reduce, debounce } from 'lodash-es';
import { Message } from '../Message';
import { createServer } from '@server/node';
import { Alert } from '@ui/Alert';
Expand All @@ -29,7 +29,7 @@ import { createConnection } from '@server/database';
### Output

```javascript
import { debounce, reduce } from 'lodash';
import { debounce, reduce } from 'lodash-es';
import React, {
ChangeEvent,
FC,
Expand Down
2 changes: 1 addition & 1 deletion docs/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Due to the package handling of the pnpm, sometimes, the plugin is not automatica
via prettier config.
```js
module.exports = {
plugins: [require('@trivago/prettier-plugin-sort-imports')],
plugins: ['@trivago/prettier-plugin-sort-imports'],
}
```

Expand Down
2 changes: 1 addition & 1 deletion examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
KeyboardEvent,
} from 'react';
import { logger } from '@core/logger';
import { reduce, debounce } from 'lodash';
import { reduce, debounce } from 'lodash-es';
import { Message } from '../Message';
import { createServer } from '@server/node';
import { Alert } from '@ui/Alert';
Expand Down
12 changes: 0 additions & 12 deletions jest.config.js

This file was deleted.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"compile": "tsc",
"preexample": "yarn run compile",
"example": "prettier --config ./examples/.prettierrc --plugin lib/src/index.js",
"test": "yarn node --experimental-vm-modules $(yarn bin jest)",
"test": "vitest run",
"type-check": "tsc --noEmit",
"prepublishOnly": "npm run compile && npm run test"
},
Expand All @@ -39,21 +39,21 @@
"@babel/traverse": "^7.26.7",
"@babel/types": "^7.26.7",
"javascript-natural-sort": "^0.7.1",
"lodash": "^4.17.21"
"lodash-es": "^4.17.21"
},
"devDependencies": {
"@babel/core": "^7.26.7",
"@types/chai": "^5.0.1",
"@types/jest": "^29.5.14",
"@types/lodash": "^4.17.14",
"@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",
"jest": "^29.7.0",
"prettier": "^3.4.2",
"prettier-plugin-svelte": "^3.3.3",
"svelte": "^4.2.19",
"ts-jest": "^29.2.5",
"typescript": "^5.7.3"
"typescript": "^5.7.3",
"vitest": "^3.2.4"
},
"peerDependencies": {
"@vue/compiler-sfc": "3.x",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { vuePreprocessor } from './preprocessors/vue-preprocessor';
import type { Options } from 'prettier';
import { createSvelteParsers } from './utils/create-svelte-parsers';

const svelteParsers = createSvelteParsers();
const svelteParsers = await createSvelteParsers();

const options: Options = {
importOrder: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest'

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

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

import { extractASTNodes } from '../extract-ast-nodes';
import { getCodeFromAst } from '../get-code-from-ast';
Expand Down Expand Up @@ -54,7 +55,7 @@ import a from 'a';`;
};
const ast = babelParser(code, parserOptions);
if (!ast) throw new Error('ast is null');
const { directives, importNodes } = extractASTNodes(ast);
const { directives, importNodes } = extractASTNodes(ast as any);

const formatted = getCodeFromAst(importNodes, directives, code, null);
expect(await format(formatted, { parser: 'babel' })).toEqual(
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-experimental-parser-plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest'

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

test('it should return empty list', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-import-nodes-matched-group.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest'

import { THIRD_PARTY_MODULES_SPECIAL_WORD } from '../../constants';
import { ImportGroups } from '../../types';
import { getImportNodes } from '../get-import-nodes';
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/get-sorted-import-specifiers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest'

import { getImportNodes } from '../get-import-nodes';
import { getSortedImportSpecifiers } from '../get-sorted-import-specifiers';
import { getSortedNodesModulesNames } from '../get-sorted-nodes-modules-names';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest'

import { getImportNodes } from '../get-import-nodes';
import { getSortedNodes } from '../get-sorted-nodes';
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/get-sorted-nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ImportDeclaration } from '@babel/types';
import { expect, test } from 'vitest'

import { getImportNodes } from '../get-import-nodes';
import { getSortedNodes } from '../get-sorted-nodes';
Expand Down
2 changes: 2 additions & 0 deletions src/utils/__tests__/is-sort-imports-ignored.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, test } from 'vitest';

import { getImportNodes } from '../get-import-nodes';
import { isSortImportsIgnored } from '../is-sort-imports-ignored';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { format } from 'prettier';
import { expect, test } from 'vitest'

import { getAllCommentsFromNodes } from '../get-all-comments-from-nodes';
import { getImportNodes } from '../get-import-nodes';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/adjust-comments-on-sorted-nodes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ImportDeclaration, addComments, removeComments } from '@babel/types';
import { clone, isEqual } from 'lodash';
import { clone, isEqual } from 'lodash-es';

import { ImportOrLine } from '../types';

Expand Down
9 changes: 5 additions & 4 deletions src/utils/create-svelte-parsers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export function createSvelteParsers() {
export async function createSvelteParsers() {
try {
var { parsers } = require('prettier-plugin-svelte');
const sveltePlugin = await import('prettier-plugin-svelte');
const { parsers } = (sveltePlugin.default || sveltePlugin) as any;
return { parsers };
} catch {
return {};
}
return { parsers };
}
}
2 changes: 1 addition & 1 deletion src/utils/get-sorted-nodes-by-import-order.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clone } from 'lodash';
import { clone } from 'lodash-es';

import { THIRD_PARTY_MODULES_SPECIAL_WORD, newLineNode, SEPARATOR_SPECIAL_WORD } from '../constants';
import { naturalSort } from '../natural-sort';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const RAW = Symbol.for('raw');

module.exports = {
export default {
print(val) {
return val[RAW];
},
Expand Down
18 changes: 13 additions & 5 deletions test-setup/run_spec.cjs → test-setup/run_spec.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';

const fs = require('fs');
const extname = require('path').extname;
const prettier = require('prettier');
const plugin = require('../src');
import fs from 'fs';
import { extname } from 'path';
import prettier from 'prettier';
import plugin from '../src/';

function run_spec(dirname, parsers, options) {
options = Object.assign(
{
plugins: [plugin.default],
plugins: [plugin],
tabWidth: 4,
},
options,
Expand All @@ -25,6 +25,8 @@ function run_spec(dirname, parsers, options) {
extname(filename) !== '.snap' &&
fs.lstatSync(path).isFile() &&
filename[0] !== '.' &&
filename !== 'ppsi.spec.js' &&
filename !== 'ppsi.spec.mjs' &&
filename !== 'ppsi.spec.cjs'
) {
const source = read(path).replace(/\r\n/g, '\n');
Expand Down Expand Up @@ -59,6 +61,12 @@ function run_spec(dirname, parsers, options) {
}
});
}
import { expect } from 'vitest';
import rawSerializer from './raw-serializer.mjs';

// Add custom snapshot serializer for Vitest
expect.addSnapshotSerializer(rawSerializer);

global.run_spec = run_spec;

function stripLocation(ast) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Angular/__snapshots__/ppsi.spec.cjs.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`imports-with-decorators.js - typescript-verify: imports-with-decorators.js 1`] = `
exports[`imports-with-decorators.js - typescript-verify > imports-with-decorators.js 1`] = `
import { Body, Controller, HttpService, Logger, Post } from "@core/common";
import { retry } from "@server/operators";

Expand Down
4 changes: 2 additions & 2 deletions tests/Babel/__snapshots__/ppsi.spec.cjs.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`imports-with-comments.js - babel-verify: imports-with-comments.js 1`] = `
exports[`imports-with-comments.js - babel-verify > imports-with-comments.js 1`] = `
// I am top level comment in this file.
import z from 'z';
import threeLevelRelativePath from "../../../threeLevelRelativePath";
Expand Down
4 changes: 2 additions & 2 deletions tests/Flow/__snapshots__/ppsi.spec.cjs.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`imports-with-comments.js - flow-verify: imports-with-comments.js 1`] = `
exports[`imports-with-comments.js - flow-verify > imports-with-comments.js 1`] = `
/**
* @flow
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`imports-with-side-effect-imports.js - typescript-verify: imports-with-side-effect-imports.js 1`] = `
exports[`imports-with-side-effect-imports.js - typescript-verify > imports-with-side-effect-imports.js 1`] = `
// I am top level comment in this file.
import thirdParty0 from "third-party0";
import something3 from "@core/something3";
Expand Down
26 changes: 13 additions & 13 deletions tests/ImportsNotSeparated/__snapshots__/ppsi.spec.cjs.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`import-export-in-between.ts - typescript-verify: import-export-in-between.ts 1`] = `
exports[`import-export-in-between.ts - typescript-verify > import-export-in-between.ts 1`] = `
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
Expand Down Expand Up @@ -47,7 +47,7 @@ function add(a: number, b: number) {

`;

exports[`import-export-only.ts - typescript-verify: import-export-only.ts 1`] = `
exports[`import-export-only.ts - typescript-verify > import-export-only.ts 1`] = `
import React from 'react';
export const a = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -57,7 +57,7 @@ export const a = 1;

`;

exports[`imports-with-comments.ts - typescript-verify: imports-with-comments.ts 1`] = `
exports[`imports-with-comments.ts - typescript-verify > imports-with-comments.ts 1`] = `
// I am top level comment in this file.
// I am second line of top level comment in this file.
import './commands';
Expand All @@ -82,7 +82,7 @@ function add(a: number, b: number) {

`;

exports[`imports-with-comments-and-third-party.ts - typescript-verify: imports-with-comments-and-third-party.ts 1`] = `
exports[`imports-with-comments-and-third-party.ts - typescript-verify > imports-with-comments-and-third-party.ts 1`] = `
// I am top level comment in this file.
// I am second line of top level comment in this file.
import './commands';
Expand All @@ -108,7 +108,7 @@ function add(a: number, b: number) {

`;

exports[`imports-with-comments-on-top.ts - typescript-verify: imports-with-comments-on-top.ts 1`] = `
exports[`imports-with-comments-on-top.ts - typescript-verify > imports-with-comments-on-top.ts 1`] = `
// I am top level comment in this file.
// I am second line of top level comment in this file.
import z from 'z';
Expand Down Expand Up @@ -151,7 +151,7 @@ function add(a: number, b: number) {

`;

exports[`imports-with-directives.ts - typescript-verify: imports-with-directives.ts 1`] = `
exports[`imports-with-directives.ts - typescript-verify > imports-with-directives.ts 1`] = `
'use strict';
'use client';
import otherthing from "@core/otherthing";
Expand Down Expand Up @@ -208,7 +208,7 @@ const workletAdd = (a: number, b: number) => {

`;

exports[`imports-with-file-level-comments.ts - typescript-verify: imports-with-file-level-comments.ts 1`] = `
exports[`imports-with-file-level-comments.ts - typescript-verify > imports-with-file-level-comments.ts 1`] = `
//@ts-ignore
// I am file top level comments
import threeLevelRelativePath from "../../../threeLevelRelativePath";
Expand Down Expand Up @@ -281,7 +281,7 @@ function add(a: number, b: number) {

`;

exports[`imports-with-interpreter-directive.ts - typescript-verify: imports-with-interpreter-directive.ts 1`] = `
exports[`imports-with-interpreter-directive.ts - typescript-verify > imports-with-interpreter-directive.ts 1`] = `
#!/usr/bin/env node
import otherthing from "@core/otherthing";
import abc from "@core/abc";
Expand Down Expand Up @@ -312,7 +312,7 @@ function add(a: number, b: number) {

`;

exports[`imports-without-third-party.ts - typescript-verify: imports-without-third-party.ts 1`] = `
exports[`imports-without-third-party.ts - typescript-verify > imports-without-third-party.ts 1`] = `
// I am top level comment
import otherthing from "@core/otherthing";
import abc from "@core/abc";
Expand All @@ -333,7 +333,7 @@ import twoLevelRelativePath from "../../twoLevelRelativePath";

`;

exports[`no-import-export.ts - typescript-verify: no-import-export.ts 1`] = `
exports[`no-import-export.ts - typescript-verify > no-import-export.ts 1`] = `
function add(a:number,b:number) {
return a + b;
}
Expand All @@ -344,7 +344,7 @@ function add(a: number, b: number) {

`;

exports[`one-import.ts - typescript-verify: one-import.ts 1`] = `
exports[`one-import.ts - typescript-verify > one-import.ts 1`] = `
// This example support/index.js is processed and
// loaded automatically before your test files.
//
Expand Down Expand Up @@ -386,7 +386,7 @@ import "./commands";

`;

exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = `
exports[`sort-imports-ignored.ts - typescript-verify > sort-imports-ignored.ts 1`] = `
// sort-imports-ignore

import './commands';
Expand Down
Loading