Skip to content

Commit

Permalink
added new rules for side effect only imports
Browse files Browse the repository at this point in the history
  • Loading branch information
cptpiepmatz committed Jun 24, 2022
1 parent c0e6a41 commit 79965a3
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/configs/primp.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"sortImports": [
"!sideEffect",
"sourceType",
"dotJSFirst",
"!namespacePresence",
Expand All @@ -12,6 +13,7 @@
"elementName"
],
"separateBy": [
"unequalSideEffectUse",
"unequalPackageState",
"unequalNamespaceUse"
],
Expand Down
2 changes: 2 additions & 0 deletions examples/configs/primp.json5
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// doesn't return 0, therefore the order is important.
// To always have the same expected result, at least the last function
// should possible never return 0.
"!sideEffect",
"sourceType",
"dotJSFirst",
"!namespacePresence",
Expand All @@ -33,6 +34,7 @@
//
// Primp uses these category functions to determine where to put empty lines
// as separation.
"unequalSideEffectUse",
"unequalPackageState",
"unequalNamespaceUse"
],
Expand Down
2 changes: 2 additions & 0 deletions examples/configs/primp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sortImports:
# doesn't return 0, therefore the order is important.
# To always have the same expected result, at least the last function
# should possible never return 0.
- !sideEffect
- sourceType
- dotJSFirst
- !namespacePresence
Expand All @@ -30,6 +31,7 @@ separateBy:
#
# Primp uses these category functions to determine where to put empty lines
# as separation.
- unequalSideEffectUse
- unequalPackageState
- unequalNamespaceUse
formatting:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cptpiepmatz/pretty-ts-imports",
"version": "1.0.0",
"version": "1.1.0",
"description": "A Tool to sort Typescript Imports with a Set of complex Rules.",
"keywords": [
"rules",
Expand Down
7 changes: 7 additions & 0 deletions spec/import_management/ImportSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ import SomeSuperLongDefaultImportThatMayTroublesBecauseItsThisLongOrReallyLong
{name: "SideEffect", isPackage: true, isRelative: false}
);
expect(sideEffectImport.elements).toHaveSize(0);
expect(sideEffectImport.isSideEffectOnly).toBeTrue();
expect(sideEffectImport.toString()).toMatch(sideEffectContent);
});

Expand All @@ -269,4 +270,10 @@ import SomeSuperLongDefaultImportThatMayTroublesBecauseItsThisLongOrReallyLong
expect(typeImport.toString()).toMatch(typeContent);
expect(typeImport.isTypeOnly).toBeTrue();
});

it("should mark explicit imports as not side effect only", function() {
for (let imported of imports) {
expect(imported.isSideEffectOnly).toBeFalse();
}
})
});
23 changes: 23 additions & 0 deletions spec/sort_rules/compare_imports/sideEffectSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path="../../types/matchers.d.ts" />
import sideEffect
from "../../../src/sort_rules/compare_imports/sideEffect";

describe("sort_rules/compare_imports/sideEffect", function() {
const imports = [
`import a from "alpha";`,
`import "beta";`,
`import c from "charlie";`,
`import "delta";`
];

const expectedOutput = [
`import "beta";`,
`import "delta";`,
`import a from "alpha";`,
`import c from "charlie";`
];

it("should get sorted correctly", function() {
expect(imports).toBeImportSorted(expectedOutput, sideEffect);
});
});
15 changes: 15 additions & 0 deletions spec/sort_rules/separate_by/unequalSideEffectUseSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Import from "../../../src/import_management/Import";
import separator from "../../../src/sort_rules/separate_by/unequalSideEffectUse";
import expectSeparator from "../../helpers/expectSeparator";

describe("sort_rules/separate_by/unequalsSideEffectUse", function() {
const sideEffect = {isSideEffectOnly: true} as Import;
const noSideEffect = {isSideEffectOnly: false} as Import;

it("should set the separator correctly", function() {
expectSeparator(separator).between(sideEffect).and(noSideEffect);
expectSeparator(separator).between(noSideEffect).and(sideEffect);
expectSeparator(separator).not.between(sideEffect).and(sideEffect);
expectSeparator(separator).not.between(noSideEffect).and(noSideEffect);
});
});
2 changes: 2 additions & 0 deletions src/config/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type DefaultConfig = FullConfig & {
const defaultConfig: DefaultConfig = {

sortImports: [
"!sideEffect",
"sourceType",
"!namespacePresence",
"pathName",
Expand All @@ -29,6 +30,7 @@ const defaultConfig: DefaultConfig = {
],

separateBy: [
"unequalSideEffectUse",
"unequalPackageState",
"unequalNamespaceUse"
],
Expand Down
5 changes: 5 additions & 0 deletions src/import_management/Import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export default class Import {
/** If the named imports were used. i.e.: "import {a, b, c}". */
readonly isNamed: boolean = false;

/** If the module imported only side effects. */
get isSideEffectOnly(): boolean {
return !this.elements.length && !this.defaultElement;
}

/**
* Constructor for the import.
* Directly reads out the declaration to make its uses easier.
Expand Down
1 change: 1 addition & 0 deletions src/sort_rules/compare_imports/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export {default as defaultPresence} from "./defaultPresence";
export {default as defaultType} from "./defaultType";
export {default as namespacePresence} from "./namespacePresence";
export {default as sourceType} from "./sourceType";
export {default as sideEffect} from "./sideEffect";
29 changes: 29 additions & 0 deletions src/sort_rules/compare_imports/sideEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ImportCompareFunction from "../ImportCompareFunction";

/**
* Compares two imports whether they are for side effects only or not.
* Imports with only side effects are considered lesser (higher position).
*
* <b>Example:</b>
* ```ts
* // unsorted
* import a from "alpha";
* import "beta";
* import c from "charlie";
*
* // sorted
* import "beta";
* import a from "alpha";
* import c from "charlie";
* ```
*
* @param a Import A
* @param b Import B
*/
const sideEffect: ImportCompareFunction = function(a, b) {
const aDefault = a.isSideEffectOnly ? 0 : 1;
const bDefault = b.isSideEffectOnly ? 0 : 1;
return aDefault - bDefault;
}

export default sideEffect;
1 change: 1 addition & 0 deletions src/sort_rules/separate_by/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@

export {default as unequalPackageState} from "./unequalPackageState";
export {default as unequalNamespaceUse} from "./unequalNamespaceUse";
export {default as unequalSideEffectUse} from "./unequalSideEffectUse";
33 changes: 33 additions & 0 deletions src/sort_rules/separate_by/unequalSideEffectUse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import SeparateByFunction from "../SeparateByFunction";

/**
* Place a separator between two imports if one of them is importing only for
* side effects.
*
* <b>Example:</b>
* ```ts
* // unseparated
* import a from "alpha";
* import b from "bravo";
* import "charlie";
* import "delta";
* import e from "echo";
*
* // separated
* import a from "alpha";
* import b from "bravo";
*
* import "charlie";
* import "delta";
*
* import e from "echo";
* ```
* @see Import#isSideEffectOnly
* @param l Leading Import
* @param f Following Import
*/
const unequalSideEffectUse: SeparateByFunction = function(l, f) {
return l.isSideEffectOnly !== f.isSideEffectOnly;
}

export default unequalSideEffectUse;

0 comments on commit 79965a3

Please sign in to comment.