diff --git a/src/constants.ts b/src/constants.ts index 28c009e..cf22b00 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -17,6 +17,7 @@ export const chunkSideOtherNode = 'other-node'; * where the not matched imports should be placed */ export const THIRD_PARTY_MODULES_SPECIAL_WORD = ''; +export const SEPARATOR_SPECIAL_WORD = ''; export const THIRD_PARTY_TYPES_SPECIAL_WORD = ''; export const TYPES_SPECIAL_WORD = ''; diff --git a/src/utils/__tests__/get-code-from-ast.spec.ts b/src/utils/__tests__/get-code-from-ast.spec.ts index 298e964..dfe638d 100644 --- a/src/utils/__tests__/get-code-from-ast.spec.ts +++ b/src/utils/__tests__/get-code-from-ast.spec.ts @@ -52,7 +52,7 @@ import a from 'a';`; sourceType: 'module', plugins: getExperimentalParserPlugins([]), }; - const ast = babelParser(code, parserOptions); + const ast: any = babelParser(code, parserOptions); if (!ast) throw new Error('ast is null'); const { directives, importNodes } = extractASTNodes(ast); diff --git a/src/utils/get-sorted-nodes-by-import-order.ts b/src/utils/get-sorted-nodes-by-import-order.ts index d8ddd62..b2e78fd 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'; @@ -40,7 +40,7 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => { ); const importOrderWithOutThirdPartyPlaceholder = importOrder.filter( - (group) => group !== THIRD_PARTY_MODULES_SPECIAL_WORD, + (group) => group !== THIRD_PARTY_MODULES_SPECIAL_WORD && group !== SEPARATOR_SPECIAL_WORD, ); for (const node of originalNodes) { @@ -52,6 +52,16 @@ export const getSortedNodesByImportOrder: GetSortedNodes = (nodes, options) => { } for (const group of importOrder) { + if (group === SEPARATOR_SPECIAL_WORD) { + if ( + finalNodes.length !== 0 && + finalNodes[finalNodes.length - 1] !== newLineNode + ) { + finalNodes.push(newLineNode); + } + continue; + } + const groupNodes = importOrderGroups[group]; if (groupNodes.length === 0) continue; diff --git a/tests/ImportsManualSeparated/__snapshots__/ppsi.spec.js.snap b/tests/ImportsManualSeparated/__snapshots__/ppsi.spec.js.snap new file mode 100644 index 0000000..a6dc0e8 --- /dev/null +++ b/tests/ImportsManualSeparated/__snapshots__/ppsi.spec.js.snap @@ -0,0 +1,366 @@ +// 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 otherthing from "@core/otherthing"; +import something from "@server/something"; +import component from "@ui/hello"; + +import a from "a"; +import c from "c"; +import thirdParty from "third-party"; +import x from "x"; + +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 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 thirdParty from "third-party"; +import z from "z"; + +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 otherthing from "@core/otherthing"; +import something from "@server/something"; +import component from "@ui/hello"; + +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 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/ImportsManualSeparated/import-export-in-between.ts b/tests/ImportsManualSeparated/import-export-in-between.ts new file mode 100644 index 0000000..16bc1d7 --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/import-export-only.ts b/tests/ImportsManualSeparated/import-export-only.ts new file mode 100644 index 0000000..44f7eff --- /dev/null +++ b/tests/ImportsManualSeparated/import-export-only.ts @@ -0,0 +1,2 @@ +import React from 'react'; +export const a = 1; diff --git a/tests/ImportsManualSeparated/imports-with-comments-and-third-party.ts b/tests/ImportsManualSeparated/imports-with-comments-and-third-party.ts new file mode 100644 index 0000000..a9fb311 --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/imports-with-comments-on-top.ts b/tests/ImportsManualSeparated/imports-with-comments-on-top.ts new file mode 100644 index 0000000..6b08ed0 --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/imports-with-comments.ts b/tests/ImportsManualSeparated/imports-with-comments.ts new file mode 100644 index 0000000..67b8e46 --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/imports-with-file-level-comments.ts b/tests/ImportsManualSeparated/imports-with-file-level-comments.ts new file mode 100644 index 0000000..8ffcfb5 --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/imports-with-interpreter-directive.ts b/tests/ImportsManualSeparated/imports-with-interpreter-directive.ts new file mode 100644 index 0000000..8e7de5f --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/imports-without-third-party.ts b/tests/ImportsManualSeparated/imports-without-third-party.ts new file mode 100644 index 0000000..ef1797a --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/no-import-export.ts b/tests/ImportsManualSeparated/no-import-export.ts new file mode 100644 index 0000000..e8f71db --- /dev/null +++ b/tests/ImportsManualSeparated/no-import-export.ts @@ -0,0 +1,3 @@ +function add(a:number,b:number) { + return a + b; +} diff --git a/tests/ImportsManualSeparated/one-import.ts b/tests/ImportsManualSeparated/one-import.ts new file mode 100644 index 0000000..35d75ba --- /dev/null +++ b/tests/ImportsManualSeparated/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/ImportsManualSeparated/ppsi.spec.js b/tests/ImportsManualSeparated/ppsi.spec.js new file mode 100644 index 0000000..43e59bc --- /dev/null +++ b/tests/ImportsManualSeparated/ppsi.spec.js @@ -0,0 +1,12 @@ +run_spec(__dirname, ['typescript'], { + importOrder: [ + '^@core/(.*)$', + '^@server/(.*)$', + '^@ui/(.*)$', + '', + '', + '', + '^[./]', + ], + importOrderParserPlugins: ['typescript'], +}); diff --git a/tests/ImportsManualSeparated/sort-imports-ignored.ts b/tests/ImportsManualSeparated/sort-imports-ignored.ts new file mode 100644 index 0000000..c53ddcf --- /dev/null +++ b/tests/ImportsManualSeparated/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; +} diff --git a/tests/Separator/__snapshots__/ppsi.spec.js.snap b/tests/Separator/__snapshots__/ppsi.spec.js.snap new file mode 100644 index 0000000..fd1a798 --- /dev/null +++ b/tests/Separator/__snapshots__/ppsi.spec.js.snap @@ -0,0 +1,188 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`imports-with-comments.ts - typescript-verify: imports-with-comments.ts 1`] = ` +import z from 'z'; +import { isEmpty } from "lodash-es"; +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 { Component } from "@angular/core"; + +@Component({ + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.css"] +}) +export class AppComponent extends BaseComponent { + title = "ng-prettier"; + + override get text(): string { + return isEmpty(this.title) ? "" : this.title; + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import { Component } from "@angular/core"; +import { isEmpty } from "lodash-es"; +import thirdParty from "third-party"; +import z from "z"; +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; +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"; + +@Component({ + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.css"], +}) +export class AppComponent extends BaseComponent { + title = "ng-prettier"; + + override get text(): string { + return isEmpty(this.title) ? "" : this.title; + } +} + +`; + +exports[`imports-with-satisfies.ts - typescript-verify: imports-with-satisfies.ts 1`] = ` +import z from 'z'; +import { isEmpty } from "lodash-es"; +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 { Component } from "@angular/core"; + +const somestring = "something" satisfies string + +const someobj = { hello : "world", world:"hello"} satisfies Record~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import { Component } from "@angular/core"; +import { isEmpty } from "lodash-es"; +import thirdParty from "third-party"; +import z from "z"; +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; +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"; + +const somestring = "something" satisfies string; + +const someobj = { hello: "world", world: "hello" } satisfies Record< + string, + string +>; + +`; + +exports[`imports-with-unsorted-modules.ts - typescript-verify: imports-with-unsorted-modules.ts 1`] = ` +import z from 'z'; +import { isEmpty, concat, flatten } from "lodash-es"; +import threeLevelRelativePathA, { nonDefaultModuleC, nonDefaultModuleA, nonDefaultModuleB } from "../../../threeLevelRelativePathA"; +import * as allThreeLevelRelativePathBModules from "../../../threeLevelRelativePathB"; +import threeLevelRelativePathC, { + nonDefaultModuleC as nonConflictingModuleNameC, + nonDefaultModuleA as nonConflictingModuleNameA, + nonDefaultModuleB as nonConflictingModuleNameB, + nonDefaultModuleE, + nonDefaultModuleD +} from "../../../threeLevelRelativePathC"; +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 { Component } from "@angular/core"; + +@Component({ + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.css"] +}) +export class AppComponent { + title = "ng-prettier"; + + get text(): string { + return isEmpty(this.title) ? "" : this.title; + } +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +import { Component } from "@angular/core"; +import { isEmpty, concat, flatten } from "lodash-es"; +import thirdParty from "third-party"; +import z from "z"; +import abc from "@core/abc"; +import otherthing from "@core/otherthing"; +import something from "@server/something"; +import component from "@ui/hello"; +import xyz from "@ui/xyz"; + +import fourLevelRelativePath from "../../../../fourLevelRelativePath"; +import threeLevelRelativePathA, { + nonDefaultModuleC, + nonDefaultModuleA, + nonDefaultModuleB, +} from "../../../threeLevelRelativePathA"; +import * as allThreeLevelRelativePathBModules from "../../../threeLevelRelativePathB"; +import threeLevelRelativePathC, { + nonDefaultModuleC as nonConflictingModuleNameC, + nonDefaultModuleA as nonConflictingModuleNameA, + nonDefaultModuleB as nonConflictingModuleNameB, + nonDefaultModuleE, + nonDefaultModuleD, +} from "../../../threeLevelRelativePathC"; +import twoLevelRelativePath from "../../twoLevelRelativePath"; +import oneLevelRelativePath from "../oneLevelRelativePath"; +import sameLevelRelativePath from "./sameLevelRelativePath"; + +@Component({ + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.css"], +}) +export class AppComponent { + title = "ng-prettier"; + + get text(): string { + return isEmpty(this.title) ? "" : this.title; + } +} + +`; diff --git a/tests/Separator/imports-with-comments.ts b/tests/Separator/imports-with-comments.ts new file mode 100644 index 0000000..21c5153 --- /dev/null +++ b/tests/Separator/imports-with-comments.ts @@ -0,0 +1,28 @@ +import z from 'z'; +import { isEmpty } from "lodash-es"; +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 { Component } from "@angular/core"; + +@Component({ + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.css"] +}) +export class AppComponent extends BaseComponent { + title = "ng-prettier"; + + override get text(): string { + return isEmpty(this.title) ? "" : this.title; + } +} diff --git a/tests/Separator/imports-with-satisfies.ts b/tests/Separator/imports-with-satisfies.ts new file mode 100644 index 0000000..c14af80 --- /dev/null +++ b/tests/Separator/imports-with-satisfies.ts @@ -0,0 +1,19 @@ +import z from 'z'; +import { isEmpty } from "lodash-es"; +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 { Component } from "@angular/core"; + +const somestring = "something" satisfies string + +const someobj = { hello : "world", world:"hello"} satisfies Record \ No newline at end of file diff --git a/tests/Separator/imports-with-unsorted-modules.ts b/tests/Separator/imports-with-unsorted-modules.ts new file mode 100644 index 0000000..488b1ff --- /dev/null +++ b/tests/Separator/imports-with-unsorted-modules.ts @@ -0,0 +1,36 @@ +import z from 'z'; +import { isEmpty, concat, flatten } from "lodash-es"; +import threeLevelRelativePathA, { nonDefaultModuleC, nonDefaultModuleA, nonDefaultModuleB } from "../../../threeLevelRelativePathA"; +import * as allThreeLevelRelativePathBModules from "../../../threeLevelRelativePathB"; +import threeLevelRelativePathC, { + nonDefaultModuleC as nonConflictingModuleNameC, + nonDefaultModuleA as nonConflictingModuleNameA, + nonDefaultModuleB as nonConflictingModuleNameB, + nonDefaultModuleE, + nonDefaultModuleD +} from "../../../threeLevelRelativePathC"; +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 { Component } from "@angular/core"; + +@Component({ + selector: "app-root", + templateUrl: "./app.component.html", + styleUrls: ["./app.component.css"] +}) +export class AppComponent { + title = "ng-prettier"; + + get text(): string { + return isEmpty(this.title) ? "" : this.title; + } +} diff --git a/tests/Separator/ppsi.spec.js b/tests/Separator/ppsi.spec.js new file mode 100644 index 0000000..1017d96 --- /dev/null +++ b/tests/Separator/ppsi.spec.js @@ -0,0 +1,5 @@ +run_spec(__dirname, ["typescript"], { + importOrder: ['^@core/(.*)$', '^@server/(.*)', '^@ui/(.*)$', '', '^[./]'], + importOrderSeparation: false, + importOrderParserPlugins : ['typescript', 'decorators-legacy', 'classProperties'] +});