Skip to content

Commit c3bdd2a

Browse files
author
Andy Hanson
committed
Make ChangeTracker#newLineCharacter public, to avoid having to pass newLineCharacter around as a parameter
1 parent 8d209a3 commit c3bdd2a

9 files changed

+58
-56
lines changed

src/services/codefixes/fixAddMissingMember.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ namespace ts.codefix {
2121
getAllCodeActions: context => {
2222
const seenNames = createMap<true>();
2323
return codeFixAll(context, errorCodes, (changes, diag) => {
24-
const { newLineCharacter, program } = context;
25-
const info = getInfo(diag.file!, diag.start!, context.program.getTypeChecker());
24+
const { program } = context;
25+
const info = getInfo(diag.file!, diag.start!, program.getTypeChecker());
2626
if (!info) return;
2727
const { classDeclaration, classDeclarationSourceFile, inJs, makeStatic, token, call } = info;
2828
if (!addToSeen(seenNames, token.text)) {
@@ -31,15 +31,15 @@ namespace ts.codefix {
3131

3232
// Always prefer to add a method declaration if possible.
3333
if (call) {
34-
addMethodDeclaration(changes, classDeclarationSourceFile, classDeclaration, token, call, newLineCharacter, makeStatic, inJs);
34+
addMethodDeclaration(changes, classDeclarationSourceFile, classDeclaration, token, call, makeStatic, inJs);
3535
}
3636
else {
3737
if (inJs) {
38-
addMissingMemberInJs(changes, classDeclarationSourceFile, classDeclaration, token.text, makeStatic, newLineCharacter);
38+
addMissingMemberInJs(changes, classDeclarationSourceFile, classDeclaration, token.text, makeStatic);
3939
}
4040
else {
4141
const typeNode = getTypeNode(program.getTypeChecker(), classDeclaration, token);
42-
addPropertyDeclaration(changes, classDeclarationSourceFile, classDeclaration, token.text, typeNode, makeStatic, newLineCharacter);
42+
addPropertyDeclaration(changes, classDeclarationSourceFile, classDeclaration, token.text, typeNode, makeStatic);
4343
}
4444
}
4545
});
@@ -96,13 +96,14 @@ 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, context.newLineCharacter));
99+
const changes = textChanges.ChangeTracker.with(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 };
103103
}
104104

105-
function addMissingMemberInJs(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean, newLineCharacter: string): void {
105+
function addMissingMemberInJs(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, makeStatic: boolean): void {
106+
const { newLineCharacter } = changeTracker;
106107
if (makeStatic) {
107108
if (classDeclaration.kind === SyntaxKind.ClassExpression) {
108109
return;
@@ -142,24 +143,24 @@ namespace ts.codefix {
142143
return typeNode || createKeywordTypeNode(SyntaxKind.AnyKeyword);
143144
}
144145

145-
function createAddPropertyDeclarationAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFixAction {
146+
function createAddPropertyDeclarationAction(context: textChanges.TextChangesContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, makeStatic: boolean, tokenName: string, typeNode: TypeNode): CodeFixAction {
146147
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, context.newLineCharacter));
148+
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, classDeclarationSourceFile, classDeclaration, tokenName, typeNode, makeStatic));
148149
return { description, changes, fixId };
149150
}
150151

151-
function addPropertyDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode, makeStatic: boolean, newLineCharacter: string): void {
152+
function addPropertyDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode, makeStatic: boolean): void {
152153
const property = createProperty(
153154
/*decorators*/ undefined,
154155
/*modifiers*/ makeStatic ? [createToken(SyntaxKind.StaticKeyword)] : undefined,
155156
tokenName,
156157
/*questionToken*/ undefined,
157158
typeNode,
158159
/*initializer*/ undefined);
159-
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, property, newLineCharacter);
160+
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, property);
160161
}
161162

