Skip to content
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
13 changes: 10 additions & 3 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export interface ImportAdder {
hasFixes(): boolean;
addImportFromDiagnostic: (diagnostic: DiagnosticWithLocation, context: CodeFixContextBase) => void;
addImportFromExportedSymbol: (exportedSymbol: Symbol, isValidTypeOnlyUseSite?: boolean) => void;
writeFixes: (changeTracker: textChanges.ChangeTracker) => void;
writeFixes: (changeTracker: textChanges.ChangeTracker, oldFileQuotePreference?: QuotePreference) => void;
}

/** @internal */
Expand Down Expand Up @@ -345,8 +345,15 @@ function createImportAdderWorker(sourceFile: SourceFile, program: Program, useAu
}
}

function writeFixes(changeTracker: textChanges.ChangeTracker) {
const quotePreference = getQuotePreference(sourceFile, preferences);
function writeFixes(changeTracker: textChanges.ChangeTracker, oldFileQuotePreference?: QuotePreference) {
let quotePreference: QuotePreference;
if (sourceFile.imports.length === 0 && oldFileQuotePreference !== undefined) {
// If the target file has no imports, we must use the same quote preference as the file we are importing from.
quotePreference = oldFileQuotePreference;
}
else {
quotePreference = getQuotePreference(sourceFile, preferences);
}
for (const fix of addToNamespace) {
addNamespaceQualifier(changeTracker, sourceFile, fix);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ function getNewStatementsAndRemoveFromOldFile(
}
}
if (importAdder) {
importAdder.writeFixes(changes);
importAdder.writeFixes(changes, quotePreference);
}
if (imports.length && body.length) {
return [
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/moveToFile_blankExistingFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ verify.moveToFile({
import { p } from './a';


import { b } from "./other";
import { b } from './other';


const y: Date = p + b;
Expand Down
36 changes: 36 additions & 0 deletions tests/cases/fourslash/moveToFile_consistentQuoteStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

/// <reference path='fourslash.ts' />

// @Filename: /bar.ts
////export const tt = 2;

// @Filename: /a.ts
////import { b } from './other';
////const a = 1;
////[|const c = a + b;|]

// @Filename: /other.ts
////export const b = 2;


verify.moveToFile({
newFileContents: {
"/a.ts":
`export const a = 1;
`,

"/bar.ts":
`import { a } from './a';

import { b } from './other';

export const tt = 2;
const c = a + b;
`,
},
interactiveRefactorArguments: { targetFile: "/bar.ts" },

preferences: {
quotePreference: "single",
}
});
2 changes: 1 addition & 1 deletion tests/cases/fourslash/moveToFile_differentDirectories2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ y;`,
"/src/dir2/bar.ts":
`import { a } from '../dir1/a';

import { b } from "../dir1/other";
import { b } from '../dir1/other';


export const y = b + a;
Expand Down