mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-24 12:04:56 -07:00
refactor: migrate checks.ts utility to core and deduplicate (#18139)
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* @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();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
Reference in New Issue
Block a user