Skip to content

Commit b4a382b

Browse files
committed
Stop explicitly storing newline in refactoring/code fix contexts
It's already in the EditorSettings and the LanguageServiceHost. Fixes microsoft#18291 Fixes microsoft#18445
1 parent 1dcc83e commit b4a382b

25 files changed

+67
-59
lines changed

src/harness/unittests/extractTestHelpers.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ namespace ts {
121121
const sourceFile = program.getSourceFile(path);
122122
const context: RefactorContext = {
123123
cancellationToken: { throwIfCancellationRequested: noop, isCancellationRequested: returnFalse },
124-
newLineCharacter,
125124
program,
126125
file: sourceFile,
127126
startPosition: selectionRange.start,
@@ -185,7 +184,6 @@ namespace ts {
185184
const sourceFile = program.getSourceFile(f.path);
186185
const context: RefactorContext = {
187186
cancellationToken: { throwIfCancellationRequested: noop, isCancellationRequested: returnFalse },
188-
newLineCharacter,
189187
program,
190188
file: sourceFile,
191189
startPosition: selectionRange.start,

src/services/codeFixProvider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ namespace ts {
77
getAllCodeActions?(context: CodeFixAllContext): CombinedCodeActions;
88
}
99

10-
export interface CodeFixContextBase extends textChanges.TextChangesContext {
10+
export interface CodeFixContextBase extends RefactorOrCodeFixContext {
1111
sourceFile: SourceFile;
1212
program: Program;
13-
host: LanguageServiceHost;
1413
cancellationToken: CancellationToken;
1514
}
1615

@@ -84,7 +83,7 @@ namespace ts {
8483

8584
export function codeFixAll(context: CodeFixAllContext, errorCodes: number[], use: (changes: textChanges.ChangeTracker, error: Diagnostic, commands: Push<CodeActionCommand>) => void): CombinedCodeActions {
8685
const commands: CodeActionCommand[] = [];
87-
const changes = textChanges.ChangeTracker.with(context, t =>
86+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t =>
8887
eachDiagnostic(context, errorCodes, diag => use(t, diag, commands)));
8988
return createCombinedCodeActions(changes, commands.length === 0 ? undefined : commands);
9089
}

src/services/codefixes/addMissingInvocationForDecorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ts.codefix {
55
registerCodeFix({
66
errorCodes,
77
getCodeActions: (context) => {
8-
const changes = textChanges.ChangeTracker.with(context, t => makeChange(t, context.sourceFile, context.span.start));
8+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => makeChange(t, context.sourceFile, context.span.start));
99
return [{ description: getLocaleSpecificMessage(Diagnostics.Call_decorator_expression), changes, fixId }];
1010
},
1111
fixIds: [fixId],

src/services/codefixes/correctQualifiedNameToIndexedAccessType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ts.codefix {
77
getCodeActions(context) {
88
const qualifiedName = getQualifiedName(context.sourceFile, context.span.start);
99
if (!qualifiedName) return undefined;
10-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, context.sourceFile, qualifiedName));
10+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChange(t, context.sourceFile, qualifiedName));
1111
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Rewrite_as_the_indexed_access_type_0), [`${qualifiedName.left.text}["${qualifiedName.right.text}"]`]);
1212
return [{ description, changes, fixId }];
1313
},

src/services/codefixes/disableJsDiagnostics.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ namespace ts.codefix {
99
registerCodeFix({
1010
errorCodes,
1111
getCodeActions(context) {
12-
const { sourceFile, program, newLineCharacter, span } = context;
12+
const { sourceFile, program, span } = context;
1313

1414
if (!isInJavaScriptFile(sourceFile) || !isCheckJsEnabledForFile(sourceFile, program.getCompilerOptions())) {
1515
return undefined;
1616
}
1717

18+
const newLineCharacter = getNewLineFromContext(context);
19+
1820
return [{
1921
description: getLocaleSpecificMessage(Diagnostics.Ignore_this_error_message),
2022
changes: [createFileTextChanges(sourceFile.fileName, [getIgnoreCommentLocationForLocation(sourceFile, span.start, newLineCharacter)])],
@@ -36,7 +38,7 @@ namespace ts.codefix {
3638
fixIds: [fixId], // No point applying as a group, doing it once will fix all errors
3739
getAllCodeActions: context => codeFixAllWithTextChanges(context, errorCodes, (changes, err) => {
3840
if (err.start !== undefined) {
39-
changes.push(getIgnoreCommentLocationForLocation(err.file!, err.start, context.newLineCharacter));
41+
changes.push(getIgnoreCommentLocationForLocation(err.file!, err.start, getNewLineFromContext(context)));
4042
}
4143
}),
4244
});

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace ts.codefix {
9696
}
9797

9898
function getActionsForAddMissingMemberInJavaScriptFile(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): CodeFixAction | undefined {
99-
const changes = textChanges.ChangeTracker.with(context, t => addMissingMemberInJs(t, classDeclarationSourceFile, classDeclaration, tokenName, makeStatic));
99+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => addMissingMemberInJs(t, classDeclarationSourceFile, classDeclaration, tokenName, makeStatic));
100100
if (changes.length === 0) return undefined;
101101
const description = formatStringFromArgs(getLocaleSpecificMessage(makeStatic ? Diagnostics.Initialize_static_property_0 : Diagnostics.Initialize_property_0_in_the_constructor), [tokenName]);
102102
return { description, changes, fixId };
@@ -142,9 +142,9 @@ namespace ts.codefix {
142142
return typeNode || createKeywordTypeNode(SyntaxKind.AnyKeyword);
143143
}
144144

145-
function createAddPropertyDeclarationAction(context: textChanges.TextChangesContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFixAction {
145+
function createAddPropertyDeclarationAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFixAction {
146146
const description = formatStringFromArgs(getLocaleSpecificMessage(makeStatic ? Diagnostics.Declare_static_property_0 : Diagnostics.Declare_property_0), [tokenName]);
147-
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, classDeclarationSourceFile, classDeclaration, tokenName, typeNode, makeStatic));
147+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => addPropertyDeclaration(t, classDeclarationSourceFile, classDeclaration, tokenName, typeNode, makeStatic));
148148
return { description, changes, fixId };
149149
}
150150

@@ -159,7 +159,7 @@ namespace ts.codefix {
159159
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, property);
160160
}
161161

162-
function createAddIndexSignatureAction(context: textChanges.TextChangesContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode): CodeFixAction {
162+
function createAddIndexSignatureAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode): CodeFixAction {
163163
// Index signatures cannot have the static modifier.
164164
const stringTypeNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
165165
const indexingParameter = createParameter(
@@ -176,14 +176,14 @@ namespace ts.codefix {
176176
[indexingParameter],
177177
typeNode);
178178

179-
const changes = textChanges.ChangeTracker.with(context, t => t.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, indexSignature));
179+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => t.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, indexSignature));
180180
// No fixId here because code-fix-all currently only works on adding individual named properties.
181181
return { description: formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Add_index_signature_for_property_0), [tokenName]), changes, fixId: undefined };
182182
}
183183

184-
function getActionForMethodDeclaration(context: textChanges.TextChangesContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier, callExpression: CallExpression, makeStatic: boolean, inJs: boolean): CodeFixAction | undefined {
184+
function getActionForMethodDeclaration(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier, callExpression: CallExpression, makeStatic: boolean, inJs: boolean): CodeFixAction | undefined {
185185
const description = formatStringFromArgs(getLocaleSpecificMessage(makeStatic ? Diagnostics.Declare_static_method_0 : Diagnostics.Declare_method_0), [token.text]);
186-
const changes = textChanges.ChangeTracker.with(context, t => addMethodDeclaration(t, classDeclarationSourceFile, classDeclaration, token, callExpression, makeStatic, inJs));
186+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => addMethodDeclaration(t, classDeclarationSourceFile, classDeclaration, token, callExpression, makeStatic, inJs));
187187
return { description, changes, fixId };
188188
}
189189

src/services/codefixes/fixAwaitInSyncFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace ts.codefix {
1111
const { sourceFile, span } = context;
1212
const nodes = getNodes(sourceFile, span.start);
1313
if (!nodes) return undefined;
14-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, nodes));
14+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChange(t, sourceFile, nodes));
1515
return [{ description: getLocaleSpecificMessage(Diagnostics.Add_async_modifier_to_containing_function), changes, fixId }];
1616
},
1717
fixIds: [fixId],

