Simplify some SerializeCLITarget types to strings instead of objects
MODIFY
internal/cli-diagnostics/printAdvice.ts
internal/cli-diagnostics/printAdvice.ts
@@ -168,7 +168,7 @@ function printAction(
...item.requestFlags,
},
},
- {type: "none"},
+ "none",
).sourceText;
opts.reporter.command(command);
return DID_PRINT;

MODIFY
internal/cli-flags/Parser.ts
internal/cli-flags/Parser.ts
@@ -1036,9 +1036,7 @@ export default class Parser<T> {
defaultFlags,
flags: rawFlags,
},
- {
- type: "none",
- },
+ "none",
).sourceText,
);
break;
@@ -1056,9 +1054,7 @@ export default class Parser<T> {
defaultFlags,
flags: rawFlags,
},
- {
- type: "command",
- },
+ "command",
),
};

MODIFY
internal/cli-flags/serializeCLIFlags.ts
internal/cli-flags/serializeCLIFlags.ts
@@ -27,7 +27,7 @@ export type SerializeCLIOptions = {
prefix?: string;
};
-export type SerializeCLITarget =
+type SerializeCLITargetObjects =
| {
type: "flag";
key: string;
@@ -41,20 +41,21 @@ export type SerializeCLITarget =
type: "arg-range";
from: number;
to?: number;
- }
- | {
- type: "none";
- }
- | {
- type: "command";
- }
- | {
- type: "program";
- }
- | {
- type: "cwd";
};
+export type SerializeCLITarget =
+ | SerializeCLITargetObjects
+ | "none"
+ | "command"
+ | "program"
+ | "cwd";
+
+function isObjectTarget(
+ target: SerializeCLITarget,
+): target is SerializeCLITargetObjects {
+ return typeof target !== "string";
+}
+
export function serializeCLIFlags(
{
args,
@@ -100,16 +101,16 @@ export function serializeCLIFlags(
}
// Only output cwd if it's the target
- if (cwd !== undefined && target.type === "cwd") {
+ if (cwd !== undefined && target === "cwd") {
push(cwd.join(), true);
}
code += prefix;
- push(`${programName} `, target.type === "program");
+ push(`${programName} `, target === "program");
if (commandName !== undefined) {
- push(`${commandName} `, target.type === "command");
+ push(`${commandName} `, target === "command");
}
// Add args
@@ -117,10 +118,14 @@ export function serializeCLIFlags(
const arg = args[i];
let isTarget = false;
- if (target.type === "arg" && i === target.key) {
+ if (isObjectTarget(target) && target.type === "arg" && i === target.key) {
isTarget = true;
}
- if (target.type === "arg-range" && target.from === i) {
+ if (
+ isObjectTarget(target) &&
+ target.type === "arg-range" &&
+ target.from === i
+ ) {
isTarget = true;
}
@@ -134,6 +139,7 @@ export function serializeCLIFlags(
// We are the end target if we're within the from-to range or we're greater than from with no to
if (
+ isObjectTarget(target) &&
target.type === "arg-range" &&
i > target.from &&
(target.to === undefined || target.to <= i)
@@ -157,7 +163,8 @@ export function serializeCLIFlags(
const values = Array.isArray(val) ? val : [val];
- const isTarget = target.type === "flag" && key === target.key;
+ const isTarget =
+ isObjectTarget(target) && target.type === "flag" && key === target.key;
if (isTarget) {
setStartColumn();
@@ -177,6 +184,7 @@ export function serializeCLIFlags(
// Only point to the value for flags that specify it
if (
isTarget &&
+ isObjectTarget(target) &&
target.type === "flag" &&
(target.target === "value" || target.target === "inner-value")
) {
@@ -210,7 +218,7 @@ export function serializeCLIFlags(
index: endColumn,
};
- if (target.type === "none") {
+ if (target === "none") {
start = undefined;
end = undefined;
}

MODIFY
internal/core/server/ServerRequest.ts
internal/core/server/ServerRequest.ts
@@ -553,7 +553,7 @@ export default class ServerRequest {
throwDiagnosticFlagError(
{
description,
- target = {type: "none"},
+ target = "none",
showHelp = true,
}: {
description: RequiredProps<Partial<DiagnosticDescription>, "message">;
@@ -566,7 +566,8 @@ export default class ServerRequest {
let {category} = description;
if (category === undefined) {
category =
- target.type === "arg" || target.type === "arg-range"
+ typeof target !== "string" &&
+ (target.type === "arg" || target.type === "arg-range")
? "args/invalid"
: "flags/invalid";
}

MODIFY
internal/core/server/commands/init.ts
internal/core/server/commands/init.ts
@@ -44,7 +44,7 @@ export default createServerCommand<Flags>({
if (server.projectManager.isBannedProjectPath(cwd)) {
const diagnostic: Diagnostic = {
description: descriptions.PROJECT_MANAGER.INITING_SENSITIVE(cwd),
- location: req.getDiagnosticLocationFromFlags({type: "cwd"}),
+ location: req.getDiagnosticLocationFromFlags("cwd"),
};
throw createSingleDiagnosticError(diagnostic);
}
@@ -55,14 +55,14 @@ export default createServerCommand<Flags>({
if (vcsClient === undefined) {
throw createSingleDiagnosticError({
- location: req.getDiagnosticLocationFromFlags({type: "cwd"}),
+ location: req.getDiagnosticLocationFromFlags("cwd"),
description: descriptions.INIT_COMMAND.EXPECTED_REPO,
});
} else {
const uncommittedFiles = await vcsClient.getUncommittedFiles();
if (uncommittedFiles.length > 0) {
throw createSingleDiagnosticError({
- location: req.getDiagnosticLocationFromFlags({type: "cwd"}),
+ location: req.getDiagnosticLocationFromFlags("cwd"),
description: descriptions.INIT_COMMAND.UNCOMMITTED_CHANGES,
});
}

MODIFY
internal/core/server/fs/RecoveryStore.ts
internal/core/server/fs/RecoveryStore.ts
@@ -258,7 +258,7 @@ export default class RecoveryStore {
await this.truncate();
const timestamp = new Date().toISOString();
const storeId = `${Date.now()}-${req.query.commandName}-${req.id}`;
- const command = req.getDiagnosticLocationFromFlags({type: "none"}, "").sourceText;
+ const command = req.getDiagnosticLocationFromFlags("none", "").sourceText;
const store: MemoryStore = {
index: {
timestamp,

Simplify some SerializeCLITarget types to strings instead of objects (#916)