Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions src/services/codefixes/convertFunctionToEs6Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace ts.codefix {
});

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker): void {
const deletedNodes: { node: Node, inList: boolean }[] = [];
const ctorSymbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, position))!;

if (!ctorSymbol || !(ctorSymbol.flags & (SymbolFlags.Function | SymbolFlags.Variable))) {
Expand All @@ -28,7 +27,7 @@ namespace ts.codefix {
switch (ctorDeclaration.kind) {
case SyntaxKind.FunctionDeclaration:
precedingNode = ctorDeclaration;
deleteNode(ctorDeclaration);
changes.delete(sourceFile, ctorDeclaration);
newClassDeclaration = createClassFromFunctionDeclaration(ctorDeclaration as FunctionDeclaration);
break;

Expand All @@ -37,10 +36,10 @@ namespace ts.codefix {
newClassDeclaration = createClassFromVariableDeclaration(ctorDeclaration as VariableDeclaration);
if ((<VariableDeclarationList>ctorDeclaration.parent).declarations.length === 1) {
copyComments(precedingNode, newClassDeclaration!, sourceFile); // TODO: GH#18217
deleteNode(precedingNode);
changes.delete(sourceFile, precedingNode);
}
else {
deleteNode(ctorDeclaration, /*inList*/ true);
changes.delete(sourceFile, ctorDeclaration);
}
break;
}
Expand All @@ -53,21 +52,6 @@ namespace ts.codefix {

// Because the preceding node could be touched, we need to insert nodes before delete nodes.
changes.insertNodeAfter(sourceFile, precedingNode!, newClassDeclaration);
for (const { node, inList } of deletedNodes) {
if (inList) {
changes.deleteNodeInList(sourceFile, node);
}
else {
changes.deleteNode(sourceFile, node);
}
}

function deleteNode(node: Node, inList = false) {
// If parent node has already been deleted, do nothing
if (!deletedNodes.some(n => isNodeDescendantOf(node, n.node))) {
deletedNodes.push({ node, inList });
}
}

function createClassElementsFromSymbol(symbol: Symbol) {
const memberElements: ClassElement[] = [];
Expand Down Expand Up @@ -115,7 +99,7 @@ namespace ts.codefix {
// delete the entire statement if this expression is the sole expression to take care of the semicolon at the end
const nodeToDelete = assignmentBinaryExpression.parent && assignmentBinaryExpression.parent.kind === SyntaxKind.ExpressionStatement
? assignmentBinaryExpression.parent : assignmentBinaryExpression;
deleteNode(nodeToDelete);
changes.delete(sourceFile, nodeToDelete);

if (!assignmentBinaryExpression.right) {
return createProperty([], modifiers, symbol.name, /*questionToken*/ undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/convertToEs6Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ namespace ts.codefix {
if (isExportsOrModuleExportsOrAlias(sourceFile, left)) {
if (isExportsOrModuleExportsOrAlias(sourceFile, right)) {
// `const alias = module.exports;` or `module.exports = alias;` can be removed.
changes.deleteNode(sourceFile, assignment.parent);
changes.delete(sourceFile, assignment.parent);
}
else {
const replacement = isObjectLiteralExpression(right) ? tryChangeModuleExportsObject(right)
Expand Down Expand Up @@ -297,7 +297,7 @@ namespace ts.codefix {
if (!right.name) changes.insertName(sourceFile, right, name);

const semi = findChildOfKind(parent, SyntaxKind.SemicolonToken, sourceFile);
if (semi) changes.deleteNode(sourceFile, semi, { useNonAdjustedEndPosition: true });
if (semi) changes.delete(sourceFile, semi);
}
else {
// `exports.f = function g() {}` -> `export const f = function g() {}` -- just replace `exports.` with `export const `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace ts.codefix {

function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, constructor: ConstructorDeclaration, superCall: ExpressionStatement): void {
changes.insertNodeAtConstructorStart(sourceFile, constructor, superCall);
changes.deleteNode(sourceFile, superCall);
changes.delete(sourceFile, superCall);
}

function getNodes(sourceFile: SourceFile, pos: number): { readonly constructor: ConstructorDeclaration, readonly superCall: ExpressionStatement } | undefined {
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/fixUnreachableCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ namespace ts.codefix {
// falls through
case SyntaxKind.WhileStatement:
case SyntaxKind.ForStatement:
changes.deleteNode(sourceFile, container);
changes.delete(sourceFile, container);
break;
default:
if (isBlock(statement.parent)) {
split(sliceAfter(statement.parent.statements, statement), shouldRemove, (start, end) => changes.deleteNodeRange(sourceFile, start, end));
}
else {
changes.deleteNode(sourceFile, statement);
changes.delete(sourceFile, statement);
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/services/codefixes/fixUnusedIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace ts.codefix {

const importDecl = tryGetFullImport(token);
if (importDecl) {
const changes = textChanges.ChangeTracker.with(context, t => t.deleteNode(sourceFile, importDecl));
const changes = textChanges.ChangeTracker.with(context, t => t.delete(sourceFile, importDecl));
return [createCodeFixAction(fixName, changes, [Diagnostics.Remove_import_from_0, showModuleSpecifier(importDecl)], fixIdDelete, Diagnostics.Delete_all_unused_declarations)];
}
const delDestructure = textChanges.ChangeTracker.with(context, t =>
Expand Down Expand Up @@ -67,7 +67,7 @@ namespace ts.codefix {
case fixIdDelete: {
const importDecl = tryGetFullImport(token);
if (importDecl) {
changes.deleteDeclaration(sourceFile, importDecl);
changes.delete(sourceFile, importDecl);
}
else if (!tryDeleteFullDestructure(token, changes, sourceFile, checker, sourceFiles, /*isFixAll*/ true) &&
!tryDeleteFullVariableStatement(sourceFile, token, changes)) {
Expand All @@ -94,15 +94,15 @@ namespace ts.codefix {
tryDeleteParameter(changes, sourceFile, decl, checker, sourceFiles, isFixAll);
}
else {
changes.deleteDeclaration(sourceFile, decl);
changes.delete(sourceFile, decl);
}
return true;
}

function tryDeleteFullVariableStatement(sourceFile: SourceFile, token: Node, changes: textChanges.ChangeTracker): boolean {
const declarationList = tryCast(token.parent, isVariableDeclarationList);
if (declarationList && declarationList.getChildren(sourceFile)[0] === token) {
changes.deleteDeclaration(sourceFile, declarationList.parent.kind === SyntaxKind.VariableStatement ? declarationList.parent : declarationList);
changes.delete(sourceFile, declarationList.parent.kind === SyntaxKind.VariableStatement ? declarationList.parent : declarationList);
return true;
}
return false;
Expand Down Expand Up @@ -140,7 +140,7 @@ namespace ts.codefix {
FindAllReferences.Core.eachSymbolReferenceInFile(token, checker, sourceFile, (ref: Node) => {
if (ref.parent.kind === SyntaxKind.PropertyAccessExpression) ref = ref.parent;
if (ref.parent.kind === SyntaxKind.BinaryExpression && ref.parent.parent.kind === SyntaxKind.ExpressionStatement) {
changes.deleteDeclaration(sourceFile, ref.parent.parent);
changes.delete(sourceFile, ref.parent.parent);
}
});
}
Expand All @@ -151,13 +151,13 @@ namespace ts.codefix {
tryDeleteParameter(changes, sourceFile, parent, checker, sourceFiles, isFixAll);
}
else {
changes.deleteDeclaration(sourceFile, isImportClause(parent) ? token : isComputedPropertyName(parent) ? parent.parent : parent);
changes.delete(sourceFile, isImportClause(parent) ? token : isComputedPropertyName(parent) ? parent.parent : parent);
}
}

function tryDeleteParameter(changes: textChanges.ChangeTracker, sourceFile: SourceFile, p: ParameterDeclaration, checker: TypeChecker, sourceFiles: ReadonlyArray<SourceFile>, isFixAll: boolean): void {
if (mayDeleteParameter(p, checker, isFixAll)) {
changes.deleteDeclaration(sourceFile, p);
changes.delete(sourceFile, p);
deleteUnusedArguments(changes, sourceFile, p, sourceFiles, checker);
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ namespace ts.codefix {
FindAllReferences.Core.eachSignatureCall(deletedParameter.parent, sourceFiles, checker, call => {
const index = deletedParameter.parent.parameters.indexOf(deletedParameter);
if (call.arguments.length > index) { // Just in case the call didn't provide enough arguments.
changes.deleteDeclaration(sourceFile, call.arguments[index]);
changes.delete(sourceFile, call.arguments[index]);
}
});
}
Expand Down
7 changes: 2 additions & 5 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ namespace ts.OrganizeImports {

// Delete or replace the first import.
if (newImportDecls.length === 0) {
changeTracker.deleteNode(sourceFile, oldImportDecls[0], {
useNonAdjustedStartPosition: true, // Leave header comment in place
useNonAdjustedEndPosition: false,
});
changeTracker.delete(sourceFile, oldImportDecls[0]);
}
else {
// Note: Delete the surrounding trivia because it will have been retained in newImportDecls.
Expand All @@ -79,7 +76,7 @@ namespace ts.OrganizeImports {

// Delete any subsequent imports.
for (let i = 1; i < oldImportDecls.length; i++) {
changeTracker.deleteNode(sourceFile, oldImportDecls[i]);
changeTracker.delete(sourceFile, oldImportDecls[i]);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/refactors/convertExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ namespace ts.refactor {

function changeExport(exportingSourceFile: SourceFile, { wasDefault, exportNode, exportName }: Info, changes: textChanges.ChangeTracker, checker: TypeChecker): void {
if (wasDefault) {
changes.deleteNode(exportingSourceFile, Debug.assertDefined(findModifier(exportNode, SyntaxKind.DefaultKeyword)));
changes.delete(exportingSourceFile, Debug.assertDefined(findModifier(exportNode, SyntaxKind.DefaultKeyword)));
}
else {
const exportKeyword = Debug.assertDefined(findModifier(exportNode, SyntaxKind.ExportKeyword));
Expand Down Expand Up @@ -155,7 +155,7 @@ namespace ts.refactor {
}
else {
// `import foo, { bar } from "./a"` --> `import { bar, foo } from "./a";`
changes.deleteNode(importingSourceFile, ref);
changes.delete(importingSourceFile, ref);
changes.insertNodeAtEndOfList(importingSourceFile, namedBindings.elements, spec);
}
break;
Expand Down Expand Up @@ -183,7 +183,7 @@ namespace ts.refactor {
changes.replaceNode(importingSourceFile, spec.parent, defaultImport);
}
else {
changes.deleteNodeInList(importingSourceFile, spec);
changes.delete(importingSourceFile, spec);
changes.insertNodeBefore(importingSourceFile, spec.parent, defaultImport);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ namespace ts.refactor.extractSymbol {
// Consume
if (node.parent.kind === SyntaxKind.ExpressionStatement) {
// If the parent is an expression statement, delete it.
changeTracker.deleteNode(context.file, node.parent, textChanges.useNonAdjustedPositions);
changeTracker.delete(context.file, node.parent);
}
else {
const localReference = createIdentifier(localNameText);
Expand Down
16 changes: 8 additions & 8 deletions src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ namespace ts.refactor {
break;
case SyntaxKind.ImportEqualsDeclaration:
if (isUnused(importDecl.name)) {
changes.deleteNode(sourceFile, importDecl);
changes.delete(sourceFile, importDecl);
}
break;
case SyntaxKind.VariableDeclaration:
Expand All @@ -342,19 +342,19 @@ namespace ts.refactor {
const namedBindingsUnused = !namedBindings ||
(namedBindings.kind === SyntaxKind.NamespaceImport ? isUnused(namedBindings.name) : namedBindings.elements.every(e => isUnused(e.name)));
if (defaultUnused && namedBindingsUnused) {
changes.deleteNode(sourceFile, importDecl);
changes.delete(sourceFile, importDecl);
}
else {
if (name && defaultUnused) {
changes.deleteNode(sourceFile, name);
changes.delete(sourceFile, name);
}
if (namedBindings) {
if (namedBindingsUnused) {
changes.deleteNode(sourceFile, namedBindings);
changes.delete(sourceFile, namedBindings);
}
else if (namedBindings.kind === SyntaxKind.NamedImports) {
for (const element of namedBindings.elements) {
if (isUnused(element.name)) changes.deleteNodeInList(sourceFile, element);
if (isUnused(element.name)) changes.delete(sourceFile, element);
}
}
}
Expand All @@ -365,20 +365,20 @@ namespace ts.refactor {
switch (name.kind) {
case SyntaxKind.Identifier:
if (isUnused(name)) {
changes.deleteNode(sourceFile, name);
changes.delete(sourceFile, name);
}
break;
case SyntaxKind.ArrayBindingPattern:
break;
case SyntaxKind.ObjectBindingPattern:
if (name.elements.every(e => isIdentifier(e.name) && isUnused(e.name))) {
changes.deleteNode(sourceFile,
changes.delete(sourceFile,
isVariableDeclarationList(varDecl.parent) && varDecl.parent.declarations.length === 1 ? varDecl.parent.parent : varDecl);
}
else {
for (const element of name.elements) {
if (isIdentifier(element.name) && isUnused(element.name)) {
changes.deleteNode(sourceFile, element.name);
changes.delete(sourceFile, element.name);
}
}
}
Expand Down
Loading