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 isIdentifierChar(code: number): boolean;
|
||||||
declare function isIdentifierName(name: string): 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;
|
const { min } = Math;
|
||||||
|
|
||||||
// a minimal leven distance implementation
|
// a minimal leven distance implementation
|
||||||
@ -9,7 +7,7 @@ const { min } = Math;
|
|||||||
// that have less than 20 ASCII characters
|
// that have less than 20 ASCII characters
|
||||||
|
|
||||||
// https://rosettacode.org/wiki/Levenshtein_distance#ES5
|
// https://rosettacode.org/wiki/Levenshtein_distance#ES5
|
||||||
function levenshtein(a, b) {
|
function levenshtein(a: string, b: string): number {
|
||||||
let t = [],
|
let t = [],
|
||||||
u = [],
|
u = [],
|
||||||
i,
|
i,
|
||||||
@ -44,7 +42,7 @@ function levenshtein(a, b) {
|
|||||||
* @param {string[]} arr
|
* @param {string[]} arr
|
||||||
* @returns {string}
|
* @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));
|
const distances = arr.map<number>(el => levenshtein(el, str));
|
||||||
return arr[distances.indexOf(min(...distances))];
|
return arr[distances.indexOf(min(...distances))];
|
||||||
}
|
}
|
||||||
@ -1,4 +1,3 @@
|
|||||||
// @flow
|
|
||||||
import { findSuggestion } from "./find-suggestion.js";
|
import { findSuggestion } from "./find-suggestion.js";
|
||||||
|
|
||||||
export class OptionValidator {
|
export class OptionValidator {
|
||||||
@ -30,13 +29,13 @@ export class OptionValidator {
|
|||||||
|
|
||||||
// note: we do not consider rewrite them to high order functions
|
// note: we do not consider rewrite them to high order functions
|
||||||
// until we have to support `validateNumberOption`.
|
// until we have to support `validateNumberOption`.
|
||||||
validateBooleanOption(
|
validateBooleanOption<T>(
|
||||||
name: string,
|
name: string,
|
||||||
value?: boolean,
|
value?: boolean,
|
||||||
defaultValue?: boolean,
|
defaultValue?: T,
|
||||||
): boolean | void {
|
): boolean | T {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
value = defaultValue;
|
return defaultValue;
|
||||||
} else {
|
} else {
|
||||||
this.invariant(
|
this.invariant(
|
||||||
typeof value === "boolean",
|
typeof value === "boolean",
|
||||||
@ -46,13 +45,13 @@ export class OptionValidator {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
validateStringOption(
|
validateStringOption<T>(
|
||||||
name: string,
|
name: string,
|
||||||
value?: string,
|
value?: string,
|
||||||
defaultValue?: string,
|
defaultValue?: T,
|
||||||
): string | void {
|
): string | T {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
value = defaultValue;
|
return defaultValue;
|
||||||
} else {
|
} else {
|
||||||
this.invariant(
|
this.invariant(
|
||||||
typeof value === "string",
|
typeof value === "string",
|
||||||
Loading…
x
Reference in New Issue
Block a user