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
2
votes
1 answer

Sweet.js macro that calls a method

I'm trying to write a sweet.js macro which needs to generate method call syntax, obj.method(), but the method is passed in to the macro as a literal expression. For example: mcall(obj, toString().length); // becomes obj.toString().length; I've got…
Daniel Buckmaster
  • 7,108
  • 6
  • 39
  • 57
2
votes
1 answer

Convert sweet.js argument into string

How would you create a string from an argument to a sweet.js macro? For example: let foo = macro { rule { $name } => { console.log('$name', $name); } } var x = 42; foo x Will output: console.log(x, x); When I'd prefer…
Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
2
votes
1 answer

is it possible to create a sweetjs macro making multiline strings possible in javascript?

I am really new to sweet.js. I would love to have multiline strings in javascript just like in EcmaScript 6: var htmlString = `Say hello to multi-line strings!`; Is it possible to formulate a sweetjs macro handling that (and how ??) ? Furthermore,…
lgersman
  • 2,178
  • 2
  • 19
  • 21
2
votes
1 answer

Implement multiline skinny arrow syntax in sweetjs

I'm playing around with sweetjs and for the life of me can't figure out why this rule for parameterless multiline skinny arrow syntax isn't matched Code: macro -> { rule infix { () | { $body ... $last:expr } } => { function( ) { $body…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
2
votes
2 answers

Sweet.js - Expand token as a string

I want to expand a token to a string. For example, I have this macro: macro String { rule { $x } => { "$x" } } I would expect String 1 to expand to "1", however it expands to just 1; How can I accomplish this? EDIT: This seems…
jviotti
  • 17,881
  • 26
  • 89
  • 148
1
vote
0 answers

Typescript Compiler - Implement custom transformer

Iv'e read alot of the Typescript github issues regarding implementation but there's little documentation or people working in this area. My goal is to basically have typescript understand that.. [1,2,3,4,5].filter(_ % 2).map(_ + 2) [{name: "test"},…
Shanon Jackson
  • 5,873
  • 1
  • 19
  • 39
1
vote
2 answers

how to add sweetalert pop-up messages in html with jascript

I'm trying to set the sweetalert pop-up messages while clicking the edit button . here is the sample code:
me text
  • 21
  • 1
  • 4
1
vote
0 answers

Defining a new operator that performs member access

I'm trying to create a custom operator that is a synonym for the dot (member access) operator, such that a>>=b results in a.b. My first attempt does not compile operator >>= left 19 = (left, right) => { return #`${left}.${right}]`; }; If i…
Kevin
  • 2,813
  • 3
  • 20
  • 30
1
vote
1 answer

Extend Javascript Syntax to Add Typing

I'd like to extend javascript to add custom type checking. e.g. function test(welcome:string, num:integer:non-zero) { console.log(welcome + num) } which would compile into: function test(welcome, num) { …
Tony J Watson
  • 629
  • 2
  • 9
  • 20
1
vote
0 answers

Can't create a haskell like macro in sweetjs

I want to create a macro like this var diffLength = | index1 === 0 then xPos[index1] | index1 === 2 then (xPos[index1] - xPos[index1 - 1]) * focusFactor | otherwise xPos[index1] - xPos[index1 - 1] which should expand like : var…
kalpa
  • 657
  • 2
  • 11
  • 22
1
vote
0 answers

accessing nested scopes in sweet.js

I'm trying to define some macros which allow a sequence of function calls to be declared for functions which are really asynchronous. The macros convert these sequence of calls into continuations; namely, a function which takes two parameters: the…
Michael
  • 9,060
  • 14
  • 61
  • 123
1
vote
1 answer

Sweet.js macro argument expand to another macro in body, which is not wanted

I am trying to write a sweet macro but have some troubles. macro to_str { case { _ ($tok) } => { return [makeValue(unwrapSyntax(#{$tok}) + '=', #{ here })]; } } macro foo { rule {($vars (,) ...) } => { $(to_str($vars) + $vars)…
astropeak
  • 147
  • 1
  • 8
1
vote
1 answer

SweetJS : Write a macro for a specific library

I'm currently working on a little project which consists about writing macros for Ramda. Here is an example : let map = macro { rule { $f $array } => { R.map($f, $array) } rule { $f } => { R.map($f) } } I tried to compile this simple sample…
ThomasC__
  • 311
  • 3
  • 9
1
vote
1 answer

Escape Parentheses in sweet.js Macro Name

I want to write a spread macro, but in order to do so, I need to override how functions are called like so: someFn(..>someArray) Which needs to output: someFn.apply(null, someArray) I have tried by creating a macro named (. I've seen that you do…
1
vote
1 answer

Sweet.js Macro taking anything passed to macro taking structure failing

I have a macro where I take in any code in between parenthesis. I then pass it to another macro that has rules on that code. I do other things in test and it is named differently, but I figured a minimal testcase would be useful here. macro testimpl…
Havvy
  • 1,471
  • 14
  • 27