Skip to content

Commit

Permalink
fix: retrive sourceFile when text change range is expected to conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
kazushisan committed Sep 10, 2024
1 parent 5cd4386 commit e8861b1
Showing 1 changed file with 41 additions and 12 deletions.
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);
}
};

0 comments on commit e8861b1

Please sign in to comment.