Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: correctly auto fix when there are multiple export specifiers #13

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/util/removeUnusedExport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,11 @@ export { B };`,

it('should remove export specifier for an identifier if its not used in some other file', () => {
const { languageService, fileService } = setup();
fileService.set('/app/main.ts', `import { remain } from './c';`);
fileService.set(
'/app/main.ts',
`import { remain } from './c';
import { d } from './d';`,
);

fileService.set(
'/app/a.ts',
Expand All @@ -568,10 +572,18 @@ const remain = 'remain';
export { c, remain };`,
);

fileService.set(
'/app/d.ts',
`const d = 'd';
const unused = 'unused';
const unused2 = 'unused2';
export { d, unused, unused2 };`,
);

removeUnusedExport({
languageService,
fileService,
targetFile: ['/app/a.ts', '/app/b.ts', '/app/c.ts'],
targetFile: ['/app/a.ts', '/app/b.ts', '/app/c.ts', '/app/d.ts'],
});

assert.equal(fileService.get('/app/a.ts').trim(), `const a = 'a';`);
Expand All @@ -582,6 +594,14 @@ export { c, remain };`,
const remain = 'remain';
export { remain };`,
);

assert.equal(
fileService.get('/app/d.ts').trim(),
`const d = 'd';
const unused = 'unused';
const unused2 = 'unused2';
export { d };`,
);
});

it('should not remove export specifier for an identifier if it has a comment to ignore', () => {
Expand Down
53 changes: 41 additions & 12 deletions lib/util/removeUnusedExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,25 @@ const getUpdatedExportDeclaration = (

const getTextChanges = (
languageService: ts.LanguageService,
sourceFile: ts.SourceFile,
file: string,
editTracker: EditTracker,
) => {
const sourceFile = languageService.getProgram()?.getSourceFile(file);

if (!sourceFile) {
throw new Error('source file not found');
}

const changes: ts.TextChange[] = [];
// usually we want to remove all unused exports in one pass, but there are some cases where we need to do multiple passes
// for example, when we have multiple export specifiers in one export declaration, we want to remove them one by one because the text change range will conflict
let aborted = false;

for (const node of getUnusedExports(languageService, sourceFile)) {
if (aborted === true) {
break;
}

if (ts.isExportSpecifier(node)) {
const specifierCount = Array.from(node.parent.elements).length;

Expand All @@ -289,6 +303,7 @@ const getTextChanges = (
continue;
}

aborted = true;
changes.push({
newText: getUpdatedExportDeclaration(node.parent.parent, node),
span: {
Expand Down Expand Up @@ -381,7 +396,7 @@ const getTextChanges = (
});
}

return changes;
return { changes, done: !aborted };
};

const disabledEditTracker: EditTracker = {
Expand Down Expand Up @@ -431,40 +446,54 @@ export const removeUnusedExport = ({
}
}

const changes = getTextChanges(languageService, sourceFile, editTracker);
let content = fileService.get(file);

editTracker.end(file);
do {
const { changes, done } = getTextChanges(
languageService,
file,
editTracker,
);

content = applyTextChanges(content, changes);

const oldContent = fileService.get(file);
let newContent = applyTextChanges(oldContent, changes);
fileService.set(file, content);

if (done) {
break;
}
// eslint-disable-next-line no-constant-condition
} while (true);

editTracker.end(file);

if (enableCodeFix) {
// eslint-disable-next-line no-constant-condition
while (true) {
fileService.set(file, newContent);
fileService.set(file, content);

const result = applyCodeFix({
fixId: fixIdDelete,
fileName: file,
languageService,
});

if (result === newContent) {
if (result === content) {
break;
}

newContent = result;
content = result;
}

fileService.set(file, newContent);
fileService.set(file, content);

newContent = applyCodeFix({
content = applyCodeFix({
fixId: fixIdDeleteImports,
fileName: file,
languageService,
});
}

fileService.set(file, newContent);
fileService.set(file, content);
}
};
Loading