mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Move createTextChange to services/utilities.ts (#21416)
* Move createTextChange to services/utilities.ts * Use separate functions instead of overloads
This commit is contained in:
@@ -24,13 +24,9 @@ namespace ts.codefix {
|
||||
},
|
||||
{
|
||||
description: getLocaleSpecificMessage(Diagnostics.Disable_checking_for_this_file),
|
||||
changes: [createFileTextChanges(sourceFile.fileName, [{
|
||||
span: {
|
||||
start: sourceFile.checkJsDirective ? sourceFile.checkJsDirective.pos : 0,
|
||||
length: sourceFile.checkJsDirective ? sourceFile.checkJsDirective.end - sourceFile.checkJsDirective.pos : 0
|
||||
},
|
||||
newText: `// @ts-nocheck${newLineCharacter}`
|
||||
}])],
|
||||
changes: [createFileTextChanges(sourceFile.fileName, [
|
||||
createTextChange(sourceFile.checkJsDirective ? createTextSpanFromBounds(sourceFile.checkJsDirective.pos, sourceFile.checkJsDirective.end) : createTextSpan(0, 0), `// @ts-nocheck${newLineCharacter}`),
|
||||
])],
|
||||
// fixId unnecessary because adding `// @ts-nocheck` even once will ignore every error in the file.
|
||||
fixId: undefined,
|
||||
}];
|
||||
@@ -62,15 +58,11 @@ namespace ts.codefix {
|
||||
const token = getTouchingToken(sourceFile, startPosition, /*includeJsDocComment*/ false);
|
||||
const tokenLeadingComments = getLeadingCommentRangesOfNode(token, sourceFile);
|
||||
if (!tokenLeadingComments || !tokenLeadingComments.length || tokenLeadingComments[0].pos >= startPosition) {
|
||||
return { lineNumber, change: createTextChange(startPosition, 0, `// @ts-ignore${newLineCharacter}`) };
|
||||
return { lineNumber, change: createTextChangeFromStartLength(startPosition, 0, `// @ts-ignore${newLineCharacter}`) };
|
||||
}
|
||||
}
|
||||
|
||||
// If all fails, add an extra new line immediately before the error span.
|
||||
return { lineNumber, change: createTextChange(position, 0, `${position === startPosition ? "" : newLineCharacter}// @ts-ignore${newLineCharacter}`) };
|
||||
}
|
||||
|
||||
function createTextChange(start: number, length: number, newText: string): TextChange {
|
||||
return { span: { start, length }, newText };
|
||||
return { lineNumber, change: createTextChangeFromStartLength(position, 0, `${position === startPosition ? "" : newLineCharacter}// @ts-ignore${newLineCharacter}`) };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace ts.codefix {
|
||||
}
|
||||
|
||||
function createChange(declaration: TypeNode, sourceFile: SourceFile, newText: string): TextChange {
|
||||
return { span: createTextSpanFromBounds(declaration.getStart(sourceFile), declaration.getEnd()), newText };
|
||||
return createTextChange(createTextSpanFromNode(declaration, sourceFile), newText);
|
||||
}
|
||||
|
||||
function typeString(type: Type, checker: TypeChecker): string {
|
||||
|
||||
@@ -191,7 +191,7 @@ namespace ts.codefix {
|
||||
|
||||
function makeChange(declaration: Declaration, start: number, type: Type | undefined, program: Program): TextChange | undefined {
|
||||
const typeString = type && typeToString(type, declaration, program.getTypeChecker());
|
||||
return typeString === undefined ? undefined : { span: createTextSpan(start, 0), newText: `: ${typeString}` };
|
||||
return typeString === undefined ? undefined : createTextChangeFromStartLength(start, 0, `: ${typeString}`);
|
||||
}
|
||||
|
||||
function getReferences(token: PropertyName | Token<SyntaxKind.ConstructorKeyword>, sourceFile: SourceFile, program: Program, cancellationToken: CancellationToken): Identifier[] {
|
||||
|
||||
@@ -1078,19 +1078,15 @@ namespace ts.formatting {
|
||||
trimTrailingWhitespacesForLines(startLine, endLine + 1, previousRange);
|
||||
}
|
||||
|
||||
function newTextChange(start: number, len: number, newText: string): TextChange {
|
||||
return { span: createTextSpan(start, len), newText };
|
||||
}
|
||||
|
||||
function recordDelete(start: number, len: number) {
|
||||
if (len) {
|
||||
edits.push(newTextChange(start, len, ""));
|
||||
edits.push(createTextChangeFromStartLength(start, len, ""));
|
||||
}
|
||||
}
|
||||
|
||||
function recordReplace(start: number, len: number, newText: string) {
|
||||
if (len || newText) {
|
||||
edits.push(newTextChange(start, len, newText));
|
||||
edits.push(createTextChangeFromStartLength(start, len, newText));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -620,10 +620,7 @@ namespace ts.textChanges {
|
||||
const sourceFile = changesInFile[0].sourceFile;
|
||||
const fileTextChanges: FileTextChanges = { fileName: sourceFile.fileName, textChanges: [] };
|
||||
for (const c of ChangeTracker.normalize(changesInFile)) {
|
||||
fileTextChanges.textChanges.push({
|
||||
span: this.computeSpan(c, sourceFile),
|
||||
newText: this.computeNewText(c, sourceFile)
|
||||
});
|
||||
fileTextChanges.textChanges.push(createTextChange(this.computeSpan(c, sourceFile), this.computeNewText(c, sourceFile)));
|
||||
}
|
||||
fileChangesList.push(fileTextChanges);
|
||||
});
|
||||
|
||||
@@ -1067,6 +1067,14 @@ namespace ts {
|
||||
return createTextSpanFromBounds(range.pos, range.end);
|
||||
}
|
||||
|
||||
export function createTextChangeFromStartLength(start: number, length: number, newText: string): TextChange {
|
||||
return createTextChange(createTextSpan(start, length), newText);
|
||||
}
|
||||
|
||||
export function createTextChange(span: TextSpan, newText: string): TextChange {
|
||||
return { span, newText };
|
||||
}
|
||||
|
||||
export const typeKeywords: ReadonlyArray<SyntaxKind> = [
|
||||
SyntaxKind.AnyKeyword,
|
||||
SyntaxKind.BooleanKeyword,
|
||||
|
||||
Reference in New Issue
Block a user