162-
function createAddIndexSignatureAction(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode): CodeFixAction {
163+
function createAddIndexSignatureAction(context: textChanges.TextChangesContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, tokenName: string, typeNode: TypeNode): CodeFixAction {
163164
// Index signatures cannot have the static modifier.
164165
const stringTypeNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
165166
const indexingParameter = createParameter(
@@ -176,19 +177,19 @@ namespace ts.codefix {
176177
[indexingParameter],
177178
typeNode);
178179

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

184-
function getActionForMethodDeclaration(context: CodeFixContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier, callExpression: CallExpression, makeStatic: boolean, inJs: boolean): CodeFixAction | undefined {
185+
function getActionForMethodDeclaration(context: textChanges.TextChangesContext, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier, callExpression: CallExpression, makeStatic: boolean, inJs: boolean): CodeFixAction | undefined {
185186
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, context.newLineCharacter, makeStatic, inJs));
187+
const changes = textChanges.ChangeTracker.with(context, t => addMethodDeclaration(t, classDeclarationSourceFile, classDeclaration, token, callExpression, makeStatic, inJs));
187188
return { description, changes, fixId };
188189
}
189190

190-
function addMethodDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier, callExpression: CallExpression, newLineCharacter: string, makeStatic: boolean, inJs: boolean) {
191+
function addMethodDeclaration(changeTracker: textChanges.ChangeTracker, classDeclarationSourceFile: SourceFile, classDeclaration: ClassLikeDeclaration, token: Identifier, callExpression: CallExpression, makeStatic: boolean, inJs: boolean) {
191192
const methodDeclaration = createMethodFromCallExpression(callExpression, token.text, inJs, makeStatic);
192-
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration, newLineCharacter);
193+
changeTracker.insertNodeAtClassStart(classDeclarationSourceFile, classDeclaration, methodDeclaration);
193194
}
194195
}

src/services/codefixes/fixClassDoesntImplementInheritedAbstractMember.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace ts.codefix {
1010
getCodeActions(context) {
1111
const { program, sourceFile, span } = context;
1212
const changes = textChanges.ChangeTracker.with(context, t =>
13-
addMissingMembers(getClass(sourceFile, span.start), sourceFile, program.getTypeChecker(), context.newLineCharacter, t));
13+
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
},
1616
fixIds: [fixId],
1717
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
18-
addMissingMembers(getClass(diag.file!, diag.start!), context.sourceFile, context.program.getTypeChecker(), context.newLineCharacter, changes);
18+
addMissingMembers(getClass(diag.file!, diag.start!), context.sourceFile, context.program.getTypeChecker(), changes);
1919
}),
2020
});
2121

@@ -28,15 +28,15 @@ namespace ts.codefix {
2828
return classDeclaration as ClassLikeDeclaration;
2929
}
3030

