Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

typescript-is

Package Overview
Dependencies
Maintainers
1
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-is - npm Package Compare versions

Comparing version 0.16.3 to 0.17.0

8

index.d.ts

@@ -168,2 +168,3 @@ /**

export class TypeGuardError extends Error {
public constructor(errorObject: { message: string, path: string[], reason: Reason }, inputObject: unknown);
public readonly path: string[];

@@ -174,2 +175,6 @@ public readonly reason: Reason;

interface ExpectedFunction {
type: 'function';
}
interface ExpectedString {

@@ -258,3 +263,4 @@ type: 'string';

type Reason = ExpectedString
type Reason = ExpectedFunction
| ExpectedString
| ExpectedNumber

@@ -261,0 +267,0 @@ | ExpectedBigInt

@@ -62,2 +62,5 @@ let defaultGetErrorObject = undefined;

for (let i = 0; i < assertions.length; i++) {
if (!assertions[i]) {
continue;
}
const errorObject = assertions[i].assertion(args[i]);

@@ -64,0 +67,0 @@ if (errorObject !== null) {

@@ -6,2 +6,17 @@ "use strict";

const transform_node_1 = require("./transform-node");
function getFunctionBehavior(options) {
if (options) {
if (options.functionBehavior) {
if (options.functionBehavior === 'ignore' || options.functionBehavior === 'basic') {
return options.functionBehavior;
}
}
else {
if (!!options.ignoreFunctions) {
return 'ignore';
}
}
}
return 'error';
}
function transformer(program, options) {

@@ -19,3 +34,3 @@ if (options && options.verbose) {

ignoreMethods: !!(options && options.ignoreMethods),
ignoreFunctions: !!(options && options.ignoreFunctions),
functionBehavior: getFunctionBehavior(options),
disallowSuperfluousObjectProperties: !!(options && options.disallowSuperfluousObjectProperties)

@@ -22,0 +37,0 @@ },

2

lib/transform-inline/visitor-context.d.ts

@@ -7,3 +7,3 @@ import * as ts from 'typescript';

ignoreMethods: boolean;
ignoreFunctions: boolean;
functionBehavior: 'error' | 'ignore' | 'basic';
disallowSuperfluousObjectProperties: boolean;

@@ -10,0 +10,0 @@ }

@@ -115,3 +115,7 @@ "use strict";

? VisitorUtils.getIgnoredTypeFunction(visitorContext)
: visitType(propertyInfo.type, visitorContext);
: (propertyInfo.isFunction
? (visitorContext.options.functionBehavior === 'basic'
? VisitorUtils.getFunctionFunction(visitorContext)
: VisitorUtils.getIgnoredTypeFunction(visitorContext))
: visitType(propertyInfo.type, visitorContext));
return ts.createBlock([

@@ -149,2 +153,12 @@ ts.createIf(ts.createBinary(ts.createStringLiteral(propertyInfo.name), ts.SyntaxKind.InKeyword, VisitorUtils.objectIdentifier), ts.createBlock([

}
function visitTypeAliasReference(type, visitorContext) {
const mapping = VisitorUtils.getTypeAliasMapping(type);
const previousTypeReference = visitorContext.previousTypeReference;
visitorContext.typeMapperStack.push(mapping);
visitorContext.previousTypeReference = type;
const result = visitType(type, visitorContext);
visitorContext.previousTypeReference = previousTypeReference;
visitorContext.typeMapperStack.pop();
return result;
}
function visitTypeReference(type, visitorContext) {

@@ -167,2 +181,13 @@ const mapping = VisitorUtils.getTypeReferenceMapping(type, visitorContext);

}
function visitFunctionType(type, visitorContext) {
if (visitorContext.options.functionBehavior === 'error') {
throw new Error('Encountered a function declaration, but functions are not supported. Issue: https://github.com/woutervh-/typescript-is/issues/50');
}
else if (visitorContext.options.functionBehavior === 'basic') {
return VisitorUtils.getFunctionFunction(visitorContext);
}
else {
return VisitorUtils.getIgnoredTypeFunction(visitorContext);
}
}
function visitObjectType(type, visitorContext) {

@@ -192,5 +217,8 @@ if (VisitorUtils.checkIsClass(type, visitorContext)) {

&& (type.symbol.valueDeclaration.kind === ts.SyntaxKind.MethodDeclaration || type.symbol.valueDeclaration.kind === ts.SyntaxKind.FunctionType)) {
if (visitorContext.options.ignoreFunctions) {
if (visitorContext.options.functionBehavior === 'ignore') {
return VisitorUtils.getIgnoredTypeFunction(visitorContext);
}
else if (visitorContext.options.functionBehavior === 'basic') {
return VisitorUtils.getFunctionFunction(visitorContext);
}
else {

@@ -200,2 +228,5 @@ throw new Error('Encountered a function declaration, but functions are not supported. Issue: https://github.com/woutervh-/typescript-is/issues/50');

}
else if (type.symbol && type.symbol.declarations && type.symbol.declarations.length >= 1 && ts.isFunctionTypeNode(type.symbol.declarations[0])) {
return visitFunctionType(type, visitorContext);
}
else {

@@ -332,3 +363,6 @@ // Index type is string -> regular object type.

function visitType(type, visitorContext) {
if ((ts.TypeFlags.Any & type.flags) !== 0) {
if (type.aliasTypeArguments && visitorContext.previousTypeReference !== type && type.target) {
return visitTypeAliasReference(type, visitorContext);
}
else if ((ts.TypeFlags.Any & type.flags) !== 0) {
// Any

@@ -335,0 +369,0 @@ return visitAny(visitorContext);

@@ -13,2 +13,3 @@ import * as ts from 'typescript';

isMethod: boolean;
isFunction: boolean;
isSymbol: boolean;

@@ -18,4 +19,6 @@ optional: boolean;

export declare function getPropertyInfo(parentType: ts.Type, symbol: ts.Symbol, visitorContext: VisitorContext): PropertyInfo;
export declare function getTypeAliasMapping(type: ts.TypeReference): Map<ts.Type, ts.Type>;
export declare function getTypeReferenceMapping(type: ts.TypeReference, visitorContext: VisitorContext): Map<ts.Type, ts.Type>;
export declare function getResolvedTypeParameter(type: ts.Type, visitorContext: VisitorContext): ts.Type | undefined;
export declare function getFunctionFunction(visitorContext: VisitorContext): string;
export declare function getStringFunction(visitorContext: VisitorContext): string;

@@ -22,0 +25,0 @@ export declare function getBooleanFunction(visitorContext: VisitorContext): string;

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createErrorObject = exports.isBigIntType = exports.createSuperfluousPropertiesLoop = exports.createAssertionFunction = exports.createStrictNullCheckStatement = exports.createDisjunctionFunction = exports.createConjunctionFunction = exports.createAcceptingFunction = exports.createBinaries = exports.getIgnoredTypeFunction = exports.getAnyFunction = exports.getUnknownFunction = exports.getNeverFunction = exports.getNullFunction = exports.getUndefinedFunction = exports.getNumberFunction = exports.getBigIntFunction = exports.getBooleanFunction = exports.getStringFunction = exports.getResolvedTypeParameter = exports.getTypeReferenceMapping = exports.getPropertyInfo = exports.setFunctionIfNotExists = exports.checkIsDateClass = exports.checkIsClass = exports.pathIdentifier = exports.objectIdentifier = void 0;
exports.createErrorObject = exports.isBigIntType = exports.createSuperfluousPropertiesLoop = exports.createAssertionFunction = exports.createStrictNullCheckStatement = exports.createDisjunctionFunction = exports.createConjunctionFunction = exports.createAcceptingFunction = exports.createBinaries = exports.getIgnoredTypeFunction = exports.getAnyFunction = exports.getUnknownFunction = exports.getNeverFunction = exports.getNullFunction = exports.getUndefinedFunction = exports.getNumberFunction = exports.getBigIntFunction = exports.getBooleanFunction = exports.getStringFunction = exports.getFunctionFunction = exports.getResolvedTypeParameter = exports.getTypeReferenceMapping = exports.getTypeAliasMapping = exports.getPropertyInfo = exports.setFunctionIfNotExists = exports.checkIsDateClass = exports.checkIsClass = exports.pathIdentifier = exports.objectIdentifier = void 0;
const ts = require("typescript");

@@ -50,2 +50,3 @@ const typescript_1 = require("typescript");

let isMethod = undefined;
let isFunction = undefined;
let optional = undefined;

@@ -59,7 +60,7 @@ if ('valueDeclaration' in symbol) {

isMethod = ts.isMethodSignature(valueDeclaration);
const isFunction = valueDeclaration.type !== undefined && ts.isFunctionTypeNode(valueDeclaration.type);
isFunction = valueDeclaration.type !== undefined && ts.isFunctionTypeNode(valueDeclaration.type);
if (isMethod && !visitorContext.options.ignoreMethods) {
throw new Error('Encountered a method declaration, but methods are not supported. Issue: https://github.com/woutervh-/typescript-is/issues/5');
}
if (isFunction && !visitorContext.options.ignoreFunctions) {
if (isFunction && visitorContext.options.functionBehavior === 'error') {
throw new Error('Encountered a function declaration, but functions are not supported. Issue: https://github.com/woutervh-/typescript-is/issues/50');

@@ -81,2 +82,3 @@ }

isMethod = false;
isFunction = false;
optional = (symbol.flags & ts.SymbolFlags.Optional) !== 0;

@@ -88,5 +90,6 @@ }

isMethod = false;
isFunction = false;
optional = (symbol.flags & ts.SymbolFlags.Optional) !== 0;
}
if (optional !== undefined && isMethod !== undefined) {
if (optional !== undefined && isMethod !== undefined && isFunction !== undefined) {
return {

@@ -96,2 +99,3 @@ name,

isMethod,
isFunction,
isSymbol: name.startsWith('__@'),

@@ -104,2 +108,16 @@ optional

exports.getPropertyInfo = getPropertyInfo;
function getTypeAliasMapping(type) {
const mapping = new Map();
if (type.aliasTypeArguments !== undefined && type.target.aliasTypeArguments !== undefined) {
const typeParameters = type.target.aliasTypeArguments;
const typeArguments = type.aliasTypeArguments;
for (let i = 0; i < typeParameters.length; i++) {
if (typeParameters[i] !== typeArguments[i]) {
mapping.set(typeParameters[i], typeArguments[i]);
}
}
}
return mapping;
}
exports.getTypeAliasMapping = getTypeAliasMapping;
function getTypeReferenceMapping(type, visitorContext) {

@@ -147,2 +165,9 @@ const mapping = new Map();

exports.getResolvedTypeParameter = getResolvedTypeParameter;
function getFunctionFunction(visitorContext) {
const name = '_function';
return setFunctionIfNotExists(name, visitorContext, () => {
return createAssertionFunction(ts.createStrictInequality(ts.createTypeOf(exports.objectIdentifier), ts.createStringLiteral('function')), { type: 'function' }, name, createStrictNullCheckStatement(exports.objectIdentifier, visitorContext));
});
}
exports.getFunctionFunction = getFunctionFunction;
function getStringFunction(visitorContext) {

@@ -186,2 +211,8 @@ const name = '_string';

return setFunctionIfNotExists(name, visitorContext, () => {
const strictNullChecks = visitorContext.compilerOptions.strictNullChecks !== undefined
? visitorContext.compilerOptions.strictNullChecks
: !!visitorContext.compilerOptions.strict;
if (!strictNullChecks) {
return createAcceptingFunction(name);
}
return createAssertionFunction(ts.createStrictInequality(exports.objectIdentifier, ts.createNull()), { type: 'null' }, name, createStrictNullCheckStatement(exports.objectIdentifier, visitorContext));

@@ -411,4 +442,6 @@ });

return createAssertionString('expected a Date');
case 'function':
return createAssertionString('expected a function');
}
}
//# sourceMappingURL=visitor-utils.js.map
{
"name": "typescript-is",
"version": "0.16.3",
"version": "0.17.0",
"engines": {

@@ -5,0 +5,0 @@ "node": ">=6.14.4"

@@ -21,3 +21,3 @@ # typescript-is

# If you want to use the decorators, ensure you have reflect-metadata in your depdendencies:
# If you want to use the decorators, ensure you have reflect-metadata in your dependencies:
npm install --save reflect-metadata

@@ -96,2 +96,4 @@ ```

Note: This will not work if `ts-loader` is configured with `transpileOnly: true`.
## Using with `webpack + ts-loader` without `ttypescript`

@@ -137,3 +139,4 @@

| `ignoreMethods` | Boolean (default: `false`). If `true`, when the transformer encounters a method, it will ignore it and simply return `true`. If `false`, an error is generated at compile time. |
| `ignoreFunctions` | Boolean (default: `false`). If `true`, when the transformer encounters a function, it will ignore it and simply return `true`. If `false`, an error is generated at compile time. |
| `ignoreFunctions` *(deprecated, use `functionBehavior` instead)* | Boolean (default: `false`). If `true`, when the transformer encounters a function, it will ignore it and simply return `true`. If `false`, an error is generated at compile time. |
| `functionBehavior` | One of `error`, `ignore`, or `basic` (default: `error`). Determines the behavior of transformer when encountering a function. `error` will cause a compile-time error, `ignore` will cause the validation function to always return `true`, and `basic` will do a simple function-type-check. Overrides `ignoreFunctions`. |
| `disallowSuperfluousObjectProperties` | Boolean (default: `false`). If `true`, objects are checked for having superfluous properties and will cause the validation to fail if they do. If `false`, no check for superfluous properties is made. |

@@ -152,3 +155,3 @@

"ignoreMethods": true,
"ignoreFunctions": true,
"functionBehavior": "ignore",
"disallowSuperfluousObjectProperties": true

@@ -155,0 +158,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc