print-script 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }