Skip to content

Commit ebbb3a4

Browse files
authored
Merge pull request microsoft#21158 from amcasey/NewlineConsistency
Handle linebreaks consistently in code fixes and refactorings
2 parents b0916ed + d97dec8 commit ebbb3a4

File tree

124 files changed

+442
-524
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+442
-524
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2568,9 +2568,7 @@ Actual: ${stringify(fullActual)}`);
25682568
const originalContent = scriptInfo.content;
25692569
for (const codeFix of codeFixes) {
25702570
this.applyEdits(codeFix.changes[0].fileName, codeFix.changes[0].textChanges, /*isFormattingEdit*/ false);
2571-
let text = this.rangeText(ranges[0]);
2572-
// TODO:GH#18445 (remove this line to see errors in many `importNameCodeFix` tests)
2573-
text = text.replace(/\r\n/g, "\n");
2571+
const text = this.rangeText(ranges[0]);
25742572
actualTextArray.push(text);
25752573
scriptInfo.updateContent(originalContent);
25762574
}

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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace ts {
1010
export interface CodeFixContextBase extends textChanges.TextChangesContext {
1111
sourceFile: SourceFile;
1212
program: Program;
13-
host: LanguageServiceHost;
1413
cancellationToken: CancellationToken;
1514
}
1615

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 = getNewLineOrDefaultFromHost(context.host, context.formatContext.options);
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, getNewLineOrDefaultFromHost(context.host, context.formatContext.options)));
4042
}
4143
}),
4244
});

src/services/codefixes/fixAddMissingMember.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ 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]);
147147
const changes = textChanges.ChangeTracker.with(context, t => addPropertyDeclaration(t, classDeclarationSourceFile, classDeclaration, tokenName, typeNode, makeStatic));
148148
return { description, changes, fixId };
@@ -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(
@@ -181,7 +181,7 @@ namespace ts.codefix {
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]);
186186
const changes = textChanges.ChangeTracker.with(context, t => addMethodDeclaration(t, classDeclarationSourceFile, classDeclaration, token, callExpression, makeStatic, inJs));
187187
return { description, changes, fixId };

src/services/codefixes/importFixes.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ namespace ts.codefix {
2929
symbolName: string;
3030
}
3131

32-
interface SymbolAndTokenContext extends SymbolContext {
32+
interface ImportCodeFixContext extends SymbolContext {
3333
symbolToken: Identifier | undefined;
34-
}
35-
36-
interface ImportCodeFixContext extends SymbolAndTokenContext {
37-
host: LanguageServiceHost;
3834
program: Program;
3935
checker: TypeChecker;
4036
compilerOptions: CompilerOptions;
@@ -173,7 +169,6 @@ namespace ts.codefix {
173169
const symbolToken = cast(getTokenAtPosition(context.sourceFile, context.span.start, /*includeJsDocComment*/ false), isIdentifier);
174170
return {
175171
host: context.host,
176-
newLineCharacter: context.newLineCharacter,
177172
formatContext: context.formatContext,
178173
sourceFile: context.sourceFile,
179174
program,

src/services/completions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,6 @@ namespace ts.Completions {
627627
host,
628628
program,
629629
checker,
630-
newLineCharacter: host.getNewLine(),
631630
compilerOptions,
632631
sourceFile,
633632
formatContext,

src/services/refactorProvider.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace ts {
1919
startPosition: number;
2020
endPosition?: number;
2121
program: Program;
22-
host: LanguageServiceHost;
2322
cancellationToken?: CancellationToken;
2423
}
2524

src/services/services.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,23 +1887,21 @@ namespace ts {
18871887
synchronizeHostData();
18881888
const sourceFile = getValidSourceFile(fileName);
18891889
const span = createTextSpanFromBounds(start, end);
1890-
const newLineCharacter = getNewLineOrDefaultFromHost(host);
18911890
const formatContext = formatting.getFormatContext(formatOptions);
18921891

18931892
return flatMap(deduplicate(errorCodes, equateValues, compareValues), errorCode => {
18941893
cancellationToken.throwIfCancellationRequested();
1895-
return codefix.getFixes({ errorCode, sourceFile, span, program, newLineCharacter, host, cancellationToken, formatContext });
1894+
return codefix.getFixes({ errorCode, sourceFile, span, program, host, cancellationToken, formatContext });
18961895
});
18971896
}
18981897

18991898
function getCombinedCodeFix(scope: CombinedCodeFixScope, fixId: {}, formatOptions: FormatCodeSettings): CombinedCodeActions {
19001899
synchronizeHostData();
19011900
Debug.assert(scope.type === "file");
19021901
const sourceFile = getValidSourceFile(scope.fileName);
1903-
const newLineCharacter = getNewLineOrDefaultFromHost(host);
19041902
const formatContext = formatting.getFormatContext(formatOptions);
19051903

1906-
return codefix.getAllFixes({ fixId, sourceFile, program, newLineCharacter, host, cancellationToken, formatContext });
1904+
return codefix.getAllFixes({ fixId, sourceFile, program, host, cancellationToken, formatContext });
19071905
}
19081906

19091907
function applyCodeActionCommand(action: CodeActionCommand): Promise<ApplyCodeActionCommandResult>;
@@ -2134,7 +2132,6 @@ namespace ts {
21342132
startPosition,
21352133
endPosition,
21362134
program: getProgram(),
2137-
newLineCharacter: formatOptions ? formatOptions.newLineCharacter : host.getNewLine(),
21382135
host,
21392136
formatContext: formatting.getFormatContext(formatOptions),
21402137
cancellationToken,

src/services/textChanges.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace ts.textChanges {
187187
}
188188

189189
export interface TextChangesContext {
190-
newLineCharacter: string;
190+
host: LanguageServiceHost;
191191
formatContext: ts.formatting.FormatContext;
192192
}
193193

@@ -199,7 +199,7 @@ namespace ts.textChanges {
199199
private readonly nodesInsertedAtClassStarts = createMap<{ sourceFile: SourceFile, cls: ClassLikeDeclaration, members: ClassElement[] }>();
200200

201201
public static fromContext(context: TextChangesContext): ChangeTracker {
202-
return new ChangeTracker(context.newLineCharacter === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed, context.formatContext);
202+
return new ChangeTracker(getNewLineOrDefaultFromHost(context.host, context.formatContext.options) === "\n" ? NewLineKind.LineFeed : NewLineKind.CarriageReturnLineFeed, context.formatContext);
203203
}
204204

205205
public static with(context: TextChangesContext, cb: (tracker: ChangeTracker) => void): FileTextChanges[] {

0 commit comments

Comments
 (0)