refactor: migrate checks.ts utility to core and deduplicate (#18139)

This commit is contained in:
Jerop Kipruto
2026-02-02 19:32:13 -05:00
committed by GitHub
parent c159c85c89
commit 09beb648b8
11 changed files with 17 additions and 18 deletions

View File

@@ -1,32 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { checkExhaustive, assumeExhaustive } from './checks.js';
describe('checks', () => {
describe('checkExhaustive', () => {
it('should throw an error with default message', () => {
expect(() => {
checkExhaustive('unexpected' as never);
}).toThrow('unexpected value unexpected!');
});
it('should throw an error with custom message', () => {
expect(() => {
checkExhaustive('unexpected' as never, 'custom message');
}).toThrow('custom message');
});
});
describe('assumeExhaustive', () => {
it('should do nothing', () => {
expect(() => {
assumeExhaustive('unexpected' as never);
}).not.toThrow();
});
});
});

View File

@@ -1,28 +0,0 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/* Fail to compile on unexpected values. */
export function assumeExhaustive(_value: never): void {}
/**
* Throws an exception on unexpected values.
*
* A common use case is switch statements:
* switch(enumValue) {
* case Enum.A:
* case Enum.B:
* break;
* default:
* checkExhaustive(enumValue);
* }
*/
export function checkExhaustive(
value: never,
msg = `unexpected value ${value}!`,
): never {
assumeExhaustive(value);
throw new Error(msg);
}

View File

@@ -4,20 +4,18 @@
* SPDX-License-Identifier: Apache-2.0
*/
import type {
Config,
ConversationRecord,
MessageRecord,
} from '@google/gemini-cli-core';
import {
checkExhaustive,
partListUnionToString,
SESSION_FILE_PREFIX,
type Config,
type ConversationRecord,
type MessageRecord,
} from '@google/gemini-cli-core';
import * as fs from 'node:fs/promises';
import path from 'node:path';
import { stripUnsafeCharacters } from '../ui/utils/textUtils.js';
import type { Part } from '@google/genai';
import { checkExhaustive } from './checks.js';
import {
MessageType,
ToolCallStatus,