aMarCruz / jscc

Tiny and powerful preprocessor for conditional comments and replacement of compile-time variables in text files
MIT License
52 stars 8 forks source link

Add support for functional macros #13

Open BobVarioa opened 3 years ago

BobVarioa commented 3 years ago

Adds support for function based macros

Examples :

assert(jscc("$_HI()", '', {
   values: {
       _HI: () => "hi"
   }
}).code === "hi")
assert(jscc('$_HI("one", "two", "three")', '', {
   values: {
       _HI: (...args) => args.join(",")
   }
}).code === "one,two,three")
assert(jscc('$_HI("one", "two", "three")', '', {
   values: {
       _HI: "hi"
   }
}).code === 'hi("one", "two", "three")') // doesn't change existing behavior of non functional values

Function based macros can only be a the top level (e.g. no $_LIB.fs.blah.read(/*etc.*/)) and arguments must be either a string or a number.

I tried to keep the same style as your code and I wrote a few tests (but since they are so simple I didn't really know what else to test).