print-script 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #! /usr/bin/env node
  2. /* eslint-disable no-console, no-var */
  3. // Util script for debugging source code generation issues
  4. var script = process.argv[2].replace(/\\n/g, '\n'),
  5. verbose = process.argv[3] === '-v';
  6. var Handlebars = require('./lib'),
  7. SourceMap = require('source-map'),
  8. SourceMapConsumer = SourceMap.SourceMapConsumer;
  9. var template = Handlebars.precompile(script, {
  10. srcName: 'input.hbs',
  11. destName: 'output.js',
  12. assumeObjects: true,
  13. compat: false,
  14. strict: true,
  15. trackIds: true,
  16. knownHelpersOnly: false
  17. });
  18. if (!verbose) {
  19. console.log(template);
  20. } else {
  21. var consumer = new SourceMapConsumer(template.map),
  22. lines = template.code.split('\n'),
  23. srcLines = script.split('\n');
  24. console.log();
  25. console.log('Source:');
  26. srcLines.forEach(function(source, index) {
  27. console.log(index + 1, source);
  28. });
  29. console.log();
  30. console.log('Generated:');
  31. console.log(template.code);
  32. lines.forEach(function(source, index) {
  33. console.log(index + 1, source);
  34. });
  35. console.log();
  36. console.log('Map:');
  37. console.log(template.map);
  38. console.log();
  39. function collectSource(lines, lineName, colName, order) {
  40. var ret = {},
  41. ordered = [],
  42. last;
  43. function collect(current) {
  44. if (last) {
  45. var mapLines = lines.slice(last[lineName] - 1, current && current[lineName]);
  46. if (mapLines.length) {
  47. if (current) {
  48. mapLines[mapLines.length - 1] = mapLines[mapLines.length - 1].slice(0, current[colName]);
  49. }
  50. mapLines[0] = mapLines[0].slice(last[colName]);
  51. }
  52. ret[last[lineName] + ':' + last[colName]] = mapLines.join('\n');
  53. ordered.push({
  54. startLine: last[lineName],
  55. startCol: last[colName],
  56. endLine: current && current[lineName]
  57. });
  58. }
  59. last = current;
  60. }
  61. consumer.eachMapping(collect, undefined, order);
  62. collect();
  63. return ret;
  64. }
  65. srcLines = collectSource(srcLines, 'originalLine', 'originalColumn', SourceMapConsumer.ORIGINAL_ORDER);
  66. lines = collectSource(lines, 'generatedLine', 'generatedColumn');
  67. consumer.eachMapping(function(mapping) {
  68. var originalSrc = srcLines[mapping.originalLine + ':' + mapping.originalColumn],
  69. generatedSrc = lines[mapping.generatedLine + ':' + mapping.generatedColumn];
  70. if (!mapping.originalLine) {
  71. console.log('generated', mapping.generatedLine + ':' + mapping.generatedColumn, generatedSrc);
  72. } else {
  73. console.log('map',
  74. mapping.source,
  75. mapping.originalLine + ':' + mapping.originalColumn,
  76. originalSrc,
  77. '->',
  78. mapping.generatedLine + ':' + mapping.generatedColumn,
  79. generatedSrc);
  80. }
  81. });
  82. }