Questions tagged [sweet.js]

Sweet.js is a Mozilla Library for adding Macro Compilation to JavaScript

Sweet.js brings the hygienic macros of languages like Scheme and Rust to JavaScript. Macros allow you to alter the syntax of JavaScript and craft the language you've always wanted.

To get a better sense of what macros can do, check out some example macros or play around with macros in the online editor.

68 questions
0
votes
1 answer

Prevent sweet.js from adding numbers to variables

Sweet.js always adds numbers to the end of the output. In most of the javascript I am generating, this is not necessary. Is there a way to configure sweet.js to not add numbers to the end of the parameters? // Example Output module.exports…
Tony J Watson
  • 629
  • 2
  • 9
  • 20
0
votes
1 answer

Inserting arbitrary strings into sweet.js output

How do you insert a arbitrary string into the output that sweet.js generates? This is very useful for programatically doing things where the string is different based on various conditions. For example, on line 25 in the code below, I would like to…
Tony J Watson
  • 629
  • 2
  • 9
  • 20
0
votes
1 answer

Add syntax to JavaScript with Clojure

I want to add new syntax to my JavaScript files much like Sweet.js, but using Clojure/ClojureScript to do the transformation. Here is what I would like to do: //original specialFunction add(a, b) { return a + b; } //transformed function…
Max Bendick
  • 139
  • 1
  • 6
0
votes
1 answer

how to use macro definition in sweet.js?

I'm new to sweet.js. I'm able to run example in official tutorial, but not in this tutorial because it uses macro definition: macro foo { rule { $x } => { $x + 'rule1' } } I'm not sure whether this code is supposed to be processed by sjs or some…
Jack Lu
  • 139
  • 2
  • 12
0
votes
1 answer

sweet.js cannot return #`else`

main.js import {另} from './chinese-macros'; 另 { } chinese-macros.js export syntax 另 = function(ctx) { return #`else`; } main.js should compile to else {} but I get this error message instead: throw this.createError(start, "not a valid…
Brandon
  • 17
  • 7
0
votes
1 answer

Sweet.js: Possible to expand a JS object into a JS object?

Is it possible to use Sweet.Js to expand { "foo": "bar" } to { "bar": "foo" } for example? My naïve attempt here doesn’t work and I don’t see an example in the documentation. Thanks.
Ethan Kent
  • 381
  • 1
  • 4
  • 20
0
votes
2 answers

sweet.js replace this. with @

I want to replace this. with @ sign like in coffee script. I've write the macro: macro (@) { case { return $a } => { return this.$a } } function LogSmth(name) { this.name = name; console.log(@name); } But got SyntaxError: [macro] Macro…
Paul Rumkin
  • 6,737
  • 2
  • 25
  • 35
0
votes
1 answer

Sweet.js Not Outputing Anything

I have a very simple sweet.js file I'm trying to compile: macro @ { rule { $exp }=>{ + $exp + } } ...using this command: sjs -o out.js my_file.js But nothing is getting output; the out.js file is created, but it…
Bryan Green
  • 441
  • 3
  • 18
0
votes
1 answer

Sweet.js prefix a match in a template

Given this macro macro type { case {_ $attr } => { return #{ var a = obj.some$attr } } } type Attr I'm trying to get this output: var a = obj.someAttr; But what I get is var a = obj.some$attr; Adding any non alphanumeric…
jhnstn
  • 2,438
  • 1
  • 18
  • 9
0
votes
1 answer

using global variables in a sweet.js case

I'm trying to create a macro that expands an incrementing value into the output every time it is called. So far I've got this, but it re-initializes the value to 0 every time: macro test1 { case { _ $x } => { var n = n ? n+1 : 0; …
Michael
  • 9,060
  • 14
  • 61
  • 123
0
votes
1 answer

How do I compile multiple files with sweet.js?

How do I compile multiple files using the sweet.js binary? So that something like this: sjs -o out *.js... ...generates separate compiled files in the out directory: out/one.js out/two.js out/three.js This is for an npm module that require()'s…
Raine Revere
  • 30,985
  • 5
  • 40
  • 52
0
votes
0 answers

"Unknown error" in sweetjs file using Visual Studio Web Essentials

I'm just starting to learn about Sweet.JS, and was excited to realize it's already built into Visual Studio 2013 Web Essentials. I decided to first make a macro to shorten anonymous functions to fn. macro fn { rule { $params $body }…
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102
0
votes
1 answer

what are some techniques for DRY-ing up macros in Sweet.js?

let's say i have these two macros which are identical except for the macro name: macro h1 { case {$name ($x (,) ...)} => { letstx $nameVal = [makeValue(unwrapSyntax(#{$name}), null)] return #{React.createElement($nameVal, $x (,) ...)} …
tony_k
  • 1,983
  • 2
  • 20
  • 27
0
votes
1 answer

what is the proper way to reference one sweet.js macro from another?

tl;dr when i have macro's in separate files i get an error, but if macro's are in same file it seems to work. test-macro-1.js: macro genVar { case {$name ($varName, $varVal, $name2)} => { letstx $ident = [makeIdent(unwrapSyntax(#{$varName}),…
tony_k
  • 1,983
  • 2
  • 20
  • 27
0
votes
2 answers

sweet.js how to match function statement with infix

I can't seem to get a match let function = macro { case infix {$name:ident $[=] | _ ($params ...) { $body ...} } => { return #{ $name = function $name ($params ...) { console.log($name.name); $body ... } } …
pelón
  • 393
  • 2
  • 5