31-
function addMissingMembers(classDeclaration: ClassLikeDeclaration, sourceFile: SourceFile, checker: TypeChecker, newLineCharacter: string, changeTracker: textChanges.ChangeTracker): void {
31+
function addMissingMembers(classDeclaration: ClassLikeDeclaration, sourceFile: SourceFile, checker: TypeChecker, changeTracker: textChanges.ChangeTracker): void {
3232
const extendsNode = getClassExtendsHeritageClauseElement(classDeclaration);
3333
const instantiatedExtendsType = checker.getTypeAtLocation(extendsNode);
3434

3535
// Note that this is ultimately derived from a map indexed by symbol names,
3636
// so duplicates cannot occur.
3737
const abstractAndNonPrivateExtendsSymbols = checker.getPropertiesOfType(instantiatedExtendsType).filter(symbolPointsToNonPrivateAndAbstractMember);
3838

39-
createMissingMemberNodes(classDeclaration, abstractAndNonPrivateExtendsSymbols, checker, member => changeTracker.insertNodeAtClassStart(sourceFile, classDeclaration, member, newLineCharacter));
39+
createMissingMemberNodes(classDeclaration, abstractAndNonPrivateExtendsSymbols, checker, member => changeTracker.insertNodeAtClassStart(sourceFile, classDeclaration, member));
4040
}
4141

4242
function symbolPointsToNonPrivateAndAbstractMember(symbol: Symbol): boolean {

src/services/codefixes/fixClassIncorrectlyImplementsInterface.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ namespace ts.codefix {
55
registerCodeFix({
66
errorCodes,
77
getCodeActions(context) {
8-
const { newLineCharacter, program, sourceFile, span } = context;
8+
const { program, sourceFile, span } = context;
99
const classDeclaration = getClass(sourceFile, span.start);
1010
const checker = program.getTypeChecker();
1111
return mapDefined<ExpressionWithTypeArguments, CodeFixAction>(getClassImplementsHeritageClauseElements(classDeclaration), implementedTypeNode => {
12-
const changes = textChanges.ChangeTracker.with(context, t => addMissingDeclarations(checker, implementedTypeNode, sourceFile, classDeclaration, newLineCharacter, t));
12+
const changes = textChanges.ChangeTracker.with(context, t => addMissingDeclarations(checker, implementedTypeNode, sourceFile, classDeclaration, t));
1313
if (changes.length === 0) return undefined;
1414
const description = formatStringFromArgs(getLocaleSpecificMessage(Diagnostics.Implement_interface_0), [implementedTypeNode.getText()]);
1515
return { description, changes, fixId };
@@ -22,7 +22,7 @@ namespace ts.codefix {
2222
const classDeclaration = getClass(diag.file!, diag.start!);
2323
if (addToSeen(seenClassDeclarations, getNodeId(classDeclaration))) {
2424
for (const implementedTypeNode of getClassImplementsHeritageClauseElements(classDeclaration)) {
25-
addMissingDeclarations(context.program.getTypeChecker(), implementedTypeNode, diag.file!, classDeclaration, context.newLineCharacter, changes);
25+
addMissingDeclarations(context.program.getTypeChecker(), implementedTypeNode, diag.file!, classDeclaration, changes);
2626
}
2727
}
2828
});
@@ -40,7 +40,6 @@ namespace ts.codefix {
4040
implementedTypeNode: ExpressionWithTypeArguments,
4141
sourceFile: SourceFile,
4242
classDeclaration: ClassLikeDeclaration,
43-
newLineCharacter: string,
4443
changeTracker: textChanges.ChangeTracker
4544
): void {
4645
// Note that this is ultimately derived from a map indexed by symbol names,
@@ -58,12 +57,12 @@ namespace ts.codefix {
5857
createMissingIndexSignatureDeclaration(implementedType, IndexKind.String);
5958
}
6059

61-
createMissingMemberNodes(classDeclaration, nonPrivateMembers, checker, member => changeTracker.insertNodeAtClassStart(sourceFile, classDeclaration, member, newLineCharacter));
60+
createMissingMemberNodes(classDeclaration, nonPrivateMembers, checker, member => changeTracker.insertNodeAtClassStart(sourceFile, classDeclaration, member));
6261

6362
function createMissingIndexSignatureDeclaration(type: InterfaceType, kind: IndexKind): void {
6463
const indexInfoOfKind = checker.getIndexInfoOfType(type, kind);
6564
if (indexInfoOfKind) {
66-
changeTracker.insertNodeAtClassStart(sourceFile, classDeclaration, checker.indexInfoToIndexSignatureDeclaration(indexInfoOfKind, kind, classDeclaration), newLineCharacter);
65+
changeTracker.insertNodeAtClassStart(sourceFile, classDeclaration, checker.indexInfoToIndexSignatureDeclaration(indexInfoOfKind, kind, classDeclaration));
6766
}
6867
}
6968
}

src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@ namespace ts.codefix {
55
registerCodeFix({
66
errorCodes,
77
getCodeActions(context) {
8-
const { sourceFile } = context;
9-
const nodes = getNodes(sourceFile, context.span.start);
8+
const { sourceFile, span } = context;
9+
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, context.newLineCharacter));
12+
const changes = textChanges.ChangeTracker.with(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],
1616
getAllCodeActions(context) {
17-
const { newLineCharacter, sourceFile } = context;
17+
const { sourceFile } = context;
1818
const seenClasses = createMap<true>(); // Ensure we only do this once per class.
1919
return codeFixAll(context, errorCodes, (changes, diag) => {
2020
const nodes = getNodes(diag.file!, diag.start!);
2121
if (!nodes) return;
2222
const { constructor, superCall } = nodes;
2323
if (addToSeen(seenClasses, getNodeId(constructor.parent))) {
24-
doChange(changes, sourceFile, constructor, superCall, newLineCharacter);
24+
doChange(changes, sourceFile, constructor, superCall);
2525
}
2626
});
2727
},
2828
});
2929

30-
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, constructor: ConstructorDeclaration, superCall: ExpressionStatement, newLineCharacter: string): void {
31-
changes.insertNodeAtConstructorStart(sourceFile, constructor, superCall, newLineCharacter);
30+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, constructor: ConstructorDeclaration, superCall: ExpressionStatement): void {
31+
changes.insertNodeAtConstructorStart(sourceFile, constructor, superCall);
3232
changes.deleteNode(sourceFile, superCall);
3333
}
3434

src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ namespace ts.codefix {
55
registerCodeFix({
66
errorCodes,
77
getCodeActions(context) {
8-
const { sourceFile } = context;
9-
const ctr = getNode(sourceFile, context.span.start);
10-
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, ctr, context.newLineCharacter));
8+
const { sourceFile, span } = context;
9+
const ctr = getNode(sourceFile, span.start);
10+
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, ctr));
1111
return [{ description: getLocaleSpecificMessage(Diagnostics.Add_missing_super_call), changes, fixId }];
1212
},
1313
fixIds: [fixId],
1414
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) =>
15-
doChange(changes, context.sourceFile, getNode(diag.file, diag.start!), context.newLineCharacter)),
15+
doChange(changes, context.sourceFile, getNode(diag.file, diag.start!))),
1616
});
1717

