How to use NodeToString method in ts-auto-mock

Best JavaScript code snippet using ts-auto-mock

ast_passes.js

Source:ast_passes.js Github

copy

Full Screen

1//All kinds of conversion passes over the source code2var jsp = require("acorn");3var walk = require("acorn/util/walk.js");4var rnonIdentMember = /[.\-_$a-zA-Z0-9]/g;5var global = new Function("return this")();6function equals( a, b ) {7 if( a.type === b.type ) {8 if( a.type === "MemberExpression" ) {9 return equals( a.object, b.object ) &&10 equals( a.property, b.property );11 }12 else if( a.type === "Identifier" ) {13 return a.name === b.name;14 }15 else if( a.type === "ThisExpression" ) {16 return true;17 }18 else {19 console.log("equals", a, b);20 unhandled();21 }22 }23 return false;24}25function getReceiver( expr ) {26 if( expr.type === "MemberExpression" ) {27 return expr.object;28 }29 return null;30}31function nodeToString( expr ) {32 if( expr == null || typeof expr !== "object" ) {33 if( expr === void 0 ) {34 return "void 0";35 }36 else if( typeof expr === "string" ) {37 return '"' + safeToEmbedString(expr) + '"';38 }39 return ("" + expr);40 }41 if( expr.type === "Identifier" ) {42 return expr.name;43 }44 else if( expr.type === "MemberExpression" ) {45 if( expr.computed )46 return nodeToString( expr.object ) + "[" + nodeToString( expr.property ) + "]";47 else48 return nodeToString( expr.object ) + "." + nodeToString( expr.property );49 }50 else if( expr.type === "UnaryExpression" ) {51 if( expr.operator === "~" ||52 expr.operator === "-" ||53 expr.operator === "+" ||54 expr.operator === "-" ) {55 return expr.operator + nodeToString( expr.argument );56 }57 return "(" + expr.operator + " " + nodeToString( expr.argument ) + ")";58 }59 else if( expr.type === "Literal" ) {60 return expr.raw;61 }62 else if( expr.type === "BinaryExpression" || expr.type === "LogicalExpression" ) {63 return "("+nodeToString(expr.left) + " " +64 expr.operator + " " +65 nodeToString(expr.right) + ")";66 }67 else if( expr.type === "ThisExpression" ) {68 return "this";69 }70 else if( expr.type === "ObjectExpression") {71 var props = expr.properties;72 var ret = [];73 for( var i = 0, len = props.length; i < len; ++i ) {74 var prop = props[i];75 ret.push( nodeToString(prop.key) + ": " + nodeToString(prop.value));76 }77 return "({"+ret.join(",\n")+"})";78 }79 else if( expr.type === "NewExpression" ) {80 return "new " + nodeToString(expr.callee) + "(" + nodeToString(expr.arguments) +")";81 }82 //assuming it is arguments83 else if( Array.isArray( expr ) ) {84 var tmp = [];85 for( var i = 0, len = expr.length; i < len; ++i ) {86 tmp.push( nodeToString(expr[i]) );87 }88 return tmp.join(", ");89 }90 else if( expr.type === "FunctionExpression" ) {91 var params = [];92 for( var i = 0, len = expr.params.length; i < len; ++i ) {93 params.push( nodeToString(expr.params[i]) );94 }95 }96 else if( expr.type === "BlockStatement" ) {97 var tmp = [];98 for( var i = 0, len = expr.body.length; i < len; ++i ) {99 tmp.push( nodeToString(expr.body[i]) );100 }101 return tmp.join(";\n");102 }103 else if( expr.type === "CallExpression" ) {104 var args = [];105 for( var i = 0, len = expr.arguments.length; i < len; ++i ) {106 args.push( nodeToString(expr.arguments[i]) );107 }108 return nodeToString( expr.callee ) + "("+args.join(",")+")";109 }110 else {111 console.log( "nodeToString", expr );112 unhandled()113 }114}115function DynamicCall( receiver, fnDereference, arg, start, end ) {116 this.receiver = receiver;117 this.fnDereference = fnDereference;118 this.arg = arg;119 this.start = start;120 this.end = end;121}122DynamicCall.prototype.toString = function() {123 return nodeToString(this.fnDereference) + ".call(" +124 nodeToString(this.receiver) + ", " +125 nodeToString(this.arg) +126 ")";127};128function DirectCall( receiver, fnName, arg, start, end ) {129 this.receiver = receiver;130 this.fnName = fnName;131 this.arg = arg;132 this.start = start;133 this.end = end;134}135DirectCall.prototype.toString = function() {136 return nodeToString(this.receiver) + "." + nodeToString(this.fnName) +137 "(" + nodeToString(this.arg) + ")"138};139function ConstantReplacement( value, start, end ) {140 this.value = value;141 this.start = start;142 this.end = end;143}144ConstantReplacement.prototype.toString = function() {145 return nodeToString(this.value);146};147function Empty(start, end) {148 this.start = start;149 this.end = end;150}151Empty.prototype.toString = function() {152 return "";153};154function Assertion( expr, exprStr, start, end ) {155 this.expr = expr;156 this.exprStr = exprStr;157 this.start = start;158 this.end = end;159}160Assertion.prototype.toString = function() {161 return 'ASSERT('+nodeToString(this.expr)+',\n '+this.exprStr+')';162};163function InlineSlice(varExpr, collectionExpression, startExpression, endExpression, start, end) {164 this.varExpr = varExpr;165 this.collectionExpression = collectionExpression;166 this.startExpression = startExpression;167 this.endExpression = endExpression;168 this.start = start;169 this.end = end;170}171InlineSlice.prototype.hasSimpleStartExpression =172function InlineSlice$hasSimpleStartExpression() {173 return this.startExpression.type === "Identifier" ||174 this.startExpression.type === "Literal";175};176InlineSlice.prototype.hasSimpleEndExpression =177function InlineSlice$hasSimpleEndExpression() {178 return this.endExpression.type === "Identifier" ||179 this.endExpression.type === "Literal";180};181InlineSlice.prototype.hasSimpleCollection = function InlineSlice$hasSimpleCollection() {182 return this.collectionExpression.type === "Identifier";183};184InlineSlice.prototype.toString = function InlineSlice$toString() {185 var init = this.hasSimpleCollection()186 ? ""187 : "var $_collection = " + nodeToString(this.collectionExpression) + ";";188 var collectionExpression = this.hasSimpleCollection()189 ? nodeToString(this.collectionExpression)190 : "$_collection";191 init += "var $_len = " + collectionExpression + ".length;";192 var varExpr = nodeToString(this.varExpr);193 //No offset arguments at all194 if( this.startExpression === firstElement ) {195 return init + "var " + varExpr + " = new Array($_len); " +196 "for(var $_i = 0; $_i < $_len; ++$_i) {" +197 varExpr + "[$_i] = " + collectionExpression + "[$_i];" +198 "}";199 }200 else {201 if( !this.hasSimpleStartExpression() ) {202 init += "var $_start = " + nodeToString(this.startExpression) + ";";203 }204 var startExpression = this.hasSimpleStartExpression()205 ? nodeToString(this.startExpression)206 : "$_start";207 //Start offset argument given208 if( this.endExpression === lastElement ) {209 return init + "var " + varExpr + " = new Array($_len - " +210 startExpression + "); " +211 "for(var $_i = " + startExpression + "; $_i < $_len; ++$_i) {" +212 varExpr + "[$_i - "+startExpression+"] = " + collectionExpression + "[$_i];" +213 "}";214 }215 //Start and end offset argument given216 else {217 if( !this.hasSimpleEndExpression() ) {218 init += "var $_end = " + nodeToString(this.endExpression) + ";";219 }220 var endExpression = this.hasSimpleEndExpression()221 ? nodeToString(this.endExpression)222 : "$_end";223 return init + "var " + varExpr + " = new Array(" + endExpression + " - " +224 startExpression + "); " +225 "for(var $_i = " + startExpression + "; $_i < " + endExpression + "; ++$_i) {" +226 varExpr + "[$_i - "+startExpression+"] = " + collectionExpression + "[$_i];" +227 "}";228 }229 }230};231var opts = {232 ecmaVersion: 5,233 strictSemicolons: false,234 allowTrailingCommas: true,235 forbidReserved: false,236 locations: false,237 onComment: null,238 ranges: false,239 program: null,240 sourceFile: null241};242var rlineterm = /[\r\n\u2028\u2029]/;243var rhorizontalws = /[ \t]/;244var convertSrc = function( src, results ) {245 if( results.length ) {246 results.sort(function(a, b){247 var ret = a.start - b.start;248 if( ret === 0 ) {249 ret = a.end - b.end;250 }251 return ret;252 });253 for( var i = 1; i < results.length; ++i ) {254 var item = results[i];255 if( item.start === results[i-1].start &&256 item.end === results[i-1].end ) {257 results.splice(i++, 1);258 }259 }260 var ret = "";261 var start = 0;262 for( var i = 0, len = results.length; i < len; ++i ) {263 var item = results[i];264 ret += src.substring( start, item.start );265 ret += item.toString();266 start = item.end;267 }268 ret += src.substring( start );269 return ret;270 }271 return src;272};273var rescape = /[\r\n\u2028\u2029"]/g;274var replacer = function( ch ) {275 return "\\u" + (("0000") +276 (ch.charCodeAt(0).toString(16))).slice(-4);277};278function safeToEmbedString( str ) {279 return str.replace( rescape, replacer );280}281function parse( src, opts, fileName) {282 if( !fileName ) {283 fileName = opts;284 opts = void 0;285 }286 try {287 return jsp.parse(src, opts);288 }289 catch(e) {290 e.message = e.message + " " + fileName;291 e.scriptSrc = src;292 throw e;293 }294}295var inlinedFunctions = Object.create(null);296var lastElement = jsp.parse("___input.length").body[0].expression;297var firstElement = jsp.parse("0").body[0].expression;298inlinedFunctions.INLINE_SLICE = function( node ) {299 var statement = node;300 node = node.expression;301 var args = node.arguments;302 if( !(2 <= args.length && args.length <= 4 ) ) {303 throw new Error("INLINE_SLICE must have exactly 2, 3 or 4 arguments");304 }305 var varExpression = args[0];306 var collectionExpression = args[1];307 var startExpression = args.length < 3308 ? firstElement309 : args[2];310 var endExpression = args.length < 4311 ? lastElement312 : args[3];313 return new InlineSlice(varExpression, collectionExpression,314 startExpression, endExpression, statement.start, statement.end);315};316var constants = {};317var ignore = [];318var astPasses = module.exports = {319 inlineExpansion: function( src, fileName ) {320 var ast = parse(src, fileName);321 var results = [];322 walk.simple(ast, {323 ExpressionStatement: function( node ) {324 if( node.expression.type !== 'CallExpression' ) {325 return;326 }327 var name = node.expression.callee.name;328 if( typeof inlinedFunctions[ name ] === "function" ) {329 try {330 results.push( inlinedFunctions[ name ]( node ) );331 }332 catch(e) {333 e.fileName = fileName;334 throw e;335 }336 }337 }338 });339 return convertSrc( src, results );340 },341 //Parse constants in from constants.js342 readConstants: function( src, fileName ) {343 var ast = parse(src, fileName);344 walk.simple(ast, {345 ExpressionStatement: function( node ) {346 if( node.expression.type !== 'CallExpression' ) {347 return;348 }349 var start = node.start;350 var end = node.end;351 node = node.expression;352 var callee = node.callee;353 if( callee.name === "CONSTANT" &&354 callee.type === "Identifier" ) {355 if( node.arguments.length !== 2 ) {356 throw new Error( "Exactly 2 arguments must be passed to CONSTANT\n" +357 src.substring(start, end)358 );359 }360 if( node.arguments[0].type !== "Identifier" ) {361 throw new Error( "Can only define identifier as a constant\n" +362 src.substring(start, end)363 );364 }365 var args = node.arguments;366 var name = args[0];367 var nameStr = name.name;368 var expr = args[1];369 var e = eval;370 constants[nameStr] = {371 identifier: name,372 value: e(nodeToString(expr))373 };374 walk.simple( expr, {375 Identifier: function( node ) {376 ignore.push(node);377 }378 });379 global[nameStr] = constants[nameStr].value;380 }381 }382 });383 },384 //Expand constants in normal source files385 expandConstants: function( src, fileName ) {386 var results = [];387 var identifiers = [];388 var ast = parse(src, fileName);389 walk.simple(ast, {390 Identifier: function( node ) {391 identifiers.push( node );392 }393 });394 for( var i = 0, len = identifiers.length; i < len; ++i ) {395 var id = identifiers[i];396 if( ignore.indexOf(id) > -1 ) {397 continue;398 }399 var constant = constants[id.name];400 if( constant === void 0 ) {401 continue;402 }403 if( constant.identifier === id ) {404 continue;405 }406 results.push( new ConstantReplacement( constant.value, id.start, id.end ) );407 }408 return convertSrc( src, results );409 },410 removeComments: function( src, fileName ) {411 var results = [];412 var rnoremove = /^[*\s\/]*(?:@preserve|jshint|global)/;413 opts.onComment = function( block, text, start, end ) {414 if( rnoremove.test(text) ) {415 return;416 }417 var e = end + 1;418 var s = start - 1;419 while(rhorizontalws.test(src.charAt(s--)));420 while(rlineterm.test(src.charAt(e++)));421 results.push( new Empty( s + 2, e - 1 ) );422 };423 var ast = parse(src, opts, fileName);424 return convertSrc( src, results );425 },426 expandAsserts: function( src, fileName ) {427 var ast = parse( src, fileName );428 var results = [];429 walk.simple(ast, {430 CallExpression: function( node ) {431 var start = node.start;432 var end = node.end;433 var callee = node.callee;434 if( callee.type === "Identifier" &&435 callee.name === "ASSERT" ) {436 if( node.arguments.length !== 1 ) {437 results.push({438 start: start,439 end: end,440 toString: function() {441 return src.substring(start, end);442 }443 });444 return;445 }446 var expr = node.arguments[0];447 var str = src.substring(expr.start, expr.end);448 str = '"' + safeToEmbedString(str) + '"'449 var assertion = new Assertion( expr, str, start, end );450 results.push( assertion );451 }452 }453 });454 return convertSrc( src, results );455 },456 removeAsserts: function( src, fileName ) {457 var ast = parse( src, fileName );458 var results = [];459 walk.simple(ast, {460 ExpressionStatement: function( node ) {461 if( node.expression.type !== 'CallExpression' ) {462 return;463 }464 var start = node.start;465 var end = node.end;466 node = node.expression;467 var callee = node.callee;468 if( callee.type === "Identifier" &&469 callee.name === "ASSERT" ) {470 var e = end + 1;471 var s = start - 1;472 while(rhorizontalws.test(src.charAt(s--)));473 while(rlineterm.test(src.charAt(e++)));474 results.push( new Empty( s + 2, e - 1) );475 }476 },477 VariableDeclaration: function(node) {478 var start = node.start;479 var end = node.end;480 if (node.kind === 'var' && node.declarations.length === 1) {481 var decl = node.declarations[0];482 if (decl.id.type === "Identifier" &&483 decl.id.name === "ASSERT") {484 var e = end + 1;485 var s = start - 1;486 while(rhorizontalws.test(src.charAt(s--)));487 while(rlineterm.test(src.charAt(e++)));488 results.push( new Empty( s + 2, e - 1) );489 }490 }491 }492 });493 return convertSrc( src, results );494 },495 asyncConvert: function( src, objName, fnProp, fileName ) {496 var ast = parse( src, fileName );497 var results = [];498 walk.simple(ast, {499 CallExpression: function( node ) {500 var start = node.start;501 var end = node.end;502 if( node.callee.type === "MemberExpression" &&503 node.callee.object.name === objName &&504 node.callee.property.name === fnProp &&505 node.arguments.length === 3506 ) {507 var args = node.arguments;508 var fnDereference = args[0];509 var dynamicReceiver = args[1];510 var arg = args[2];511 var receiver = getReceiver(fnDereference);512 if( receiver == null || !equals(receiver, dynamicReceiver) ) {513 //Have to use fnDereference.call(dynamicReceiver, arg);514 results.push(515 new DynamicCall( dynamicReceiver, fnDereference, arg, start, end )516 );517 }518 else {519 var fnName = fnDereference.property;520 results.push(521 new DirectCall( receiver, fnName, arg, start, end )522 );523 //Can use receiver.fnName( arg );524 }525 }526 }527 });528 return convertSrc( src, results );529 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var ts_auto_mock_1 = require("ts-auto-mock");2var mock = ts_auto_mock_1.createMock({3});4console.log(ts_auto_mock_1.NodeToString(mock));5var ts_auto_mock_1 = require("ts-auto-mock");6var mock = ts_auto_mock_1.createMock({7});8console.log(ts_auto_mock_1.NodeToString(mock));9var ts_auto_mock_1 = require("ts-auto-mock");10var mock = ts_auto_mock_1.createMock({11});12console.log(ts_auto_mock_1.NodeToString(mock));13var ts_auto_mock_1 = require("ts-auto-mock");14var mock = ts_auto_mock_1.createMock({15});16console.log(ts_auto_mock_1.NodeToString(mock));17var ts_auto_mock_1 = require("ts-auto-mock");18var mock = ts_auto_mock_1.createMock({19});20console.log(ts_auto_mock_1.NodeToString(mock));21var ts_auto_mock_1 = require("ts-auto-mock");22var mock = ts_auto_mock_1.createMock({23});24console.log(ts_auto_mock_1.NodeToString(mock));25var ts_auto_mock_1 = require("ts-auto-mock");26var mock = ts_auto_mock_1.createMock({27});28console.log(ts_auto_mock_1.NodeToString(mock));29var ts_auto_mock_1 = require("ts-auto-mock");

