Skip to content

Commit

Permalink
🤖 Cherry-pick PR #36751 into release-3.8 (#36757)
Browse files Browse the repository at this point in the history
Component commits:
ebbba5b fix(36416): empty import path causes server crash

Co-authored-by: Alexander T. <[email protected]>
  • Loading branch information
TypeScript Bot and a-tarasyuk authored Feb 12, 2020
1 parent 5f623b2 commit 1986a5f
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,10 @@ namespace ts.textChanges {
}

case SyntaxKind.ImportDeclaration:
const isFirstImport = sourceFile.imports.length && node === first(sourceFile.imports).parent || node === find(sourceFile.statements, isImportDeclaration);
deleteNode(changes, sourceFile, node,
// For first import, leave header comment in place
node === sourceFile.imports[0].parent ? { leadingTriviaOption: LeadingTriviaOption.Exclude } : undefined);
isFirstImport ? { leadingTriviaOption: LeadingTriviaOption.Exclude } : undefined);
break;

case SyntaxKind.BindingElement:
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fourslash/unusedImportDeclaration_withEmptyPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////// leading trivia
////import * as foo from ""; // trailing trivia

verify.codeFix({
description: "Remove import from ''",
newFileContent:
`// leading trivia
// trailing trivia`,
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/unusedImportDeclaration_withEmptyPath1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////// leading trivia
////import * as foo from "";
////import { b } from "./b";
////import { c } from "./c";

// @Filename: /b.ts
////export const b = null;

// @Filename: /c.ts
////export const c = null;

verify.codeFix({
index: 0,
description: "Remove import from ''",
newFileContent:
`// leading trivia
import { b } from "./b";
import { c } from "./c";`,
});
22 changes: 22 additions & 0 deletions tests/cases/fourslash/unusedImportDeclaration_withEmptyPath2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////// leading trivia
////import { b } from "./b";
////import * as foo from "";
////import { c } from "./c";

// @Filename: /b.ts
////export const b = null;

// @Filename: /c.ts
////export const c = null;

verify.codeFix({
index: 1,
description: "Remove import from ''",
newFileContent:
`// leading trivia
import { b } from "./b";
import { c } from "./c";`,
});
23 changes: 23 additions & 0 deletions tests/cases/fourslash/unusedImportDeclaration_withEmptyPath3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////// leading trivia
////import { b } from "./b";
////import { c } from "./c";
////import * as foo from "";

// @Filename: /b.ts
////export const b = null;

// @Filename: /c.ts
////export const c = null;

verify.codeFix({
index: 2,
description: "Remove import from ''",
newFileContent:
`// leading trivia
import { b } from "./b";
import { c } from "./c";
`,
});
35 changes: 35 additions & 0 deletions tests/cases/fourslash/unusedImportDeclaration_withEmptyPath4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////// leading trivia
////import * as a from "";
////import * as b from "";
////import * as c from "";

verify.codeFix({
index: 0,
description: "Remove import from ''",
newFileContent:
`// leading trivia
import * as b from "";
import * as c from "";`,
});

verify.codeFix({
index: 1,
description: "Remove import from ''",
newFileContent:
`// leading trivia
import * as a from "";
import * as c from "";`,
});

verify.codeFix({
index: 2,
description: "Remove import from ''",
newFileContent:
`// leading trivia
import * as a from "";
import * as b from "";
`
});
44 changes: 44 additions & 0 deletions tests/cases/fourslash/unusedImportDeclaration_withEmptyPath5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// <reference path='fourslash.ts' />

// @Filename: /main.ts
////// leading trivia
////import { a } from "./a";
////import { b } from "./b";
////import { c } from "./c";

// @Filename: /a.ts
////export const a = null;

// @Filename: /b.ts
////export const b = null;

// @Filename: /c.ts
////export const c = null;

verify.codeFix({
index: 0,
description: "Remove import from './a'",
newFileContent:
`// leading trivia
import { b } from "./b";
import { c } from "./c";`,
});

verify.codeFix({
index: 1,
description: "Remove import from './b'",
newFileContent:
`// leading trivia
import { a } from "./a";
import { c } from "./c";`,
});

verify.codeFix({
index: 2,
description: "Remove import from './c'",
newFileContent:
`// leading trivia
import { a } from "./a";
import { b } from "./b";
`,
});

0 comments on commit 1986a5f

Please sign in to comment.