Fix(54376) (#54975)

This commit is contained in:
navya9singh 2023-07-12 10:00:32 -07:00 committed by GitHub
parent 48c356b452
commit a602c668fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 6 deletions

View File

@ -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 */
@ -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);
}

View File

@ -254,7 +254,7 @@ function getNewStatementsAndRemoveFromOldFile(
}
}
if (importAdder) {
importAdder.writeFixes(changes);
importAdder.writeFixes(changes, quotePreference);
}
if (imports.length && body.length) {
return [

View File

@ -22,7 +22,7 @@ verify.moveToFile({
import { p } from './a';
import { b } from "./other";
import { b } from './other';
const y: Date = p + b;

View File

@ -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",
}
});

View File

@ -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;