Permalink
Browse files

first commit

  • Loading branch information...
kittens committed Sep 28, 2014
0 parents commit c97696c224d718d96848df9e1577f337b45464be
Showing with 2,007 additions and 0 deletions.
  1. +11 −0 .editorconfig
  2. +6 −0 .gitignore
  3. +2 −0 .jshintignore
  4. +22 −0 .jshintrc
  5. +5 −0 .npmignore
  6. +5 −0 .travis.yml
  7. +22 −0 LICENSE
  8. +21 −0 Makefile
  9. +134 −0 README.md
  10. +134 −0 bin/6to5
  11. +5 −0 bin/cache-templates
  12. +1 −0 browserify.js
  13. +19 −0 lib/6to5/browserify.js
  14. +66 −0 lib/6to5/middleware.js
  15. +45 −0 lib/6to5/node.js
  16. +1 −0 lib/6to5/templates/arguments-slice-assign-arg.js
  17. +1 −0 lib/6to5/templates/arguments-slice-assign.js
  18. +1 −0 lib/6to5/templates/arguments-slice.js
  19. +1 −0 lib/6to5/templates/array-concat.js
  20. +1 −0 lib/6to5/templates/class-method.js
  21. +7 −0 lib/6to5/templates/class.js
  22. +5 −0 lib/6to5/templates/exports-alias-var.js
  23. +1 −0 lib/6to5/templates/exports-assign.js
  24. +1 −0 lib/6to5/templates/exports-default.js
  25. +1 −0 lib/6to5/templates/exports-require-assign-key.js
  26. +5 −0 lib/6to5/templates/exports-wildcard.js
  27. +1 −0 lib/6to5/templates/function-bind-this.js
  28. +1 −0 lib/6to5/templates/function-call-this.js
  29. +1 −0 lib/6to5/templates/function-call.js
  30. +1 −0 lib/6to5/templates/if-undefined-set-to.js
  31. +8 −0 lib/6to5/templates/inherits.js
  32. +4 −0 lib/6to5/templates/object-define-properties-closure.js
  33. +1 −0 lib/6to5/templates/object-define-properties.js
  34. +1 −0 lib/6to5/templates/prototype-identifier.js
  35. +1 −0 lib/6to5/templates/require-assign-key.js
  36. +1 −0 lib/6to5/templates/require-assign.js
  37. +107 −0 lib/6to5/transform.js
  38. +27 −0 lib/6to5/transformers/arrow-functions.js
  39. +59 −0 lib/6to5/transformers/block-binding.js
  40. +132 −0 lib/6to5/transformers/classes.js
  41. +17 −0 lib/6to5/transformers/default-parameters.js
  42. 0 lib/6to5/transformers/destructuring-assignment.js
  43. 0 lib/6to5/transformers/generators.js
  44. +85 −0 lib/6to5/transformers/modules.js
  45. +32 −0 lib/6to5/transformers/property-method-assignment.js
  46. +19 −0 lib/6to5/transformers/rest-parameters.js
  47. +64 −0 lib/6to5/transformers/spread.js
  48. +40 −0 lib/6to5/transformers/template-literals.js
  49. +68 −0 lib/6to5/traverse/index.js
  50. +46 −0 lib/6to5/traverse/visitor-keys.json
  51. +182 −0 lib/6to5/util.js
  52. +35 −0 package.json
  53. +1 −0 register.js
  54. +7 −0 test/api.js
  55. +51 −0 test/classes.js
  56. +3 −0 test/errors.js
  57. +1 −0 test/fixtures/arrow-functions/empty-arguments/actual.js
  58. +3 −0 test/fixtures/arrow-functions/empty-arguments/expected.js
  59. +1 −0 test/fixtures/arrow-functions/empty-block/actual.js
  60. +2 −0 test/fixtures/arrow-functions/empty-block/expected.js
  61. +1 −0 test/fixtures/arrow-functions/inside-call/actual.js
  62. +3 −0 test/fixtures/arrow-functions/inside-call/expected.js
  63. +1 −0 test/fixtures/arrow-functions/multiple-arguments/actual.js
  64. +3 −0 test/fixtures/arrow-functions/multiple-arguments/expected.js
  65. +1 −0 test/fixtures/arrow-functions/paran-insertion/actual.js
  66. +3 −0 test/fixtures/arrow-functions/paran-insertion/expected.js
  67. +1 −0 test/fixtures/arrow-functions/single-argument/actual.js
  68. +3 −0 test/fixtures/arrow-functions/single-argument/expected.js
  69. +1 −0 test/fixtures/arrow-functions/this-usage/actual.js
  70. +3 −0 test/fixtures/arrow-functions/this-usage/expected.js
  71. +5 −0 test/fixtures/block-binding/block-multiple-let-inside/actual.js
  72. +7 −0 test/fixtures/block-binding/block-multiple-let-inside/expected.js
  73. +4 −0 test/fixtures/block-binding/block-single-let-inside/actual.js
  74. +6 −0 test/fixtures/block-binding/block-single-let-inside/expected.js
  75. +4 −0 test/fixtures/block-binding/closure-ignore-children/actual.js
  76. +4 −0 test/fixtures/block-binding/closure-ignore-children/expected.js
  77. +3 −0 test/fixtures/block-binding/for-block-wrap/actual.js
  78. +5 −0 test/fixtures/block-binding/for-block-wrap/expected.js
  79. +5 −0 test/fixtures/block-binding/for-in-block-wrap/actual.js
  80. +13 −0 test/fixtures/block-binding/for-in-block-wrap/expected.js
  81. +1 −0 test/fixtures/block-binding/ignore-root/actual.js
  82. +5 −0 test/fixtures/block-binding/ignore-root/expected.js
  83. +5 −0 test/fixtures/block-binding/this-usage/actual.js
  84. +13 −0 test/fixtures/block-binding/this-usage/expected.js
  85. +8 −0 test/fixtures/classes/accessing-super-class/actual.js
  86. +17 −0 test/fixtures/classes/accessing-super-class/expected.js
  87. +5 −0 test/fixtures/classes/constructor/actual.js
  88. +6 −0 test/fixtures/classes/constructor/expected.js
  89. +8 −0 test/fixtures/classes/instance-getter-and-setter/actual.js
  90. +15 −0 test/fixtures/classes/instance-getter-and-setter/expected.js
  91. +5 −0 test/fixtures/classes/instance-getter/actual.js
  92. +12 −0 test/fixtures/classes/instance-getter/expected.js
  93. +5 −0 test/fixtures/classes/instance-method/actual.js
  94. +8 −0 test/fixtures/classes/instance-method/expected.js
  95. +5 −0 test/fixtures/classes/instance-setter/actual.js
  96. +12 −0 test/fixtures/classes/instance-setter/expected.js
  97. +1 −0 test/fixtures/classes/plain-class/actual.js
  98. +5 −0 test/fixtures/classes/plain-class/expected.js
  99. +1 −0 test/fixtures/classes/super-class/actual.js
  100. +13 −0 test/fixtures/classes/super-class/expected.js
  101. +3 −0 test/fixtures/comments/preserve-multi-line/actual.js
  102. +3 −0 test/fixtures/comments/preserve-multi-line/expected.js
  103. +5 −0 test/fixtures/comments/preserve-single-line/actual.js
  104. +5 −0 test/fixtures/comments/preserve-single-line/expected.js
  105. +7 −0 test/fixtures/default-parameters/multiple/actual.js
  106. +12 −0 test/fixtures/default-parameters/multiple/expected.js
  107. +3 −0 test/fixtures/default-parameters/single/actual.js
  108. +5 −0 test/fixtures/default-parameters/single/expected.js
  109. +1 −0 test/fixtures/modules/export-as-from/actual.js
  110. +1 −0 test/fixtures/modules/export-as-from/expected.js
  111. +5 −0 test/fixtures/modules/export-default/actual.js
  112. +5 −0 test/fixtures/modules/export-default/expected.js
  113. +1 −0 test/fixtures/modules/export-destructuring-from/actual.js
  114. +2 −0 test/fixtures/modules/export-destructuring-from/expected.js
  115. +3 −0 test/fixtures/modules/export-function/actual.js
  116. +3 −0 test/fixtures/modules/export-function/expected.js
  117. +1 −0 test/fixtures/modules/export-var/actual.js
  118. +7 −0 test/fixtures/modules/export-var/expected.js
  119. +1 −0 test/fixtures/modules/export-wildcard-from/actual.js
  120. +5 −0 test/fixtures/modules/export-wildcard-from/expected.js
  121. +1 −0 test/fixtures/modules/import-as-from/actual.js
  122. +1 −0 test/fixtures/modules/import-as-from/expected.js
  123. +1 −0 test/fixtures/modules/import-destructuring-from/actual.js
  124. +2 −0 test/fixtures/modules/import-destructuring-from/expected.js
  125. +1 −0 test/fixtures/modules/import-from/actual.js
  126. +1 −0 test/fixtures/modules/import-from/expected.js
  127. +1 −0 test/fixtures/modules/module-form/actual.js
  128. +1 −0 test/fixtures/modules/module-form/expected.js
  129. +8 −0 test/fixtures/property-methods-assignment/getter-and-setter/actual.js
  130. +13 −0 test/fixtures/property-methods-assignment/getter-and-setter/expected.js
  131. +5 −0 test/fixtures/property-methods-assignment/getter/actual.js
  132. +10 −0 test/fixtures/property-methods-assignment/getter/expected.js
  133. +5 −0 test/fixtures/property-methods-assignment/method/actual.js
  134. +5 −0 test/fixtures/property-methods-assignment/method/expected.js
  135. +5 −0 test/fixtures/property-methods-assignment/setter/actual.js
  136. +10 −0 test/fixtures/property-methods-assignment/setter/expected.js
  137. +3 −0 test/fixtures/rest-parameters/multiple/actual.js
  138. +3 −0 test/fixtures/rest-parameters/multiple/expected.js
  139. +3 −0 test/fixtures/rest-parameters/single/actual.js
  140. +3 −0 test/fixtures/rest-parameters/single/expected.js
  141. +3 −0 test/fixtures/source-maps/options.json
  142. +9 −0 test/fixtures/spread/arguments/actual.js
  143. +11 −0 test/fixtures/spread/arguments/expected.js
  144. +1 −0 test/fixtures/spread/array-literals/actual.js
  145. +5 −0 test/fixtures/spread/array-literals/expected.js
  146. +1 −0 test/fixtures/spread/contexted-method-call-multiple-args/actual.js
  147. +4 −0 test/fixtures/spread/contexted-method-call-multiple-args/expected.js
  148. +1 −0 test/fixtures/spread/contexted-method-call-single-arg/actual.js
  149. +1 −0 test/fixtures/spread/contexted-method-call-single-arg/expected.js
  150. +1 −0 test/fixtures/spread/method-call-multiple-args/actual.js
  151. +4 −0 test/fixtures/spread/method-call-multiple-args/expected.js
  152. +1 −0 test/fixtures/spread/method-call-single-arg/actual.js
  153. +1 −0 test/fixtures/spread/method-call-single-arg/expected.js
  154. +1 −0 test/fixtures/template-literals/escape-quotes/actual.js
  155. +1 −0 test/fixtures/template-literals/escape-quotes/expected.js
  156. +1 −0 test/fixtures/template-literals/functions/actual.js
  157. +1 −0 test/fixtures/template-literals/functions/expected.js
  158. +1 −0 test/fixtures/template-literals/multiple/actual.js
  159. +1 −0 test/fixtures/template-literals/multiple/expected.js
  160. +1 −0 test/fixtures/template-literals/none/actual.js
  161. +1 −0 test/fixtures/template-literals/none/expected.js
  162. +1 −0 test/fixtures/template-literals/only/actual.js
  163. +1 −0 test/fixtures/template-literals/only/expected.js
  164. +1 −0 test/fixtures/template-literals/single/actual.js
  165. +1 −0 test/fixtures/template-literals/single/expected.js
  166. +38 −0 test/index.js
  167. +3 −0 test/mocha.opts
