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

Control the separation of import groups as mentioned in #36 #42

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
Christian Rodemeyer committed Mar 25, 2021
commit 85470ee69cd5433382315caddddfd7dc07d63ff2
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"",
"^[./]"
],
"importOrderSeparation": true
"importOrderSeparation": false
}
30 changes: 11 additions & 19 deletions src/utils/get-sorted-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,19 @@ export const getSortedNodes = (
) => {
const originalNodes = nodes.map(clone);

// ignore empty regex separation if importOrderSeparation is true to not break existing configs
order = importOrderSeparation ? compact(order) : order;

const newLine =
importOrderSeparation && nodes.length > 1 ? newLineNode : null;
const sortedNodesByImportOrder = order.reduce((res, val) => {
if (val === '') return [...res, newLineNode];
const x = originalNodes.filter(
(node) => node.source.value.match(new RegExp(val)) !== null,
);

const sorted = originalNodes
.filter((node) => node.source.value.match(new RegExp(val)) !== null)
.sort((a, b) => naturalSort(a.source.value, b.source.value));

// remove "found" imports from the list of nodes
pull(originalNodes, ...x);

if (x.length > 0) {
x.sort((a, b) => naturalSort(a.source.value, b.source.value));
if (res.length > 0) {
return compact([...res, newLine, ...x]);
}
return x;
}
return res;
pull(originalNodes, ...sorted);

return compact([...res, newLine, ...sorted]);
}, [] as (ImportDeclaration | ExpressionStatement)[]);

const sortedNodesNotInImportOrder = originalNodes.filter(
Expand All @@ -62,12 +53,13 @@ export const getSortedNodes = (

const allSortedNodes = compact([
...sortedNodesNotInImportOrder,
newLine,
...sortedNodesByImportOrder,
newLineNode, // insert a newline after all sorted imports
]);
if (allSortedNodes.length > 1 && allSortedNodes[0] === newLineNode) {
allSortedNodes.shift(); // remove trailing new line

// remove trailing newlines
while (allSortedNodes[0] === newLineNode) {
allSortedNodes.shift();
}

// maintain a copy of the nodes to extract comments from
Expand Down
307 changes: 307 additions & 0 deletions tests/ImportsSeparatedByEmptyRegex/__snapshots__/ppsi.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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";
export { random } from './random';
import c from 'c';
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import a from 'a';
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
export default {
title: 'hello',
};
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import x from 'x';

function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import a from "a";
import c from "c";
import thirdParty from "third-party";
import x from "x";

import otherthing from "@core/otherthing";
import something from "@server/something";
import component from "@ui/hello";

import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";

export { random } from "./random";

export default {
title: "hello",
};

function add(a: number, b: number) {
return a + b;
}

`;

exports[`import-export-only.ts - typescript-verify: import-export-only.ts 1`] = `
import React from 'react';
export const a = 1;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import React from "react";

export const a = 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';

// Comment
// Comment

function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// I am top level comment in this file.
// I am second line of top level comment in this file.
import "./commands";

// Comment
// Comment

function add(a: number, b: number) {
return a + b;
}

`;

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';
import React from 'react';
// Comment
// Comment

function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// I am top level comment in this file.
// I am second line of top level comment in this file.
import React from "react";

import "./commands";

// Comment
// Comment

function add(a: number, b: number) {
return a + b;
}

`;

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';
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import abc from "@core/abc";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import xyz from "@ui/xyz";
import qwerty from "@server/qwerty";

function add(a:number,b:number) {
return a + b;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// I am top level comment in this file.
// I am second line of top level comment in this file.
import thirdParty from "third-party";
import z from "z";

import abc from "@core/abc";
import otherthing from "@core/otherthing";
import qwerty from "@server/qwerty";
import something from "@server/something";
import component from "@ui/hello";
import xyz from "@ui/xyz";

import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";

function add(a: number, b: number) {
return a + b;
}

`;

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";
// I am stick to sameLevelRelativePath
import sameLevelRelativePath from "./sameLevelRelativePath";
// I am stick to third party comment
import thirdParty from "third-party";
// leading comment
import {
random // inner comment
} from './random';
// leading comment
export {
random // inner comment
} from './random';
import c from 'c';
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import a from 'a';
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
export default {
title: 'hello',
};
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import x from 'x';

// I am function comment

function add(a:number,b:number) {
return a + b; // I am inside function
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//@ts-ignore
// I am file top level comments
import a from "a";
import c from "c";
// I am stick to third party comment
import thirdParty from "third-party";
import x from "x";

import otherthing from "@core/otherthing";
import something from "@server/something";
import component from "@ui/hello";

import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import twoLevelRelativePath from "../../twoLevelRelativePath";
import oneLevelRelativePath from "../oneLevelRelativePath";
// leading comment
import {
random, // inner comment
} from "./random";
// I am stick to sameLevelRelativePath
import sameLevelRelativePath from "./sameLevelRelativePath";

// leading comment
export {
random, // inner comment
} from "./random";

export default {
title: "hello",
};

// I am function comment

function add(a: number, b: number) {
return a + b; // I am inside function
}

`;

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";
// I am comment
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// I am top level comment
import abc from "@core/abc";
import otherthing from "@core/otherthing";
import something from "@server/something";
import component from "@ui/hello";

import fourLevelRelativePath from "../../../../fourLevelRelativePath";
// I am comment
import twoLevelRelativePath from "../../twoLevelRelativePath";

`;

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

`;

exports[`one-import.ts - typescript-verify: one-import.ts 1`] = `
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands';

// Alternatively you can use CommonJS syntax:
// require('./commands')
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This example support/index.js is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************
// Import commands.js using ES2015 syntax:
import "./commands";

// Alternatively you can use CommonJS syntax:
// require('./commands')

`;
20 changes: 20 additions & 0 deletions tests/ImportsSeparatedByEmptyRegex/import-export-in-between.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import threeLevelRelativePath from "../../../threeLevelRelativePath";
import sameLevelRelativePath from "./sameLevelRelativePath";
import thirdParty from "third-party";
export { random } from './random';
import c from 'c';
import oneLevelRelativePath from "../oneLevelRelativePath";
import otherthing from "@core/otherthing";
import a from 'a';
import twoLevelRelativePath from "../../twoLevelRelativePath";
import component from "@ui/hello";
export default {
title: 'hello',
};
import fourLevelRelativePath from "../../../../fourLevelRelativePath";
import something from "@server/something";
import x from 'x';

function add(a:number,b:number) {
return a + b;
}
2 changes: 2 additions & 0 deletions tests/ImportsSeparatedByEmptyRegex/import-export-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from 'react';
export const a = 1;
Loading