From b9c05b60cddc388e2798392d44245fb540780cf3 Mon Sep 17 00:00:00 2001 From: Chason Choate Date: Thu, 19 Dec 2024 22:32:06 -0600 Subject: [PATCH 1/2] Added current-state snapshots Capturing the current state of transformations so we can see how the upcoming changes affect them. --- .../__snapshots__/ppsi.spec.js.snap | 460 ++++++++++++++++++ .../import-export-in-between.ts | 20 + .../import-export-only.ts | 2 + .../import-with-type-imports-together.ts | 6 + .../imports-with-comments-and-third-party.ts | 10 + .../imports-with-comments-on-top.ts | 19 + .../imports-with-comments.ts | 10 + .../imports-with-directives.ts | 30 ++ .../imports-with-file-level-comments.ts | 33 ++ .../imports-with-interpreter-directive.ts | 13 + .../imports-without-third-party.ts | 8 + .../no-import-export.ts | 3 + tests/ImportsSeparatedByUser/one-import.ts | 19 + tests/ImportsSeparatedByUser/ppsi.spec.js | 5 + .../sort-imports-ignored.ts | 11 + 15 files changed, 649 insertions(+) create mode 100644 tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap create mode 100644 tests/ImportsSeparatedByUser/import-export-in-between.ts create mode 100644 tests/ImportsSeparatedByUser/import-export-only.ts create mode 100644 tests/ImportsSeparatedByUser/import-with-type-imports-together.ts create mode 100644 tests/ImportsSeparatedByUser/imports-with-comments-and-third-party.ts create mode 100644 tests/ImportsSeparatedByUser/imports-with-comments-on-top.ts create mode 100644 tests/ImportsSeparatedByUser/imports-with-comments.ts create mode 100644 tests/ImportsSeparatedByUser/imports-with-directives.ts create mode 100644 tests/ImportsSeparatedByUser/imports-with-file-level-comments.ts create mode 100644 tests/ImportsSeparatedByUser/imports-with-interpreter-directive.ts create mode 100644 tests/ImportsSeparatedByUser/imports-without-third-party.ts create mode 100644 tests/ImportsSeparatedByUser/no-import-export.ts create mode 100644 tests/ImportsSeparatedByUser/one-import.ts create mode 100644 tests/ImportsSeparatedByUser/ppsi.spec.js create mode 100644 tests/ImportsSeparatedByUser/sort-imports-ignored.ts diff --git a/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap b/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap new file mode 100644 index 00000000..bb5d393f --- /dev/null +++ b/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap @@ -0,0 +1,460 @@ +// 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[`import-with-type-imports-together.ts - typescript-verify: import-with-type-imports-together.ts 1`] = ` +import { foo } from "@server/foo" +import type { Quux } from "./quux" +import { Link } from "@ui/Link" +import type { Bar } from "@server/bar" +import type { LinkProps } from "@ui/Link/LinkProps" +import { baz } from "./baz" +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import type { Bar } from "@server/bar"; +import { foo } from "@server/foo"; + +import { Link } from "@ui/Link"; +import type { LinkProps } from "@ui/Link/LinkProps"; + +import { baz } from "./baz"; +import type { Quux } from "./quux"; + +`; + +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-directives.ts - typescript-verify: imports-with-directives.ts 1`] = ` +'use strict'; +'use client'; + +// comment after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; + +// Comment + +function add(a:number,b:number) { + return a + b; +} + +function addStrict(a:number,b:number) { + 'use strict'; + return a + b; +} + +'preserve me'; + +const workletAdd = (a:number,b:number) => { + 'worklet'; + return a + b; +} + +(function() { + 'use strict'; + // some iffe example + return true; +})(); +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +"use strict"; +"use client"; + +// comment after directives +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; + +// Comment + +function add(a: number, b: number) { + return a + b; +} + +function addStrict(a: number, b: number) { + "use strict"; + return a + b; +} + +("preserve me"); + +const workletAdd = (a: number, b: number) => { + "worklet"; + return a + b; +}; + +(function () { + "use strict"; + // some iffe example + return true; +})(); + +`; + +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-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"; +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; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +#!/usr/bin/env node +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 twoLevelRelativePath from "../../twoLevelRelativePath"; + +function add(a: number, b: number) { + return a + b; +} + +`; + +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') + +`; + +exports[`sort-imports-ignored.ts - typescript-verify: sort-imports-ignored.ts 1`] = ` +// sort-imports-ignore + +import './commands'; +import b from 'b'; +import a from 'a'; + +// Comment + +function add(a:number,b:number) { + return a + b; +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// sort-imports-ignore + +import "./commands"; +import b from "b"; +import a from "a"; + +// Comment + +function add(a: number, b: number) { + return a + b; +} + +`; diff --git a/tests/ImportsSeparatedByUser/import-export-in-between.ts b/tests/ImportsSeparatedByUser/import-export-in-between.ts new file mode 100644 index 00000000..16bc1d77 --- /dev/null +++ b/tests/ImportsSeparatedByUser/import-export-in-between.ts @@ -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; +} diff --git a/tests/ImportsSeparatedByUser/import-export-only.ts b/tests/ImportsSeparatedByUser/import-export-only.ts new file mode 100644 index 00000000..44f7eff4 --- /dev/null +++ b/tests/ImportsSeparatedByUser/import-export-only.ts @@ -0,0 +1,2 @@ +import React from 'react'; +export const a = 1; diff --git a/tests/ImportsSeparatedByUser/import-with-type-imports-together.ts b/tests/ImportsSeparatedByUser/import-with-type-imports-together.ts new file mode 100644 index 00000000..b6d28272 --- /dev/null +++ b/tests/ImportsSeparatedByUser/import-with-type-imports-together.ts @@ -0,0 +1,6 @@ +import { foo } from "@server/foo" +import type { Quux } from "./quux" +import { Link } from "@ui/Link" +import type { Bar } from "@server/bar" +import type { LinkProps } from "@ui/Link/LinkProps" +import { baz } from "./baz" diff --git a/tests/ImportsSeparatedByUser/imports-with-comments-and-third-party.ts b/tests/ImportsSeparatedByUser/imports-with-comments-and-third-party.ts new file mode 100644 index 00000000..a9fb311d --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-with-comments-and-third-party.ts @@ -0,0 +1,10 @@ +// 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; +} diff --git a/tests/ImportsSeparatedByUser/imports-with-comments-on-top.ts b/tests/ImportsSeparatedByUser/imports-with-comments-on-top.ts new file mode 100644 index 00000000..6b08ed06 --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-with-comments-on-top.ts @@ -0,0 +1,19 @@ +// 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; +} diff --git a/tests/ImportsSeparatedByUser/imports-with-comments.ts b/tests/ImportsSeparatedByUser/imports-with-comments.ts new file mode 100644 index 00000000..67b8e461 --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-with-comments.ts @@ -0,0 +1,10 @@ +// 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; +} diff --git a/tests/ImportsSeparatedByUser/imports-with-directives.ts b/tests/ImportsSeparatedByUser/imports-with-directives.ts new file mode 100644 index 00000000..221ec608 --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-with-directives.ts @@ -0,0 +1,30 @@ +'use strict'; +'use client'; + +// comment after directives +import otherthing from "@core/otherthing"; +import abc from "@core/abc"; + +// Comment + +function add(a:number,b:number) { + return a + b; +} + +function addStrict(a:number,b:number) { + 'use strict'; + return a + b; +} + +'preserve me'; + +const workletAdd = (a:number,b:number) => { + 'worklet'; + return a + b; +} + +(function() { + 'use strict'; + // some iffe example + return true; +})(); diff --git a/tests/ImportsSeparatedByUser/imports-with-file-level-comments.ts b/tests/ImportsSeparatedByUser/imports-with-file-level-comments.ts new file mode 100644 index 00000000..8ffcfb5d --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-with-file-level-comments.ts @@ -0,0 +1,33 @@ +//@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 +} diff --git a/tests/ImportsSeparatedByUser/imports-with-interpreter-directive.ts b/tests/ImportsSeparatedByUser/imports-with-interpreter-directive.ts new file mode 100644 index 00000000..8e7de5f6 --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-with-interpreter-directive.ts @@ -0,0 +1,13 @@ +#!/usr/bin/env node +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; +} diff --git a/tests/ImportsSeparatedByUser/imports-without-third-party.ts b/tests/ImportsSeparatedByUser/imports-without-third-party.ts new file mode 100644 index 00000000..ef1797a2 --- /dev/null +++ b/tests/ImportsSeparatedByUser/imports-without-third-party.ts @@ -0,0 +1,8 @@ +// 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"; diff --git a/tests/ImportsSeparatedByUser/no-import-export.ts b/tests/ImportsSeparatedByUser/no-import-export.ts new file mode 100644 index 00000000..e8f71db3 --- /dev/null +++ b/tests/ImportsSeparatedByUser/no-import-export.ts @@ -0,0 +1,3 @@ +function add(a:number,b:number) { + return a + b; +} diff --git a/tests/ImportsSeparatedByUser/one-import.ts b/tests/ImportsSeparatedByUser/one-import.ts new file mode 100644 index 00000000..35d75ba6 --- /dev/null +++ b/tests/ImportsSeparatedByUser/one-import.ts @@ -0,0 +1,19 @@ +// 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') diff --git a/tests/ImportsSeparatedByUser/ppsi.spec.js b/tests/ImportsSeparatedByUser/ppsi.spec.js new file mode 100644 index 00000000..967d2fcd --- /dev/null +++ b/tests/ImportsSeparatedByUser/ppsi.spec.js @@ -0,0 +1,5 @@ +run_spec(__dirname, ["typescript"], { + importOrder: ['^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '^[./]'], + importOrderSeparation: true, + importOrderParserPlugins: ['typescript'] +}); diff --git a/tests/ImportsSeparatedByUser/sort-imports-ignored.ts b/tests/ImportsSeparatedByUser/sort-imports-ignored.ts new file mode 100644 index 00000000..c53ddcf0 --- /dev/null +++ b/tests/ImportsSeparatedByUser/sort-imports-ignored.ts @@ -0,0 +1,11 @@ +// sort-imports-ignore + +import './commands'; +import b from 'b'; +import a from 'a'; + +// Comment + +function add(a:number,b:number) { + return a + b; +} From d2bf6dc1cfa21f3e1e195fc66be871c0faabffd0 Mon Sep 17 00:00:00 2001 From: Chason Choate Date: Thu, 19 Dec 2024 22:56:49 -0600 Subject: [PATCH 2/2] Add `` support When `importOrderSeparation` is enabled, users can further control separation placement with the `` keyword. --- README.md | 3 + src/constants.ts | 3 +- .../get-sorted-nodes-by-import-order.spec.ts | 138 ++++++++++++++++++ src/utils/get-sorted-nodes-by-import-order.ts | 10 +- .../__snapshots__/ppsi.spec.js.snap | 14 -- tests/ImportsSeparatedByUser/ppsi.spec.js | 2 +- 6 files changed, 152 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9c29388f..d89f3130 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,9 @@ between sorted import declarations group. The separation takes place according t "importOrderSeparation": true, ``` +If this option is enabled and `` is used in the `importOrder` array, the plugin +will ONLY add newlines at those locations and at the end of the imports. + #### `importOrderSortSpecifiers` **type**: `boolean` diff --git a/src/constants.ts b/src/constants.ts index 28c009e7..b348b187 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -20,8 +20,9 @@ export const THIRD_PARTY_MODULES_SPECIAL_WORD = ''; export const THIRD_PARTY_TYPES_SPECIAL_WORD = ''; export const TYPES_SPECIAL_WORD = ''; +export const SEPARATOR_SPECIAL_WORD = ''; -const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE = +export const PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE = 'PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE'; export const newLineNode = expressionStatement( diff --git a/src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts b/src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts index cc861b61..a9cc8f6e 100644 --- a/src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts +++ b/src/utils/__tests__/get-sorted-nodes-by-import-order.spec.ts @@ -4,6 +4,8 @@ import { getImportNodes } from '../get-import-nodes'; import { getSortedNodes } from '../get-sorted-nodes'; import { getSortedNodesModulesNames } from '../get-sorted-nodes-modules-names'; import { getSortedNodesNames } from '../get-sorted-nodes-names'; +import { PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE } from "../../constants" +import { ImportOrLine } from "../../types" const code = `// first comment // second comment @@ -337,3 +339,139 @@ test('it returns all sorted nodes with namespace specifiers at the top', () => { 'z', ]); }); + +test('it returns the default separations if `importOrderSeparation` is false', () => { + const result = getImportNodes(code); + const sorted = getSortedNodes(result, { + importOrder: ['', '^a$', '^t$', '', '^k$', '^B', ''], + importOrderCaseInsensitive: false, + importOrderSeparation: false, + importOrderGroupNamespaceSpecifiers: false, + importOrderSortSpecifiers: false, + importOrderSideEffects: true, + }); + expect(getSeparationData(sorted)).toEqual([ + { type: "ImportDeclaration", value: 'XY' }, + { type: "ImportDeclaration", value: 'Xa' }, + { type: "ImportDeclaration", value: 'c' }, + { type: "ImportDeclaration", value: 'g' }, + { type: "ImportDeclaration", value: 'x' }, + { type: "ImportDeclaration", value: 'z' }, + { type: "ImportDeclaration", value: 'a' }, + { type: "ImportDeclaration", value: 't' }, + { type: "ImportDeclaration", value: 'k' }, + { type: "ImportDeclaration", value: 'BY' }, + { type: "ImportDeclaration", value: 'Ba' }, + { type: "ExpressionStatement", value: undefined }, + ]); +}); + +test('it returns default import module separations', () => { + const result = getImportNodes(code); + const sorted = getSortedNodes(result, { + importOrder: ['^a$', '^t$', '^k$', '^B'], + importOrderCaseInsensitive: false, + importOrderSeparation: true, + importOrderGroupNamespaceSpecifiers: false, + importOrderSortSpecifiers: false, + importOrderSideEffects: true, + }); + expect(getSeparationData(sorted)).toEqual([ + { type: "ImportDeclaration", value: 'XY' }, + { type: "ImportDeclaration", value: 'Xa' }, + { type: "ImportDeclaration", value: 'c' }, + { type: "ImportDeclaration", value: 'g' }, + { type: "ImportDeclaration", value: 'x' }, + { type: "ImportDeclaration", value: 'z' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ImportDeclaration", value: 'a' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ImportDeclaration", value: 't' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ImportDeclaration", value: 'k' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ImportDeclaration", value: 'BY' }, + { type: "ImportDeclaration", value: 'Ba' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ExpressionStatement", value: undefined }, + ]); +}); + +test('it returns targeted import module separations', () => { + const result = getImportNodes(code); + const sorted = getSortedNodes(result, { + importOrder: ['^a$', '', '^t$', '', '^k$', '^B'], + importOrderCaseInsensitive: false, + importOrderSeparation: true, + importOrderGroupNamespaceSpecifiers: false, + importOrderSortSpecifiers: false, + importOrderSideEffects: true, + }); + expect(getSeparationData(sorted)).toEqual([ + { type: "ImportDeclaration", value: 'XY' }, + { type: "ImportDeclaration", value: 'Xa' }, + { type: "ImportDeclaration", value: 'c' }, + { type: "ImportDeclaration", value: 'g' }, + { type: "ImportDeclaration", value: 'x' }, + { type: "ImportDeclaration", value: 'z' }, + { type: "ImportDeclaration", value: 'a' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ImportDeclaration", value: 't' }, + { type: "ExpressionStatement", value: undefined }, + { type: "ImportDeclaration", value: 'k' }, + { type: "ImportDeclaration", value: 'BY' }, + { type: "ImportDeclaration", value: 'Ba' }, + { type: "ExpressionStatement", value: undefined }, + ]); +}); + +test('it never returns a separation at the top of the list (leading separator)', () => { + const result = getImportNodes(` + import './test'; + `.trim()); + const sorted = getSortedNodes(result, { + importOrder: ['', '^[./]'], + importOrderCaseInsensitive: false, + importOrderSeparation: true, + importOrderGroupNamespaceSpecifiers: false, + importOrderSortSpecifiers: false, + importOrderSideEffects: true, + }); + expect(getSeparationData(sorted)).toEqual([ + { type: "ImportDeclaration", value: './test' }, + { type: "ExpressionStatement", value: undefined }, + ]); +}); + +test('it never returns a separation at the top of the list (zero preceding imports)', () => { + const result = getImportNodes(` + import './test'; + `.trim()); + const sorted = getSortedNodes(result, { + importOrder: ['^a.*$', '', '^[./]'], + importOrderCaseInsensitive: false, + importOrderSeparation: true, + importOrderGroupNamespaceSpecifiers: false, + importOrderSortSpecifiers: false, + importOrderSideEffects: true, + }); + expect(getSeparationData(sorted)).toEqual([ + { type: "ImportDeclaration", value: './test' }, + { type: "ExpressionStatement", value: undefined }, + ]); +}); + +// Focuses the nodes solely to the import declarations and the new lines +function getSeparationData(nodes: ImportOrLine[]): { type: "ImportDeclaration" | "ExpressionStatement", value?: string }[] { + return nodes + .filter(node => node.type === "ImportDeclaration" + || ( + node.type === "ExpressionStatement" + && node.expression.type === "StringLiteral" + && node.expression.value === PRETTIER_PLUGIN_SORT_IMPORTS_NEW_LINE + )) + .map(x => ({ + type: x.type, + value: x.type === "ImportDeclaration" ? x.source.value : undefined + })); +} diff --git a/src/utils/get-sorted-nodes-by-import-order.ts b/src/utils/get-sorted-nodes-by-import-order.ts index d8ddd623..5450df7e 100644 --- a/src/utils/get-sorted-nodes-by-import-order.ts +++ b/src/utils/get-sorted-nodes-by-import-order.ts @@ -1,6 +1,6 @@ import { clone } from 'lodash'; -import { THIRD_PARTY_MODULES_SPECIAL_WORD, newLineNode } from '../constants'; +import { THIRD_PARTY_MODULES_SPECIAL_WORD, newLineNode, SEPARATOR_SPECIAL_WORD } from '../constants'; import { naturalSort } from '../natural-sort'; import { GetSortedNodes, ImportGroups, ImportOrLine } from '../types'; import { getImportNodesMatchedGroup } from './get-import-nodes-matched-group'; @@ -51,9 +51,14 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => { importOrderGroups[matchedGroup].push(node); } + const hasUserProvidedSeparators = options.importOrder.includes(SEPARATOR_SPECIAL_WORD); + let safeToAddNewLine = false; for (const group of importOrder) { const groupNodes = importOrderGroups[group]; + if (importOrderSeparation && group === SEPARATOR_SPECIAL_WORD && safeToAddNewLine) { + finalNodes.push(newLineNode); + } if (groupNodes.length === 0) continue; const sortedInsideGroup = getSortedNodesGroup(groupNodes, { @@ -68,8 +73,9 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => { } finalNodes.push(...sortedInsideGroup); + safeToAddNewLine = true; - if (importOrderSeparation) { + if (importOrderSeparation && !hasUserProvidedSeparators) { finalNodes.push(newLineNode); } } diff --git a/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap b/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap index bb5d393f..b83765d9 100644 --- a/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap +++ b/tests/ImportsSeparatedByUser/__snapshots__/ppsi.spec.js.snap @@ -26,13 +26,10 @@ 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"; @@ -74,7 +71,6 @@ import { foo } from "@server/foo"; import { Link } from "@ui/Link"; import type { LinkProps } from "@ui/Link/LinkProps"; - import { baz } from "./baz"; import type { Quux } from "./quux"; @@ -157,16 +153,13 @@ function add(a:number,b:number) { // 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"; @@ -286,13 +279,10 @@ 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"; @@ -339,13 +329,11 @@ function add(a:number,b:number) { #!/usr/bin/env node 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 twoLevelRelativePath from "../../twoLevelRelativePath"; @@ -368,11 +356,9 @@ 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"; diff --git a/tests/ImportsSeparatedByUser/ppsi.spec.js b/tests/ImportsSeparatedByUser/ppsi.spec.js index 967d2fcd..3f674010 100644 --- a/tests/ImportsSeparatedByUser/ppsi.spec.js +++ b/tests/ImportsSeparatedByUser/ppsi.spec.js @@ -1,5 +1,5 @@ run_spec(__dirname, ["typescript"], { - importOrder: ['^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '^[./]'], + importOrder: ['^@core/(.*)$', '^@server/(.*)', '', '^@ui/(.*)$', '^[./]'], importOrderSeparation: true, importOrderParserPlugins: ['typescript'] });