Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow rename of lwc component inside of __tests__ directory #4225

Merged
merged 18 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b3d798a
fix: allow lwc component rename from inside __tests__ directory
Jun 21, 2022
de82062
fix: expand when clause, handle __test__ directory in path
Jun 21, 2022
51b544f
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jun 22, 2022
6576044
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jun 23, 2022
1226ba2
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jun 24, 2022
8143f14
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jun 27, 2022
8e10d5a
chore: update based on PR comments
Jun 28, 2022
07eb4f9
Merge branch 'dehru/fix-rename-component-test' of https://github.com/…
Jun 28, 2022
0c451c5
Merge branch 'develop' into dehru/fix-rename-component-test
gbockus-sf Jun 29, 2022
c574169
feat: refactor how we find the component directory
Jun 30, 2022
b25ab41
Merge branch 'dehru/fix-rename-component-test' of https://github.com/…
Jun 30, 2022
deb7dbc
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jun 30, 2022
bd7a94a
chore: renable tests
Jun 30, 2022
edd9d87
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jul 7, 2022
7f1b926
chore: update message after input from doc
Jul 8, 2022
f000b36
Merge branch 'dehru/fix-rename-component-test' of https://github.com/…
Jul 8, 2022
5ac0d5a
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jul 8, 2022
71e9e10
Merge branch 'develop' into dehru/fix-rename-component-test
dehru Jul 11, 2022
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
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
},
{
"command": "sfdx.lightning.rename",
"when": "sfdx:project_opened && resource =~ /.*/(lwc|aura)/[^/]+(/[^/]+\\.(html|css|js|xml|svg|cmp|app|design|auradoc))?$/"
"when": "sfdx:project_opened && resource =~ /.*/(lwc|aura)/.*(/[^/]+\\.(html|css|js|xml|svg|cmp|app|design|auradoc))?$/"
Copy link
Contributor Author

@dehru dehru Jun 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, show the command to the user on anything inside the /lwc/ and /aura/ directories that have these file extensions. This includes the component subdirectory named __tests__ and files inside there.

If the user comes up with their own folder structure with a folder named lwc, this should still work to resolve the parent component directory.

}
],
"commandPalette": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const RENAME_INPUT_PLACEHOLDER = 'rename_component_input_placeholder';
const RENAME_INPUT_PROMPT = 'rename_component_input_prompt';
const RENAME_INPUT_DUP_ERROR = 'rename_component_input_dup_error';
const RENAME_INPUT_DUP_FILE_NAME_ERROR = 'rename_component_input_dup_file_name_error';
const RENAME_ERROR = 'rename_component_error';
const RENAME_WARNING = 'rename_component_warning';
const LWC = 'lwc';
const AURA = 'aura';
Expand All @@ -44,8 +45,14 @@ export class RenameLwcComponentExecutor extends LibraryCommandletExecutor<Compon
let newComponentName = response.data.name?.trim();
if (newComponentName && this.sourceFsPath) {
newComponentName = await inputGuard(this.sourceFsPath, newComponentName);
await renameComponent(this.sourceFsPath, newComponentName);
return true;
try {
await renameComponent(this.sourceFsPath, newComponentName);
return true;
} catch (err) {
const errorMessage = nls.localize(RENAME_ERROR);
notificationService.showErrorMessage(errorMessage);
throw err;
}
}
return false;
}
Expand Down Expand Up @@ -132,9 +139,21 @@ async function renameComponent(sourceFsPath: string, newName: string) {
notificationService.showWarningMessage(nls.localize(RENAME_WARNING));
}

export function getLightningComponentDirectory(sourceFsPath: string): string {
const directories = sourceFsPath.split(path.sep);
const rootDir = directories.includes(LWC) ? LWC : AURA;
const lwcDirectoryIndex = directories.lastIndexOf(rootDir);
if (lwcDirectoryIndex > -1) {
directories.splice(lwcDirectoryIndex + 2);
}
return directories.join(path.sep);
}

async function getComponentPath(sourceFsPath: string): Promise<string> {
const stats = await fs.promises.stat(sourceFsPath);
return stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath;
let dirname = stats.isFile() ? path.dirname(sourceFsPath) : sourceFsPath;
dirname = getLightningComponentDirectory(dirname);
return dirname;
}

function getComponentName(componentPath: string): string {
Expand Down
2 changes: 2 additions & 0 deletions packages/salesforcedx-vscode-core/src/messages/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,8 @@ export const messages = {
'Press Enter to confirm your input or Escape to cancel',
rename_component_warning:
'Warning: References to the old name will not be updated. Update manually and redeploy once all changes have been made.',
rename_component_error:
'Unable to rename the component. Try renaming the component manually and then redeploying your changes.',
error_function_type: 'Unable to determine type of executing function.',
error_unable_to_get_started_function:
'Unable to access the function in "{0}".'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import {inputGuard, isNameMatch, RenameLwcComponentExecutor} from '../../../src/commands/forceRenameLightningComponent';
import {getLightningComponentDirectory, inputGuard, isNameMatch, RenameLwcComponentExecutor} from '../../../src/commands/forceRenameLightningComponent';
import { nls } from '../../../src/messages';

const RENAME_INPUT_DUP_ERROR = 'rename_component_input_dup_error';
Expand Down Expand Up @@ -399,5 +399,58 @@ describe('Force Rename Lightning Component', () => {
expect(exceptionThrownLwc).to.equal(true);
expect(exceptionThrownAura).to.equal(true);
});

});

describe('getLightningComponentDirectory function', () => {

it('works with simple component folder', () => {
const folders = ['src', 'main', 'default', 'lwc', 'cmp'];
const folderPath = folders.join(path.sep);
const parentDirectory = getLightningComponentDirectory(folderPath);
expect(parentDirectory).to.equal(folderPath);
});

it('works with __tests__ folder in the path', () => {
const folders = ['src', 'main', 'default', 'lwc', 'cmp', '__tests__'];
const folderPath = folders.join(path.sep);

const parentFolder = folders.slice(0, -1);
const parentFolderPath = parentFolder.join(path.sep);

const parentDirectory = getLightningComponentDirectory(folderPath);
expect(parentDirectory).to.equal(parentFolderPath);
});

it('works with child folder of __tests__ folder', () => {
const folders = ['lwc', 'cmp', '__tests__', 'data'];
const folderPath = folders.join(path.sep);

const parentFolder = folders.slice(0, -2);
const parentFolderPath = parentFolder.join(path.sep);

const parentDirectory = getLightningComponentDirectory(folderPath);
expect(parentDirectory).to.equal(parentFolderPath);
});

it('works with templates folder of component', () => {
const folders = ['lwc', 'cmp', 'templates'];
const folderPath = folders.join(path.sep);

const parentFolder = folders.slice(0, -1);
const parentFolderPath = parentFolder.join(path.sep);

const parentDirectory = getLightningComponentDirectory(folderPath);
expect(parentDirectory).to.equal(parentFolderPath);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffb-sfdc - I ended up with your first suggestion. I think this is a good balance. Thanks for the input.

});

it('works with nested lwc folder of component', () => {
const folders = ['src', 'main', 'default', 'lwc', 'other', 'lwc', 'cmp'];
const folderPath = folders.join(path.sep);

const parentDirectory = getLightningComponentDirectory(folderPath);
expect(parentDirectory).to.equal(folderPath);
});

});
});