src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ts.codefix {
99
errorCodes,
1010
getCodeActions(context) {
1111
const { program, sourceFile, span } = context;
12-
const changes = textChanges.ChangeTracker.with(context, t =>
12+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t =>
1313
addMissingMembers(getClass(sourceFile, span.start), sourceFile, program.getTypeChecker(), t));
1414
return changes.length === 0 ? undefined : [{ description: getLocaleSpecificMessage(Diagnostics.Implement_inherited_abstract_class), changes, fixId }];
1515
},

src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ts.codefix {
1010
const classDeclaration = getClass(sourceFile, span.start);
1111
const checker = program.getTypeChecker();
1212
return mapDefined<ExpressionWithTypeArguments, CodeFixAction>(getClassImplementsHeritageClauseElements(classDeclaration), implementedTypeNode => {
13-
const changes = textChanges.ChangeTracker.with(context, t => addMissingDeclarations(checker, implementedTypeNode, sourceFile, classDeclaration, t));
13+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => addMissingDeclarations(checker, implementedTypeNode, sourceFile, classDeclaration, t));
1414
if (changes.length === 0) return undefined;
1515
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Implement_interface_0), [implementedTypeNode.getText()]);
1616
return { description, changes, fixId };

src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ts.codefix {
99
const nodes = getNodes(sourceFile, span.start);
1010
if (!nodes) return undefined;
1111
const { constructor, superCall } = nodes;
12-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, constructor, superCall));
12+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChange(t, sourceFile, constructor, superCall));
1313
return [{ description: getLocaleSpecificMessage(Diagnostics.Make_super_call_the_first_statement_in_the_constructor), changes, fixId }];
1414
},
1515
fixIds: [fixId],

