Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9989b89b92 | ||
|
|
361ef02a88 | ||
|
|
72369c90a2 | ||
|
|
fa26174d3f | ||
|
|
227d51a556 | ||
|
|
a5f00aa3f6 | ||
|
|
a47723c66c | ||
|
|
ab2f652bdf | ||
|
|
5aff7709f7 | ||
|
|
06ba731452 | ||
|
|
f4c81531ad | ||
|
|
19b115c76b | ||
|
|
96b08bf7df | ||
|
|
8f435d59da | ||
|
|
6df03c00a8 | ||
|
|
4ec134814e |
@@ -9,8 +9,3 @@ branches:
|
||||
|
||||
before_script: "npm install -g codeclimate-test-reporter"
|
||||
script: "make test-travis"
|
||||
|
||||
addons:
|
||||
code_climate:
|
||||
repo_token:
|
||||
secure: "PfP9sDUJzSznDb+ZEPO2cignXabSTXJxEVm5ESRPgEcFr+/4b0pt3hI8R9b+9mLtBEwtw3DLUq48MOeqEKnq29csQmpjPVcN6gT4uR2DdNa1JpVgDuwxT05NB3fBea9U2reM73iV8ylCgPHExr2uGR9/87JzR2beY/56EUL5NjY="
|
||||
|
||||
22
README.md
22
README.md
@@ -1,22 +1,22 @@
|
||||
<p align="center">
|
||||
<img alt="6to5" src="http://i.imgur.com/hVl9KRw.png">
|
||||
<img alt="6to5" src="https://raw.githubusercontent.com/6to5/logo/master/logo.png" width="546">
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://travis-ci.org/sebmck/6to5">
|
||||
<img alt="Travis Status" src="http://img.shields.io/travis/sebmck/6to5.svg?branch=master&style=flat&label=travis">
|
||||
<a href="https://travis-ci.org/6to5/6to5">
|
||||
<img alt="Travis Status" src="http://img.shields.io/travis/6to5/6to5.svg?branch=master&style=flat&label=travis">
|
||||
</a>
|
||||
|
||||
<a href="https://codeclimate.com/github/sebmck/6to5">
|
||||
<img alt="Code Climate Score" src="http://img.shields.io/codeclimate/github/sebmck/6to5.svg?style=flat">
|
||||
<a href="https://codeclimate.com/github/6to5/6to5">
|
||||
<img alt="Code Climate Score" src="http://img.shields.io/codeclimate/github/6to5/6to5.svg?style=flat">
|
||||
</a>
|
||||
|
||||
<a href="https://codeclimate.com/github/sebmck/6to5">
|
||||
<img alt="Coverage" src="http://img.shields.io/codeclimate/coverage/github/sebmck/6to5.svg?style=flat">
|
||||
<a href="https://codeclimate.com/github/6to5/6to5">
|
||||
<img alt="Coverage" src="http://img.shields.io/codeclimate/coverage/github/6to5/6to5.svg?style=flat">
|
||||
</a>
|
||||
|
||||
<a href="https://david-dm.org/sebmck/6to5">
|
||||
<img alt="Dependency Status" src="http://img.shields.io/david/sebmck/6to5.svg?style=flat">
|
||||
<a href="https://david-dm.org/6to5/6to5">
|
||||
<img alt="Dependency Status" src="http://img.shields.io/david/6to5/6to5.svg?style=flat">
|
||||
</a>
|
||||
</p>
|
||||
|
||||
@@ -73,10 +73,10 @@ It's as easy as:
|
||||
### Plugins
|
||||
|
||||
- [Broccoli](https://github.com/very-geek/broccoli-6to5-transpiler)
|
||||
- [Browserify](https://github.com/sebmck/6to5-browserify)
|
||||
- [Browserify](https://github.com/6to5/6to5-browserify)
|
||||
- [Brunch](https://github.com/es128/6to5-brunch)
|
||||
- [Duo](https://github.com/bdo-labs/duo6to5)
|
||||
- [Connect](https://github.com/sebmck/6to5-connect)
|
||||
- [Connect](https://github.com/6to5/6to5-connect)
|
||||
- [Gulp](https://github.com/sindresorhus/gulp-6to5)
|
||||
- [Grunt](https://github.com/sindresorhus/grunt-6to5)
|
||||
- [Jade](https://github.com/Apoxx/jade-6to5)
|
||||
|
||||
@@ -62,15 +62,6 @@ exports.MethodDefinition = function (node, print) {
|
||||
this._method(node, print);
|
||||
};
|
||||
|
||||
exports.ReturnStatement = function (node, print) {
|
||||
this.push("return");
|
||||
if (node.argument) {
|
||||
this.space();
|
||||
print(node.argument);
|
||||
}
|
||||
this.semicolon();
|
||||
};
|
||||
|
||||
exports.FunctionDeclaration =
|
||||
exports.FunctionExpression = function (node, print) {
|
||||
this.push("function");
|
||||
|
||||
@@ -52,25 +52,20 @@ exports.WhileStatement = function (node, print) {
|
||||
print.block(node.body);
|
||||
};
|
||||
|
||||
exports.ForInStatement = function (node, print) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
this.push(" in ");
|
||||
print(node.right);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
var buildForXStatement = function (op) {
|
||||
return function (node, print) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
this.push(" " + op + " ");
|
||||
print(node.right);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
};
|
||||
};
|
||||
|
||||
exports.ForOfStatement = function (node, print) {
|
||||
this.keyword("for");
|
||||
this.push("(");
|
||||
print(node.left);
|
||||
this.push(" of ");
|
||||
print(node.right);
|
||||
this.push(")");
|
||||
print.block(node.body);
|
||||
};
|
||||
exports.ForInStatement = buildForXStatement("in");
|
||||
exports.ForOfStatement = buildForXStatement("of");
|
||||
|
||||
exports.DoWhileStatement = function (node, print) {
|
||||
this.keyword("do");
|
||||
@@ -82,23 +77,23 @@ exports.DoWhileStatement = function (node, print) {
|
||||
this.push(");");
|
||||
};
|
||||
|
||||
exports.BreakStatement = function (node, print) {
|
||||
this.push("break");
|
||||
if (node.label) {
|
||||
this.space();
|
||||
print(node.label);
|
||||
}
|
||||
this.semicolon();
|
||||
var buildLabelStatement = function (prefix, key) {
|
||||
return function (node, print) {
|
||||
this.push(prefix);
|
||||
|
||||
var label = node[key || "label"];
|
||||
if (label) {
|
||||
this.space();
|
||||
print(label);
|
||||
}
|
||||
|
||||
this.semicolon();
|
||||
};
|
||||
};
|
||||
|
||||
exports.ContinueStatement = function (node, print) {
|
||||
this.push("continue");
|
||||
if (node.label) {
|
||||
this.space();
|
||||
print(node.label);
|
||||
}
|
||||
this.semicolon();
|
||||
};
|
||||
exports.ContinueStatement = buildLabelStatement("continue");
|
||||
exports.ReturnStatement = buildLabelStatement("return", "argument");
|
||||
exports.BreakStatement = buildLabelStatement("break");
|
||||
|
||||
exports.LabeledStatement = function (node, print) {
|
||||
print(node.label);
|
||||
|
||||
@@ -37,7 +37,7 @@ Transformer.prototype.transform = function (file) {
|
||||
astRun("enter");
|
||||
|
||||
var build = function (exit) {
|
||||
return function (node, parent, opts) {
|
||||
return function (node, parent, scope) {
|
||||
// add any node type aliases that exist
|
||||
var types = [node.type].concat(t.ALIAS_KEYS[node.type] || []);
|
||||
|
||||
@@ -54,7 +54,7 @@ Transformer.prototype.transform = function (file) {
|
||||
if (exit) fn = fns.exit;
|
||||
if (!fn) return;
|
||||
|
||||
return fn(node, parent, file, opts.scope);
|
||||
return fn(node, parent, file, scope);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
var esutils = require("esutils");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.Property = function (node) {
|
||||
// ignore key literals that are valid identifiers
|
||||
var key = node.key;
|
||||
if (t.isLiteral(key) && esutils.keyword.isIdentifierName(key.value)) {
|
||||
key.type = "Identifier";
|
||||
key.name = key.value;
|
||||
delete key.value;
|
||||
|
||||
node.computed = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,7 +11,21 @@ var buildVariableAssign = function (kind, id, init) {
|
||||
}
|
||||
};
|
||||
|
||||
var normalise = function (node) {
|
||||
if (t.isParenthesizedExpression(node)) {
|
||||
return node.expression;
|
||||
} else {
|
||||
return node;
|
||||
}
|
||||
};
|
||||
|
||||
var isPattern = function (node) {
|
||||
return t.isPattern(normalise(node));
|
||||
};
|
||||
|
||||
var push = function (kind, nodes, elem, parentId) {
|
||||
elem = normalise(elem);
|
||||
|
||||
if (t.isObjectPattern(elem)) {
|
||||
pushObjectPattern(kind, nodes, elem, parentId);
|
||||
} else if (t.isArrayPattern(elem)) {
|
||||
@@ -28,7 +42,7 @@ var pushObjectPattern = function (kind, nodes, pattern, parentId) {
|
||||
var pattern2 = prop.value;
|
||||
var patternId2 = t.memberExpression(parentId, prop.key);
|
||||
|
||||
if (t.isPattern(pattern2)) {
|
||||
if (isPattern(pattern2)) {
|
||||
push(kind, nodes, pattern2, patternId2);
|
||||
} else {
|
||||
nodes.push(buildVariableAssign(kind, pattern2, patternId2));
|
||||
@@ -80,7 +94,7 @@ exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
if (!t.isVariableDeclaration(declar)) return;
|
||||
|
||||
var pattern = declar.declarations[0].id;
|
||||
if (!t.isPattern(pattern)) return;
|
||||
if (!isPattern(pattern)) return;
|
||||
|
||||
var key = t.identifier(file.generateUid("ref", scope));
|
||||
node.left = t.variableDeclaration(declar.kind, [
|
||||
@@ -103,7 +117,7 @@ exports.Function = function (node, parent, file, scope) {
|
||||
var hasDestructuring = false;
|
||||
|
||||
node.params = node.params.map(function (pattern) {
|
||||
if (!t.isPattern(pattern)) return pattern;
|
||||
if (!isPattern(pattern)) return pattern;
|
||||
|
||||
hasDestructuring = true;
|
||||
var parentId = t.identifier(file.generateUid("ref", scope));
|
||||
@@ -132,7 +146,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) {
|
||||
var expr = node.expression;
|
||||
if (expr.type !== "AssignmentExpression") return;
|
||||
|
||||
if (!t.isPattern(expr.left)) return;
|
||||
if (!isPattern(expr.left)) return;
|
||||
|
||||
var nodes = [];
|
||||
|
||||
@@ -153,7 +167,7 @@ exports.VariableDeclaration = function (node, parent, file, scope) {
|
||||
|
||||
var hasPattern = false;
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (t.isPattern(declar.id)) {
|
||||
if (isPattern(declar.id)) {
|
||||
hasPattern = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -143,6 +143,87 @@ var checkFor = function (forParent, block) {
|
||||
return has;
|
||||
};
|
||||
|
||||
var hoistVarDeclarations = function (block, pushDeclar) {
|
||||
traverse(block, function (node) {
|
||||
if (t.isForStatement(node)) {
|
||||
if (isVar(node.init)) {
|
||||
node.init = t.sequenceExpression(pushDeclar(node.init));
|
||||
}
|
||||
} else if (t.isFor(node)) {
|
||||
if (isVar(node.left)) {
|
||||
node.left = node.left.declarations[0].id;
|
||||
}
|
||||
} else if (isVar(node)) {
|
||||
return pushDeclar(node).map(t.expressionStatement);
|
||||
} else if (t.isFunction(node)) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var getParams = function (info, letReferences) {
|
||||
var params = _.cloneDeep(letReferences);
|
||||
_.each(params, function (param) {
|
||||
param.name = info.duplicates[param.name] || param.name;
|
||||
});
|
||||
return params;
|
||||
};
|
||||
|
||||
var getLetReferences = function (block, info, letReferences) {
|
||||
var closurify = false;
|
||||
|
||||
// traverse through this block, stopping on functions and checking if they
|
||||
// contain any outside let references
|
||||
traverse(block, function (node, parent, scope) {
|
||||
if (t.isFunction(node)) {
|
||||
traverse(node, function (node, parent) {
|
||||
// not an identifier so we have no use
|
||||
if (!t.isIdentifier(node)) return;
|
||||
|
||||
// not a direct reference
|
||||
if (!t.isReferenced(node, parent)) return;
|
||||
|
||||
// this scope has a variable with the same name so it couldn't belong
|
||||
// to our let scope
|
||||
if (scope.hasOwn(node.name)) return;
|
||||
|
||||
closurify = true;
|
||||
|
||||
// this key doesn't appear just outside our scope
|
||||
if (!_.contains(info.outsideKeys, node.name)) return;
|
||||
|
||||
// push this badboy
|
||||
letReferences[node.name] = node;
|
||||
});
|
||||
|
||||
return false;
|
||||
} else if (t.isFor(node)) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
return closurify;
|
||||
};
|
||||
|
||||
var buildPushDeclar = function (body) {
|
||||
return function (node) {
|
||||
body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) {
|
||||
return t.variableDeclarator(declar.id);
|
||||
})));
|
||||
|
||||
var replace = [];
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (!declar.init) return;
|
||||
|
||||
var expr = t.assignmentExpression("=", declar.id, declar.init);
|
||||
replace.push(t.inherits(expr, declar));
|
||||
});
|
||||
|
||||
return replace;
|
||||
};
|
||||
};
|
||||
|
||||
var run = function (forParent, block, parent, file, scope) {
|
||||
if (block._letDone) return;
|
||||
block._letDone = true;
|
||||
@@ -160,38 +241,9 @@ var run = function (forParent, block, parent, file, scope) {
|
||||
// outside let references that we need to wrap
|
||||
var letReferences = {};
|
||||
|
||||
// are there any outside let references within any functions?
|
||||
var closurify = false;
|
||||
|
||||
// traverse through this block, stopping on functions and checking if they
|
||||
// contain any outside let references
|
||||
traverse(block, function (node, parent, opts) {
|
||||
if (t.isFunction(node)) {
|
||||
traverse(node, function (node, parent) {
|
||||
// not an identifier so we have no use
|
||||
if (!t.isIdentifier(node)) return;
|
||||
|
||||
// not a direct reference
|
||||
if (!t.isReferenced(node, parent)) return;
|
||||
|
||||
// this scope has a variable with the same name so it couldn't belong
|
||||
// to our let scope
|
||||
if (opts.scope.hasOwn(node.name)) return;
|
||||
|
||||
closurify = true;
|
||||
|
||||
// this key doesn't appear just outside our scope
|
||||
if (!_.contains(info.outsideKeys, node.name)) return;
|
||||
|
||||
// push this badboy
|
||||
letReferences[node.name] = node;
|
||||
});
|
||||
|
||||
return false;
|
||||
} else if (t.isFor(node)) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
// returns whether or not there are any outside let references within any
|
||||
// functions
|
||||
var closurify = getLetReferences(block, info, letReferences);
|
||||
|
||||
letReferences = _.values(letReferences);
|
||||
|
||||
@@ -205,39 +257,10 @@ var run = function (forParent, block, parent, file, scope) {
|
||||
var body = [];
|
||||
|
||||
// hoist a `VariableDeclaration` and add `AssignmentExpression`s in it's place
|
||||
var pushDeclar = function (node) {
|
||||
body.push(t.variableDeclaration(node.kind, node.declarations.map(function (declar) {
|
||||
return t.variableDeclarator(declar.id);
|
||||
})));
|
||||
|
||||
var replace = [];
|
||||
|
||||
_.each(node.declarations, function (declar) {
|
||||
if (!declar.init) return;
|
||||
|
||||
var expr = t.assignmentExpression("=", declar.id, declar.init);
|
||||
replace.push(t.inherits(expr, declar));
|
||||
});
|
||||
|
||||
return replace;
|
||||
};
|
||||
var pushDeclar = buildPushDeclar(body);
|
||||
|
||||
// hoist var references to retain scope
|
||||
traverse(block, function (node) {
|
||||
if (t.isForStatement(node)) {
|
||||
if (isVar(node.init)) {
|
||||
node.init = t.sequenceExpression(pushDeclar(node.init));
|
||||
}
|
||||
} else if (t.isFor(node)) {
|
||||
if (isVar(node.left)) {
|
||||
node.left = node.left.declarations[0].id;
|
||||
}
|
||||
} else if (isVar(node)) {
|
||||
return pushDeclar(node).map(t.expressionStatement);
|
||||
} else if (t.isFunction(node)) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
hoistVarDeclarations(block, pushDeclar);
|
||||
|
||||
// set let references to plain var references
|
||||
standardiseLets(declarators);
|
||||
@@ -249,11 +272,8 @@ var run = function (forParent, block, parent, file, scope) {
|
||||
// replace the current block body with the one we've built
|
||||
block.body = body;
|
||||
|
||||
// cahnge duplicate let references to their uid if they have one
|
||||
var params = _.cloneDeep(letReferences);
|
||||
_.each(params, function (param) {
|
||||
param.name = info.duplicates[param.name] || param.name;
|
||||
});
|
||||
// change duplicate let references to their uid if they have one
|
||||
var params = getParams(info, letReferences);
|
||||
|
||||
var call = t.callExpression(fn, params);
|
||||
var ret = t.identifier(file.generateUid("ret", scope));
|
||||
@@ -299,7 +319,6 @@ var run = function (forParent, block, parent, file, scope) {
|
||||
|
||||
body.push(t.switchStatement(ret, cases));
|
||||
}
|
||||
|
||||
} else {
|
||||
body.push(t.expressionStatement(call));
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ function traverse(parent, callbacks, opts) {
|
||||
|
||||
// enter
|
||||
if (callbacks.enter) {
|
||||
result = callbacks.enter(node, parent, opts2);
|
||||
result = callbacks.enter(node, parent, opts2.scope);
|
||||
|
||||
// stop iteration
|
||||
if (result === false) return;
|
||||
@@ -67,7 +67,7 @@ function traverse(parent, callbacks, opts) {
|
||||
|
||||
// exit
|
||||
if (callbacks.exit) {
|
||||
maybeReplace(callbacks.exit(node, parent, opts2));
|
||||
maybeReplace(callbacks.exit(node, parent, opts2.scope));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -185,6 +185,8 @@ t.getIds = function (node, map) {
|
||||
search = search.concat(id.specifiers);
|
||||
} else if (t.isMemberExpression(id)) {
|
||||
search.push(id.object);
|
||||
} else if (t.isParenthesizedExpression(id)) {
|
||||
search.push(id.expression);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -245,7 +245,7 @@ var loadTemplates = function () {
|
||||
if (!fs.existsSync(templatesLoc)) {
|
||||
throw new Error("no templates directory - this is most likely the " +
|
||||
"result of a broken `npm publish`. Please report to " +
|
||||
"https://githut.com/sebmck/6to5/issues");
|
||||
"https://githut.com/6to5/6to5/issues");
|
||||
}
|
||||
|
||||
_.each(fs.readdirSync(templatesLoc), function (name) {
|
||||
|
||||
26
package.json
26
package.json
@@ -1,15 +1,15 @@
|
||||
{
|
||||
"name": "6to5",
|
||||
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
|
||||
"version": "1.11.0",
|
||||
"version": "1.11.2",
|
||||
"author": "Sebastian McKenzie <sebmck@gmail.com>",
|
||||
"homepage": "https://github.com/sebmck/6to5",
|
||||
"homepage": "https://github.com/6to5/6to5",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/sebmck/6to5.git"
|
||||
"url": "https://github.com/6to5/6to5.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/sebmck/6to5/issues"
|
||||
"url": "https://github.com/6to5/6to5/issues"
|
||||
},
|
||||
"preferGlobal": true,
|
||||
"main": "lib/6to5/index.js",
|
||||
@@ -36,29 +36,29 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"ast-types": "~0.6.0",
|
||||
"commander": "2.4.0",
|
||||
"commander": "2.5.0",
|
||||
"fs-readdir-recursive": "0.0.2",
|
||||
"lodash": "2.4.1",
|
||||
"mkdirp": "0.5.0",
|
||||
"es6-shim": "0.18.0",
|
||||
"es6-shim": "0.20.2",
|
||||
"es6-symbol": "0.1.1",
|
||||
"regexpu": "0.3.0",
|
||||
"source-map": "0.1.40",
|
||||
"regenerator-6to5": "https://github.com/sebmck/regenerator-6to5/archive/b7bc53e1a655879974aad53a8ceb93a70efaa08d.tar.gz",
|
||||
"chokidar": "0.10.0",
|
||||
"source-map-support": "0.2.7",
|
||||
"regenerator-6to5": "https://github.com/6to5/regenerator-6to5/archive/b7bc53e1a655879974aad53a8ceb93a70efaa08d.tar.gz",
|
||||
"chokidar": "0.10.5",
|
||||
"source-map-support": "0.2.8",
|
||||
"esutils": "1.1.4",
|
||||
"acorn-6to5": "https://github.com/sebmck/acorn-6to5/archive/f5110383517eef0bea78c2da2a1fb01fbed74e4e.tar.gz",
|
||||
"acorn-6to5": "https://github.com/6to5/acorn-6to5/archive/f5110383517eef0bea78c2da2a1fb01fbed74e4e.tar.gz",
|
||||
"estraverse": "^1.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"istanbul": "0.3.2",
|
||||
"matcha": "0.5.0",
|
||||
"mocha": "1.21.5",
|
||||
"mocha": "2.0.1",
|
||||
"uglify-js": "2.4.15",
|
||||
"browserify": "6.1.0",
|
||||
"browserify": "6.2.0",
|
||||
"rimraf": "2.2.8",
|
||||
"jshint": "2.5.6",
|
||||
"jshint": "2.5.10",
|
||||
"chai": "^1.9.2"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
[a, b] = f();
|
||||
({ x }) = e();
|
||||
|
||||
@@ -4,3 +4,6 @@ var _ref = f();
|
||||
|
||||
a = _ref[0];
|
||||
b = _ref[1];
|
||||
var _ref2 = e();
|
||||
|
||||
x = _ref2.x;
|
||||
|
||||
Reference in New Issue
Block a user