1818
function getNode(sourceFile: SourceFile, pos: number): ConstructorDeclaration {
@@ -21,8 +21,8 @@ namespace ts.codefix {
2121
return token.parent as ConstructorDeclaration;
2222
}
2323

24-
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, ctr: ConstructorDeclaration, newLineCharacter: string) {
24+
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, ctr: ConstructorDeclaration) {
2525
const superCall = createStatement(createCall(createSuper(), /*typeArguments*/ undefined, /*argumentsArray*/ emptyArray));
26-
changes.insertNodeAtConstructorStart(sourceFile, ctr, superCall, newLineCharacter);
26+
changes.insertNodeAtConstructorStart(sourceFile, ctr, superCall);
2727
}
2828
}

src/services/codefixes/importFixes.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ namespace ts.codefix {
257257
}
258258

259259
function getCodeActionForNewImport(context: SymbolContext & { kind: ImportKind }, moduleSpecifier: string): ImportCodeAction {
260-
const { kind, sourceFile, newLineCharacter, symbolName } = context;
260+
const { kind, sourceFile, symbolName } = context;
261261
const lastImportDeclaration = findLast(sourceFile.statements, isAnyImportSyntax);
262262

263263
const moduleSpecifierWithoutQuotes = stripQuotes(moduleSpecifier);
@@ -275,6 +275,7 @@ namespace ts.codefix {
275275
createExternalModuleReference(quotedModuleSpecifier));
276276

277277
const changes = ChangeTracker.with(context, changeTracker => {
278+
const { newLineCharacter } = changeTracker;
278279
if (lastImportDeclaration) {
279280
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl, { suffix: newLineCharacter });
280281
}

0 commit comments

Comments
 (0)