Skip to content

Avoid use of instanceof #1625

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

Merged
merged 2 commits into from
Jun 19, 2025
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
20 changes: 10 additions & 10 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
),
vscode.commands.registerCommand(
Commands.RESET_PACKAGE,
async () => await resetPackage(ctx)
async folder => await resetPackage(ctx, folder)
),
vscode.commands.registerCommand("swift.runScript", async () => await runSwiftScript(ctx)),
vscode.commands.registerCommand("swift.openPackage", async () => {
Expand Down Expand Up @@ -182,27 +182,27 @@ export function register(ctx: WorkspaceContext): vscode.Disposable[] {
async () => await insertFunctionComment(ctx)
),
vscode.commands.registerCommand(Commands.USE_LOCAL_DEPENDENCY, async (item, dep) => {
if (item instanceof PackageNode) {
if (PackageNode.isPackageNode(item)) {
return await useLocalDependency(item.name, ctx, dep);
}
}),
vscode.commands.registerCommand("swift.editDependency", async item => {
if (item instanceof PackageNode) {
return await editDependency(item.name, ctx);
vscode.commands.registerCommand("swift.editDependency", async (item, folder) => {
if (PackageNode.isPackageNode(item)) {
return await editDependency(item.name, ctx, folder);
}
}),
vscode.commands.registerCommand(Commands.UNEDIT_DEPENDENCY, async item => {
if (item instanceof PackageNode) {
return await uneditDependency(item.name, ctx);
vscode.commands.registerCommand(Commands.UNEDIT_DEPENDENCY, async (item, folder) => {
if (PackageNode.isPackageNode(item)) {
return await uneditDependency(item.name, ctx, folder);
}
}),
vscode.commands.registerCommand("swift.openInWorkspace", async item => {
if (item instanceof PackageNode) {
if (PackageNode.isPackageNode(item)) {
return await openInWorkspace(item);
}
}),
vscode.commands.registerCommand("swift.openExternal", item => {
if (item instanceof PackageNode) {
if (PackageNode.isPackageNode(item)) {
return openInExternalEditor(item);
}
}),
Expand Down
9 changes: 7 additions & 2 deletions src/commands/dependencies/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ import { createSwiftTask } from "../../tasks/SwiftTaskProvider";
import { FolderOperation, WorkspaceContext } from "../../WorkspaceContext";
import { executeTaskWithUI } from "../utilities";
import { packageName } from "../../utilities/tasks";
import { FolderContext } from "../../FolderContext";

/**
* Setup package dependency to be edited
* @param identifier Identifier of dependency we want to edit
* @param ctx workspace context
*/
export async function editDependency(identifier: string, ctx: WorkspaceContext) {
const currentFolder = ctx.currentFolder;
export async function editDependency(
identifier: string,
ctx: WorkspaceContext,
folder: FolderContext | undefined
) {
const currentFolder = folder ?? ctx.currentFolder;
if (!currentFolder) {
return;
}
Expand Down
8 changes: 6 additions & 2 deletions src/commands/dependencies/unedit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ import { FolderContext } from "../../FolderContext";
* @param identifier Identifier of dependency
* @param ctx workspace context
*/
export async function uneditDependency(identifier: string, ctx: WorkspaceContext) {
const currentFolder = ctx.currentFolder;
export async function uneditDependency(
identifier: string,
ctx: WorkspaceContext,
folder: FolderContext | undefined
) {
const currentFolder = folder ?? ctx.currentFolder;
if (!currentFolder) {
ctx.outputChannel.log("currentFolder is not set.");
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/resetPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { packageName } from "../utilities/tasks";
/**
* Executes a {@link vscode.Task task} to reset the complete cache/build directory.
*/
export async function resetPackage(ctx: WorkspaceContext) {
const current = ctx.currentFolder;
export async function resetPackage(ctx: WorkspaceContext, folder: FolderContext | undefined) {
const current = folder ?? ctx.currentFolder;
if (!current) {
return;
}
Expand Down
18 changes: 18 additions & 0 deletions src/ui/ProjectPanelProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ async function getChildren(directoryPath: string, parentId?: string): Promise<Fi
export class PackageNode {
private id: string;

/**
* "instanceof" has a bad effect in our nightly tests when the VSIX
* bundled source is used. For example:
*
* ```
* vscode.commands.registerCommand(Commands.UNEDIT_DEPENDENCY, async (item, folder) => {
* if (item instanceof PackageNode) {
* return await uneditDependency(item.name, ctx, folder);
* }
* }),
* ```
*
* So instead we'll check for this set boolean property. Even if the implementation of the
* {@link PackageNode} class changes, this property should not need to change
*/
static isPackageNode = (item: { __isPackageNode?: boolean }) => item.__isPackageNode ?? false;
__isPackageNode = true;

constructor(
private dependency: ResolvedDependency,
private childDependencies: (dependency: Dependency) => ResolvedDependency[],
Expand Down
21 changes: 13 additions & 8 deletions test/integration-tests/commands/dependency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import { FolderContext } from "../../../src/FolderContext";
import { WorkspaceContext } from "../../../src/WorkspaceContext";
import { Commands } from "../../../src/commands";
import { activateExtensionForSuite, findWorkspaceFolder } from "../utilities/testutilities";
import { executeTaskAndWaitForResult, waitForNoRunningTasks } from "../../utilities/tasks";
import { createBuildAllTask } from "../../../src/tasks/SwiftTaskProvider";
import { waitForNoRunningTasks } from "../../utilities/tasks";

suite("Dependency Commmands Test Suite @slow", function () {
// full workflow's interaction with spm is longer than the default timeout
Expand Down Expand Up @@ -59,7 +58,6 @@ suite("Dependency Commmands Test Suite @slow", function () {
setup(async () => {
await waitForNoRunningTasks();
treeProvider = new ProjectPanelProvider(workspaceContext);
await executeTaskAndWaitForResult(await createBuildAllTask(depsContext));
});

teardown(() => {
Expand All @@ -69,7 +67,9 @@ suite("Dependency Commmands Test Suite @slow", function () {
async function getDependency() {
const headers = await treeProvider.getChildren();
const header = headers.find(n => n.name === "Dependencies") as PackageNode;
expect(header).to.not.be.undefined;
if (!header) {
return;
}
const children = await header.getChildren();
return children.find(
n => n.name.toLocaleLowerCase() === "swift-markdown"
Expand All @@ -83,7 +83,7 @@ suite("Dependency Commmands Test Suite @slow", function () {
async function getDependencyInState(state: "remote" | "editing") {
for (let i = 0; i < 10; i++) {
const dep = await getDependency();
if (dep.type === state) {
if (dep?.type === state) {
return dep;
}
await new Promise(resolve => setTimeout(resolve, 1000));
Expand All @@ -98,7 +98,8 @@ suite("Dependency Commmands Test Suite @slow", function () {
const result = await vscode.commands.executeCommand(
Commands.USE_LOCAL_DEPENDENCY,
item,
localDep
localDep,
depsContext
);
expect(result).to.be.true;

Expand All @@ -112,7 +113,10 @@ suite("Dependency Commmands Test Suite @slow", function () {
await useLocalDependencyTest();

// spm reset
const result = await vscode.commands.executeCommand(Commands.RESET_PACKAGE);
const result = await vscode.commands.executeCommand(
Commands.RESET_PACKAGE,
depsContext
);
expect(result).to.be.true;

const dep = await getDependencyInState("remote");
Expand All @@ -125,7 +129,8 @@ suite("Dependency Commmands Test Suite @slow", function () {

const result = await vscode.commands.executeCommand(
Commands.UNEDIT_DEPENDENCY,
await getDependency()
await getDependencyInState("editing"),
depsContext
);
expect(result).to.be.true;

Expand Down
Loading