torture.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * JS-grammar torture fixture (javascript variant: no type machinery, JS class
  3. * fields use `field_definition` with a `property` field).
  4. */
  5. import { EventEmitter } from 'node:events';
  6. const { promisify } = require('node:util');
  7. /** Legacy prototype-style helper. */
  8. function legacyHelper(a, b) {
  9. return a + b;
  10. }
  11. const arrow = (x) => legacyHelper(x, 1);
  12. class Widget extends EventEmitter {
  13. static registry = new Map();
  14. #privateField = 1;
  15. label = 'w';
  16. onTick = () => {
  17. this.render();
  18. };
  19. wrapped = debounce(function () {
  20. expensive();
  21. }, 50);
  22. constructor(opts) {
  23. super();
  24. this.opts = opts;
  25. register(this.onTick);
  26. }
  27. render() {
  28. paint(this.label);
  29. }
  30. static create(opts) {
  31. return new Widget(opts);
  32. }
  33. }
  34. // AMD-style wrapper — anonymous, but inner functions must still surface.
  35. (function () {
  36. function hiddenInner() {
  37. return 7;
  38. }
  39. hiddenInner();
  40. })();
  41. module.exports.makeWidget = function makeWidget(opts) {
  42. return Widget.create(opts);
  43. };
  44. // Vuex module shape (store-file signals: mutations + actions + getters).
  45. const mutations = {
  46. SET_USER(state, user) {
  47. state.user = user;
  48. },
  49. };
  50. const actions = {
  51. async loadUser({ commit }, id) {
  52. const user = await fetchUser(id);
  53. commit('SET_USER', user);
  54. },
  55. };
  56. export default {
  57. namespaced: true,
  58. state: () => ({ user: null }),
  59. mutations,
  60. actions,
  61. getters: {
  62. userName(state) {
  63. return state.user?.name;
  64. },
  65. },
  66. };
  67. // Initializer walks attributed to the declared symbol (#693). A plain call
  68. // leaked to the FILE node; a non-exported object literal was skipped outright.
  69. const eagerConfig = loadConfig();
  70. const handlerMap = { onSave: () => persist(eagerConfig), onLoad: loadConfig() };
  71. const lazyList = [() => persist(eagerConfig)];
  72. // --- CommonJS export assignments (#1675) -----------------------------------
  73. exports.getItems = async (req, res) => { res.json(await findItems()); };
  74. module.exports.deleteItem = function (req, res) { removeItem(req.params.id); res.end(); };
  75. exports.plain = 42;
  76. handlers.onSave = () => { persist(); };
  77. // --- call-expression receivers (#1683) ----------------------------------------
  78. function bucketChains(d, k, v) {
  79. d.setdefault(k, []).append(v);
  80. make().run();
  81. (0, make)().run();
  82. arr[0]().go();
  83. obj.make().run().again();
  84. }