Skip to content

Commit 0acb3ec

Browse files
feat: sort import statements
1 parent c98fb60 commit 0acb3ec

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

__fixtures/config/withModules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
const fs = require('fs');
12
const os = require('os');
23
const path = require('path');
3-
const fs = require('fs');
44

55
module.exports = {};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import fs from "fs";
12
import os from "os";
23
import path from "path";
3-
import fs from "fs";
44

55
export default {};

src/configBuilder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("configBuilder", () => {
3636

3737
configBuilder = new ConfigBuilder(cb, { language: "ts" });
3838

39-
expectConfig({ foo: "bar" });
39+
expectConfig({ foo: "bar", __language: "ts" });
4040
});
4141
});
4242

src/fsUtils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,41 @@ export const writeTestplaneConfig = async (dirPath: string, testplaneConfig: Tes
8181

8282
const getObjectRepr = _.flow([toIndentedJson, withComments, withReplacedQuotes, withExpressions]);
8383

84+
/**
85+
* Order:
86+
* - External imports
87+
* - Local imports
88+
* - External type imports
89+
* - Local type imports
90+
*/
91+
const importsComparator = (a: string, b: string): number => {
92+
if (!modules[a]) {
93+
return 1;
94+
}
95+
96+
if (!modules[b]) {
97+
return -1;
98+
}
99+
100+
const isAType = a.startsWith("type ");
101+
const isBType = b.startsWith("type ");
102+
103+
if (isAType !== isBType) {
104+
return isAType ? 1 : -1;
105+
}
106+
107+
const isALocal = modules[a].startsWith(".");
108+
const isBLocal = modules[b].startsWith(".");
109+
110+
if (isALocal !== isBLocal) {
111+
return isALocal ? 1 : -1;
112+
}
113+
114+
return modules[a].localeCompare(modules[b]);
115+
};
116+
84117
const configImports = Object.keys(modules)
118+
.sort(importsComparator)
85119
.map(importName => template.getImportModule(importName, modules[importName]))
86120
.join("\n");
87121

0 commit comments

Comments
 (0)