UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

86 lines (84 loc) 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const ArgumentError_1 = require("./ArgumentError"); const ArgumentTypeError_1 = require("./ArgumentTypeError"); const ArgumentNullOrWhitespaceError_1 = require("./ArgumentNullOrWhitespaceError"); const ArgumentOutOfRangeError_1 = require("./ArgumentOutOfRangeError"); const InvalidOperationError_1 = require("./InvalidOperationError"); const NotImplementedError_1 = require("./NotImplementedError"); /** * Thows if not a type. * @param value - Value to check the type of. * @param expectedType - Expected type. * @param argName - Argument name. */ function throwIfNotType(value, expectedType, argName) { if (typeof value !== expectedType) throw new ArgumentTypeError_1.ArgumentTypeError(argName, expectedType, typeof value); } exports.throwIfNotType = throwIfNotType; /** * Throws if the value is not a string or is whitespace. * @param value - Value to check. * @param argName - Arg name. */ function throwIfNotStringOrWhitespace(value, argName) { if (typeof value !== "string") throw new ArgumentTypeError_1.ArgumentTypeError(argName, "string", typeof value); if (value.trim().length === 0) throw new ArgumentNullOrWhitespaceError_1.ArgumentNullOrWhitespaceError(argName); } exports.throwIfNotStringOrWhitespace = throwIfNotStringOrWhitespace; /** * Throws a NotImplementedError if a node doesn't match the expected syntax kind. * @param node - Node. * @param syntaxKind - Syntax kind that's expected. * @param message - Optional message to throw. */ function throwIfNotSyntaxKind(node, syntaxKind, message) { if (node.getKind() !== syntaxKind) throw new NotImplementedError_1.NotImplementedError(message || `Expected node to be syntax kind ${ts.SyntaxKind[syntaxKind]}, but was ${node.getKindName()}.`); } exports.throwIfNotSyntaxKind = throwIfNotSyntaxKind; /** * Throws an ArgumentOutOfRangeError if an argument's value is out of an inclusive range. * @param value - Value. * @param range - Range. * @param argName - Argument name. */ function throwIfOutOfRange(value, range, argName) { if (value < range[0] || value > range[1]) throw new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError(argName, value, range); } exports.throwIfOutOfRange = throwIfOutOfRange; /** * Gets an error saying that a feature is not implemented for a certain syntax kind. * @param syntaxKind - Syntax kind that isn't implemented. */ function getNotImplementedForSyntaxKindError(syntaxKind) { return new NotImplementedError_1.NotImplementedError(`Not implemented feature for syntax kind '${ts.SyntaxKind[syntaxKind]}'.`); } exports.getNotImplementedForSyntaxKindError = getNotImplementedForSyntaxKindError; /** * Throws an Argument * @param value * @param argName */ function throwIfNegative(value, argName) { if (value < 0) throw new ArgumentError_1.ArgumentError(argName, "Expected a non-negative value."); } exports.throwIfNegative = throwIfNegative; /** * Throws when the value is null or undefined * @param value - Value to check. * @param errorMessage - Error message to throw when not defined. */ function throwIfNullOrUndefined(value, errorMessage) { if (value == null) throw new InvalidOperationError_1.InvalidOperationError(typeof errorMessage === "string" ? errorMessage : errorMessage()); return value; } exports.throwIfNullOrUndefined = throwIfNullOrUndefined; //# sourceMappingURL=helpers.js.map