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
6 changes: 0 additions & 6 deletions .quality-baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@
"singleUseVariable"
]
},
"src/core/commands/rename-command.ts": {
"lastCommitId": "bf89440ad4d87ed9c05abccbdce0a584a374bedf",
"violations": [
"singleUseVariable"
]
},
"src/core/commands/select-command.ts": {
"lastCommitId": "876773659621d2929ad9c16187ad30ab7dcc177b",
"violations": [
Expand Down
19 changes: 13 additions & 6 deletions src/core/commands/rename-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { VariableLocator, VariableNodeResult } from '../locators/variable-locato
import { RenameVariableTransformation } from '../transformations/rename-variable-transformation';
import { LocationRange } from '../ast/location-range';
import { NodeAnalyzer } from '../services/node-analyzer';
import { VariableNameValidator } from '../services/variable-name-validator';
import { ScopeAnalyzer } from '../services/scope-analyzer';

export class RenameCommand implements RefactoringCommand {
readonly name = 'rename';
Expand All @@ -15,15 +17,15 @@ export class RenameCommand implements RefactoringCommand {
private consoleOutput!: ConsoleOutput;
private astService!: ASTService;
private variableLocator!: VariableLocator;
private nameValidator!: VariableNameValidator;

async execute(file: string, options: CommandOptions): Promise<void> {
this.validateOptions(options);
this.astService = ASTService.createForFile(file);
this.variableLocator = new VariableLocator(this.astService.getProject());
const sourceFile = this.astService.loadSourceFile(file);
const node = this.findTargetNode(options);
await this.performRename(node, options.to as string);
await this.astService.saveSourceFile(sourceFile);
this.nameValidator = new VariableNameValidator();
await this.performRename(this.findTargetNode(options), options.to as string);
await this.astService.saveSourceFile(this.astService.loadSourceFile(file));
}

private findTargetNode(options: CommandOptions): Node {
Expand All @@ -47,8 +49,9 @@ export class RenameCommand implements RefactoringCommand {
NodeAnalyzer.validateIdentifierNode(node);
const sourceFile = node.getSourceFile();
const nodeResult = this.findVariableNodesAtPosition(node, sourceFile);
const transformation = this.createRenameTransformation(nodeResult, newName);
await transformation.transform(sourceFile);

this.validateNewName(nodeResult.declaration, newName);
await this.createRenameTransformation(nodeResult, newName).transform(sourceFile);
}

private findVariableNodesAtPosition(node: Node, sourceFile: SourceFile) {
Expand All @@ -71,4 +74,8 @@ export class RenameCommand implements RefactoringCommand {
setConsoleOutput(consoleOutput: ConsoleOutput): void {
this.consoleOutput = consoleOutput;
}

private validateNewName(declarationNode: Node, newName: string): void {
this.nameValidator.generateUniqueName(newName, ScopeAnalyzer.getNodeScope(declarationNode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Variable name 'param' already exists in this scope. Please choose a different name.
9 changes: 9 additions & 0 deletions tests/fixtures/commands/rename/parameter-conflict.expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @description Test rename with parameter conflict
* @command refakts rename "[parameter-conflict.input.ts 7:11-7:12]" --to "param"
* @expect-error true
*/
function f(param: number) {
const x = 1;
return x + param;
}
9 changes: 9 additions & 0 deletions tests/fixtures/commands/rename/parameter-conflict.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @description Test rename with parameter conflict
* @command refakts rename "[parameter-conflict.input.ts 7:11-7:12]" --to "param"
* @expect-error true
*/
function f(param: number) {
const x = 1;
return x + param;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Variable name 'y' already exists in this scope. Please choose a different name.
14 changes: 14 additions & 0 deletions tests/fixtures/commands/rename/parent-scope-conflict.expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @description Test rename with parent scope conflict
* @command refakts rename "[parent-scope-conflict.input.ts 7:11-7:12]" --to "y"
* @expect-error true
*/
function f() {
const x = 1;
{
const y = 2;
{
return x;
}
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/commands/rename/parent-scope-conflict.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @description Test rename with parent scope conflict
* @command refakts rename "[parent-scope-conflict.input.ts 7:11-7:12]" --to "y"
* @expect-error true
*/
function f() {
const x = 1;
{
const y = 2;
{
return x;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Variable name 'y' already exists in this scope. Please choose a different name.
10 changes: 10 additions & 0 deletions tests/fixtures/commands/rename/same-scope-conflict.expected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @description Test rename with same scope conflict
* @command refakts rename "[same-scope-conflict.input.ts 7:11-7:12]" --to "y"
* @expect-error true
*/
function f() {
const x = 1;
const y = 2;
return x + y;
}
10 changes: 10 additions & 0 deletions tests/fixtures/commands/rename/same-scope-conflict.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @description Test rename with same scope conflict
* @command refakts rename "[same-scope-conflict.input.ts 7:11-7:12]" --to "y"
* @expect-error true
*/
function f() {
const x = 1;
const y = 2;
return x + y;
}