Full Screen

Using AI Code Generation

copy

Full Screen

1import * as tsAutoMock from 'ts-auto-mock';2const mock = tsAutoMock.createMock<TestInterface>();3console.log(tsAutoMock.NodeToString(mock));4import * as tsAutoMock from 'ts-auto-mock';5const mock = tsAutoMock.createMock<TestInterface>();6console.log(tsAutoMock.NodeToString(mock));7import * as tsAutoMock from 'ts-auto-mock';8const mock = tsAutoMock.createMock<TestInterface>();9console.log(tsAutoMock.NodeToString(mock));10import * as tsAutoMock from 'ts-auto-mock';11const mock = tsAutoMock.createMock<TestInterface>();12console.log(tsAutoMock.NodeToString(mock));

Full Screen

Using AI Code Generation

copy

Full Screen

1const { NodeToString } = require('ts-auto-mock');2const { createMock } = require('ts-auto-mock');3const { createMockFunction } = require('ts-auto-mock');4const { createMockInterface } = require('ts-auto-mock');5const { NodeToString } = require('ts-auto-mock');6const { createMock } = require('ts-auto-mock');7const { createMockFunction } = require('ts-auto-mock');8const { createMockInterface } = require('ts-auto-mock');9const mock = createMock({name: 'string', age: 10});10console.log(NodeToString(mock));11const mockFunction = createMockFunction('string');12console.log(NodeToString(mockFunction));13const mockInterface = createMockInterface('interface ITest {name: string}');14console.log(NodeToString(mockInterface));15const { NodeToString } = require('ts-auto-mock');16const { createMock } = require('ts-auto-mock');17const { createMockFunction } = require('ts-auto-mock');18const { createMockInterface } = require('ts-auto-mock');19const mock = createMock({name: 'string', age: 10});20console.log(NodeToString(mock));21const mockFunction = createMockFunction('string');22console.log(NodeToString(mockFunction));23const mockInterface = createMockInterface('interface ITest {name: string}');24console.log(NodeToString(mockInterface));25{ name: 'string', age: 10 } 26{ name: 'string', age: 10 }