@@ -0,0 +1,11 @@
root = true
[*]
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.js]
indent_style = space
indent_size = 2
@@ -0,0 +1,6 @@
.DS_Store
node_modules
*.log
*.cache
/templates.json
coverage
@@ -0,0 +1,2 @@
node_modules
test
@@ -0,0 +1,22 @@
{
"esnext": true,
"indent": 2,
"maxlen": 80,
"freeze": true,
"camelcase": true,
"unused": true,
"eqnull": true,
"proto": true,
"newcap": true,
"supernew": true,
"noyield": true,
"evil": true,
"node": true,
"boss": true,
"expr": true,
"undef": true,
"loopfunc": true,
"white": true,
"maxparams": 5,
"maxdepth": 4
}
@@ -0,0 +1,5 @@
.DS_Store
node_modules
*.log
*.cache
lib/6to5/templates
@@ -0,0 +1,5 @@
language: node_js
node_js:
- 0.8
- 0.10
- 0.11
22 LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2014 Sebastian McKenzie
MIT License
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
MOCHA_CMD = node_modules/mocha/bin/_mocha
export NODE_ENV = test
.PHONY: clean test test-cov test-travis publish
clean:
rm -rf coverage templates.json
test:
$(MOCHA_CMD)
test-cov:
make clean
node node_modules/istanbul/lib/cli.js cover $(MOCHA_CMD) --
publish:
make clean
node bin/cache-templates
npm publish
make clean
134 README.md
@@ -0,0 +1,134 @@
# 6to6
**6to5** turns ES6 code into vanilla ES5, so you can use ES6 features **today.**
6to5 is:
- Fast - [10x faster than Traceur](#comparison-to-traceur).
- Compact - maps directly to the equivalent ES5.
- Easy - with Browserify support, Node API, Connect Middleware and a CLI.
- Concise - we do not pollute any scope with unneccesary variables or functions declarations.
## Features
| Name | Implemented |
| -------------------------- | ----------- |
| Arrow functions | ✓ |
| Classes | ✓ |
| Default parameters | ✓ |
| Spread | ✓ |
| Block binding | ✓ |
| Property method assignment | ✓ |
| Rest parameters | ✓ |
| Template literals | ✓ |
| Modules | ✓ |
| Destructuring assignment | |
| Generators | |
## Installation
$ npm install -g 6to5
## Usage
### CLI
Compile the file `script.js` and output it to `script-compiled.js`.
$ 6to5 script.js -o script-compiled.js
Compile the entire `src` directory and output it to the `lib` directory.
$ 6to5 src -d lib
Compile the file `script.js` and output it to stdout.
$ 6to5 script.js
### Node
```javascript
var to5 = require("6to5");
to5.transform("code();");
to5.transformFileSync("script.js");
to5.transformFile("script.js", function (err, data) {
});
```
##### Options
```javascript
to5.transform("code();", {
// List of transformers to EXCLUDE
// See `features` for valid names.
blacklist: [],
// List of transformers to ONLY use.
// See `features` for valid names.
whitelist: [],
// Append source map and comment to bottom of returned output.
sourceMap: false,
// Filename for use in errors etc.
filename: "unknown",
// Format options
// See https://github.com/Constellation/escodegen/wiki/API for options.
format: {}
});
```
#### Require hook
All subsequent files required by node will be transformed into ES5 compatible
code.
```javascript
require("6to5/register");
```
#### Connect Middleware
```javascript
var to5 = require("6to5");
app.use(6to5.middleware({
transform: {
// options to use when transforming files
},
src: "assets",
dest: "cache"
}));
app.use(connect.static("cache"));
```
### Browserify
#### CLI
$ browserify script.js -t 6to5/browserify --outfile bundle.js
#### Node
```javascript
var to5 = require("6to5");
browserify()
.transform(to5.browserify)
.require("script.js", { entry: true })
.bundle({ debug: true })
.pipe(fs.createWriteStream("bundle.js"));
```
## Caveats
### Generators
### Let
## Comparison to Traceur
134 bin/6to5
@@ -0,0 +1,134 @@
#!/usr/bin/env node
var commander = require("commander");
var chokidar = require("chokidar");
var readdir = require("fs-readdir-recursive");
var mkdirp = require("mkdirp");
var 6to5 = require("../lib/6to5/node");
var path = require("path");
var util = require("../lib/6to5/util");
var fs = require("fs");
var _ = require("lodash");
commander.option("-m, --source-maps", "Generate source maps alongside the compiled JavaScript files. Adds sourceMappingURL directives to the JavaScript as well.");
//commander.option("-w, --watch", "Watch, only works with --out-dir");
var list = function (val) {
return val ? val.split(",") : [];
};
commander.option("-w, --whitelist [whitelist]", "Whitelist", list);
commander.option("-b, --blacklist [blacklist]", "Blacklist", list);
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
commander.option("-d, --out-dir [out]", "Compile an input directory of modules into an output directory");
var pkg = require("../package.json");
commander.version(pkg.version);
commander.usage("[options] <files ...>");
commander.parse(process.argv);
//
var errors = [];
var filenames = commander.args;
if (!filenames.length) errors.push("no filenames input");
_.each(filenames, function (filename) {
if (!fs.existsSync(filename)) {
errors.push(filename + " doesn't exist");
}
});
if (commander.outFile && commander.outDir) {
errors.push("cannot have --out-file and --out-dir");
}
if (errors.length) {
console.error(errors.join(". "));
process.exit(2);
}
//
var readdirFilter = function (filename) {
return readdir(filename).filter(util.canCompile);
};
var mainOpts = {
blacklist: commander.blacklist,
whitelist: commander.whitelist,
sourceMap: commander.sourceMaps,
tolerant: commander.tolerant
};
var data = [];
var compile = function (filename) {
var opts = _.clone(mainOpts);
opts.filename = filename;
var file = fs.readFileSync(filename);
return 6to5.transform(file, opts);
};
if (commander.outDir) {
var write = function (src, relative) {
var data = compile(src);
var dest = path.join(commander.outDir, relative);
var up = path.normalize(dest + "/..");
mkdirp.sync(up);
fs.writeFileSync(dest, data);
console.log(src + " -> " + dest);
};
var handle = function (filename) {
var stat = fs.statSync(filename);
if (stat.isDirectory(filename)) {
var dirname = filename;
_.each(readdirFilter(dirname), function (filename) {
write(path.join(dirname, filename), filename);
});
} else {
write(filename, filename);
}
};
_.each(filenames, handle);
} else {
var data = [];
var _filenames = filenames;
filenames = [];
_.each(_filenames, function (filename) {
var stat = fs.statSync(filename);
if (stat.isDirectory()) {
var dirname = filename;
_.each(readdirFilter(filename), function (filename) {
filenames.push(path.join(dirname, filename));
});
} else {
filenames.push(filename);
}
});
_.each(filenames, function (filename) {
data.push(compile(filename) + "\n");
});
data = data.join("");
if (commander.outFile) {
fs.writeFileSync(commander.out, data);
} else {
console.log(data);
}
}
@@ -0,0 +1,5 @@
#!/usr/bin/env node
var fs = require("fs");
var templates = require("../lib/6to5/util").templates;
fs.writeFileSync("templates.json", JSON.stringify(templates));
@@ -0,0 +1 @@
module.exports = require("./lib/6to5/browserify");
@@ -0,0 +1,19 @@
var transform = require("./transform");
var through = require("through");
module.exports = function (filename) {
var data = "";
var write = function (buf) {
data += buf;
};
var end = function () {
var out = transform(data, { filename: filename });
stream.queue(out);
stream.queue(null);
};
var stream = through(write, end);
return stream;
};
Oops, something went wrong.

21 comments on commit c97696c

@jdalton

This comment has been minimized.

Show comment
Hide comment
@jdalton

jdalton Sep 1, 2015

Member

❤️

Member

jdalton replied Sep 1, 2015

❤️

@kittens

This comment has been minimized.

Show comment
Hide comment
@kittens

kittens Sep 1, 2015

Member

💀

Member

kittens replied Sep 1, 2015

💀

@jamiebuilds

This comment has been minimized.

Show comment
Hide comment
@jamiebuilds

jamiebuilds Sep 1, 2015

Member

🍻

Member

jamiebuilds replied Sep 1, 2015

🍻

@prathamesh-sonpatki

This comment has been minimized.

Show comment
Hide comment
@prathamesh-sonpatki

prathamesh-sonpatki Sep 1, 2015

Contributor

🚀

Contributor

prathamesh-sonpatki replied Sep 1, 2015

🚀

@antn

This comment has been minimized.

Show comment
Hide comment
@antn

antn Sep 28, 2015

Contributor

❤️ 1 year later 👍

Contributor

antn replied Sep 28, 2015

❤️ 1 year later 👍

@kubosho

This comment has been minimized.

Show comment
Hide comment
@kubosho

kubosho Sep 28, 2015

Happy 1st birthday! 🎂

Happy 1st birthday! 🎂

@jucrouzet

This comment has been minimized.

Show comment
Hide comment
@jucrouzet

jucrouzet Sep 28, 2015

Contributor

Happy birthday to you babel (former 6to5), thanks for the billion hours you saved us :)

