refactor: move @babel/helper-validator-option to ts (#12410)
This commit is contained in:
parent
581aeb9a23
commit
73f30329a6
@ -133,3 +133,27 @@ declare module "@babel/helper-validator-identifier" {
|
||||
declare function isIdentifierChar(code: number): boolean;
|
||||
declare function isIdentifierName(name: string): boolean;
|
||||
}
|
||||
|
||||
declare module "@babel/helper-validator-option" {
|
||||
declare class OptionValidator {
|
||||
descriptor: string;
|
||||
constructor(descriptor: string): OptionValidator;
|
||||
validateTopLevelOptions(options: Object, TopLevelOptionShape: Object): void;
|
||||
validateBooleanOption<T>(
|
||||
name: string,
|
||||
value?: boolean,
|
||||
defaultValue?: T
|
||||
): boolean | T;
|
||||
validateStringOption<T>(
|
||||
name: string,
|
||||
value?: string,
|
||||
defaultValue?: T
|
||||
): string | T;
|
||||
invariant(condition: boolean, message: string): void;
|
||||
formatMessage(message: string): string;
|
||||
}
|
||||
declare function findSuggestion(
|
||||
str: string,
|
||||
arr: $ReadonlyArray<string>
|
||||
): string;
|
||||
}
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
// @flow
|
||||
|
||||
const { min } = Math;
|
||||
|
||||
// a minimal leven distance implementation
|
||||
@ -9,7 +7,7 @@ const { min } = Math;
|
||||
// that have less than 20 ASCII characters
|
||||
|
||||
// https://rosettacode.org/wiki/Levenshtein_distance#ES5
|
||||
function levenshtein(a, b) {
|
||||
function levenshtein(a: string, b: string): number {
|
||||
let t = [],
|
||||
u = [],
|
||||
i,
|
||||
@ -44,7 +42,7 @@ function levenshtein(a, b) {
|
||||
* @param {string[]} arr
|
||||
* @returns {string}
|
||||
*/
|
||||
export function findSuggestion(str: string, arr: string[]): string {
|
||||
export function findSuggestion(str: string, arr: readonly string[]): string {
|
||||
const distances = arr.map<number>(el => levenshtein(el, str));
|
||||
return arr[distances.indexOf(min(...distances))];
|
||||
}
|
||||
@ -1,4 +1,3 @@
|
||||
// @flow
|
||||
import { findSuggestion } from "./find-suggestion.js";
|
||||
|
||||
export class OptionValidator {
|
||||
@ -30,13 +29,13 @@ export class OptionValidator {
|
||||
|
||||
// note: we do not consider rewrite them to high order functions
|
||||
// until we have to support `validateNumberOption`.
|
||||
validateBooleanOption(
|
||||
validateBooleanOption<T>(
|
||||
name: string,
|
||||
value?: boolean,
|
||||
defaultValue?: boolean,
|
||||
): boolean | void {
|
||||
defaultValue?: T,
|
||||
): boolean | T {
|
||||
if (value === undefined) {
|
||||
value = defaultValue;
|
||||
return defaultValue;
|
||||
} else {
|
||||
this.invariant(
|
||||
typeof value === "boolean",
|
||||
@ -46,13 +45,13 @@ export class OptionValidator {
|
||||
return value;
|
||||
}
|
||||
|
||||
validateStringOption(
|
||||
validateStringOption<T>(
|
||||
name: string,
|
||||
value?: string,
|
||||
defaultValue?: string,
|
||||
): string | void {
|
||||
defaultValue?: T,
|
||||
): string | T {
|
||||
if (value === undefined) {
|
||||
value = defaultValue;
|
||||
return defaultValue;
|
||||
} else {
|
||||
this.invariant(
|
||||
typeof value === "string",
|
||||
Loading…
x
Reference in New Issue
Block a user