getify / let-er Goto Github PK
View Code? Open in Web Editor NEWDEPRECATED: Transpile non-ES6 let-blocks into ES6 (or ES3)
DEPRECATED: Transpile non-ES6 let-blocks into ES6 (or ES3)
ES6 is adding the let
keyword, which will allow block-scoped variable declarations like so:
if (something) {
let isTrue = something;
console.log(isTrue);
}
isTrue; // ReferenceError
Unfortunately, the better let (foo) { .. }
style of code (which btw works in FF since forever), is not supported, which is why BlockScoper will be useful not only today but into the future.
This code transpiler currently makes the try/catch rewrite for let blocks, but it would probably be nice to have it be able to use the new let
keyword in the future once it arrives via ES6.
For instance, we can use naked blocks, and use the style I suggested here: https://gist.github.com/getify/5285514
So, the code transpilation for let (foo) { .. }
could just be in ES6:
/*let*/{ let foo;
// ..
}
Need to investigate whether this would just be a flag you used to switch between try/catch and let
declarations, or if there's anyway feasible to have a runtime feature-test for let
to automatically switch. Probably not, but worth exploring briefly.
you know what to do.
relies on getify/literalizer#4
To significantly reduce the download size of the library, the expression ''()
which is used to throw an error could be replaced with 0()
.
This would need thorough investigation to ensure it is syntactically valid in all environments.
make a grunt contrib task for let-er, dummy. :)
Consider simplifying this tool to purely transpile to the ES6 equiv syntax (like the --es6
tag does), and avoiding any of the try..catch
ES3 stuff, deferring that to some other tool. That would make it clearer that the point of this tool is to put it into the build process before something like traceur.
In the following snippet, the comments are not kept. Investigate how, if any, to keep them.
let /*not-kept*/
// neither is this one
(a,b,c) /* and neither this one */ // or this one
{
a = b * c;
}
Currently, only this style is supported:
let (foo) { .. }
Need to support usage like this:
let (foo = 42, bar) { .. }
The way to handle multiple declarations is to have nested try/catch's, and the way to handle assignments appears to be to change the value being throw
n from void 0
to the assigned value in question. For example:
try{throw 42}catch
/*let*/(foo) {
try{throw void 0}catch
/*let*/(bar) {
// ...
}
}
Should BlockScoper insert let
blocks anywhere that it finds declarations like let foo = 42;
?
The problem that will make that hard to do would be that let
declarations are hoisted to the nearest containing block, so to insert a let
block in the right place (not just where the let
declaration is found), I'll have to actually do full JS parsing and not just regex style pattern matching. That takes this whole thing to a much more complicated level. But it's worth exploring I suppose.
ES6 is going to give us the let operator, which will let us do things like let (foo) { .. }
let-statements and let-expressions won't be in ES6
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.