Skip to content

Instantly share code, notes, and snippets.

@mateusvahl
Created December 31, 2014 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateusvahl/45bfb3e10a84e34b0074 to your computer and use it in GitHub Desktop.
Save mateusvahl/45bfb3e10a84e34b0074 to your computer and use it in GitHub Desktop.
`@` alias to `this` with Sweet.js
/**
Sweet.js version: 0.7.2
`this` nickname `@`
Expected:
@ -> this
@something -> this.something
example:
function hello (tag) {
var element = @document.querySelectorAll(tag);
console.log(element, @);
Object.prototype.toString.call(@, 5);
return @;
}
*/
macro @ {
rule { ; } => { this; }
rule { , } => { this, }
rule { $exp } => { this.$exp }
rule { } => { this }
}
@KamilaBorowska
Copy link

To have support for @[name] syntax from CoffeeScript, a following rule can be added before rule { $exp } => { this.$exp }.

rule { [$exp:expr] } => { this[$exp] }

Not sure entirely why last rule doesn't work in that particular case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment