Skip to content

Commit 27d8a02

Browse files
committed
Add export {} to maintain module shape
1 parent 544c3e6 commit 27d8a02

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

packages/knip/src/IssueFixer.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { load, save } from './util/package-json.ts';
99
import { extname, join } from './util/path.ts';
1010
import { removeExport } from './util/remove-export.ts';
1111

12+
const MODULE_MARKER = /^[ \t]*(import|export)\b/m;
13+
1214
export const fix = async (issues: Issues, counters: Counters, options: MainOptions) => {
1315
const fixer = new IssueFixer(options);
1416
const touchedFiles = await fixer.fixIssues(issues);
@@ -79,12 +81,14 @@ class IssueFixer {
7981
for (const [filePath, fixes] of allFixes) {
8082
if (fixes.length === 0) continue;
8183
const absFilePath = join(this.options.cwd, filePath);
82-
const sourceFileText = fixes
84+
const originalSource = await readFile(absFilePath, 'utf-8');
85+
let sourceFileText = fixes
8386
.sort((a, b) => b[0] - a[0])
84-
.reduce(
85-
(text, [start, end, flags]) => removeExport({ text, start, end, flags }),
86-
await readFile(absFilePath, 'utf-8')
87-
);
87+
.reduce((text, [start, end, flags]) => removeExport({ text, start, end, flags }), originalSource);
88+
89+
if (MODULE_MARKER.test(originalSource) && !MODULE_MARKER.test(sourceFileText)) {
90+
sourceFileText = `${sourceFileText.trimEnd()}\n\nexport {};\n`;
91+
}
8892

8993
await writeFile(absFilePath, sourceFileText);
9094

packages/knip/test/e2e/fix-tsgo.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const fixtures = [
2727
'fix-members',
2828
'ns-spread-reexport',
2929
'package-entry-bare',
30+
're-exports-aliased-ns',
3031
're-exports-destructure-spread',
3132
're-exports-recursive',
3233
're-exports-spread',

0 commit comments

Comments
 (0)