GPTScript / AiScript

A Minimal, Full-Stack, Tool-Assisted Language. Native to Browsers and Bun. Strictly & Strongly-Typed.
https://github.com/GPTScript/AiScript
Mozilla Public License 2.0
9 stars 1 forks source link

No meta programming / subscripting on imports / exports #8

Open coolaj86 opened 2 years ago

coolaj86 commented 2 years ago

Rationale

Example: Exports

Bad

Object.assign(module.exports, {
  create,
  get,
  update,
  destroy
});

Good

let Crud = module.exports;

Crud.create = function () {
  // ...
};

// ...

Example: Dynamic Imports

Bad

let DynamicThings = {};
Fs.readdir(`./things/`).forEach(function (node) {
  DynamicThings[node] = require(`./things/${node}`);
});

Good

If possible, the requires should be listed manually

let DynamicThings = {
  Foo: require('./foo.js'),
  Bar: require('./bar.js'),
};

Otherwise, create a template for npm pre-publish (which also runs on npm install, locally) or similar:

let js = `"use strict";
let DynamicThings = {};
`;

Fs.readdir(`./things/`).forEach(function (node) {
  // the final require will have the literal value of ${node}, not the variable
  js += `DynamicThings.${node} = require(\`./things/${node}\`);\n`
});
js += `\n`;

Fs.writeFile(`./generated-things.js`, js, `utf8`);

Require the generated file:

let DynamicThings = require('/generated-things.js');