Contributor

jucrouzet replied Sep 28, 2015

Happy birthday to you babel (former 6to5), thanks for the billion hours you saved us :)

@jarofghosts

This comment has been minimized.

Show comment
Hide comment

💜

@GGAlanSmithee

This comment has been minimized.

Show comment
Hide comment
@felixSchl

This comment has been minimized.

Show comment
Hide comment

🍻

@arnold-almeida

This comment has been minimized.

Show comment
Hide comment
@kasiriveni

This comment has been minimized.

Show comment
Hide comment

👏

@reggi

This comment has been minimized.

Show comment
Hide comment

💐

@mariusschulz

This comment has been minimized.

Show comment
Hide comment

🏆

@kentcdodds

This comment has been minimized.

Show comment
Hide comment
@kentcdodds

kentcdodds Nov 17, 2015

Member

💖

Member

kentcdodds replied Nov 17, 2015

💖

@jethrolarson

This comment has been minimized.

Show comment
Hide comment

🍻

@nmn

This comment has been minimized.

Show comment
Hide comment
@nmn

nmn Nov 17, 2015

Contributor

🙏👏😍

Contributor

nmn replied Nov 17, 2015

🙏👏😍

@jg-rc

This comment has been minimized.

Show comment
Hide comment
@jg-rc

jg-rc Nov 19, 2015

👏 🍺

👏 🍺

@mattstyles

This comment has been minimized.

Show comment
Hide comment

😍 💋

@zry656565

This comment has been minimized.

Show comment
Hide comment

🍺

@arjamizo

This comment has been minimized.

Show comment
Hide comment
@arjamizo

arjamizo Jan 6, 2016

It would be really awesome to have some kind of a short description what was the idea behind this code :)! Just for people like me, who are interested in understanding origin of successful project without having to actually read the code, because - I truly believe - that in the code we can find like only "the 12.5%" of the primary idea, which in the end appears to be successful.

edit: I think this might be it: https://medium.com/@sebmck/2015-in-review-51ac7035e272

It would be really awesome to have some kind of a short description what was the idea behind this code :)! Just for people like me, who are interested in understanding origin of successful project without having to actually read the code, because - I truly believe - that in the code we can find like only "the 12.5%" of the primary idea, which in the end appears to be successful.

edit: I think this might be it: https://medium.com/@sebmck/2015-in-review-51ac7035e272

Please sign in to comment.