Thanks to visit codestin.com
Credit goes to github.com

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[] {

src/services/utilities.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,8 +1259,10 @@ namespace ts {
12591259
/**
12601260
* The default is CRLF.
12611261
*/
1262-
export function getNewLineOrDefaultFromHost(host: LanguageServiceHost | LanguageServiceShimHost) {
1263-
return host.getNewLine ? host.getNewLine() : carriageReturnLineFeed;
1262+
export function getNewLineOrDefaultFromHost(host: LanguageServiceHost | LanguageServiceShimHost, formatSettings?: FormatCodeSettings) {
1263+
return (formatSettings && formatSettings.newLineCharacter) ||
1264+
(host.getNewLine && host.getNewLine()) ||
1265+
carriageReturnLineFeed;
12641266
}
12651267

12661268
export function lineBreakPart() {

tests/cases/fourslash/autoFormattingOnPasting.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public testMethod( )
1111
}`);
1212
// We're missing scenarios of formatting option settings due to bug 693273 - [TypeScript] Need to improve fourslash support for formatting options.
1313
// Missing scenario ** Uncheck Tools->Options->Text Editor->TypeScript->Formatting->General->Format on paste **
14-
//verify.currentFileContentIs("module TestModule {\r\n\
15-
// class TestClass{\r\n\
16-
//private foo;\r\n\
17-
//public testMethod( )\r\n\
18-
//{}\r\n\
19-
//}\r\n\
14+
//verify.currentFileContentIs("module TestModule {\n\
15+
// class TestClass{\n\
16+
//private foo;\n\
17+
//public testMethod( )\n\
18+
//{}\n\
19+
//}\n\
2020
//}");
2121
// Missing scenario ** Check Tools->Options->Text Editor->TypeScript->Formatting->General->Format on paste **
2222
verify.currentFileContentIs(`module TestModule {

tests/cases/fourslash/classInterfaceInsert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
verify.quickInfoAt("className", "class Sphere");
1313

1414
goTo.marker('interfaceGoesHere');
15-
edit.insert("\r\ninterface Surface {\r\n reflect: () => number;\r\n}\r\n");
15+
edit.insert("\ninterface Surface {\n reflect: () => number;\n}\n");
1616

1717
verify.quickInfoAt("className", "class Sphere");

tests/cases/fourslash/codeFixAddMissingMember.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
verify.codeFix({
1010
description: "Declare property 'foo'",
1111
index: 0,
12-
// TODO: GH#18445
1312
newFileContent: `class C {
14-
foo: number;\r
13+
foo: number;
1514
method() {
1615
this.foo = 10;
1716
}

tests/cases/fourslash/codeFixAddMissingMember2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
verify.codeFix({
1010
description: "Add index signature for property 'foo'",
1111
index: 1,
12-
// TODO: GH#18445
1312
newFileContent: `class C {
14-
[x: string]: number;\r
13+
[x: string]: number;
1514
method() {
1615
this.foo = 10;
1716
}

tests/cases/fourslash/codeFixAddMissingMember3.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
verify.codeFix({
1010
description: "Declare static property 'foo'",
1111
index: 0,
12-
// TODO: GH#18445
1312
newFileContent: `class C {
14-
static foo: number;\r
13+
static foo: number;
1514
static method() {
1615
this.foo = 10;
1716
}

tests/cases/fourslash/codeFixAddMissingMember4.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
verify.codeFix({
1616
description: "Initialize property 'foo' in the constructor",
1717
index: 0,
18-
// TODO: GH#18445
1918
newFileContent: `class C {
20-
constructor() {\r
21-
this.foo = undefined;\r
19+
constructor() {
20+
this.foo = undefined;
2221
}
2322
method() {
2423
this.foo === 10;

tests/cases/fourslash/codeFixAddMissingMember5.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
verify.codeFix({
1414
description: "Initialize static property 'foo'",
1515
index: 0,
16-
// TODO: GH#18445
1716
newFileContent: `class C {
1817
static method() {
1918
()=>{ this.foo === 10 };
2019
}
21-
}\r
22-
C.foo = undefined;\r
20+
}
21+
C.foo = undefined;
2322
`
2423
});

tests/cases/fourslash/codeFixAddMissingMember6.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
verify.codeFix({
1414
description: "Initialize property 'foo' in the constructor",
1515
index: 0,
16-
// TODO: GH#18445
1716
newFileContent: `class C {
18-
constructor() {\r
19-
this.foo = undefined;\r
17+
constructor() {
18+
this.foo = undefined;
2019
}
2120
prop = ()=>{ this.foo === 10 };
2221
}`

tests/cases/fourslash/codeFixAddMissingMember7.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
verify.codeFix({
1212
description: "Initialize static property 'foo'",
1313
index: 2,
14-
// TODO: GH#18445
1514
newFileContent: `class C {
1615
static p = ()=>{ this.foo === 10 };
17-
}\r
18-
C.foo = undefined;\r
16+
}
17+
C.foo = undefined;
1918
`
2019
});

tests/cases/fourslash/codeFixAddMissingMember_all.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
verify.codeFixAll({
1212
fixId: "addMissingMember",
1313
newFileContent:
14-
// TODO: GH#18445
1514
`class C {
16-
x: number;\r
17-
y(): any {\r
18-
throw new Error("Method not implemented.");\r
19-
}\r
15+
x: number;
16+
y(): any {
17+
throw new Error("Method not implemented.");
18+
}
2019
method() {
2120
this.x = 0;
2221
this.y();

tests/cases/fourslash/codeFixAddMissingMember_all_js.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
verify.codeFixAll({
1717
fixId: "addMissingMember",
1818
newFileContent:
19-
// TODO: GH#18445
2019
`class C {
21-
y() {\r
22-
throw new Error("Method not implemented.");\r
23-
}\r
24-
constructor() {\r
25-
this.x = undefined;\r
20+
y() {
21+
throw new Error("Method not implemented.");
22+
}
23+
constructor() {
24+
this.x = undefined;
2625
}
2726
method() {
2827
this.x;

tests/cases/fourslash/codeFixClassExprClassImplementClassFunctionVoidInferred.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ verify.codeFix({
1212
`class A {
1313
f() {}
1414
}
15-
let B = class implements A {\r
16-
f(): void {\r
17-
throw new Error("Method not implemented.");\r
18-
}\r
15+
let B = class implements A {
16+
f(): void {
17+
throw new Error("Method not implemented.");
18+
}
1919
}`
2020
});

tests/cases/fourslash/codeFixClassExprExtendsAbstractExpressionWithTypeArgs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ verify.codeFix({
2020
return C;
2121
}
2222
23-
let B = class extends foo("s")<number> {\r
24-
a: string | number;\r
23+
let B = class extends foo("s")<number> {
24+
a: string | number;
2525
}`
2626
});

tests/cases/fourslash/codeFixClassExtendAbstractExpressionWithTypeArgs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ verify.codeFix({
2020
return C;
2121
}
2222
23-
class B extends foo("s")<number> {\r
24-
a: string | number;\r
23+
class B extends foo("s")<number> {
24+
a: string | number;
2525
}`
2626
});

tests/cases/fourslash/codeFixClassExtendAbstractGetterSetter.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ verify.codeFix({
4242
// Don't need to add anything in this case.
4343
abstract class B extends A {}
4444
45-
class C extends A {\r
46-
a: string | number;\r
47-
b: this;\r
48-
c: A;\r
49-
d: string | number;\r
50-
e: this;\r
51-
f: A;\r
52-
g: string;\r
45+
class C extends A {
46+
a: string | number;
47+
b: this;
48+
c: A;
49+
d: string | number;
50+
e: this;
51+
f: A;
52+
g: string;
5353
}`
5454
});

0 commit comments

Comments
 (0)