src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ts.codefix {
77
getCodeActions(context) {
88
const { sourceFile, span } = context;
99
const ctr = getNode(sourceFile, span.start);
10-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, ctr));
10+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChange(t, sourceFile, ctr));
1111
return [{ description: getLocaleSpecificMessage(Diagnostics.Add_missing_super_call), changes, fixId }];
1212
},
1313
fixIds: [fixId],

src/services/codefixes/fixExtendsInterfaceBecomesImplements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ts.codefix {
99
const nodes = getNodes(sourceFile, context.span.start);
1010
if (!nodes) return undefined;
1111
const { extendsToken, heritageClauses } = nodes;
12-
const changes = textChanges.ChangeTracker.with(context, t => doChanges(t, sourceFile, extendsToken, heritageClauses));
12+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChanges(t, sourceFile, extendsToken, heritageClauses));
1313
return [{ description: getLocaleSpecificMessage(Diagnostics.Change_extends_to_implements), changes, fixId }];
1414
},
1515
fixIds: [fixId],

src/services/codefixes/fixForgottenThisPropertyAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ts.codefix {
77
getCodeActions(context) {
88
const { sourceFile } = context;
99
const token = getNode(sourceFile, context.span.start);
10-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, token));
10+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChange(t, sourceFile, token));
1111
return [{ description: getLocaleSpecificMessage(Diagnostics.Add_this_to_unresolved_variable), changes, fixId }];
1212
},
1313
fixIds: [fixId],

src/services/codefixes/fixInvalidImportSyntax.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace ts.codefix {
3232
createImportClause(namespace.name, /*namedBindings*/ undefined),
3333
node.moduleSpecifier
3434
);
35-
const changeTracker = textChanges.ChangeTracker.fromContext(context);
35+
const changeTracker = textChanges.ChangeTracker.fromContext(toTextChangesContext(context));
3636
changeTracker.replaceNode(sourceFile, node, replacement, { useNonAdjustedEndPosition: true });
3737
const changes = changeTracker.getChanges();
3838
variations.push({
@@ -48,7 +48,7 @@ namespace ts.codefix {
4848
namespace.name,
4949
createExternalModuleReference(node.moduleSpecifier)
5050
);
51-
const changeTracker = textChanges.ChangeTracker.fromContext(context);
51+
const changeTracker = textChanges.ChangeTracker.fromContext(toTextChangesContext(context));
5252
changeTracker.replaceNode(sourceFile, node, replacement, { useNonAdjustedEndPosition: true });
5353
const changes = changeTracker.getChanges();
5454
variations.push({
@@ -86,7 +86,7 @@ namespace ts.codefix {
8686
addRange(fixes, getCodeFixesForImportDeclaration(context, relatedImport));
8787
}
8888
const propertyAccess = createPropertyAccess(expr, "default");
89-
const changeTracker = textChanges.ChangeTracker.fromContext(context);
89+
const changeTracker = textChanges.ChangeTracker.fromContext(toTextChangesContext(context));
9090
changeTracker.replaceNode(sourceFile, expr, propertyAccess, {});
9191
const changes = changeTracker.getChanges();
9292
fixes.push({

src/services/codefixes/fixSpelling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.codefix {
1212
const info = getInfo(sourceFile, context.span.start, context.program.getTypeChecker());
1313
if (!info) return undefined;
1414
const { node, suggestion } = info;
15-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, node, suggestion));
15+
const changes = textChanges.ChangeTracker.with(toTextChangesContext(context), t => doChange(t, sourceFile, node, suggestion));
1616
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Change_spelling_to_0), [suggestion]);
1717
return [{ description, changes, fixId }];
1818
},

src/services/codefixes/fixUnusedIdentifier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ namespace ts.codefix {
1313
const token = getToken(sourceFile, context.span.start);
1414
const result: CodeFixAction[] = [];
1515

16-
const deletion = textChanges.ChangeTracker.with(context, t => tryDeleteDeclaration(t, sourceFile, token));
16+
const deletion = textChanges.ChangeTracker.with(toTextChangesContext(context), t => tryDeleteDeclaration(t, sourceFile, token));
1717
if (deletion.length) {
1818
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Remove_declaration_for_Colon_0), [token.getText()]);
1919
result.push({ description, changes: deletion, fixId: fixIdDelete });
2020
}
2121

22-
const prefix = textChanges.ChangeTracker.with(context, t => tryPrefixDeclaration(t, context.errorCode, sourceFile, token));
22+
const prefix = textChanges.ChangeTracker.with(toTextChangesContext(context), t => tryPrefixDeclaration(t, context.errorCode, sourceFile, token));
2323
if (prefix.length) {
2424
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Prefix_0_with_an_underscore), [token.getText()]);
2525
result.push({ description, changes: prefix, fixId: fixIdPrefix });

0 commit comments

Comments
 (0)