Comparing version 1.3.1 to 1.4.0
@@ -7,10 +7,32 @@ var traverse = require("../traverse"); | ||
exports.ClassDeclaration = function (node) { | ||
return b.variableDeclaration("var", [ | ||
b.variableDeclarator(node.id, buildClass(node)) | ||
]); | ||
}; | ||
exports.ClassExpression = function (node) { | ||
return buildClass(node); | ||
}; | ||
var getMemberExpressionObject = function (node) { | ||
while (node.type === "MemberExpression") { | ||
node = node.object; | ||
} | ||
return node; | ||
}; | ||
var buildClass = function (node) { | ||
var superName = node.superClass; | ||
var className = node.id; | ||
var root = util.template("class", { | ||
var superClassReference = node.superClass; | ||
if (superName && superName.type === "MemberExpression") { | ||
superClassReference = getMemberExpressionObject(superName); | ||
} | ||
var container = util.template("class", { | ||
CLASS_NAME: className | ||
}, true); | ||
}); | ||
var container = root.declarations[0].init; | ||
var block = container.callee.body; | ||
@@ -27,4 +49,4 @@ var body = block.body; | ||
container.arguments.push(superName); | ||
container.callee.params.push(superName); | ||
container.arguments.push(superClassReference); | ||
container.callee.params.push(superClassReference); | ||
} | ||
@@ -36,3 +58,3 @@ | ||
return root; | ||
return container; | ||
}; | ||
@@ -39,0 +61,0 @@ |
@@ -9,81 +9,96 @@ var util = require("../util"); | ||
exports.VariableDeclaration = function (node, parent, opts, generateUid) { | ||
var nodes = []; | ||
var hasPattern = false; | ||
_.each(node.declarations, function (declar) { | ||
if (isPattern(declar.id)) { | ||
hasPattern = true; | ||
return false; | ||
var buildVariableAssign = function (kind, id, init) { | ||
return b.variableDeclaration(kind, [ | ||
b.variableDeclarator(id, init) | ||
]); | ||
}; | ||
var push = function (kind, nodes, pattern, parentId) { | ||
if (pattern.type === "ObjectPattern") { | ||
pushObjectPattern(kind, nodes, pattern, parentId); | ||
} else if (pattern.type === "ArrayPattern") { | ||
pushArrayPattern(kind, nodes, pattern, parentId); | ||
} | ||
}; | ||
var pushObjectPattern = function (kind, nodes, pattern, parentId) { | ||
_.each(pattern.properties, function (prop) { | ||
var pattern2 = prop.value; | ||
var patternId2 = b.memberExpression(parentId, prop.key, false); | ||
if (isPattern(pattern2)) { | ||
push(kind, nodes, pattern2, patternId2); | ||
} else { | ||
nodes.push(buildVariableAssign(kind, pattern2, patternId2)); | ||
} | ||
}); | ||
if (!hasPattern) return; | ||
}; | ||
// | ||
var pushArrayPattern = function (kind, nodes, pattern, parentId) { | ||
_.each(pattern.elements, function (elem, i) { | ||
if (!elem) return; | ||
var buildVariableAssign = function (id, init) { | ||
return b.variableDeclaration(node.kind, [ | ||
b.variableDeclarator(id, init) | ||
]); | ||
}; | ||
var newPatternId = b.memberExpression(parentId, b.literal(i), true); | ||
var push = function (pattern, parentId) { | ||
if (pattern.type === "ObjectPattern") { | ||
pushObjectPattern(pattern, parentId); | ||
} else if (pattern.type === "ArrayPattern") { | ||
pushArrayPattern(pattern, parentId); | ||
if (elem.type === "Identifier") { | ||
nodes.push(buildVariableAssign(kind, elem, newPatternId)); | ||
} else { | ||
push(kind, nodes, elem, newPatternId); | ||
} | ||
}; | ||
}); | ||
}; | ||
var pushObjectPattern = function (pattern, parentId) { | ||
_.each(pattern.properties, function (prop) { | ||
var id = prop.value; | ||
var pushPattern = function (kind, nodes, pattern, parentId, generateUid) { | ||
if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") { | ||
var key = generateUid("ref"); | ||
var init = b.memberExpression(parentId, prop.key, false); | ||
nodes.push(util.template("variable-assign", { | ||
KEY: key, | ||
VALUE: parentId | ||
}, true)); | ||
if (isPattern(id)) { | ||
push(id, init); | ||
} else { | ||
nodes.push(buildVariableAssign(id, init)); | ||
} | ||
}); | ||
}; | ||
parentId = b.identifier(key); | ||
} | ||
var pushArrayPattern = function (pattern, parentId) { | ||
_.each(pattern.elements, function (id, i) { | ||
var init = b.memberExpression(parentId, b.literal(i), true); | ||
push(kind, nodes, pattern, parentId); | ||
}; | ||
if (id.type === "Identifier") { | ||
nodes.push(buildVariableAssign(id, init)); | ||
} else { | ||
push(id, init); | ||
} | ||
}); | ||
}; | ||
exports.FunctionDeclaration = | ||
exports.FunctionExpression = function (node, parent, opts, generateUid) { | ||
var block = node.body; | ||
var nodes = []; | ||
var pushPattern = function (id, init) { | ||
if (init.type !== "MemberExpression" && init.type !== "Identifier") { | ||
var key = generateUid("ref"); | ||
node.params = node.params.map(function (pattern) { | ||
if (!isPattern(pattern)) return pattern; | ||
nodes.push(util.template("variable-assign", { | ||
KEY: key, | ||
VALUE: init | ||
}, true)); | ||
var parentId = b.identifier(generateUid("ref")); | ||
init = b.identifier(key); | ||
} | ||
pushPattern("var", nodes, pattern, parentId, generateUid); | ||
push(id, init); | ||
}; | ||
return parentId; | ||
}); | ||
// | ||
block.body = nodes.concat(block.body); | ||
}; | ||
exports.VariableDeclaration = function (node, parent, opts, generateUid) { | ||
var nodes = []; | ||
var hasPattern = false; | ||
_.each(node.declarations, function (declar) { | ||
var init = declar.init; | ||
var id = declar.id; | ||
if (isPattern(declar.id)) { | ||
hasPattern = true; | ||
return false; | ||
} | ||
}); | ||
if (!hasPattern) return; | ||
if (isPattern(id)) { | ||
pushPattern(id, init); | ||
_.each(node.declarations, function (declar) { | ||
var patternId = declar.init; | ||
var pattern = declar.id; | ||
if (isPattern(pattern)) { | ||
pushPattern(node.kind, nodes, pattern, patternId, generateUid); | ||
} else { | ||
nodes.push(buildVariableAssign(id, init)); | ||
nodes.push(buildVariableAssign(node.kind, declar.id, declar.init)); | ||
} | ||
@@ -90,0 +105,0 @@ }); |
@@ -0,1 +1,2 @@ | ||
var path = require("path"); | ||
var util = require("../util"); | ||
@@ -5,19 +6,34 @@ var b = require("ast-types").builders; | ||
var toModuleNameIdentifier = function (node) { | ||
var id = node.source.value; | ||
id = path.basename(id, path.extname(id)); | ||
id = util.camelCase(id); | ||
return b.identifier(id); | ||
}; | ||
exports.ImportDeclaration = function (node) { | ||
var nodes = []; | ||
_.each(node.specifiers, function (specifier) { | ||
var variableName = specifier.name || specifier.id; | ||
if (node.specifiers.length) { | ||
_.each(node.specifiers, function (specifier) { | ||
var variableName = specifier.name || specifier.id; | ||
var key = specifier.id.name; | ||
var key = specifier.id.name; | ||
var templateName = "require-assign"; | ||
if (specifier.type === "ImportSpecifier") { | ||
if (key !== "default") templateName += "-key"; | ||
} | ||
var templateName = "require-assign"; | ||
if (node.kind !== "default") templateName += "-key"; | ||
nodes.push(util.template(templateName, { | ||
VARIABLE_NAME: variableName.name, | ||
MODULE_NAME: node.source, | ||
KEY: key | ||
nodes.push(util.template(templateName, { | ||
VARIABLE_NAME: variableName.name, | ||
MODULE_NAME: node.source.raw, | ||
KEY: key | ||
})); | ||
}); | ||
} else { | ||
nodes.push(util.template("require-assign", { | ||
VARIABLE_NAME: toModuleNameIdentifier(node), | ||
MODULE_NAME: node.source.raw | ||
})); | ||
}); | ||
} | ||
@@ -27,12 +43,3 @@ return nodes; | ||
exports.ModuleDeclaration = function (node) { | ||
return util.template("require-assign", { | ||
VARIABLE_NAME: node.id, | ||
MODULE_NAME: node.source | ||
}); | ||
}; | ||
exports.ExportDeclaration = function (node) { | ||
var nodes = []; | ||
var pushSpecifiers = function (node, nodes) { | ||
_.each(node.specifiers, function (specifier) { | ||
@@ -44,8 +51,18 @@ var variableName = specifier.name || specifier.id; | ||
nodes.push(util.template("exports-wildcard", { | ||
MODULE_NAME: node.source | ||
MODULE_NAME: node.source.raw | ||
}, true)); | ||
} else { | ||
nodes.push(util.template("exports-require-assign-key", { | ||
var templateName; | ||
if (variableName.name === "default") { | ||
templateName = "exports-default-require"; | ||
} else { | ||
templateName = "exports-require-assign"; | ||
} | ||
if (specifier.id.name !== "default") templateName += "-key"; | ||
nodes.push(util.template(templateName, { | ||
VARIABLE_NAME: variableName.name, | ||
MODULE_NAME: node.source, | ||
MODULE_NAME: node.source.raw, | ||
KEY: specifier.id | ||
@@ -55,38 +72,53 @@ }, true)); | ||
} else { | ||
nodes.push(util.template("exports-assign", { | ||
VALUE: specifier.id, | ||
KEY: specifier.id | ||
}, true)); | ||
if (variableName.name === "default") { | ||
nodes.push(util.template("exports-default", { | ||
VALUE: specifier.id | ||
}, true)); | ||
} else { | ||
nodes.push(util.template("exports-assign", { | ||
VALUE: specifier.id, | ||
KEY: variableName | ||
}, true)); | ||
} | ||
} | ||
}); | ||
}; | ||
var pushDeclaration = function (node, nodes) { | ||
var declar = node.declaration; | ||
if (declar) { | ||
if (node.default) { | ||
nodes.push(util.template("exports-default", { | ||
VALUE: declar | ||
}, true)); | ||
} else { | ||
if (declar.type === "VariableDeclaration") { | ||
nodes.push(declar); | ||
if (node.default) { | ||
if (declar.type === "FunctionDeclaration") { | ||
declar.type = "FunctionExpression"; | ||
} else if (declar.type === "ClassDeclaration") { | ||
declar.type = "ClassExpression"; | ||
} | ||
_.each(declar.declarations, function (declar) { | ||
nodes.push(util.template("exports-alias-var", { | ||
STRING_KEY: b.literal(declar.id.name), | ||
KEY: declar.id | ||
}, true)); | ||
}); | ||
} else if (declar.type === "FunctionDeclaration") { | ||
declar.type = "FunctionExpression"; | ||
nodes.push(util.template("exports-default", { | ||
VALUE: declar | ||
}, true)); | ||
} else { | ||
var id = declar.id; | ||
if (declar.type === "VariableDeclaration") { | ||
id = declar.declarations[0].id; | ||
} | ||
nodes.push(util.template("exports-assign", { | ||
KEY: declar.id, | ||
VALUE: declar | ||
}, true)); | ||
} | ||
} | ||
nodes.push(declar); | ||
nodes.push(util.template("exports-assign", { | ||
VALUE: id, | ||
KEY: id | ||
}, true)); | ||
} | ||
}; | ||
exports.ExportDeclaration = function (node) { | ||
var nodes = []; | ||
if (node.declaration) { | ||
pushDeclaration(node, nodes); | ||
} else { | ||
pushSpecifiers(node, nodes); | ||
} | ||
return nodes; | ||
}; |
var estraverse = require("estraverse"); | ||
var escodegen = require("escodegen"); | ||
var traverse = require("./traverse"); | ||
var esprima = require("esprima"); | ||
var esprima = require("esprima-fb"); | ||
var path = require("path"); | ||
@@ -82,2 +82,8 @@ var fs = require("fs"); | ||
exports.camelCase = function (value) { | ||
return value.replace(/[-_\s]+(.)?/g, function (match, c){ | ||
return c ? c.toUpperCase() : ""; | ||
}); | ||
}; | ||
exports.canCompile = function (filename) { | ||
@@ -84,0 +90,0 @@ var ext = path.extname(filename); |
{ | ||
"name": "6to5", | ||
"description": "Turn ES6 code into vanilla ES5 with source maps and no runtime", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"author": "Sebastian McKenzie <sebmck@gmail.com>", | ||
@@ -43,3 +43,3 @@ "homepage": "https://github.com/sebmck/6to5", | ||
"escodegen": "https://github.com/Constellation/escodegen/archive/624550d0d6edf812a661902c6908dedff66005a7.tar.gz", | ||
"esprima": "https://github.com/esnext/esprima/archive/ebda4fb14a2aad61c54a83006c061c1649636985.tar.gz", | ||
"esprima-fb": "7001.1.0-dev-harmony-fb", | ||
"estraverse": "1.5.1", | ||
@@ -59,4 +59,5 @@ "fs-readdir-recursive": "0.0.2", | ||
"esnext": "0.11.1", | ||
"es6now": "0.8.11" | ||
"es6now": "0.8.11", | ||
"jstransform": "^6.3.2" | ||
} | ||
} |
@@ -29,3 +29,3 @@ <p align="center"> | ||
- **Compact** - maps directly to the equivalent ES5 with **no runtime**. | ||
- **Concise** - does not pollute scope with unneccesary variables. | ||
- **Concise** - does not pollute scope with unnecessary variables. | ||
@@ -137,3 +137,4 @@ ## Installation | ||
// If truthy, adds a `map` property to returned output. | ||
// If set to "inline", a comment with a sourceMappingURL directive is added to the bottom | ||
// If set to "inline", a comment with a sourceMappingURL directive is added to | ||
// the bottom of the returned code. | ||
sourceMap: false, | ||
@@ -159,2 +160,30 @@ | ||
## Modules | ||
6to5 modules compile straight to CommonJS, because of this various liberties are | ||
taken into account to ease their usage. | ||
```javascript | ||
import "foo"; // var foo = require("foo"); | ||
import "foo-bar"; // var fooBar = require("foo-bar"); | ||
import "./directory/foo-bar"; // var fooBar = require("./directory/foo-bar"); | ||
import foo from "foo"; // var foo = require("foo"); | ||
import * as foo from "foo"; // var foo = require("foo"); | ||
import { bar } from "foo"; // var bar = require("foo").bar; | ||
import foo as bar from "foo"; // var bar = require("foo").foo; | ||
export { test }; // exports.test = test; | ||
export var test = 5; // var test = 5; exports.test = test; | ||
export default test; // module.exports = exports = test; | ||
``` | ||
If you'd like to disable this behaviour and use the more ES6-like | ||
[es6-module-transpiler](https://github.com/esnext/es6-module-transpiler) you can | ||
use the following: | ||
$ 6to5 script.js -o script-compiled.js --blacklist modules | ||
$ compile-modules convert script-compiled.js -o script-compiled.js | ||
## Caveats | ||
@@ -198,24 +227,24 @@ | ||
| | 6to5 | Traceur | esnext | es6now | es6-transpiler | | ||
| ---------------------------- | ---- | ------- | ------ | ------ | -------------- | | ||
| No runtime | ✓ | | | | ✓ | | ||
| Source maps | ✓ | ✓ | ✓ | | ✓ | | ||
| No compiler global pollution | ✓ | | ✓ | | ✓ | | ||
| Array comprehension | ✓ | ✓ | ✓ | | ✓ | | ||
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Block binding | ✓ | ✓ | | | ✓ | | ||
| Classes | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Computed property names | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Constants | ✓ | ✓ | | | ✓ | | ||
| Default parameters | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Destructuring | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| For-of | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Generator comprehension | | ✓ | ✓ | | ✓ | | ||
| Generators | | ✓ | ✓ | | | | ||
| Modules | ✓ | ✓ | | ✓ | | | ||
| Property method assignment | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Property name shorthand | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Rest parameters | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Spread | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Template literals | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| | 6to5 | Traceur | esnext | es6now | es6-transpiler | jstransform | | ||
| ---------------------------- | ---- | ------- | ------ | ------ | -------------- | ----------- | | ||
| No runtime | ✓ | | | | ✓ | ✓ | | ||
| Source maps | ✓ | ✓ | ✓ | | ✓ | ✓ | | ||
| No compiler global pollution | ✓ | | ✓ | | ✓ | ✓ | | ||
| Array comprehension | ✓ | ✓ | ✓ | | ✓ | | | ||
| Arrow functions | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Block binding | ✓ | ✓ | | | ✓ | | | ||
| Classes | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Computed property names | ✓ | ✓ | ✓ | ✓ | ✓ | | | ||
| Constants | ✓ | ✓ | | | ✓ | | | ||
| Default parameters | ✓ | ✓ | ✓ | ✓ | ✓ | | | ||
| Destructuring | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| For-of | ✓ | ✓ | ✓ | ✓ | ✓ | | | ||
| Generator comprehension | | ✓ | ✓ | | ✓ | | | ||
| Generators | | ✓ | ✓ | | | | | ||
| Modules | ✓ | ✓ | | ✓ | | | | ||
| Property method assignment | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Property name shorthand | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Rest parameters | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| Spread | ✓ | ✓ | ✓ | ✓ | ✓ | | | ||
| Template literals | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
@@ -222,0 +251,0 @@ #### Performance |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
1136
291
1
57405
8
32