How to use esprima.parse method in istanbul

Best JavaScript code snippet using istanbul

api-tests.js

Source:api-tests.js Github

copy

Full Screen

...96});97describe('esprima.parse', function () {98 it('should not accept zero parameter', function () {99 assert.throws(function () {100 esprima.parse();101 });102 });103 it('should accept one string parameter', function () {104 assert.doesNotThrow(function () {105 esprima.parse('function f(){}');106 });107 });108 it('should accept a String object', function () {109 assert.doesNotThrow(function () {110 esprima.parse(new String('some.property = value'));111 });112 });113 it('should accept two parameters (source and parsing options)', function () {114 var options = { range: false };115 assert.doesNotThrow(function () {116 esprima.parse('var x', options);117 });118 });119 it('should accept three parameters (source, options, and delegate)', function () {120 var options = { range: false };121 assert.doesNotThrow(function () {122 esprima.parse('var x', options, function f(entry) {123 return entry;124 });125 });126 });127 it('should exclude location information by default', function () {128 var ast = esprima.parse('answer = 42');129 assert.ifError(ast.range);130 assert.ifError(ast.loc);131 });132 it('should exclude comments by default', function () {133 var ast = esprima.parse('42 // answer');134 assert.ifError(ast.comments);135 });136 it('should exclude list of tokens by default', function () {137 var ast = esprima.parse('3.14159');138 assert.ifError(ast.tokens);139 });140 it('should include index-based location for the nodes when specified', function () {141 var ast = esprima.parse('answer = 42', { range: true });142 assert.deepEqual(ast.range, [0, 11]);143 assert.ifError(ast.loc);144 });145 it('should include line and column-based location for the nodes when specified', function () {146 var ast = esprima.parse('answer = 42', { loc: true });147 assert.deepEqual(ast.loc.start, { line: 1, column: 0 });148 assert.deepEqual(ast.loc.end, { line: 1, column: 11 });149 assert.ifError(ast.loc.source);150 assert.ifError(ast.range);151 });152 it('should include source information for the nodes when specified', function () {153 var ast = esprima.parse('answer = 42', { loc: true, source: 'example.js' });154 assert.deepEqual(ast.loc.source, 'example.js');155 assert.ifError(ast.range);156 });157 it('should include the list of comments when specified', function () {158 var ast = esprima.parse('42 // answer', { comment: true });159 assert.deepEqual(ast.comments.length, 1);160 assert.deepEqual(ast.comments[0], { type: 'Line', value: ' answer' });161 });162 it('should include the list of comments with index-based location when specified', function () {163 var ast = esprima.parse('42 // answer', { comment: true, range: true });164 assert.deepEqual(ast.comments.length, 1);165 assert.deepEqual(ast.comments[0], { type: 'Line', value: ' answer', range: [3, 12] });166 });167 it('should include the list of comments with line and column-based location when specified', function () {168 var ast = esprima.parse('42 // answer', { comment: true, loc: true });169 assert.deepEqual(ast.comments.length, 1);170 assert.deepEqual(ast.comments[0].type, 'Line');171 assert.deepEqual(ast.comments[0].value, ' answer');172 assert.deepEqual(ast.comments[0].loc.start, { line: 1, column: 3 });173 assert.deepEqual(ast.comments[0].loc.end, { line: 1, column: 12 });174 assert.ifError(ast.comments[0].range);175 });176 it('should be able to attach comments', function () {177 var ast, statement, expression, comments;178 ast = esprima.parse('/* universe */ 42', { attachComment: true });179 statement = ast.body[0];180 expression = statement.expression;181 comments = statement.leadingComments;182 assert.deepEqual(expression, { type: 'Literal', value: 42, raw: '42' });183 assert.deepEqual(statement.leadingComments, [{ type: 'Block', value: ' universe ', range: [0, 14] }]);184 });185 it('should not implicity collect comments when comment attachment is specified', function () {186 var ast = esprima.parse('/* universe */ 42', { attachComment: true });187 assert.equal(ast.comments, undefined);188 });189 it('should include the list of tokens when specified', function () {190 var ast = esprima.parse('x = 1', { tokens: true });191 assert.deepEqual(ast.tokens.length, 3);192 assert.deepEqual(ast.tokens[0], { type: 'Identifier', value: 'x' });193 assert.deepEqual(ast.tokens[1], { type: 'Punctuator', value: '=' });194 assert.deepEqual(ast.tokens[2], { type: 'Numeric', value: '1' });195 });196 it('should include the list of tokens with index-based location when specified', function () {197 var ast = esprima.parse('y = 2', { tokens: true, range: true });198 assert.deepEqual(ast.tokens[0], { type: 'Identifier', value: 'y', range: [0, 1] });199 assert.deepEqual(ast.tokens[1], { type: 'Punctuator', value: '=', range: [2, 3] });200 assert.deepEqual(ast.tokens[2], { type: 'Numeric', value: '2', range: [4, 5] });201 });202 it('should include the list of tokens with line and column-based location when specified', function () {203 var ast = esprima.parse('z = 3', { tokens: true, loc: true });204 assert.deepEqual(ast.tokens.length, 3);205 assert.deepEqual(ast.tokens[0].type, 'Identifier');206 assert.deepEqual(ast.tokens[0].value, 'z');207 assert.deepEqual(ast.tokens[0].loc.start, { line: 1, column: 0 });208 assert.deepEqual(ast.tokens[0].loc.end, { line: 1, column: 1 });209 assert.ifError(ast.tokens[0].range);210 });211 it('should not understand JSX syntax by default', function () {212 assert.throws(function () {213 esprima.parse('<head/>');214 });215 });216 it('should not understand JSX syntax if it is explicitly not to be supported', function () {217 assert.throws(function () {218 esprima.parse('<b/>', { jsx: false });219 });220 });221 it('should understand JSX syntax if specified', function () {222 var ast, statement, expression;223 ast = esprima.parse('<title/>', { jsx: true });224 statement = ast.body[0];225 expression = statement.expression;226 assert.deepEqual(expression.type, 'JSXElement');227 assert.deepEqual(expression.openingElement.type, 'JSXOpeningElement');228 assert.deepEqual(expression.openingElement.name, { type: 'JSXIdentifier', name: 'title' });229 assert.deepEqual(expression.closingElement, null);230 });231 it('should understand JSX fragment', function () {232 assert.doesNotThrow(function () {233 esprima.parse('<></>', { jsx: true });234 });235 });236 it('should understand JSX fragment syntax', function () {237 var ast = esprima.parse('<></>', { jsx: true });238 var statement = ast.body[0];239 var expression = statement.expression;240 assert.deepEqual(expression.type, 'JSXElement');241 assert.deepEqual(expression.openingElement.type, 'JSXOpeningFragment');242 assert.deepEqual(expression.closingElement.type, 'JSXClosingFragment');243 });244 it('should understand JSX fragment syntax', function () {245 var ast = esprima.parse('<></>', { jsx: true });246 var statement = ast.body[0];247 var expression = statement.expression;248 assert.deepEqual(expression.type, 'JSXElement');249 assert.deepEqual(expression.openingElement.type, 'JSXOpeningFragment');250 assert.deepEqual(expression.closingElement.type, 'JSXClosingFragment');251 });252 it('should never produce shallow copied nodes', function () {253 var ast, pattern, expr;254 ast = esprima.parse('let {a, b} = {x, y, z}');255 pattern = ast.body[0].declarations[0].id;256 expr = ast.body[0].declarations[0].init;257 pattern.properties[0].key.name = 'foo';258 expr.properties[0].key.name = 'bar';259 assert.deepEqual(pattern.properties[0].value, { type: 'Identifier', name: 'a' });260 assert.deepEqual(pattern.properties[1].value, { type: 'Identifier', name: 'b' });261 assert.deepEqual(expr.properties[0].value, { type: 'Identifier', name: 'x' });262 assert.deepEqual(expr.properties[1].value, { type: 'Identifier', name: 'y' });263 assert.deepEqual(expr.properties[2].value, { type: 'Identifier', name: 'z' });264 });265});266describe('esprima.parseModule', function () {267 it('should not accept zero parameter', function () {268 assert.throws(function () {269 esprima.parseModule();270 });271 });272 it('should accept one string parameter', function () {273 assert.doesNotThrow(function () {274 esprima.parseModule('function f(){}');275 });276 });277 it('should accept a String object', function () {278 assert.doesNotThrow(function () {279 esprima.parseModule(new String('some.property = value'));280 });281 });282 it('should accept two parameters (source and parsing options)', function () {283 var options = { range: false };284 assert.doesNotThrow(function () {285 esprima.parseModule('var x', options);286 });287 });288 it('should accept three parameters (source, options, and delegate)', function () {289 var options = { range: false };290 assert.doesNotThrow(function () {291 esprima.parseModule('var x', options, function f(entry) {292 return entry;293 });294 });295 });296 it('should exclude location information by default', function () {297 var ast = esprima.parseModule('answer = 42');298 assert.ifError(ast.range);299 assert.ifError(ast.loc);300 });301 it('should exclude comments by default', function () {302 var ast = esprima.parseModule('42 // answer');303 assert.ifError(ast.comments);304 });305 it('should exclude list of tokens by default', function () {306 var ast = esprima.parseModule('3.14159');307 assert.ifError(ast.tokens);308 });309 it('should include index-based location for the nodes when specified', function () {310 var ast = esprima.parseModule('answer = 42', { range: true });311 assert.deepEqual(ast.range, [0, 11]);312 assert.ifError(ast.loc);313 });314 it('should include line and column-based location for the nodes when specified', function () {315 var ast = esprima.parseModule('answer = 42', { loc: true });316 assert.deepEqual(ast.loc.start, { line: 1, column: 0 });317 assert.deepEqual(ast.loc.end, { line: 1, column: 11 });318 assert.ifError(ast.loc.source);319 assert.ifError(ast.range);320 });321 it('should include source information for the nodes when specified', function () {322 var ast = esprima.parseModule('answer = 42', { loc: true, source: 'example.js' });323 assert.deepEqual(ast.loc.source, 'example.js');324 assert.ifError(ast.range);325 });326 it('should include the list of comments when specified', function () {327 var ast = esprima.parseModule('42 // answer', { comment: true });328 assert.deepEqual(ast.comments.length, 1);329 assert.deepEqual(ast.comments[0], { type: 'Line', value: ' answer' });330 });331 it('should include the list of comments with index-based location when specified', function () {332 var ast = esprima.parseModule('42 // answer', { comment: true, range: true });333 assert.deepEqual(ast.comments.length, 1);334 assert.deepEqual(ast.comments[0], { type: 'Line', value: ' answer', range: [3, 12] });335 });336 it('should include the list of comments with line and column-based location when specified', function () {337 var ast = esprima.parseModule('42 // answer', { comment: true, loc: true });338 assert.deepEqual(ast.comments.length, 1);339 assert.deepEqual(ast.comments[0].type, 'Line');340 assert.deepEqual(ast.comments[0].value, ' answer');341 assert.deepEqual(ast.comments[0].loc.start, { line: 1, column: 3 });342 assert.deepEqual(ast.comments[0].loc.end, { line: 1, column: 12 });343 assert.ifError(ast.comments[0].range);344 });345 it('should be able to attach comments', function () {346 var ast, statement, expression, comments;347 ast = esprima.parseModule('/* universe */ 42', { attachComment: true });348 statement = ast.body[0];349 expression = statement.expression;350 comments = statement.leadingComments;351 assert.deepEqual(expression, { type: 'Literal', value: 42, raw: '42' });352 assert.deepEqual(statement.leadingComments, [{ type: 'Block', value: ' universe ', range: [0, 14] }]);353 });354 it('should not implicity collect comments when comment attachment is specified', function () {355 var ast = esprima.parseModule('/* universe */ 42', { attachComment: true });356 assert.equal(ast.comments, undefined);357 });358 it('should include the list of tokens when specified', function () {359 var ast = esprima.parseModule('x = 1', { tokens: true });360 assert.deepEqual(ast.tokens.length, 3);361 assert.deepEqual(ast.tokens[0], { type: 'Identifier', value: 'x' });362 assert.deepEqual(ast.tokens[1], { type: 'Punctuator', value: '=' });363 assert.deepEqual(ast.tokens[2], { type: 'Numeric', value: '1' });364 });365 it('should include the list of tokens with index-based location when specified', function () {366 var ast = esprima.parseModule('y = 2', { tokens: true, range: true });367 assert.deepEqual(ast.tokens[0], { type: 'Identifier', value: 'y', range: [0, 1] });368 assert.deepEqual(ast.tokens[1], { type: 'Punctuator', value: '=', range: [2, 3] });369 assert.deepEqual(ast.tokens[2], { type: 'Numeric', value: '2', range: [4, 5] });370 });371 it('should include the list of tokens with line and column-based location when specified', function () {372 var ast = esprima.parseModule('z = 3', { tokens: true, loc: true });373 assert.deepEqual(ast.tokens.length, 3);374 assert.deepEqual(ast.tokens[0].type, 'Identifier');375 assert.deepEqual(ast.tokens[0].value, 'z');376 assert.deepEqual(ast.tokens[0].loc.start, { line: 1, column: 0 });377 assert.deepEqual(ast.tokens[0].loc.end, { line: 1, column: 1 });378 assert.ifError(ast.tokens[0].range);379 });380 it('should not understand JSX syntax by default', function () {381 assert.throws(function () {382 esprima.parseModule('<head/>');383 });384 });385 it('should not understand JSX syntax if it is explicitly not to be supported', function () {386 assert.throws(function () {387 esprima.parseModule('<b/>', { jsx: false });388 });389 });390 it('should understand JSX syntax if specified', function () {391 var ast, statement, expression;392 ast = esprima.parseModule('<title/>', { jsx: true });393 statement = ast.body[0];394 expression = statement.expression;395 assert.deepEqual(expression.type, 'JSXElement');396 assert.deepEqual(expression.openingElement.type, 'JSXOpeningElement');397 assert.deepEqual(expression.openingElement.name, { type: 'JSXIdentifier', name: 'title' });398 assert.deepEqual(expression.closingElement, null);399 });400 it('should never produce shallow copied nodes', function () {401 var ast, pattern, expr;402 ast = esprima.parseModule('let {a, b} = {x, y, z}');403 pattern = ast.body[0].declarations[0].id;404 expr = ast.body[0].declarations[0].init;405 pattern.properties[0].key.name = 'foo';406 expr.properties[0].key.name = 'bar';407 assert.deepEqual(pattern.properties[0].value, { type: 'Identifier', name: 'a' });408 assert.deepEqual(pattern.properties[1].value, { type: 'Identifier', name: 'b' });409 assert.deepEqual(expr.properties[0].value, { type: 'Identifier', name: 'x' });410 assert.deepEqual(expr.properties[1].value, { type: 'Identifier', name: 'y' });411 assert.deepEqual(expr.properties[2].value, { type: 'Identifier', name: 'z' });412 });413 it('should perform strict mode parsing', function () {414 assert.throws(function () {415 esprima.parseModule('with (x) {}');416 });417 });418});419describe('esprima.parseScript', function () {420 it('should not accept zero parameter', function () {421 assert.throws(function () {422 esprima.parseScript();423 });424 });425 it('should accept one string parameter', function () {426 assert.doesNotThrow(function () {427 esprima.parseScript('function f(){}');428 });429 });430 it('should accept a String object', function () {431 assert.doesNotThrow(function () {432 esprima.parseScript(new String('some.property = value'));433 });434 });435 it('should accept two parameters (source and parsing options)', function () {436 var options = { range: false };437 assert.doesNotThrow(function () {438 esprima.parseScript('var x', options);439 });440 });441 it('should accept three parameters (source, options, and delegate)', function () {442 var options = { range: false };443 assert.doesNotThrow(function () {444 esprima.parseScript('var x', options, function f(entry) {445 return entry;446 });447 });448 });449 it('should exclude location information by default', function () {450 var ast = esprima.parseScript('answer = 42');451 assert.ifError(ast.range);452 assert.ifError(ast.loc);453 });454 it('should exclude comments by default', function () {455 var ast = esprima.parseScript('42 // answer');456 assert.ifError(ast.comments);457 });458 it('should exclude list of tokens by default', function () {459 var ast = esprima.parseScript('3.14159');460 assert.ifError(ast.tokens);461 });462 it('should include index-based location for the nodes when specified', function () {463 var ast = esprima.parseScript('answer = 42', { range: true });464 assert.deepEqual(ast.range, [0, 11]);465 assert.ifError(ast.loc);466 });467 it('should include line and column-based location for the nodes when specified', function () {468 var ast = esprima.parseScript('answer = 42', { loc: true });469 assert.deepEqual(ast.loc.start, { line: 1, column: 0 });470 assert.deepEqual(ast.loc.end, { line: 1, column: 11 });471 assert.ifError(ast.loc.source);472 assert.ifError(ast.range);473 });474 it('should include source information for the nodes when specified', function () {475 var ast = esprima.parseScript('answer = 42', { loc: true, source: 'example.js' });476 assert.deepEqual(ast.loc.source, 'example.js');477 assert.ifError(ast.range);478 });479 it('should include the list of comments when specified', function () {480 var ast = esprima.parseScript('42 // answer', { comment: true });481 assert.deepEqual(ast.comments.length, 1);482 assert.deepEqual(ast.comments[0], { type: 'Line', value: ' answer' });483 });484 it('should include the list of comments with index-based location when specified', function () {485 var ast = esprima.parseScript('42 // answer', { comment: true, range: true });486 assert.deepEqual(ast.comments.length, 1);487 assert.deepEqual(ast.comments[0], { type: 'Line', value: ' answer', range: [3, 12] });488 });489 it('should include the list of comments with line and column-based location when specified', function () {490 var ast = esprima.parseScript('42 // answer', { comment: true, loc: true });491 assert.deepEqual(ast.comments.length, 1);492 assert.deepEqual(ast.comments[0].type, 'Line');493 assert.deepEqual(ast.comments[0].value, ' answer');494 assert.deepEqual(ast.comments[0].loc.start, { line: 1, column: 3 });495 assert.deepEqual(ast.comments[0].loc.end, { line: 1, column: 12 });496 assert.ifError(ast.comments[0].range);497 });498 it('should be able to attach comments', function () {499 var ast, statement, expression, comments;500 ast = esprima.parseScript('/* universe */ 42', { attachComment: true });501 statement = ast.body[0];502 expression = statement.expression;503 comments = statement.leadingComments;504 assert.deepEqual(expression, { type: 'Literal', value: 42, raw: '42' });505 assert.deepEqual(statement.leadingComments, [{ type: 'Block', value: ' universe ', range: [0, 14] }]);506 });507 it('should not implicity collect comments when comment attachment is specified', function () {508 var ast = esprima.parseScript('/* universe */ 42', { attachComment: true });509 assert.equal(ast.comments, undefined);510 });511 it('should include the list of tokens when specified', function () {512 var ast = esprima.parseScript('x = 1', { tokens: true });513 assert.deepEqual(ast.tokens.length, 3);514 assert.deepEqual(ast.tokens[0], { type: 'Identifier', value: 'x' });515 assert.deepEqual(ast.tokens[1], { type: 'Punctuator', value: '=' });516 assert.deepEqual(ast.tokens[2], { type: 'Numeric', value: '1' });517 });518 it('should include the list of tokens with index-based location when specified', function () {519 var ast = esprima.parseScript('y = 2', { tokens: true, range: true });520 assert.deepEqual(ast.tokens[0], { type: 'Identifier', value: 'y', range: [0, 1] });521 assert.deepEqual(ast.tokens[1], { type: 'Punctuator', value: '=', range: [2, 3] });522 assert.deepEqual(ast.tokens[2], { type: 'Numeric', value: '2', range: [4, 5] });523 });524 it('should include the list of tokens with line and column-based location when specified', function () {525 var ast = esprima.parseScript('z = 3', { tokens: true, loc: true });526 assert.deepEqual(ast.tokens.length, 3);527 assert.deepEqual(ast.tokens[0].type, 'Identifier');528 assert.deepEqual(ast.tokens[0].value, 'z');529 assert.deepEqual(ast.tokens[0].loc.start, { line: 1, column: 0 });530 assert.deepEqual(ast.tokens[0].loc.end, { line: 1, column: 1 });531 assert.ifError(ast.tokens[0].range);532 });533 it('should not understand JSX syntax by default', function () {534 assert.throws(function () {535 esprima.parseScript('<head/>');536 });537 });538 it('should not understand JSX syntax if it is explicitly not to be supported', function () {539 assert.throws(function () {540 esprima.parseScript('<b/>', { jsx: false });541 });542 });543 it('should understand JSX syntax if specified', function () {544 var ast, statement, expression;545 ast = esprima.parseScript('<title/>', { jsx: true });546 statement = ast.body[0];547 expression = statement.expression;548 assert.deepEqual(expression.type, 'JSXElement');549 assert.deepEqual(expression.openingElement.type, 'JSXOpeningElement');550 assert.deepEqual(expression.openingElement.name, { type: 'JSXIdentifier', name: 'title' });551 assert.deepEqual(expression.closingElement, null);552 });553 it('should never produce shallow copied nodes', function () {554 var ast, pattern, expr;555 ast = esprima.parseScript('let {a, b} = {x, y, z}');556 pattern = ast.body[0].declarations[0].id;557 expr = ast.body[0].declarations[0].init;558 pattern.properties[0].key.name = 'foo';559 expr.properties[0].key.name = 'bar';560 assert.deepEqual(pattern.properties[0].value, { type: 'Identifier', name: 'a' });561 assert.deepEqual(pattern.properties[1].value, { type: 'Identifier', name: 'b' });562 assert.deepEqual(expr.properties[0].value, { type: 'Identifier', name: 'x' });563 assert.deepEqual(expr.properties[1].value, { type: 'Identifier', name: 'y' });564 assert.deepEqual(expr.properties[2].value, { type: 'Identifier', name: 'z' });565 });566});567describe('esprima.parse delegate', function () {568 it('should receive all the nodes', function () {569 var list = [];570 function collect(node) {571 list.push(node.type);572 }573 esprima.parse('/* universe */ answer = 42', {}, collect);574 assert.deepEqual(list.length, 5);575 assert.deepEqual(list, ['Identifier', 'Literal', 'AssignmentExpression', 'ExpressionStatement', 'Program']);576 });577 it('should receive the comments if specified', function () {578 var list = [];579 function collect(node) {580 list.push(node);581 }582 esprima.parse('/* prolog */ answer = 42 // epilog', { comment: true }, collect);583 assert.deepEqual(list.length, 7);584 assert.deepEqual(list[0], { type: 'BlockComment', value: ' prolog ' });585 assert.deepEqual(list[1], { type: 'Identifier', name: 'answer' });586 assert.deepEqual(list[2], { type: 'LineComment', value: ' epilog' });587 assert.deepEqual(list[3], { type: 'Literal', value: 42, raw: '42' });588 assert.deepEqual(list[4].type, 'AssignmentExpression');589 assert.deepEqual(list[5].type, 'ExpressionStatement');590 assert.deepEqual(list[6].type, 'Program');591 });592 it('should be able to walk the tree and pick a node', function () {593 var constant = null;594 function walk(node) {595 if (node.type === 'Literal') {596 constant = node;597 }598 }599 esprima.parse('answer = 42 // universe', {}, walk);600 assert.deepEqual(constant, { type: 'Literal', value: 42, raw: '42' });601 });602 it('should be able to mutate each node', function () {603 var ast = esprima.parse('42', { range: true }, function (node) {604 node.start = node.range[0];605 node.end = node.range[1];606 });607 assert.deepEqual(ast.start, 0);608 assert.deepEqual(ast.end, 2);609 });610 it('should be affected by cover grammars', function () {611 var list = [];612 esprima.parse('(x, y) => z', {}, function (n) {613 list.push(n.type)614 });615 // (x, y) will be a SequenceExpression first before being reinterpreted as616 // the formal parameter list for an ArrowFunctionExpression617 assert.deepEqual(list.length, 7);618 assert.deepEqual(list[0], 'Identifier'); // x619 assert.deepEqual(list[1], 'Identifier'); // y620 assert.deepEqual(list[2], 'SequenceExpression'); // x, y621 assert.deepEqual(list[3], 'Identifier'); // z622 assert.deepEqual(list[4], 'ArrowFunctionExpression'); // (x, y) => z623 assert.deepEqual(list[5], 'ExpressionStatement');624 });625 it('should be affected by async arrow function cover grammar', function () {626 var list = [];627 esprima.parse('async (a) => 1', {}, function (n) {628 list.push(n.type)629 });630 // async (a) will be a CallExpression first before being reinterpreted as631 // the formal parameter list for an (async) ArrowFunctionExpression632 assert.deepEqual(list.length, 7);633 assert.deepEqual(list[0], 'Identifier'); // async634 assert.deepEqual(list[1], 'Identifier'); // a635 assert.deepEqual(list[2], 'CallExpression'); // async (a)636 assert.deepEqual(list[3], 'Literal'); // 1637 assert.deepEqual(list[4], 'ArrowFunctionExpression'); // async (a) => 1638 assert.deepEqual(list[5], 'ExpressionStatement');639 });640 it('should receive metadata of each node', function () {641 var starts = [], ends = [];642 esprima.parse('x = y + z', { range: false }, function (node, metadata) {643 starts.push(metadata.start);644 ends.push(metadata.end);645 });646 assert.deepEqual(starts.length, 7);647 assert.deepEqual(starts[0], { line: 1, column: 0, offset: 0 }); // x648 assert.deepEqual(starts[1], { line: 1, column: 4, offset: 4 }); // y649 assert.deepEqual(starts[2], { line: 1, column: 8, offset: 8 }); // z650 assert.deepEqual(starts[3], { line: 1, column: 4, offset: 4 }); // y + z651 assert.deepEqual(starts[4], { line: 1, column: 0, offset: 0 }); // x = y + z652 assert.deepEqual(starts[5], { line: 1, column: 0, offset: 0 }); // x = y + z653 assert.deepEqual(starts[6], { line: 1, column: 0, offset: 0 }); // x = y + z654 assert.deepEqual(ends.length, 7);655 assert.deepEqual(ends[0], { line: 1, column: 1, offset: 1 }); // x656 assert.deepEqual(ends[1], { line: 1, column: 5, offset: 5 }); // y657 assert.deepEqual(ends[2], { line: 1, column: 9, offset: 9 }); // z658 assert.deepEqual(ends[3], { line: 1, column: 9, offset: 9 }); // y + z659 assert.deepEqual(ends[4], { line: 1, column: 9, offset: 9 }); // x = y + z660 assert.deepEqual(ends[5], { line: 1, column: 9, offset: 9 }); // x = y + z661 assert.deepEqual(ends[6], { line: 1, column: 9, offset: 9 }); // x = y + z662 });663 it('should receive metadata of comments', function () {664 var starts = [], ends = [];665 esprima.parse('42 // answer', { comment: true }, function (node, metadata) {666 starts.push(metadata.start);667 ends.push(metadata.end);668 });669 assert.deepEqual(starts.length, 4);670 assert.deepEqual(starts[0], { line: 1, column: 3, offset: 3 });671 assert.deepEqual(starts[1], { line: 1, column: 0, offset: 0 });672 assert.deepEqual(starts[2], { line: 1, column: 0, offset: 0 });673 assert.deepEqual(starts[3], { line: 1, column: 0, offset: 0 });674 assert.deepEqual(ends.length, 4);675 assert.deepEqual(ends[0], { line: 1, column: 12, offset: 12 });676 assert.deepEqual(ends[1], { line: 1, column: 2, offset: 2 });677 assert.deepEqual(ends[2], { line: 1, column: 12, offset: 12 });678 assert.deepEqual(ends[3], { line: 1, column: 12, offset: 12 });679 });680 it('can be used for custom comment attachment', function () {681 var code, attacher, tree;682 function LineAttacher() {683 this.variableDeclarations = [];684 this.lineComments = [];685 }686 // Attach a line comment to a variable declaration in the same line.687 // Example: `var foo = 42; // bar` will have "bar" in the new property688 // called `annotation` of the variable declaration statement.689 LineAttacher.prototype.attach = function () {690 var i, j, declaration, comment;691 for (i = 0; i < this.variableDeclarations.length; ++i) {692 declaration = this.variableDeclarations[i];693 for (j = 0; j < this.lineComments.length; ++j) {694 comment = this.lineComments[j];695 if (declaration.line === comment.line) {696 declaration.node.annotation = comment.comment;697 break;698 }699 }700 }701 }702 LineAttacher.prototype.visit = function (node, metadata) {703 if (node.type === 'VariableDeclaration') {704 this.variableDeclarations.push({705 node: node,706 line: metadata.start.line707 });708 } else if (node.type === 'LineComment') {709 this.lineComments.push({710 comment: node,711 line: metadata.start.line712 });713 }714 this.attach();715 }716 code = [717 'var x = 42 // answer',718 'var y = 3',719 '// foobar'720 ].join('\n');721 attacher = new LineAttacher();722 tree = esprima.parse(code, { comment: true }, function (node, metadata) {723 attacher.visit(node, metadata);724 });725 // There will be two variable declaration statement.726 // Only the first one will have `annotation` since there is727 // a line comment in the same line.728 assert.deepEqual(tree.body.length, 2);729 assert.deepEqual(tree.body[0].annotation, { type: 'LineComment', value: ' answer' });730 assert.deepEqual(tree.body[1].annotation, undefined);731 });732});733describe('esprima.tokenize', function () {734 it('should not accept zero parameter', function () {735 assert.throws(function () {736 esprima.tokenize();...

Full Screen

Full Screen

tests.js

Source:tests.js Github

copy

Full Screen

...30 });31 describe('#hasWhitelisted()', function() {32 it('should return a boolean checking if it respects the whitelist ' +33 'conditions', function() {34 var basic = esprima.parse('var i = 0;');35 assert.equal(parser.hasWhitelisted(basic, [statement.FOR]), false);36 var forLoop = esprima.parse('for (;false;) {}');37 assert.equal(parser.hasWhitelisted(forLoop, [statement.FOR]), true);38 var twoSiblingStatement = esprima.parse('if (true) {}' +39 'for (;false;) {}');40 assert.equal(parser.hasWhitelisted(twoSiblingStatement,41 [statement.FOR, statement.IF]), true);42 var twoNestedStatement = esprima.parse('if (true) {' +43 'for (;false;) {' +44 'console.log(true);' +45 '}' +46 '}');47 assert.equal(parser.hasWhitelisted(twoNestedStatement,48 [statement.FOR, statement.IF]), true);49 var cantFind = esprima.parse('for (;1;) {} if (1) {}');50 assert.equal(parser.hasWhitelisted(cantFind, [statement.WHILE]), false);51 var cantFindNested = esprima.parse('for (;1;) { if (1) {} }');52 assert.equal(parser.hasWhitelisted(cantFindNested, [statement.WHILE]), false);53 });54 });55 describe('#hasBlacklisted()', function() {56 it('should return an error message if the blacklisted statement exists, ' +57 'null otherwise',58 function() {59 var none = esprima.parse('var i = 0;');60 assert.equal(parser.hasBlacklisted(basic, []), null);61 var basic = esprima.parse('var i = 0;');62 assert.equal(parser.hasBlacklisted(basic, [statement.FOR]), null);63 var forLoop = esprima.parse('for (;false;) {}');64 assert.notEqual(parser.hasBlacklisted(forLoop, [statement.FOR]), null);65 var twoSiblingStatement = esprima.parse('if (true) {}' +66 'for (;false;) {}');67 assert.notEqual(parser.hasBlacklisted(twoSiblingStatement,68 [statement.FOR, statement.IF]),69 null);70 var twoNestedStatement = esprima.parse('if (true) {' +71 'for (;false;) {' +72 'console.log(true);' +73 '}' +74 '}');75 assert.notEqual(parser.hasBlacklisted(twoNestedStatement,76 [statement.FOR]), null);77 var cantFind = esprima.parse('for (;1;) {} if (1) {}');78 assert.equal(parser.hasBlacklisted(cantFind, [statement.WHILE]), null);79 var cantFindNested = esprima.parse('for (;1;) { if (1) {} }');80 assert.equal(parser.hasBlacklisted(cantFindNested,81 [statement.WHILE]), null);82 }83 );84 });85 describe('#checkStructure()', function() {86 it('should return true if the code respects the structure given',87 function() {88 var basic = esprima.parse('var i = 0');89 assert.equal(parser.checkStructure(basic, []), true);90 var noIfStatement = esprima.parse('var i = 0');91 assert.equal(parser.checkStructure(noIfStatement, [{92 type: statement.IF93 }]), false);94 var ifStatement = esprima.parse('if (1) {}');95 assert.equal(parser.checkStructure(ifStatement, [{96 type: statement.IF97 }]), true);98 var nested = esprima.parse('if (1) {' +99 'for (;1;) {}' +100 '}');101 assert.equal(parser.checkStructure(nested, [{102 type: statement.IF,103 children: [{104 type: statement.FOR105 }]106 }]), true);107 var siblings = esprima.parse('if (1) {}' +108 'for (;1;) {}');109 assert.equal(parser.checkStructure(siblings, [{110 type: statement.IF111 }, {112 type: statement.FOR113 }]), true);114 var reverseSiblings = esprima.parse('if (1) {}' +115 'for (;1;) {}');116 assert.equal(parser.checkStructure(reverseSiblings, [{117 type: statement.FOR118 }, {119 type: statement.IF120 }]), false);121 }122 );123 });...

Full Screen

Full Screen

CT_parse.js

Source:CT_parse.js Github

copy

Full Screen

...18 var cint = 19 {20 varname : name,21 /* Server side */22 declarationS : esprima.parse(declstrS).body[0],23 setServerName : function (sname) {24 this.declarationS.callee.object.name = sname25 },26 /* Client side */27 declarationC : esprima.parse(declstrC).body[0],28 get : function () {29 return esprima.parse(name + ".get()").body[0]30 },31 add : function (nr) {32 return esprima.parse(name + ".add(" + nr + ")").body[0]33 },34 set : function (exp) {35 return esprima.parse(name + ".set(" + exp + ")").body[0] 36 },37 setIfEmpty : function (exp) {38 return esprima.parse(name + ".setIfEmpty(" + exp + ")").body[0];39 }40 };41 return cint;42 }43 var CString = function (name) {44 var declstrS = "server.declare('" + name + "', CloudTypes.CString)",45 declstrC = "state.get('" + name + "')";46 cstring = 47 {48 varname : name,49 /* Server side */50 declarationS : esprima.parse(declstrS).body[0],51 setServerName : function (sname) {52 this.declarationS.callee.object.name = sname53 },54 /* Client side */55 declarationC : esprima.parse(declstrC).body[0],56 get : function () {57 return esprima.parse(name + ".get()").body[0]58 },59 set : function (str) {60 return esprima.parse(name + ".set(" + str + ")").body[0]61 },62 setIfEmpty : function (str) {63 return esprima.parse(name + ".setIfEmpty(" + str + ")").body[0]64 }65 };66 return cstring;67 }68 module.CInt = CInt;69 module.CString = CString;70 module.CloudTypes = CloudTypes;71 return module;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var esprima = require('esprima');2var estraverse = require('estraverse');3var escodegen = require('escodegen');4var code = "var a = 1; var b = 2; var c = 3;";5var ast = esprima.parse(code);6estraverse.traverse(ast, {7 enter: function (node, parent) {8 if (node.type === 'VariableDeclaration') {9 node.declarations.forEach(function (decl) {10 decl.init = {11 };12 });13 }14 }15});16var output = escodegen.generate(ast);17console.log(output);18var a = 0;19var b = 0;20var c = 0;

Full Screen

Using AI Code Generation

copy

Full Screen

1var fs = require('fs')2var esprima = require('esprima')3var escodegen = require('escodegen')4var estraverse = require('estraverse')5var code = fs.readFileSync('test.js', 'utf-8')6var ast = esprima.parse(code)7var instrumentedAST = escodegen.attachComments(ast, ast.comments, ast.tokens)8console.log(JSON.stringify(instrumentedAST, null, 4))9{10 {11 {12 "id": {13 },14 "init": {15 }16 }17 },18 {19 {20 "id": {21 },22 "init": {23 }24 }25 },26 {27 {28 "id": {29 },30 "init": {31 }32 }33 },34 {35 "expression": {36 "left": {37 },38 "right": {39 }40 }41 },42 {43 "expression": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var esprima = require('esprima');2var fs = require('fs');3var code = fs.readFileSync('test.js', 'utf8');4var ast = esprima.parse(code);5var istanbul = require('istanbul');6var instrumenter = new istanbul.Instrumenter();7var code = 'var a = 1; var b = 2; var c = a + b;';8var instrumentedCode = instrumenter.instrumentSync(code, 'test.js');9console.log(instrumentedCode);10var istanbul = require('istanbul');11var instrumenter = new istanbul.Instrumenter();12var code = 'var a = 1; var b = 2; var c = a + b;';13var instrumentedCode = instrumenter.instrumentSync(code, 'test.js');14var esprima = require('esprima');15var ast = esprima.parse(instrumentedCode);16console.log(ast);

Full Screen

Using AI Code Generation

copy

Full Screen

1var esprima = require("esprima");2var fs = require("fs");3var escodegen = require("escodegen");4var code = fs.readFileSync("test.js");5var ast = esprima.parse(code);6console.log(ast);7var codegen = escodegen.generate(ast);8console.log(codegen);

Full Screen

Using AI Code Generation

copy

Full Screen

1var esprima = require('esprima');2var code = 'function foo() { return 42; }';3var ast = esprima.parse(code);4console.log(ast);5var escodegen = require('escodegen');6var ast = esprima.parse('a = b + c');7var generatedCode = escodegen.generate(ast);8console.log(generatedCode);9var escope = require('escope');10var ast = esprima.parse('var a = 1; function f() { a = 2; }');11var scopeManager = escope.analyze(ast);12scopeManager.scopes.forEach(function(scope) {13 scope.variables.forEach(function(variable) {14 console.log(variable.name);15 });16});17var esrecurse = require('esrecurse');18var ast = esprima.parse('function foo() { return 42; }');19esrecurse.visit(ast, {20 enter: function(node, parent) {21 console.log('entering ' + node.type);22 },23 leave: function(node, parent) {24 console.log('leaving ' + node.type);25 }26});27var esmangle = require('esmangle');28var ast = esprima.parse('var a = 1; a = 2;');29var optimized = esmangle.optimize(ast);30var generatedCode = escodegen.generate(optimized);31console.log(generatedCode);32var escodegen = require('escodegen');33escodegen.attachComments(ast, ast.comments, ast.tokens);34console.log(escodegen.generate(ast));35var estraverse = require('estraverse');36var ast = esprima.parse('var a = 1; a = 2;');37estraverse.traverse(ast, {38 enter: function(node, parent) {39 console.log('entering ' + node.type);40 },41 leave: function(node, parent) {42 console.log('leaving ' + node.type);43 }44});

Full Screen

Using AI Code Generation

copy

Full Screen

1var esprima = require('esprima');2var code = 'var x = 1 + 1;';3var ast = esprima.parse(code);4console.log(ast);5console.log('-------------------------------');6var esprima = require('esprima');7var code = 'var x = 1 + 1;';8var ast = esprima.parseScript(code);9console.log(ast);10console.log('-------------------------------');11var esprima = require('esprima');12var code = 'var x = 1 + 1;';13var ast = esprima.parseScript(code);14console.log(ast);15console.log('-------------------------------');16var esprima = require('esprima');17var code = 'var x = 1 + 1;';18var ast = esprima.parse(code);19console.log(ast);20console.log('-------------------------------');21var esprima = require('esprima');22var code = 'var x = 1 + 1;';23var ast = esprima.parse(code);24console.log(ast);25{ type: 'Program',26 [ { type: 'VariableDeclaration',27 kind: 'var' } ],28 sourceType: 'script' }29{ type: 'Program',30 [ { type: 'VariableDeclaration',31 kind: 'var' } ],32 sourceType: 'script' }33{ type: 'Program',34 [ { type: 'VariableDeclaration',35 kind: 'var' } ],36 sourceType: 'script' }37{ type: 'Program',38 [ { type: 'VariableDeclaration',39 kind: 'var' } ],40 sourceType: 'script' }41{ type: 'Program',42 [ { type: 'VariableDeclaration',43 kind: 'var' } ],44 sourceType: 'script' }

Full Screen

Using AI Code Generation

copy

Full Screen

1var code = escodegen.generate(ast);2fs.writeFileSync('test2.js', code);3fs.writeFileSync('test2.js', code);4fs.writeFileSync('test2.js', code);5fs.writeFileSync('test2.js', code);6fs.writeFileSync('test2.js', code);7fs.writeFileSync('test2.js', code);8fs.writeFileSync('test2.js', code);9fs.writeFileSync('test2.js', code);10fs.writeFileSync('test2.js', code);11fs.writeFileSync('test2.js', code);12fs.writeFileSync('test2.js', code);

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 istanbul 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