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
7
votes
0 answers

Using Sweet.js and Babel

So I've been trying to get Sweet.js working on my ES6 project, using Webpack to compile it all. I've been able to get each to work separately, but no matter how I've tried to put them together it has given some sort of error... I think it's mostly…
foxfriends
  • 91
  • 1
  • 6
6
votes
2 answers

What is a JavaScript Macro?

Recently I have seen people talk about the using of macros in JavaScript. I have no idea what that means and after looking up documentation on MDN I came up without any answer. So that leads me to my question … What are JavaScript macros? How/why…
user3084728
  • 565
  • 1
  • 8
  • 16
5
votes
1 answer

What's the use case for babel-plugin-macros vs. Sweet.js?

I'm curious about macros in JavaScript and am a little confused about how babel-plugin-macros and Sweet.js compare to each other. Do they both solve the same problem? Do they both fit into the same toolchain? In particular, I'm hoping to use one of…
Max Heiber
  • 14,346
  • 12
  • 59
  • 97
4
votes
1 answer

sweet.js: transforming occurrences of a repeated token

I want to define a sweet macro that transforms { a, b } # o into { o.a, o.b } My current attempt is macro (#) { case infix { { $prop:ident (,) ... } | _ $o } => { return #{ { $prop: $o.$prop (,) ... } } } } However, this give…
user663031
3
votes
0 answers

Local variable between inner macro calls

I'm trying to create a pair of sweet js macros where an inner and outer macro share a local variable: syntax cdo = function(ctx) { let call = ctx.next().value; let dummy = #`dummy`.get(0); let context_id =…
Paul Johnson
  • 1,329
  • 1
  • 12
  • 25
3
votes
0 answers

Concatenating a syntax object in sweet.js

I'd like to create a macro in sweet.js that allows the user to write for loops in javascript using something akin to the index notation used in tensor calculus. If I supply it with something like: tensor foo[i][j] = baz[j] + bar[i]; sweet.js will…
16807
  • 1,418
  • 2
  • 18
  • 32
3
votes
2 answers

What to do about sweet.js renaming top-level variables in Typescript output?

Typescript compiles class ClassName { } to var ClassName = function () { function ClassName() { } return ClassName; }(); I run that JS code through sweet.js, which even when there are no macros defined, produces something like…
Dustin Wehr
  • 165
  • 9
3
votes
2 answers

Lazy evaluation macro with Sweet.js

I just got in JavaScript and noticed that lazy evaluation is not directly supported in this language. Natively the code turns into the hell of boiler plate like this: function lazy(f) { var v = undefined; return function() { if (v ==…
Ryoichiro Oka
  • 1,949
  • 2
  • 13
  • 20
3
votes
8 answers

Javascript macro: implementing F# style forward pipe operator

I want to implement a higher order function (hof) that essentially works like F# style forward-pipe operator (passes a value as the first argument to another function, myFunc). The only way I can think of is this: function hof(val, myFunc,…
tldr
  • 11,924
  • 15
  • 75
  • 120
2
votes
0 answers

Getting the literal value of a parameter to a sweet.js macro

I've got a macro defined and I'm trying to test one of the values passed as a parameter to determine how to emit code. However, I keep getting errors thrown when I try to use the value. Here is a simple test that illustrates the problem: let Test…
Michael
  • 9,060
  • 14
  • 61
  • 123
2
votes
1 answer

How to break SweetJS hygiene for local variable?

I am attempting to use SweetJS in my project. In order to better understand and learn SweetJS I thought I would start with a simple "class" macro (I know a few exist, just playing around here...). I can NOT seem to get SweetJS to stop messing with…
th317erd
  • 304
  • 3
  • 11
2
votes
2 answers

How to create a parameterized infix macro in sweet.js

In the bluebird wiki article about JavaScript optimization killers, the author mentions that passing the arguments keyword to any function (except apply) will cause the parent function to not be optimizable. I would like to create a sweet.js macro…
kzh
  • 19,810
  • 13
  • 73
  • 97
2
votes
1 answer

Sweet.js: macro that targets variables before the tokens?

One of the items on my TODO list is creating a fun coding language that 'compiles' to JavaScript. I would like to try out Sweet.js for this, but I'm wondering if it is possible to write a rule that targets keywords that are before the rule? As an…
thomaux
  • 19,133
  • 10
  • 76
  • 103
2
votes
1 answer

Sweet.js - How to put variable in identifier name and string variable?

I'm brand new to Sweet.js. My first simple macro is the following macro test { rule { $className($entityName) } => { function test$className() { console.print("$className"); console.print("$entityName"); } } } test…
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
2
votes
2 answers

Generate dynamic comments with sweet.js

How can I transform the following snippet: let myVar: string = 'test'; To following output: // type {string} var myVar = 'test'; using sweetjs? UPDATE I'm looking for a way to transform the exact first code snippet to the second one. Including the…
Diullei
  • 11,420
  • 2
  • 27
  • 31
1
2 3 4 5