Full Screen

Using AI Code Generation

copy

Full Screen

1import { NodeToString } from 'ts-auto-mock';2const node = ts.createSourceFile('test1.ts', 'const x = 1;', ts.ScriptTarget.ES2015);3const result = NodeToString(node);4console.log(result);5import { NodeToString } from 'ts-auto-mock';6const node = ts.createSourceFile('test2.ts', 'const x = 2;', ts.ScriptTarget.ES2015);7const result = NodeToString(node);8console.log(result);9import { NodeToString } from 'ts-auto-mock';10const node = ts.createSourceFile('test3.ts', 'const x = 3;', ts.ScriptTarget.ES2015);11const result = NodeToString(node);12console.log(result);13import { NodeToString } from 'ts-auto-mock';14const node = ts.createSourceFile('test4.ts', 'const x = 4;', ts.ScriptTarget.ES2015);15const result = NodeToString(node);16console.log(result);17import { NodeToString } from 'ts-auto-mock';18const node = ts.createSourceFile('test5.ts', 'const x = 5;', ts.ScriptTarget.ES2015);19const result = NodeToString(node);20console.log(result);21import { NodeToString } from 'ts-auto-mock';22const node = ts.createSourceFile('test6.ts', 'const x = 6;', ts.ScriptTarget.ES2015);23const result = NodeToString(node);24console.log(result);25import { NodeToString } from 'ts-auto-mock';26const node = ts.createSourceFile('test7.ts', 'const x = 7;', ts.ScriptTarget.ES2015);27const result = NodeToString(node);28console.log(result);29import { NodeToString } from 'ts

Full Screen

Using AI Code Generation

copy

Full Screen

1import { NodeToString } from 'ts-auto-mock';2import { MyInterface } from './myInterface';3const myInterfaceCode = NodeToString(MyInterface);4console.log(myInterfaceCode);5export interface MyInterface {6 myProperty: string;7}8interface MyInterface {9 myProperty: string;10}11import { createMock } from 'ts-auto-mock';12const myInterfaceMock = createMock<MyInterface>();13console.log(myInterfaceMock);14{15}16import { createMock } from 'ts-auto-mock';17const myInterfaceMock = createMock<MyInterface>({18});19console.log(myInterfaceMock);20{21}22import { createMock } from 'ts-auto-mock';23const myInterfaceMock = createMock<MyInterface>({24 myProperty: () => 'myValue'25});26console.log(myInterfaceMock);27{28}29import { createMock } from 'ts-auto-mock';30const myInterfaceMock = createMock<MyInterface>({31 myProperty: () => 'myValue'32});33console.log(myInterfaceMock.myProperty);34import { createMock } from 'ts-auto-mock';35const myInterfaceMock = createMock<MyInterface>({36 myProperty: () => 'myValue'37});38console.log(myInterfaceMock.myProperty());

Full Screen

Using AI Code Generation

copy

Full Screen

1import { NodeToString } from 'ts-auto-mock';2import { MyInterface } from './test2';3const myInterface: MyInterface = {4 myMethod: () => {5 return 'myMethodValue';6 },7};8console.log(NodeToString(myInterface));9export interface MyInterface {10 myProperty: string;11 myMethod(): string;12}

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