5 ms·
this is already covered in basically any AST transform systems (babylon, acorn, TS...)
by longlho 9y ago
this is already covered in basically any AST transform systems (babylon, acorn, TS...)
- zeckalpha 9y agoThis predates those, and is hygienic.
- adgasf 9y agoI believe that Babel lacks custom infix operators whilst Sweet.js provides them.
- btown 9y agoYou're absolutely right; Babel's parser is very much hand-coded, and adding syntax requires deep knowledge of the internals. Not impossible, but not for the faint of heart. My dreams of having the safe navigation operator from Coffeescript (a?.b?.c) must yet remain unfulfilled... https://github.com/babel/babylon/blob/master/src/parser/expression.js#L461 https://github.com/babel/babylon/blob/master/src/parser/expr...
- scribu 9y agoThat raises a question: What's stopping Babel from using Sweet.js instead of a custom parser? Are there any transpilers that do use Sweet.js?
- zeckalpha 9y agoContracts.coffee IIRC, uses Sweet for Contracts.
- djfm 9y agoWell "a.b.c" violates the principle that you should never access a property more than one level deep anyway ;) But in this case it's pretty straightforward to write a function like "get('b', 'c')(a)" or use a lib like lodash [1] [1]: https://lodash.com/docs/#get https://lodash.com/docs/#get
- btown 9y agoThe Law of Demeter doesn't always apply neatly, especially when you're introspecting JSON sent over the wire and you don't have a "class" with object oriented behavior for each sub object (at best, they're strongly typed). And the runtime overhead AND verbosity of a function may not be ideal, with the string parsing required in lodash and the awkward syntax of the curried function. Something that compiles natively in a DRY way to a && a.b ... would be used widely imo.
- chocolateboy 9y ago> My dreams of having the safe navigation operator from Coffeescript (a?.b?.c) must yet remain unfulfilled... Not for long: https://github.com/babel/babel/pull/5813 https://github.com/babel/babel/pull/5813
- btown 9y agoYou just made my day :)
- andreypopp 9y agoThat's not just "AST transform system", Sweet.js provides a way to extend grammar with new productions. That allows to introduce new syntax constructs.