How to use createVoidZero method in ts-auto-mock

Best JavaScript code snippet using ts-auto-mock

serializeType.ts

Source:serializeType.ts Github

copy

Full Screen

1import { types as t } from '@babel/core';2import { NodePath } from '@babel/traverse';3type InferArray<T> = T extends Array<infer A> ? A : never;4type Parameter = InferArray<t.ClassMethod['params']> | t.ClassProperty;5function createVoidZero() {6 return t.unaryExpression('void', t.numericLiteral(0));7}8/**9 * Given a paramater (or class property) node it returns the first identifier10 * containing the TS Type Annotation.11 *12 * @todo Array and Objects spread are not supported.13 * @todo Rest parameters are not supported.14 */15function getTypedNode(param: Parameter): t.Identifier | t.ClassProperty | t.ObjectPattern | null {16 if (param == null) return null;17 if (param.type === 'ClassProperty') return param;18 if (param.type === 'Identifier') return param;19 if (param.type === 'ObjectPattern') return param;20 if (param.type === 'AssignmentPattern' && param.left.type === 'Identifier')21 return param.left;22 if (param.type === 'TSParameterProperty')23 return getTypedNode(param.parameter);24 return null;25}26export function serializeType(27 classPath: NodePath<t.ClassDeclaration>,28 param: Parameter29) {30 const node = getTypedNode(param);31 if (node == null) return createVoidZero();32 if (!node.typeAnnotation || node.typeAnnotation.type !== 'TSTypeAnnotation')33 return createVoidZero();34 const annotation = node.typeAnnotation.typeAnnotation;35 const className = classPath.node.id ? classPath.node.id.name : '';36 return serializeTypeNode(className, annotation);37}38function serializeTypeReferenceNode(39 className: string,40 node: t.TSTypeReference41) {42 /**43 * We need to save references to this type since it is going44 * to be used as a Value (and not just as a Type) here.45 *46 * This is resolved in main plugin method, calling47 * `path.scope.crawl()` which updates the bindings.48 */49 const reference = serializeReference(node.typeName);50 /**51 * We should omit references to self (class) since it will throw a52 * ReferenceError at runtime due to babel transpile output.53 */54 if (isClassType(className, reference)) {55 return t.identifier('Object');56 }57 /**58 * We don't know if type is just a type (interface, etc.) or a concrete59 * value (class, etc.).60 * `typeof` operator allows us to use the expression even if it is not61 * defined, fallback is just `Object`.62 */63 return t.conditionalExpression(64 t.binaryExpression(65 '===',66 t.unaryExpression('typeof', reference),67 t.stringLiteral('undefined')68 ),69 t.identifier('Object'),70 t.cloneDeep(reference)71 );72}73/**74 * Checks if node (this should be the result of `serializeReference`) member75 * expression or identifier is a reference to self (class name).76 * In this case, we just emit `Object` in order to avoid ReferenceError.77 */78export function isClassType(className: string, node: t.Expression): boolean {79 switch (node.type) {80 case 'Identifier':81 return node.name === className;82 case 'MemberExpression':83 return isClassType(className, node.object);84 default:85 throw new Error(86 `The property expression at ${node.start} is not valid as a Type to be used in Reflect.metadata`87 );88 }89}90function serializeReference(91 typeName: t.Identifier | t.TSQualifiedName92): t.Identifier | t.MemberExpression {93 if (typeName.type === 'Identifier') {94 return t.identifier(typeName.name);95 }96 return t.memberExpression(serializeReference(typeName.left), typeName.right);97}98type SerializedType =99 | t.Identifier100 | t.UnaryExpression101 | t.ConditionalExpression;102/**103 * Actual serialization given the TS Type annotation.104 * Result tries to get the best match given the information available.105 *106 * Implementation is adapted from original TSC compiler source as107 * available here:108 * https://github.com/Microsoft/TypeScript/blob/2932421370df720f0ccfea63aaf628e32e881429/src/compiler/transformers/ts.ts109 */110function serializeTypeNode(className: string, node: t.TSType): SerializedType {111 if (node === undefined) {112 return t.identifier('Object');113 }114 switch (node.type) {115 case 'TSVoidKeyword':116 case 'TSUndefinedKeyword':117 case 'TSNullKeyword':118 case 'TSNeverKeyword':119 return createVoidZero();120 case 'TSParenthesizedType':121 return serializeTypeNode(className, node.typeAnnotation);122 case 'TSFunctionType':123 case 'TSConstructorType':124 return t.identifier('Function');125 case 'TSArrayType':126 case 'TSTupleType':127 return t.identifier('Array');128 case 'TSTypePredicate':129 case 'TSBooleanKeyword':130 return t.identifier('Boolean');131 case 'TSStringKeyword':132 return t.identifier('String');133 case 'TSObjectKeyword':134 return t.identifier('Object');135 case 'TSLiteralType':136 switch (node.literal.type) {137 case 'StringLiteral':138 return t.identifier('String');139 case 'NumericLiteral':140 return t.identifier('Number');141 case 'BooleanLiteral':142 return t.identifier('Boolean');143 default:144 /**145 * @todo Use `path` error building method.146 */147 throw new Error('Bad type for decorator' + node.literal);148 }149 case 'TSNumberKeyword':150 case 'TSBigIntKeyword' as any: // Still not in ``@babel/core` typings151 return t.identifier('Number');152 case 'TSSymbolKeyword':153 return t.identifier('Symbol');154 case 'TSTypeReference':155 return serializeTypeReferenceNode(className, node);156 case 'TSIntersectionType':157 case 'TSUnionType':158 return serializeTypeList(className, node.types);159 case 'TSConditionalType':160 return serializeTypeList(className, [node.trueType, node.falseType]);161 case 'TSTypeQuery':162 case 'TSTypeOperator':163 case 'TSIndexedAccessType':164 case 'TSMappedType':165 case 'TSTypeLiteral':166 case 'TSAnyKeyword':167 case 'TSUnknownKeyword':168 case 'TSThisType':169 //case SyntaxKind.ImportType:170 break;171 default:172 throw new Error('Bad type for decorator');173 }174 return t.identifier('Object');175}176/**177 * Type lists need some refining. Even here, implementation is slightly178 * adapted from original TSC compiler:179 *180 * https://github.com/Microsoft/TypeScript/blob/2932421370df720f0ccfea63aaf628e32e881429/src/compiler/transformers/ts.ts181 */182function serializeTypeList(183 className: string,184 types: ReadonlyArray<t.TSType>185): SerializedType {186 let serializedUnion: SerializedType | undefined;187 for (let typeNode of types) {188 while (typeNode.type === 'TSParenthesizedType') {189 typeNode = typeNode.typeAnnotation; // Skip parens if need be190 }191 if (typeNode.type === 'TSNeverKeyword') {192 continue; // Always elide `never` from the union/intersection if possible193 }194 if (195 typeNode.type === 'TSNullKeyword' ||196 typeNode.type === 'TSUndefinedKeyword'197 ) {198 continue; // Elide null and undefined from unions for metadata, just like what we did prior to the implementation of strict null checks199 }200 const serializedIndividual = serializeTypeNode(className, typeNode);201 if (202 t.isIdentifier(serializedIndividual) &&203 serializedIndividual.name === 'Object'204 ) {205 // One of the individual is global object, return immediately206 return serializedIndividual;207 }208 // If there exists union that is not void 0 expression, check if the the common type is identifier.209 // anything more complex and we will just default to Object210 else if (serializedUnion) {211 // Different types212 if (213 !t.isIdentifier(serializedUnion) ||214 !t.isIdentifier(serializedIndividual) ||215 serializedUnion.name !== serializedIndividual.name216 ) {217 return t.identifier('Object');218 }219 } else {220 // Initialize the union type221 serializedUnion = serializedIndividual;222 }223 }224 // If we were able to find common type, use it225 return serializedUnion || createVoidZero(); // Fallback is only hit if all union constituients are null/undefined/never...

Full Screen

Full Screen

serializeTypeReference.ts

Source:serializeTypeReference.ts Github

copy

Full Screen

1import { NodePath } from '@babel/traverse';2import * as t from '@babel/types';3type InferArray<T> = T extends Array<infer A> ? A : never;4type Parameter = InferArray<t.ClassMethod['params']> | t.ClassProperty;5function createVoidZero() {6 return t.unaryExpression('void', t.numericLiteral(0));7}8/**9 * Given a paramater (or class property) node it returns the first identifier10 * containing the TS Type Annotation.11 *12 * @todo Array and Objects spread are not supported.13 * @todo Rest parameters are not supported.14 */15function getTypedNode(16 param: Parameter17): t.Identifier | t.ClassProperty | t.ObjectPattern | null {18 if (param == null) return null;19 if (param.type === 'ClassProperty') return param;20 if (param.type === 'Identifier') return param;21 if (param.type === 'ObjectPattern') return param;22 if (param.type === 'AssignmentPattern' && param.left.type === 'Identifier')23 return param.left;24 if (param.type === 'TSParameterProperty')25 return getTypedNode(param.parameter);26 return null;27}28export function serializeType(29 classPath: NodePath<t.ClassDeclaration>,30 param: Parameter31) {32 const node = getTypedNode(param);33 if (node == null) return createVoidZero();34 if (!node.typeAnnotation || node.typeAnnotation.type !== 'TSTypeAnnotation')35 return createVoidZero();36 const annotation = node.typeAnnotation.typeAnnotation;37 const className = classPath.node.id ? classPath.node.id.name : '';38 return serializeTypeNode(className, annotation);39}40function serializeTypeReferenceNode(41 className: string,42 node: t.TSTypeReference43) {44 /**45 * We need to save references to this type since it is going46 * to be used as a Value (and not just as a Type) here.47 *48 * This is resolved in main plugin method, calling49 * `path.scope.crawl()` which updates the bindings.50 */51 const reference = serializeReference(node.typeName);52 /**53 * We should omit references to self (class) since it will throw a54 * ReferenceError at runtime due to babel transpile output.55 */56 if (isClassType(className, reference)) {57 return t.identifier('Object');58 }59 return t.cloneNode(reference);60}61/**62 * Checks if node (this should be the result of `serializeReference`) member63 * expression or identifier is a reference to self (class name).64 * In this case, we just emit `Object` in order to avoid ReferenceError.65 */66export function isClassType(className: string, node: t.Expression): boolean {67 switch (node.type) {68 case 'Identifier':69 return node.name === className;70 case 'MemberExpression':71 return isClassType(className, node.object);72 default:73 throw new Error(74 `The property expression at ${node.start} is not valid as a Type to be used in Reflect.metadata`75 );76 }77}78function serializeReference(79 typeName: t.Identifier | t.TSQualifiedName80): t.Identifier | t.MemberExpression {81 if (typeName.type === 'Identifier') {82 return t.identifier(typeName.name);83 }84 return t.memberExpression(serializeReference(typeName.left), typeName.right);85}86type SerializedType =87 | t.Identifier88 | t.MemberExpression89 | t.UnaryExpression90 | t.ConditionalExpression;91/**92 * Actual serialization given the TS Type annotation.93 * Result tries to get the best match given the information available.94 *95 * Implementation is adapted from original TSC compiler source as96 * available here:97 * https://github.com/Microsoft/TypeScript/blob/2932421370df720f0ccfea63aaf628e32e881429/src/compiler/transformers/ts.ts98 */99function serializeTypeNode(className: string, node: t.TSType): SerializedType {100 if (node === undefined) {101 return t.identifier('Object');102 }103 switch (node.type) {104 case 'TSVoidKeyword':105 case 'TSUndefinedKeyword':106 case 'TSNullKeyword':107 case 'TSNeverKeyword':108 return createVoidZero();109 case 'TSParenthesizedType':110 return serializeTypeNode(className, node.typeAnnotation);111 case 'TSFunctionType':112 case 'TSConstructorType':113 return t.identifier('Function');114 case 'TSArrayType':115 case 'TSTupleType':116 return t.identifier('Array');117 case 'TSTypePredicate':118 case 'TSBooleanKeyword':119 return t.identifier('Boolean');120 case 'TSStringKeyword':121 return t.identifier('String');122 case 'TSObjectKeyword':123 return t.identifier('Object');124 case 'TSLiteralType':125 switch (node.literal.type) {126 case 'StringLiteral':127 return t.identifier('String');128 case 'NumericLiteral':129 return t.identifier('Number');130 case 'BooleanLiteral':131 return t.identifier('Boolean');132 default:133 /**134 * @todo Use `path` error building method.135 */136 throw new Error('Bad type for decorator' + node.literal);137 }138 case 'TSNumberKeyword':139 case 'TSBigIntKeyword' as any: // Still not in ``@babel/core` typings140 return t.identifier('Number');141 case 'TSSymbolKeyword':142 return t.identifier('Symbol');143 case 'TSTypeReference':144 return serializeTypeReferenceNode(className, node);145 case 'TSIntersectionType':146 case 'TSUnionType':147 return serializeTypeList(className, node.types);148 case 'TSConditionalType':149 return serializeTypeList(className, [node.trueType, node.falseType]);150 case 'TSTypeQuery':151 case 'TSTypeOperator':152 case 'TSIndexedAccessType':153 case 'TSMappedType':154 case 'TSTypeLiteral':155 case 'TSAnyKeyword':156 case 'TSUnknownKeyword':157 case 'TSThisType':158 //case SyntaxKind.ImportType:159 break;160 default:161 throw new Error('Bad type for decorator');162 }163 return t.identifier('Object');164}165/**166 * Type lists need some refining. Even here, implementation is slightly167 * adapted from original TSC compiler:168 *169 * https://github.com/Microsoft/TypeScript/blob/2932421370df720f0ccfea63aaf628e32e881429/src/compiler/transformers/ts.ts170 */171function serializeTypeList(172 className: string,173 types: ReadonlyArray<t.TSType>174): SerializedType {175 let serializedUnion: SerializedType | undefined;176 for (let typeNode of types) {177 while (typeNode.type === 'TSParenthesizedType') {178 typeNode = typeNode.typeAnnotation; // Skip parens if need be179 }180 if (typeNode.type === 'TSNeverKeyword') {181 continue; // Always elide `never` from the union/intersection if possible182 }183 if (184 typeNode.type === 'TSNullKeyword' ||185 typeNode.type === 'TSUndefinedKeyword'186 ) {187 continue; // Elide null and undefined from unions for metadata, just like what we did prior to the implementation of strict null checks188 }189 const serializedIndividual = serializeTypeNode(className, typeNode);190 if (191 t.isIdentifier(serializedIndividual) &&192 serializedIndividual.name === 'Object'193 ) {194 // One of the individual is global object, return immediately195 return serializedIndividual;196 }197 // If there exists union that is not void 0 expression, check if the the common type is identifier.198 // anything more complex and we will just default to Object199 else if (serializedUnion) {200 // Different types201 if (202 !t.isIdentifier(serializedUnion) ||203 !t.isIdentifier(serializedIndividual) ||204 serializedUnion.name !== serializedIndividual.name205 ) {206 return t.identifier('Object');207 }208 } else {209 // Initialize the union type210 serializedUnion = serializedIndividual;211 }212 }213 // If we were able to find common type, use it214 return serializedUnion || createVoidZero(); // Fallback is only hit if all union constituients are null/undefined/never...

Full Screen

Full Screen

undefined.ts

Source:undefined.ts Github

copy

Full Screen

1import type * as ts from 'typescript';2import { createVoidZero } from '../../../typescriptFactory/typescriptFactory';3export function GetUndefinedDescriptor(): ts.Expression {4 return createVoidZero();...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock';2const voidZero = createVoidZero();3const voidZero2 = createVoidZero();4const voidZero3 = createVoidZero();5const voidZero4 = createVoidZero();6const voidZero5 = createVoidZero();7const voidZero6 = createVoidZero();8const voidZero7 = createVoidZero();9const voidZero8 = createVoidZero();10const voidZero9 = createVoidZero();11const voidZero10 = createVoidZero();12const voidZero11 = createVoidZero();13const voidZero12 = createVoidZero();14const voidZero13 = createVoidZero();15const voidZero14 = createVoidZero();16const voidZero15 = createVoidZero();17const voidZero16 = createVoidZero();18const voidZero17 = createVoidZero();19const voidZero18 = createVoidZero();20const voidZero19 = createVoidZero();21const voidZero20 = createVoidZero();22const voidZero21 = createVoidZero();

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock/extension';2const voidZero = createVoidZero();3import { createVoidZero } from 'ts-auto-mock/extension';4const voidZero = createVoidZero();5import { createVoidZero } from 'ts-auto-mock/extension';6const voidZero = createVoidZero();7import { createVoidZero } from 'ts-auto-mock/extension';8const voidZero = createVoidZero();9import { createVoidZero } from 'ts-auto-mock/extension';10const voidZero = createVoidZero();11import { createVoidZero } from 'ts-auto-mock/extension';12const voidZero = createVoidZero();13import { createVoidZero } from 'ts-auto-mock/extension';14const voidZero = createVoidZero();15import { createVoidZero } from 'ts-auto-mock/extension';16const voidZero = createVoidZero();17import { createVoidZero } from 'ts-auto-mock/extension';18const voidZero = createVoidZero();19import { createVoidZero } from 'ts-auto-mock/extension';20const voidZero = createVoidZero();21import { createVoidZero } from 'ts-auto-mock/extension';22const voidZero = createVoidZero();23import { createVoidZero } from 'ts-auto-mock/extension

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock';2const result = createVoidZero();3import { createVoidZero } from 'ts-auto-mock';4const result = createVoidZero();5import { createVoidZero } from 'ts-auto-mock';6const result = createVoidZero();7import { createVoidZero } from 'ts-auto-mock';8const result = createVoidZero();9import { createVoidZero } from 'ts-auto-mock';10const result = createVoidZero();11import { createVoidZero } from 'ts-auto-mock';12const result = createVoidZero();13import { createVoidZero } from 'ts-auto-mock';14const result = createVoidZero();15import { createVoidZero } from 'ts-auto-mock';16const result = createVoidZero();17import { createVoidZero } from 'ts-auto-mock';18const result = createVoidZero();19import { createVoidZero } from 'ts-auto-mock';20const result = createVoidZero();21import { createVoidZero } from 'ts-auto-mock';22const result = createVoidZero();

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock';2const voidZero = createVoidZero();3import { createVoidZero } from 'ts-auto-mock';4const voidZero = createVoidZero();5import { createVoidZero } from 'ts-auto-mock';6const voidZero = createVoidZero();7import { createVoidZero } from 'ts-auto-mock';8const voidZero = createVoidZero();9import { createVoidZero } from 'ts-auto-mock';10const voidZero = createVoidZero();11import { createVoidZero } from 'ts-auto-mock';12const voidZero = createVoidZero();13import { createVoidZero } from 'ts-auto-mock';14const voidZero = createVoidZero();15import { createVoidZero } from 'ts-auto-mock';16const voidZero = createVoidZero();17import { createVoidZero } from 'ts-auto-mock';18const voidZero = createVoidZero();19import { createVoidZero } from 'ts-auto-mock';20const voidZero = createVoidZero();

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock';2const voidZero = createVoidZero();3const voidZero2 = createVoidZero();4import { createVoidZero } from 'ts-auto-mock';5const voidZero = createVoidZero();6import { createVoidZero } from 'ts-auto-mock';7const voidZero = createVoidZero();8import { createVoidZero } from 'ts-auto-mock';9const voidZero = createVoidZero();10import { createVoidZero } from 'ts-auto-mock';11const voidZero = createVoidZero();12import { createVoidZero } from 'ts-auto-mock';13const voidZero = createVoidZero();14import { createVoidZero } from 'ts-auto-mock';15const voidZero = createVoidZero();16import { createVoidZero } from 'ts-auto-mock';17const voidZero = createVoidZero();18import { createVoidZero } from 'ts-auto-mock';19const voidZero = createVoidZero();20import { createVoidZero } from 'ts-auto-mock';21const voidZero = createVoidZero();

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock';2export const mock: void = createVoidZero();3import { createVoidZero } from 'ts-auto-mock';4export const mock: void = createVoidZero();5import { createVoidZero } from 'ts-auto-mock';6import { mock } from './test2';7export const mock: void = createVoidZero();8import { createVoidZero } from 'ts-auto-mock';9import { mock } from './test1';10export const mock: void = createVoidZero();11import { createVoidZero } from 'ts-auto-mock';12export const mock: void = createVoidZero<void>();13import { createVoidZero } from 'ts-auto-mock';14export const mock: void = createVoidZero<void>();15import { createVoidZero } from 'ts-auto-mock';16import { mock } from './test2

Full Screen

Using AI Code Generation

copy

Full Screen

1import { createVoidZero } from 'ts-auto-mock';2const a = createVoidZero();3const b = undefined;4class A {5 public name: string;6 public age: number;7}8import { createType } from 'ts-auto-mock';9const a = createType<A>();10const b = new A();11class A {12 public name: string;13 public age: number;14}15import { createInstanceOf } from 'ts-auto-mock';16const a = createInstanceOf<A>();17const b = new A();18class A {19 public name: string;20 public age: number;21}22import { createPartial } from 'ts-auto-mock';23const a = createPartial<A>();24const b = new A();25class A {26 public name: string;27 public age: number;28}29import { createMock } from 'ts-auto-mock';30const a = createMock<A>();31const b = new A();32class A {33 public name: string;34 public age: number;35}36import { createStrictMock } from 'ts-auto-mock';37const a = createStrictMock<A>();

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run ts-auto-mock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful