parseopts.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var test = require('tap').test;
  2. var detective = require('../');
  3. var fs = require('fs');
  4. var src = fs.readFileSync(__dirname + '/files/both.js');
  5. test('nodes specified in opts and parseopts { range: true }', function (t) {
  6. var modules = detective.find(src, { nodes: true, parse: { range: true } });
  7. t.deepEqual(modules.strings, [ 'a', 'b' ]);
  8. t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]);
  9. t.deepEqual(
  10. modules.nodes.map(function (n) {
  11. var arg = n.arguments[0];
  12. return arg.value || arg.left.value;
  13. }),
  14. [ 'a', 'b', 'c', 'd' ],
  15. 'has a node for each require');
  16. var range = modules.nodes[0].range;
  17. t.equal(range[0], 0, 'includes range start');
  18. t.equal(range[1], 12, 'includes range end');
  19. t.end();
  20. });
  21. test('nodes specified in opts and parseopts { range: false }', function (t) {
  22. var modules = detective.find(src, { nodes: true, parse: { range: false } });
  23. t.deepEqual(modules.strings, [ 'a', 'b' ]);
  24. t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]);
  25. t.deepEqual(
  26. modules.nodes.map(function (n) {
  27. var arg = n.arguments[0];
  28. return arg.value || arg.left.value;
  29. }),
  30. [ 'a', 'b', 'c', 'd' ],
  31. 'has a node for each require');
  32. t.notOk(modules.nodes[0].range, 'includes no ranges');
  33. t.end();
  34. });
  35. test('nodes specified in opts and parseopts { range: true, loc: true }', function (t) {
  36. var modules = detective.find(src, { nodes: true, parse: { range: true, loc: true } });
  37. t.deepEqual(modules.strings, [ 'a', 'b' ]);
  38. t.deepEqual(modules.expressions, [ "'c' + x", "'d' + y" ]);
  39. t.deepEqual(
  40. modules.nodes.map(function (n) {
  41. var arg = n.arguments[0];
  42. return arg.value || arg.left.value;
  43. }),
  44. [ 'a', 'b', 'c', 'd' ],
  45. 'has a node for each require');
  46. var range = modules.nodes[0].range;
  47. t.equal(range[0], 0, 'includes range start');
  48. t.equal(range[1], 12, 'includes range end');
  49. var loc = modules.nodes[0].loc;
  50. t.equal(loc.start.line, 1, 'includes start line');
  51. t.equal(loc.start.column, 0, 'includes start column');
  52. t.equal(loc.end.line, 1, 'includes end line');
  53. t.equal(loc.end.column, 12, 'includes end column');
  54. t.end();
  55. });