Compare commits

...

16 Commits

Author SHA1 Message Date
Sebastian McKenzie
9989b89b92 v1.11.2 2014-11-09 22:27:52 +11:00
Sebastian McKenzie
361ef02a88 support ParenthesizedExpression in types.getIds 2014-11-09 22:26:48 +11:00
Sebastian McKenzie
72369c90a2 update dependencies 2014-11-09 22:11:06 +11:00
Sebastian McKenzie
fa26174d3f better isParenthesizedExpression support for destructuring 2014-11-09 22:10:59 +11:00
Sebastian McKenzie
227d51a556 add AssignmentExpression ObjectPattern test 2014-11-09 22:10:43 +11:00
Sebastian McKenzie
a5f00aa3f6 v1.11.1 2014-11-09 21:39:19 +11:00
Sebastian McKenzie
a47723c66c fix destructuring to support ParanthesizedExpression 2014-11-09 21:37:59 +11:00
Sebastian McKenzie
ab2f652bdf break up let scoping transformer some more 2014-11-09 21:25:14 +11:00
Sebastian McKenzie
5aff7709f7 add back _property-literals transformer that somehow disappeared? 2014-11-09 21:25:05 +11:00
Sebastian McKenzie
06ba731452 traverse: pass opts.scope instead of opts 2014-11-09 21:24:48 +11:00
Sebastian McKenzie
f4c81531ad break up let scoping 2014-11-09 20:31:46 +11:00
Sebastian McKenzie
19b115c76b generator: dry up ForXStatements and *Statements 2014-11-09 20:31:36 +11:00
Sebastian McKenzie
96b08bf7df remove codeclimate from travis 2014-11-09 20:20:21 +11:00
Sebastian McKenzie
8f435d59da change sebmck to new 6to5 org 2014-11-09 20:08:36 +11:00
Sebastian McKenzie
6df03c00a8 Merge pull request #130 from thejameskyle/patch-1
Update logo on README
2014-11-09 20:05:11 +11:00
James Kyle
4ec134814e Update logo on README 2014-11-09 01:04:32 -08:00
14 changed files with 183 additions and 149 deletions

View File

@@ -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="

View File

@@ -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&amp;style=flat&amp;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&amp;style=flat&amp;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)

View File

@@ -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");

View File

@@ -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);

View File

@@ -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);
};
};

View File

@@ -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;
}
};

View File

@@ -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;
}

View File

@@ -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));
}

View File

@@ -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));
}
};

View File

@@ -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);
}
}

View File

@@ -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) {

View File

@@ -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"
}
}

View File

@@ -1 +1,2 @@
[a, b] = f();
({ x }) = e();

View File

@@ -4,3 +4,6 @@ var _ref = f();
a = _ref[0];
b = _ref[1];
var _ref2 = e();
x = _ref2.x;