getOps.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var fs = require('fs');
  4. var path = require('path');
  5. var execSync = require('child_process').execSync;
  6. var $ = require('cheerio');
  7. var fromEntries = require('object.fromentries');
  8. if (process.argv.length !== 3) {
  9. throw new RangeError('please provide a year');
  10. }
  11. var year = parseInt(process.argv[2], 10);
  12. if (year < 2016) {
  13. throw new RangeError('ES2016+ only');
  14. }
  15. var edition = year - 2009;
  16. var specHTMLurl = new URL('https://raw.githubusercontent.com/tc39/ecma262/es' + year + '/spec.html');
  17. var specHTML = String(execSync('curl -q --silent ' + specHTMLurl, { maxBuffer: Infinity }));
  18. var root = $(specHTML);
  19. var aOps = root.filter('[aoid]').add(root.find('[aoid]'));
  20. var missings = [];
  21. var entries = aOps.toArray().map(function (x) {
  22. var op = $(x);
  23. var aoid = op.attr('aoid');
  24. var id = op.attr('id');
  25. if (!id) {
  26. id = op.closest('[id]').attr('id');
  27. }
  28. if (!id) {
  29. missings.push(aoid);
  30. }
  31. return [
  32. aoid,
  33. 'https://ecma-international.org/ecma-262/' + edition + '.0/#' + id
  34. ];
  35. });
  36. if (missings.length > 0) {
  37. console.error('Missing URLs:', missings);
  38. process.exit(1);
  39. }
  40. entries.sort(function (a, b) { return a[0].localeCompare(b[0]); });
  41. var obj = fromEntries(entries);
  42. var outputPath = path.join('operations', year + '.js');
  43. fs.writeFileSync(outputPath, '\'use strict\';\n\nmodule.exports = ' + JSON.stringify(obj, null, '\t') + ';\n');
  44. console.log('npx eslint --quiet --fix ' + outputPath);
  45. execSync('npx eslint --quiet --fix ' + outputPath);