Skip to content

Commit d35ea02

Browse files
authored
Merge pull request #29247 from ajafff/organizeimports-crash
Fix crash in organizeImports
2 parents b2f76e9 + 6a9ad0e commit d35ea02

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/services/organizeImports.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ namespace ts.OrganizeImports {
2828
organizeImportsWorker(topLevelExportDecls, coalesceExports);
2929

3030
for (const ambientModule of sourceFile.statements.filter(isAmbientModule)) {
31-
const ambientModuleBody = getModuleBlock(ambientModule as ModuleDeclaration)!; // TODO: GH#18217
31+
if (!ambientModule.body) { continue; }
3232

33-
const ambientModuleImportDecls = ambientModuleBody.statements.filter(isImportDeclaration);
33+
const ambientModuleImportDecls = ambientModule.body.statements.filter(isImportDeclaration);
3434
organizeImportsWorker(ambientModuleImportDecls, coalesceAndOrganizeImports);
3535

36-
const ambientModuleExportDecls = ambientModuleBody.statements.filter(isExportDeclaration);
36+
const ambientModuleExportDecls = ambientModule.body.statements.filter(isExportDeclaration);
3737
organizeImportsWorker(ambientModuleExportDecls, coalesceExports);
3838
}
3939

@@ -81,11 +81,6 @@ namespace ts.OrganizeImports {
8181
}
8282
}
8383

84-
function getModuleBlock(moduleDecl: ModuleDeclaration): ModuleBlock | undefined {
85-
const body = moduleDecl.body;
86-
return body && !isIdentifier(body) ? (isModuleBlock(body) ? body : getModuleBlock(body)) : undefined;
87-
}
88-
8984
function removeUnusedImports(oldImports: ReadonlyArray<ImportDeclaration>, sourceFile: SourceFile, program: Program) {
9085
const typeChecker = program.getTypeChecker();
9186
const jsxNamespace = typeChecker.getJsxNamespace();

src/testRunner/unittests/services/organizeImports.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,16 @@ export const Other = 1;
274274
assert.isEmpty(changes);
275275
});
276276

277+
it("doesn't crash on shorthand ambient module", () => {
278+
const testFile = {
279+
path: "/a.ts",
280+
content: "declare module '*';",
281+
};
282+
const languageService = makeLanguageService(testFile);
283+
const changes = languageService.organizeImports({ type: "file", fileName: testFile.path }, testFormatSettings, emptyOptions);
284+
assert.isEmpty(changes);
285+
});
286+
277287
testOrganizeImports("Renamed_used",
278288
{
279289
path: "/test.ts",

0 commit comments

Comments
 (0)