From c60ff902f1f3f5901d59afd23263cb9c760f1bce Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 9 Nov 2018 16:02:16 -0800 Subject: [PATCH] Include code fix after prologue Fixes #15515 --- src/services/textChanges.ts | 21 ++++++++- .../importNameCodeFixWithPrologue.ts | 45 +++++++++++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/importNameCodeFixWithPrologue.ts diff --git a/src/services/textChanges.ts b/src/services/textChanges.ts index 2c6a5899fdf..9c773f73f43 100644 --- a/src/services/textChanges.ts +++ b/src/services/textChanges.ts @@ -1005,9 +1005,26 @@ namespace ts.textChanges { } } - function getInsertionPositionAtSourceFileTop({ text }: SourceFile): number { - const shebang = getShebang(text); + function getInsertionPositionAtSourceFileTop(sourceFile: SourceFile): number { + let lastPrologue: PrologueDirective | undefined; + for (const node of sourceFile.statements) { + if (isPrologueDirective(node)) { + lastPrologue = node; + } + else { + break; + } + } + let position = 0; + const text = sourceFile.text; + if (lastPrologue) { + position = lastPrologue.end; + advancePastLineBreak(); + return position; + } + + const shebang = getShebang(text); if (shebang !== undefined) { position = shebang.length; advancePastLineBreak(); diff --git a/tests/cases/fourslash/importNameCodeFixWithPrologue.ts b/tests/cases/fourslash/importNameCodeFixWithPrologue.ts new file mode 100644 index 00000000000..383f8b1555e --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFixWithPrologue.ts @@ -0,0 +1,45 @@ +/// + +// @Filename: /a.ts +////"use strict"; +////export class A { } + +// @Filename: /b.ts +////"use strict"; +////export class B extends A { } + +// @Filename: /c.ts +/////*--------------------------------------------------------------------------------------------- +//// * Copyright (c) Microsoft Corporation. All rights reserved. +//// * Licensed under the MIT License. See License.txt in the project root for license information. +//// *--------------------------------------------------------------------------------------------*/ +////"use strict"; +////export class B extends A { } + +goTo.file("/b.ts"); +verify.codeFixAll({ + fixId: "fixMissingImport", + fixAllDescription: "Add all missing imports", + newFileContent: +`"use strict"; + +import { A } from "./a"; + +export class B extends A { }`, +}); + +goTo.file("/c.ts"); +verify.codeFixAll({ + fixId: "fixMissingImport", + fixAllDescription: "Add all missing imports", + newFileContent: +`/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ +"use strict"; + +import { A } from "./a"; + +export class B extends A { }`, +}); \ No newline at end of file