breuleux/earl-grey

Shorthand for `require: "./servers" as servers`

Closed this issue · 12 comments

davej commented

A lot of my require statements are for local modules, something like:

require:
  "./proxy" as proxy,
  "./server" as server,
  "./router" as router

Do you think it is worthwhile having a shorthand syntax for this pattern?

davej commented

The most obvious shorthand would be:

require:
  "./proxy", "./server", "./router", "./comments/parser", "./../info", "express/sub-module"

which would compile to something like:

let proxy = require("./proxy");
let server = require("./server");
let router = require("./router");
let parser = require("./comments/parser");
let info = require("./../info");
let subModule = require("express/sub-module");

EG would obviously want to check for local namespace collisions. The disadvantage to this of course is that perhaps it's a bit magical and implicit.

I agree that a shorthand would be useful. I was thinking more along the lines of

require:
   .proxy
   .server
   .router
   .comments/parser
   ..info
   express/sub-module
davej commented

I'd be happy with that. Is it possibly a bit confusing because of the existing dot string syntax though? It might be reasonable to expect that require: .proxy would be the same as require: "proxy".

I don't think it'll be a problem. Python uses similar syntax for this. I guess another option is require: ./proxy without the quotes.

davej commented

Ok, cool, both options sound good to me!

I implemented the shorthand, it seems to work. Both require: ./xyz and require: .xyz will work. As an extra, ...x/y is shorthand for ../../x/y (each additional dot goes up one more level).

davej commented

Thanks I'll install from master and try it out.

davej commented

Possibly a bug.

This works fine:

require:
  .proxy

But when I add more than one require it bugs out

require:
  .proxy,
  .servers
E.syntax.failure: The macro expected something different (could not locate error in the source).

/Users/dave/git/test/index.eg 1:0-3:11
  1: require:
  2:   ./proxy,
  3:   ./servers

I tried both the .module and ./module syntax and the behaviour was the same.

Yeah, to implement the feature I would simply get the raw source and interpret it as a string. Unfortunately in this case it picked up the trailing comma as being part of the name. I fixed that and also improved the error message if the same problem was to reoccur.

For the record, you don't need commas to separate the package names if they are on their own line.

davej commented

Would it be worthwhile also supporting non-js files? e.g.

require:
  ./package.json

require: ./package already works, mind you.

davej commented

Ah excellent, I didn't realise that.