Compare commits

...

37 Commits

Author SHA1 Message Date
Sebastian McKenzie
3e51b65095 v2.4.6 2015-01-03 05:29:28 +11:00
Sebastian McKenzie
371df9ad09 add 2.4.6 changelog 2015-01-03 05:25:48 +11:00
Sebastian McKenzie
d43d5ff409 move coreAliasing and undefinedToVoid transformers down 2015-01-03 05:24:43 +11:00
Sebastian McKenzie
c2c8e52430 add modules caveat - fixes #368 2015-01-03 05:23:12 +11:00
Sebastian McKenzie
57c0ebc5f4 v2.4.5 2015-01-02 22:22:52 +11:00
Sebastian McKenzie
339bf82481 avoid printing comments if they've already been output - fixes #365 2015-01-02 22:22:01 +11:00
Sebastian McKenzie
d981c06444 v2.4.4 2015-01-02 19:24:56 +11:00
Sebastian McKenzie
8269e1488b fix incorrect scripts reference in browser 2015-01-02 19:23:50 +11:00
Sebastian McKenzie
ddda7a020f add 2.4.4 changelog 2015-01-02 19:22:03 +11:00
Sebastian McKenzie
7e9340864a lowercase object rest/spread feature name in doc/index 2015-01-02 19:20:14 +11:00
Sebastian McKenzie
5ec19f23c3 add test skeleton for types and add jsdoc comments to types 2015-01-02 19:20:05 +11:00
Sebastian McKenzie
ba48b66458 add types.appendToMemberExpression and types.prependToMemberExpression helpers 2015-01-02 16:46:58 +11:00
Sebastian McKenzie
6e8c73f65f fix incorrect member expression properties 2015-01-02 16:39:35 +11:00
Sebastian McKenzie
b34fcb0cd9 split up method binding and add description for memoization assignment operator 2015-01-02 14:33:51 +11:00
Sebastian McKenzie
ff7129f1aa remove ES5 methods from caveats and change IE classes note to IE 10 and below 2015-01-02 14:33:38 +11:00
Sebastian McKenzie
fbabd193a4 add module type to 6to5 browser 2015-01-02 14:33:18 +11:00
Sebastian McKenzie
876d69798c v2.4.3 2015-01-02 05:19:42 +11:00
Sebastian McKenzie
1a963ddc06 remove unused variable 2015-01-02 05:19:00 +11:00
Sebastian McKenzie
beb5acea6b better addImport with good hoisting etc 2015-01-02 05:18:03 +11:00
Sebastian McKenzie
6a290f233e add 2.4.3 changelog 2015-01-02 05:01:18 +11:00
Sebastian McKenzie
74563d88ed upgrade acorn-6to5 2015-01-02 05:00:23 +11:00
Sebastian McKenzie
a18177026c add support for statements in asyncToGenerator and bluebirdCoroutines transformers 2015-01-02 04:58:59 +11:00
Sebastian McKenzie
a813341433 v2.4.2 2015-01-02 04:25:44 +11:00
Sebastian McKenzie
4968de9803 fix linting errors 2015-01-02 04:24:56 +11:00
Sebastian McKenzie
0de2d33e8d add 2.4.2 changelog 2015-01-02 04:24:19 +11:00
Sebastian McKenzie
6f81ddd0d5 upgrade acorn-6to5 2015-01-02 04:24:19 +11:00
Sebastian McKenzie
6b83055b12 alias flat references in coreAliasing transformer 2015-01-02 04:24:19 +11:00
Sebastian McKenzie
080c23d5ed Merge pull request #360 from mquandalle/patch-1
Extend the license to the new year
2015-01-02 04:06:22 +11:00
Sebastian McKenzie
293bec58d7 remove appveyor badge 2015-01-02 03:53:33 +11:00
Sebastian McKenzie
93ceafc732 better uid names 2015-01-02 03:53:28 +11:00
Sebastian McKenzie
b2bac04b72 update object getter memoization docs to reflect new changes 2015-01-02 03:04:26 +11:00
Sebastian McKenzie
e549c37ca1 v2.4.1 2015-01-02 03:00:33 +11:00
Sebastian McKenzie
02512da38d fix generation tests with parenthesized expressions and trailing comments 2015-01-02 02:59:53 +11:00
Sebastian McKenzie
abe9c4fd30 add 2.4.1 changelog 2015-01-02 02:57:16 +11:00
Sebastian McKenzie
af21c52cc6 set generator on correct node #252 2015-01-02 02:55:55 +11:00
Sebastian McKenzie
706626f79a better whitespace handling of parenthesized comments due to trailing comments 2015-01-02 02:55:40 +11:00
Maxime Quandalle
c7a02ca289 Extend the license to the new year 2015-01-01 13:03:38 +01:00
54 changed files with 589 additions and 202 deletions

View File

@@ -2,6 +2,35 @@
Gaps between patch versions are faulty/broken releases.
## 2.4.6
* Move `coreAliasing` and `undefinedToVoid` transformers down to catch `moduleFormatter` transforms.
## 2.4.5
* Avoid printing comments if they've already been output.
## 2.4.4
* Add `module` type to browser build `<script>` handler.
* Fix some `MemberExpression` modifying incorrectly setting `property` to a `MemberExpression`.
## 2.4.3
* Upgrade `acorn-6to5`.
* Add support for `FunctionDeclaration`s in `bluebirdCoroutines` and `asyncToGenerators` transformers.
## 2.4.2
* Upgrade `acorn-6to5`.
* Better uids generated for various transformers based on parent node.
* Alias flat references in `coreAliasing` transformer.
## 2.4.1
* Better whitespace handling of parenthesized expressions due to trailing comments.
* Fix `yield` inside of comprehensions.
## 2.4.0
* Use a closure always for classes with a super.

View File

@@ -1,4 +1,4 @@
Copyright (c) 2014 Sebastian McKenzie
Copyright (c) 2014-2015 Sebastian McKenzie
MIT License

View File

@@ -11,10 +11,6 @@
<img alt="Travis Status" src="http://img.shields.io/travis/6to5/6to5/master.svg?style=flat&amp;label=travis">
</a>
<a href="https://ci.appveyor.com/project/sebmck/6to5">
<img alt="Appveyor Status" src="http://img.shields.io/appveyor/ci/sebmck/6to5.svg?style=flat&amp;label=appveyor">
</a>
<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>

View File

@@ -9,12 +9,15 @@ You may alternatively selectively include what you need:
| Feature | Requirements |
| --------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| Abstract References | [experimental](experimental.md), `Symbol` |
| Array destructuring | `Array.isArray`, `Array.from` |
| Array destructuring | `Array.from` |
| Async functions, Generators | [experimental](experimental.md), [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js) |
| Comprehensions | [experimental](experimental.md), `Array.isArray`, `Array.from` |
| Comprehensions | [experimental](experimental.md), `Array.from` |
| For Of | `Symbol`, `prototype[Symbol.iterator]` |
| Modules | `Object.assign`* |
| Object spread/rest | [experimental](experimental.md), `Object.assign` |
| Spread | `Array.isArray`, `Array.from` |
| Spread | `Array.from` |
*Only required for exporting a non-function `default` with additional `export`s.
## ES5
@@ -26,13 +29,13 @@ ES5 such as lower versions of IE then using the
## Internet Explorer
### Classes (9 and below)
### Classes (10 and below)
If you're inheriting from a class then static properties are inherited from it
via [\_\_proto\_\_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto),
this is widely supported but you may run into problems with much older browsers.
**NOTE:** `__proto__` is not supported on IE <= 9 so static properties
**NOTE:** `__proto__` is not supported in IE <= 10 so static properties
**will not** be inherited. A possible workaround is to use `super();`:
```javascript
@@ -56,4 +59,4 @@ unfortunate as it's required to set getters and setters. Due to this if
you plan on supporting IE8 or below then the user of getters and setters
isn't recommended.
Reference: [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Internet_Explorer_8_specific_notes).
**Reference**: [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty#Internet_Explorer_8_specific_notes).

View File

@@ -52,7 +52,7 @@ And it doesn't end here! To see all the ways you can use 6to5, check out the
- [Let scoping](features.md#let-scoping)
- [Modules](features.md#modules)
- [Numeric literals](features.md#numeric-literals)
- [Object Rest/Spread](features.md#object-rest-spread) ([experimental](experimental.md))
- [Object rest/spread](features.md#object-rest-spread) ([experimental](experimental.md))
- [Property method assignment](features.md#property-method-assignment)
- [Property name shorthand](features.md#property-name-shorthand)
- [React/JSX](react.md)

View File

@@ -18,11 +18,19 @@ to5.transform("code", { playground: true });
* [Memoization operator](#memoization-operator)
* [Method binding](#method-binding)
* [Method binding function shorthand](#method-binding-function-shorthand)
* [Object getter memoization](#object-getter-memoization)
* [This shorthand](#this-shorthand)
### Memoization assignment operator
The memoization assignment operator allows you to lazily set an object property.
It checks whether there's a property defined on the object and if there isn't then
the right hand value is set.
This means that `obj.x` in the following `var x = { x: undefined }; obj.x ?= 2;`
will still be `undefined` because it's already been defined on the object.
```javascript
var obj = {};
obj.x ?= 2;
@@ -42,7 +50,7 @@ var obj = {};
obj.x ?= 2;
```
equivalent to:
equivalent to
```javascript
var obj = {};
@@ -54,17 +62,25 @@ if (!Object.prototype.hasOwnProperty.call(obj, "x")) obj.x = 2;
```javascript
var fn = obj#method;
var fn = obj#method("foob");
["foo", "bar"].map(#toUpperCase); // ["FOO", "BAR"]
[1.1234, 23.53245, 3].map(#toFixed(2)); // ["1.12", "23.53", "3.00"]
```
equivalent to:
equivalent to
```javascript
var fn = obj.method.bind(obj);
var fn = obj.method.bind(obj, "foob");
```
### Method binding function shorthand
```javascript
["foo", "bar"].map(#toUpperCase); // ["FOO", "BAR"]
[1.1234, 23.53245, 3].map(#toFixed(2)); // ["1.12", "23.53", "3.00"]
```
equivalent to
```javascript
["foo", "bar"].map(function (val) { return val.toUpperCase(); });
[1.1234, 23.53245, 3].map(function (val) { return val.toFixed(2); });
```
@@ -90,23 +106,27 @@ equivalent to
```javascript
var foo = {
get bar() {
if (this._barRan) return this._bar;
this._barRan = true;
return this._bar = complex();
return Object.defineProperty(this, "bar", {
value: complex(),
enumerable: true,
configurable: true,
writable: true
}).bar;
}
};
class Foo {
get bar() {
if (this._barRan) return this._bar;
this._barRan = true;
return this._bar = complex();
return Object.defineProperty(this, "bar", {
value: complex(),
enumerable: true,
configurable: true,
writable: true
}).bar;
}
}
```
**NOTE:** Memoised functions will return the result of the **first** execution, regardless of arguments.
### This shorthand
```javascript

View File

@@ -34,7 +34,7 @@ transform.load = function (url, callback, opts, hold) {
var runScripts = function () {
var scripts = [];
var types = ["text/ecmascript-6", "text/6to5"];
var types = ["text/ecmascript-6", "text/6to5", "module"];
var index = 0;
var exec = function () {
@@ -60,8 +60,9 @@ var runScripts = function () {
}
};
var _scripts = global.document.getElementsByTagName("script");
for (var i in _scripts) {
var _scripts = global.document .getElementsByTagName("script");
for (var i = 0; i < _scripts.length; ++i) {
var _script = _scripts[i];
if (types.indexOf(_script.type) >= 0) scripts.push(_script);
}

View File

@@ -10,10 +10,11 @@ var t = require("./types");
var _ = require("lodash");
function File(opts) {
this.opts = File.normaliseOptions(opts);
this.transformers = this.getTransformers();
this.uids = {};
this.ast = {};
this.dynamicImports = [];
this.opts = File.normaliseOptions(opts);
this.transformers = this.getTransformers();
this.uids = {};
this.ast = {};
}
File.declarations = [
@@ -156,7 +157,8 @@ File.prototype.parseShebang = function (code) {
File.prototype.addImport = function (id, source) {
var specifiers = [t.importSpecifier(t.identifier("default"), id)];
var declar = t.importDeclaration(specifiers, t.literal(source));
this.ast.program.body.unshift(declar);
declar._blockHoist = 3;
this.dynamicImports.push(declar);
};
File.prototype.addDeclaration = function (name) {

View File

@@ -148,10 +148,11 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
};
if (this[node.type]) {
// only compute if this node needs parens if our parent has been changed
// since acorn would've wrapped us in a ParanthesizedExpression
var needsParens = n.needsParens(node, parent);
var needsCommentParens = t.isExpression(node) && node.leadingComments && node.leadingComments.length;
var needsParens = needsCommentParens || n.needsParens(node, parent);
if (needsParens) this.push("(");
if (needsCommentParens) this.indent();
this.printLeadingComments(node, parent);
@@ -162,6 +163,10 @@ CodeGenerator.prototype.print = function (node, parent, opts) {
this[node.type](node, this.buildPrint(node), parent);
if (needsCommentParens) {
this.newline();
this.dedent();
}
if (needsParens) this.push(")");
this.map.mark(node, "end");
@@ -269,14 +274,21 @@ CodeGenerator.prototype._printComments = function (comments) {
var self = this;
_.each(comments, function (comment) {
var skip = false;
// find the original comment in the ast and set it as displayed
_.each(self.ast.comments, function (origComment) {
if (origComment.start === comment.start) {
// comment has already been output
if (origComment._displayed) skip = true;
origComment._displayed = true;
return false;
}
});
if (skip) return;
// whitespace before
self.newline(self.whitespace.getNewlinesBefore(comment));

View File

@@ -98,6 +98,10 @@ exports.MemberExpression = function (node, print) {
var obj = node.object;
print(obj);
if (!node.computed && t.isMemberExpression(node.property)) {
throw new TypeError("Got a MemberExpression for MemberExpression property");
}
if (node.computed) {
this.push("[");
print(node.property);

View File

@@ -60,10 +60,6 @@ Node.prototype.needsParens = function () {
if (!parent) return false;
if (t.isExpression(node) && node.leadingComments && node.leadingComments.length) {
return true;
}
if (t.isNewExpression(parent) && parent.callee === node) {
return t.isCallExpression(node) || _.some(node, function (val) {
return t.isCallExpression(val);

View File

@@ -83,14 +83,14 @@ _.each({
_declarations: require("./transformers/_declarations"),
coreAliasing: require("./transformers/optional-core-aliasing"),
undefinedToVoid: require("./transformers/optional-undefined-to-void"),
// wrap up
_aliasFunctions: require("./transformers/_alias-functions"),
_moduleFormatter: require("./transformers/_module-formatter"),
useStrict: require("./transformers/use-strict"),
coreAliasing: require("./transformers/optional-core-aliasing"),
undefinedToVoid: require("./transformers/optional-undefined-to-void"),
// spec
specPropertyLiterals: require("./transformers/spec-property-literals"),
specMemberExpressionLiterals: require("./transformers/spec-member-expression-literals")

View File

@@ -1,4 +1,5 @@
var util = require("../../util");
var t = require("../../types");
exports.Property = function (node) {
if (node.method) node.method = false;
@@ -21,7 +22,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasAny) return;
if (node.properties.length) {
var objId = util.getUid(parent, file);
var objId = t.getUid(parent, file);
return util.template("object-define-properties-closure", {
KEY: objId,

View File

@@ -87,11 +87,9 @@ Class.prototype.run = function () {
if (superName) {
this.closure = true;
// so we're only evaluating it once
var superRefName = "super";
if (className) superRefName = className.name + "Super";
var superRef = file.generateUidIdentifier(superRefName, this.scope);
// so we're only evaluating it once
var superRef = t.getUid(superName, this.file);
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(superRef, superName)
]));
@@ -278,7 +276,7 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
if (callee.object.name !== "super") return;
// super.test(); -> ClassName.prototype.MethodName.call(this);
callee.property = t.memberExpression(callee.property, t.identifier("call"));
t.appendToMemberExpression(callee, t.identifier("call"));
node.arguments.unshift(t.thisExpression());
}
}

View File

@@ -1,5 +1,4 @@
var util = require("../../util");
var t = require("../../types");
var t = require("../../types");
exports.ObjectExpression = function (node, parent, file) {
var hasComputed = false;
@@ -14,7 +13,7 @@ exports.ObjectExpression = function (node, parent, file) {
if (!hasComputed) return;
var objId = util.getUid(parent, file);
var objId = t.getUid(parent, file);
var body = [];
var container = t.functionExpression(null, [], t.blockStatement(body));

View File

@@ -75,7 +75,7 @@ var pushArrayPattern = function (opts, nodes, pattern, parentId) {
var toArray = opts.file.toArray(parentId, !hasSpreadElement && pattern.elements.length);
var _parentId = opts.file.generateUidIdentifier("ref", opts.scope);
var _parentId = t.getUid(parentId, opts.file, opts.scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(_parentId, toArray)
]));
@@ -113,7 +113,7 @@ var pushPattern = function (opts) {
var scope = opts.scope;
if (!t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) {
var key = file.generateUidIdentifier("ref", scope);
var key = t.getUid(parentId, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(key, parentId)

View File

@@ -1,5 +1,11 @@
var t = require("../../types");
exports.ast = {
before: function (ast, file) {
ast.program.body = file.dynamicImports.concat(ast.program.body);
}
};
exports.ImportDeclaration = function (node, parent, file) {
var nodes = [];
@@ -11,6 +17,12 @@ exports.ImportDeclaration = function (node, parent, file) {
file.moduleFormatter.importDeclaration(node, nodes, parent);
}
if (nodes.length === 1) {
// inherit `_blockHoist`
// this for `_blockHoist` in File.prototype.addImport
nodes[0]._blockHoist = node._blockHoist;
}
return nodes;
};

View File

@@ -87,13 +87,7 @@ exports.CallExpression = function (node, parent, file, scope) {
callee.object = t.assignmentExpression("=", temp, callee.object);
}
if (callee.computed) {
callee.object = t.memberExpression(callee.object, callee.property, true);
callee.property = t.identifier("apply");
callee.computed = false;
} else {
callee.property = t.memberExpression(callee.property, t.identifier("apply"));
}
t.appendToMemberExpression(callee, t.identifier("apply"));
} else {
node.callee = t.memberExpression(node.callee, t.identifier("apply"));
}

View File

@@ -4,8 +4,8 @@ var t = require("../../types");
exports.experimental = true;
var build = function (node, file) {
var uid = file.generateUidIdentifier("arr");
var build = function (node, parent, file, scope) {
var uid = t.getUid(parent, file, scope);
var container = util.template("array-comprehension-container", {
KEY: uid
@@ -16,7 +16,7 @@ var build = function (node, file) {
var body = block.body;
if (traverse.hasType(node, "YieldExpression", t.FUNCTION_TYPES)) {
container.generator = true;
container.callee.generator = true;
container = t.yieldExpression(container, true);
}
@@ -55,8 +55,8 @@ exports._build = function (node, buildBody) {
);
};
exports.ComprehensionExpression = function (node, parent, file) {
exports.ComprehensionExpression = function (node, parent, file, scope) {
if (node.generator) return;
return build(node, file);
return build(node, parent, file, scope);
};

View File

@@ -1,5 +1,4 @@
var bluebirdCoroutines = require("./optional-bluebird-coroutines");
var t = require("../../types");
exports.optional = true;
@@ -8,7 +7,5 @@ exports.manipulateOptions = bluebirdCoroutines.manipulateOptions;
exports.Function = function (node, parent, file) {
if (!node.async || node.generator) return;
bluebirdCoroutines._Function(node);
return t.callExpression(file.addDeclaration("async-to-generator"), [node]);
return bluebirdCoroutines._Function(node, file.addDeclaration("async-to-generator"));
};

View File

@@ -8,7 +8,7 @@ exports.manipulateOptions = function (opts) {
exports.optional = true;
exports._Function = function (node) {
exports._Function = function (node, callId) {
node.async = false;
node.generator = true;
@@ -21,14 +21,25 @@ exports._Function = function (node) {
}
}
});
var call = t.callExpression(callId, [node]);
if (t.isFunctionDeclaration(node)) {
var declar = t.variableDeclaration("var", [
t.variableDeclarator(node.id, call)
]);
declar._blockHoist = true;
return declar;
} else {
return call;
}
};
exports.Function = function (node, parent, file) {
if (!node.async || node.generator) return;
exports._Function(node);
var id = t.identifier("Bluebird");
file.addImport(id, "bluebird");
return t.callExpression(t.memberExpression(id, t.identifier("coroutine")), [node]);
return exports._Function(node, t.memberExpression(id, t.identifier("coroutine")));
};

View File

@@ -4,6 +4,10 @@ var core = require("core-js/library");
var t = require("../../types");
var _ = require("lodash");
var coreHas = function (node) {
return node.name !== "_" && _.has(core, node.name);
};
exports.optional = true;
exports.ast = {
@@ -24,11 +28,13 @@ exports.ast = {
if (!t.isReferenced(obj, node)) return;
var coreHasObject = obj.name !== "_" && _.has(core, obj.name);
if (coreHasObject && _.has(core[obj.name], prop.name)) {
if (!node.computed && coreHas(obj) && _.has(core[obj.name], prop.name)) {
this.stop();
return t.memberExpression(file._coreId, node);
return t.prependToMemberExpression(node, file._coreId);
}
} else if (t.isIdentifier(node) && !t.isMemberExpression(parent) && t.isReferenced(node, parent) && coreHas(node)) {
// new Promise -> new _core.Promise
return t.memberExpression(file._coreId, node);
} else if (t.isCallExpression(node)) {
// arr[Symbol.iterator]() -> _core.$for.getIterator(arr)

View File

@@ -10,7 +10,7 @@ var getPropRef = function (nodes, prop, file, scope) {
if (t.isIdentifier(prop)) {
return t.literal(prop.name);
} else {
var temp = file.generateUidIdentifier("propKey", scope);
var temp = t.getUid(prop, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, prop)
]));
@@ -20,7 +20,7 @@ var getPropRef = function (nodes, prop, file, scope) {
var getObjRef = function (nodes, obj, file, scope) {
if (t.isDynamic(obj)) {
var temp = file.generateUidIdentifier("obj", scope);
var temp = t.getUid(obj, file, scope);
nodes.push(t.variableDeclaration("var", [
t.variableDeclarator(temp, obj)
]));

View File

@@ -64,7 +64,14 @@ _.each(t.FLIPPED_ALIAS_KEYS, function (types, type) {
addAssert(type, is);
});
//
/*
* Shallowly checks to see if the passed `node` will evaluate to a
* falsy. This is if `node` is a `Literal` and `value` is falsy or
* `node` is an `Identifier` with a name of `undefiend`.
*
* @param {Object} node
* @returns {Boolean}
*/
t.isFalsyExpression = function (node) {
if (t.isLiteral(node)) {
@@ -75,7 +82,14 @@ t.isFalsyExpression = function (node) {
return false;
};
//
/**
* Turn an array of statement `nodes` into a `SequenceExpression`.
*
* Variable declarations are turned into simple assignments and their
* declarations hoisted to the top of the current scope.
*
* Expression statements are just resolved to their standard expression.
*/
t.toSequenceExpression = function (nodes, scope) {
var exprs = [];
@@ -116,7 +130,41 @@ t.shallowEqual = function (actual, expected) {
return same;
};
//
/**
* Description
*
* @param {Object} member
* @param {Object} append
* @param {Boolean} [computed]
* @returns {Object} member
*/
t.appendToMemberExpression = function (member, append, computed) {
member.object = t.memberExpression(member.object, member.property, member.computed);
member.property = append;
member.computed = !!computed;
return member;
};
/**
* Description
*
* @param {Object} member
* @param {Object} append
* @returns {Object} member
*/
t.prependToMemberExpression = function (member, append) {
member.object = t.memberExpression(append, member.object);
return member;
};
/*
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isDynamic = function (node) {
if (t.isExpressionStatement(node)) {
@@ -130,7 +178,14 @@ t.isDynamic = function (node) {
}
};
// todo: https://github.com/eventualbuddha/ast-util/blob/9bf91c5ce8/lib/index.js#L454-L507
/**
* Description
*
* @param {Object} node
* @param {Object} parent
* @returns {Boolean}
*/
t.isReferenced = function (node, parent) {
// we're a property key and we aren't computed so we aren't referenced
if (t.isProperty(parent) && parent.key === node && !parent.computed) return false;
@@ -152,11 +207,29 @@ t.isReferenced = function (node, parent) {
return false;
};
/**
* Description
*
* @param {String} name
* @returns {Boolean}
*/
t.isValidIdentifier = function (name) {
return _.isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isKeywordES6(name, true);
};
/*
* Description
*
* @param {String} name
* @returns {String}
*/
t.toIdentifier = function (name) {
if (t.isIdentifier(name)) return name.name;
// replace all non-valid identifiers with dashes
name = name.replace(/[^a-zA-Z0-9]/g, "-");
name = name.replace(/[^a-zA-Z0-9$_]/g, "-");
// remove all dashes and numbers from start of name
name = name.replace(/^[-0-9]+/, "");
@@ -166,18 +239,32 @@ t.toIdentifier = function (name) {
return c ? c.toUpperCase() : "";
});
// remove underscores from start of name
name = name.replace(/^\_/, "");
return name || '_';
};
t.isValidIdentifier = function (name) {
return _.isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isKeywordES6(name, true);
};
/**
* Description
*
* @param {Object} node
* @param {String} key
*/
t.ensureBlock = function (node, key) {
key = key || "body";
node[key] = t.toBlock(node[key], node);
};
/**
* Description
*
* @param {Object} node
* @param {Boolean} [ignore]
* @returns {Object|Boolean}
*/
t.toStatement = function (node, ignore) {
if (t.isStatement(node)) {
return node;
@@ -211,6 +298,14 @@ t.toStatement = function (node, ignore) {
return node;
};
/**
* Description
*
* @param {Object} node
* @param {Object} parent
* @returns {Object}
*/
t.toBlock = function (node, parent) {
if (t.isBlockStatement(node)) {
return node;
@@ -231,6 +326,67 @@ t.toBlock = function (node, parent) {
return t.blockStatement(node);
};
/*
* Description
*
* @param {Object} parent
* @param {File} file
* @param {Scope} scope
* @returns {Object}
*/
t.getUid = function (parent, file, scope) {
var node = parent;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isProperty(node)) {
node = node.key;
}
if (t.isIdentifier(node)) {
id = node.name;
} else if (t.isLiteral(node)) {
id = node.value;
} else if (t.isMemberExpression(node)) {
var parts = [];
var add = function (node) {
if (t.isMemberExpression(node)) {
add(node.object);
add(node.property);
} else if (t.isIdentifier(node)) {
parts.push(node.name);
} else if (t.isLiteral(node)) {
parts.push(node.value);
}
};
add(node);
id = parts.join("$");
}
id = id.replace(/^_/, "");
return file.generateUidIdentifier(id, scope);
};
/**
* Description
*
* @param {Object} node
* @param {Boolean} [map]
* @param {Array} [ignoreTypes]
* @returns {Array|Object}
*/
t.getIds = function (node, map, ignoreTypes) {
ignoreTypes = ignoreTypes || [];
@@ -281,10 +437,24 @@ t.getIds.arrays = {
ObjectPattern: ["properties"]
};
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isLet = function (node) {
return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let);
};
/**
* Description
*
* @param {Object} node
* @returns {Boolean}
*/
t.isVar = function (node) {
return t.isVariableDeclaration(node, { kind: "var" }) && !node._let;
};
@@ -293,6 +463,13 @@ t.isVar = function (node) {
t.COMMENT_KEYS = ["leadingComments", "trailingComments"];
/**
* Description
*
* @param {Object} child
* @returns {Object} child
*/
t.removeComments = function (child) {
_.each(t.COMMENT_KEYS, function (key) {
delete child[key];
@@ -300,6 +477,14 @@ t.removeComments = function (child) {
return child;
};
/**
* Description
*
* @param {Object} child
* @param {Object} parent
* @returns {Object} child
*/
t.inheritsComments = function (child, parent) {
_.each(t.COMMENT_KEYS, function (key) {
child[key] = _.uniq(_.compact([].concat(child[key], parent[key])));
@@ -307,7 +492,13 @@ t.inheritsComments = function (child, parent) {
return child;
};
//
/**
* Description
*
* @param {Object} child
* @param {Object} parent
* @returns {Object} child
*/
t.inherits = function (child, parent) {
child.loc = parent.loc;
@@ -318,10 +509,24 @@ t.inherits = function (child, parent) {
return child;
};
/**
* Description
*
* @param {Object} specifier
* @returns {String}
*/
t.getSpecifierName = function (specifier) {
return specifier.name || specifier.id;
};
/**
* Description
*
* @param {Object} specifier
* @returns {Boolean}
*/
t.isSpecifierDefault = function (specifier) {
return t.isIdentifier(specifier.id) && specifier.id.name === "default";
};

View File

@@ -52,22 +52,6 @@ exports.arrayify = function (val) {
throw new TypeError("illegal type for arrayify");
};
exports.getUid = function (parent, file) {
var node;
if (t.isAssignmentExpression(parent)) {
node = parent.left;
} else if (t.isVariableDeclarator(parent)) {
node = parent.id;
}
var id = "ref";
if (t.isIdentifier(node)) id = node.name;
return file.generateUidIdentifier(id);
};
exports.isAbsolute = function (loc) {
if (!loc) return false;
if (loc[0] === "/") return true; // unix

View File

@@ -1,7 +1,7 @@
{
"name": "6to5",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "2.4.0",
"version": "2.4.6",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://github.com/6to5/6to5",
"repository": {
@@ -35,7 +35,7 @@
"test": "make test"
},
"dependencies": {
"acorn-6to5": "0.11.1-1",
"acorn-6to5": "0.11.1-3",
"ast-types": "~0.6.1",
"chokidar": "0.11.1",
"commander": "2.5.0",

View File

@@ -6,5 +6,6 @@ var test = {
[( /*
* Inside bracket init
*/
"b")]: "2"
"b"
)]: "2"
}, ok = 42;

View File

@@ -7,7 +7,8 @@ var test = {
[( /*
* Inside bracket init
*/
"b")]: "2",
"b"
)]: "2",
["c"
/*
@@ -18,8 +19,9 @@ var test = {
["d"]: "4",
[(
// Inside bracket, line comment
"e")]: "5",
// Inside bracket, line comment
"e"
)]: "5",
["f"
// After bracket, line comment

View File

@@ -14,32 +14,32 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
woops["super"].test();
_TestSuper.call(this);
_TestSuper.prototype.test.call(this);
foob(_TestSuper);
_Foo.call(this);
_Foo.prototype.test.call(this);
foob(_Foo);
_TestSuper.call.apply(_TestSuper, [this].concat(_slice.call(arguments)));
_TestSuper.call.apply(_TestSuper, [this, "test"].concat(_slice.call(arguments)));
_Foo.call.apply(_Foo, [this].concat(_slice.call(arguments)));
_Foo.call.apply(_Foo, [this, "test"].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype, [this, "test"].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
Test.prototype.test = function () {
_TestSuper.prototype.test.call(this);
_TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this].concat(_slice.call(arguments)));
_TestSuper.prototype.test.call.apply(_TestSuper.prototype.test, [this, "test"].concat(_slice.call(arguments)));
_Foo.prototype.test.call(this);
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this].concat(_slice.call(arguments)));
_Foo.prototype.test.call.apply(_Foo.prototype.test, [this, "test"].concat(_slice.call(arguments)));
};
Test.foo = function () {
_TestSuper.foo.call(this);
_TestSuper.foo.call.apply(_TestSuper.foo, [this].concat(_slice.call(arguments)));
_TestSuper.foo.call.apply(_TestSuper.foo, [this, "test"].concat(_slice.call(arguments)));
_Foo.foo.call(this);
_Foo.foo.call.apply(_Foo.foo, [this].concat(_slice.call(arguments)));
_Foo.foo.call.apply(_Foo.foo, [this, "test"].concat(_slice.call(arguments)));
};
return Test;

View File

@@ -13,13 +13,13 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
_TestSuper.prototype.test;
_TestSuper.prototype.test.whatever;
_Foo.prototype.test;
_Foo.prototype.test.whatever;
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
return Test;
})();

View File

@@ -13,16 +13,16 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
_TestSuper.prototype.test.whatever();
_TestSuper.prototype.test.call(this);
_Foo.prototype.test.whatever();
_Foo.prototype.test.call(this);
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
Test.test = function () {
return _TestSuper.wow.call(this);
return _Foo.wow.call(this);
};
return Test;

View File

@@ -17,12 +17,12 @@ var Test = function Test() {
};
var Foo = (function () {
var _FooSuper = Bar;
var _Bar = Bar;
var Foo = function Foo() {
this.state = "test";
};
_inherits(Foo, _FooSuper);
_inherits(Foo, _Bar);
return Foo;
})();

View File

@@ -13,27 +13,27 @@ var _inherits = function (child, parent) {
};
var BaseController = (function () {
var _BaseControllerSuper = Chaplin.Controller;
var _Chaplin$Controller = Chaplin.Controller;
var BaseController = function BaseController() {
if (_BaseControllerSuper) {
_BaseControllerSuper.apply(this, arguments);
if (_Chaplin$Controller) {
_Chaplin$Controller.apply(this, arguments);
}
};
_inherits(BaseController, _BaseControllerSuper);
_inherits(BaseController, _Chaplin$Controller);
return BaseController;
})();
var BaseController2 = (function () {
var _BaseController2Super = Chaplin.Controller.Another;
var _Chaplin$Controller$Another = Chaplin.Controller.Another;
var BaseController2 = function BaseController2() {
if (_BaseController2Super) {
_BaseController2Super.apply(this, arguments);
if (_Chaplin$Controller$Another) {
_Chaplin$Controller$Another.apply(this, arguments);
}
};
_inherits(BaseController2, _BaseController2Super);
_inherits(BaseController2, _Chaplin$Controller$Another);
return BaseController2;
})();

View File

@@ -13,14 +13,14 @@ var _inherits = function (child, parent) {
};
var Test = (function () {
var _TestSuper = Foo;
var _Foo = Foo;
var Test = function Test() {
if (_TestSuper) {
_TestSuper.apply(this, arguments);
if (_Foo) {
_Foo.apply(this, arguments);
}
};
_inherits(Test, _TestSuper);
_inherits(Test, _Foo);
return Test;
})();

View File

@@ -24,15 +24,15 @@ var a = _ref2[0];
var b = _ref2[1];
var _ref3 = [3, 4];
var _ref4 = _slicedToArray(_ref3, 2);
var _ref3 = _slicedToArray(_ref3, 2);
var c = _ref4[0];
var d = _ref4[1];
var _ref5 = { e: 5, f: 6 };
var c = _ref3[0];
var d = _ref3[1];
var _ref4 = { e: 5, f: 6 };
var e = _ref5.e;
var f = _ref5.f;
var _ref6 = { a: 7, b: 8 };
var e = _ref4.e;
var f = _ref4.f;
var _ref5 = { a: 7, b: 8 };
var g = _ref6.a;
var h = _ref6.b;
var g = _ref5.a;
var h = _ref5.b;

View File

@@ -21,10 +21,10 @@ var _ref = ["hello", [", ", "junk"], ["world"]];
var _ref2 = _slicedToArray(_ref, 4);
var a = _ref2[0];
var _ref3 = _slicedToArray(_ref2[1], 1);
var _ref2$1 = _slicedToArray(_ref2[1], 1);
var b = _ref3[0];
var _ref4 = _slicedToArray(_ref2[2], 1);
var b = _ref2$1[0];
var _ref2$2 = _slicedToArray(_ref2[2], 1);
var c = _ref4[0];
var c = _ref2$2[0];
var d = _ref2[3];

View File

@@ -1,6 +1,6 @@
"use strict";
var _temp, _ref;
var _temp, _temp2;
var _slicedToArray = function (arr, i) {
if (Array.isArray(arr)) {
return arr;
@@ -17,4 +17,4 @@ var _slicedToArray = function (arr, i) {
}
};
console.log((_temp = [123], _ref = _slicedToArray(_temp, 1), x = _ref[0], _temp));
console.log((_temp = [123], _temp2 = _slicedToArray(_temp, 1), x = _temp2[0], _temp));

View File

@@ -21,10 +21,10 @@ var _ref = ["foo", "hello", [", ", "junk"], ["world"]];
var _ref2 = _slicedToArray(_ref, 5);
var a = _ref2[1];
var _ref3 = _slicedToArray(_ref2[2], 1);
var _ref2$2 = _slicedToArray(_ref2[2], 1);
var b = _ref3[0];
var _ref4 = _slicedToArray(_ref2[3], 1);
var b = _ref2$2[0];
var _ref2$3 = _slicedToArray(_ref2[3], 1);
var c = _ref4[0];
var c = _ref2$3[0];
var d = _ref2[4];

View File

@@ -16,11 +16,11 @@ var _slicedToArray = function (arr, i) {
}
};
var _ref = _slicedToArray(rect.topLeft, 2);
var _rect$topLeft = _slicedToArray(rect.topLeft, 2);
var x1 = _ref[0];
var y1 = _ref[1];
var _ref2 = _slicedToArray(rect.bottomRight, 2);
var x1 = _rect$topLeft[0];
var y1 = _rect$topLeft[1];
var _rect$bottomRight = _slicedToArray(rect.bottomRight, 2);
var x2 = _ref2[0];
var y2 = _ref2[1];
var x2 = _rect$bottomRight[0];
var y2 = _rect$bottomRight[1];

View File

@@ -31,17 +31,17 @@ function unpackObject(_ref2) {
console.log(unpackObject({ title: "title", author: "author" }));
var unpackArray = function (_ref3, _ref5) {
var _ref4 = _slicedToArray(_ref3, 3);
var unpackArray = function (_ref3, _ref4) {
var _ref3 = _slicedToArray(_ref3, 3);
var a = _ref4[0];
var b = _ref4[1];
var c = _ref4[2];
var _ref6 = _slicedToArray(_ref5, 3);
var a = _ref3[0];
var b = _ref3[1];
var c = _ref3[2];
var _ref4 = _slicedToArray(_ref4, 3);
var x = _ref6[0];
var y = _ref6[1];
var z = _ref6[2];
var x = _ref4[0];
var y = _ref4[1];
var z = _ref4[2];
return a + b + c;
};

View File

@@ -3,14 +3,14 @@
function add() {
var _arguments = arguments;
return (function () {
var _arr = [];
var _ref = [];
for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var i = _step.value;
_arr.push(i * _arguments[0]);
_ref.push(i * _arguments[0]);
}
return _arr;
return _ref;
})();
}

View File

@@ -1,17 +1,17 @@
"use strict";
var seattlers = (function () {
var _arr = [];
var _seattlers = [];
for (var _iterator = countries[Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var customers = _step.value;
for (var _iterator2 = customers[Symbol.iterator](), _step2; !(_step2 = _iterator2.next()).done;) {
var c = _step2.value;
if (c.city == "Seattle") {
_arr.push({ name: c.name, age: c.age });
_seattlers.push({ name: c.name, age: c.age });
}
}
}
return _arr;
return _seattlers;
})();

View File

@@ -3,14 +3,14 @@
function add() {
var _this = this;
return (function () {
var _arr = [];
var _ref = [];
for (var _iterator = [1, 2, 3][Symbol.iterator](), _step; !(_step = _iterator.next()).done;) {
var i = _step.value;
_arr.push(i * _this.multiplier);
_ref.push(i * _this.multiplier);
}
return _arr;
return _ref;
})();
}

View File

@@ -0,0 +1,3 @@
async function foo() {
var wat = await bar();
}

View File

@@ -0,0 +1,42 @@
"use strict";
var _asyncToGenerator = function (fn) {
return function () {
var gen = fn.apply(this, arguments);
return new Promise(function (resolve, reject) {
function step(getNext) {
var next;
try {
next = getNext();
} catch (e) {
reject(e);
return;
}
if (next.done) {
resolve(next.value);
return;
}
Promise.resolve(next.value).then(function (v) {
step(function () {
return gen.next(v);
});
}, function (e) {
step(function () {
return gen["throw"](e);
});
});
}
step(function () {
return gen.next();
});
});
};
};
var foo = _asyncToGenerator(function* foo() {
var wat = yield bar();
});

View File

@@ -0,0 +1,3 @@
async function foo() {
var wat = await bar();
}

View File

@@ -0,0 +1,11 @@
"use strict";
var _interopRequire = function (obj) {
return obj && (obj["default"] || obj);
};
var Bluebird = _interopRequire(require("bluebird"));
var foo = Bluebird.coroutine(function* foo() {
var wat = yield bar();
});

View File

@@ -27,10 +27,10 @@ var _ref = ["hello", [", ", "junk"], ["world"]];
var _ref2 = _slicedToArray(_ref, 4);
var a = _ref2[0];
var _ref3 = _slicedToArray(_ref2[1], 1);
var _ref2$1 = _slicedToArray(_ref2[1], 1);
var b = _ref3[0];
var _ref4 = _slicedToArray(_ref2[2], 1);
var b = _ref2$1[0];
var _ref2$2 = _slicedToArray(_ref2[2], 1);
var c = _ref4[0];
var c = _ref2$2[0];
var d = _ref2[3];

View File

@@ -1,6 +1,6 @@
"use strict";
var _propKey2, _obj2, _propKey4;
var _ref2, _obj2, _ref4;
var _hasOwn = Object.prototype.hasOwnProperty;
var obj = {};
@@ -9,17 +9,17 @@ if (!_hasOwn.call(obj, "x")) obj.x = 2;
console.log((!_hasOwn.call(obj, "x") && (obj.x = 2), obj.x));
var _propKey = x();
var _ref = x();
if (!_hasOwn.call(obj, _propKey)) obj[_propKey] = 2;
if (!_hasOwn.call(obj, _ref)) obj[_ref] = 2;
console.log((_propKey2 = x(), !_hasOwn.call(obj, _propKey2) && (obj[_propKey2] = 2), obj[_propKey2]));
console.log((_ref2 = x(), !_hasOwn.call(obj, _ref2) && (obj[_ref2] = 2), obj[_ref2]));
var _obj = obj[y()];
var _propKey3 = x();
var _ref3 = x();
if (!_hasOwn.call(_obj, _propKey3)) _obj[_propKey3] = 2;
if (!_hasOwn.call(_obj, _ref3)) _obj[_ref3] = 2;
console.log((_obj2 = obj[y()], _propKey4 = x(), !_hasOwn.call(_obj2, _propKey4) && (_obj2[_propKey4] = 2), _obj2[_propKey4]));
console.log((_obj2 = obj[y()], _ref4 = x(), !_hasOwn.call(_obj2, _ref4) && (_obj2[_ref4] = 2), _obj2[_ref4]));

55
test/types.js Normal file
View File

@@ -0,0 +1,55 @@
var assert = require("assert");
var t = require("../lib/6to5/types");
suite("types", function () {
test("isFalsyExpression", function () {
assert.ok(t.isFalsyExpression(t.literal("")));
assert.ok(t.isFalsyExpression(t.literal(null)));
assert.ok(t.isFalsyExpression(t.literal(0)));
assert.ok(t.isFalsyExpression(t.identifier("undefined")));
assert.ok(!t.isFalsyExpression(t.literal("foobar")));
assert.ok(!t.isFalsyExpression(t.literal(5)));
assert.ok(!t.isFalsyExpression(t.identifier("foobar")));
});
test("toSequenceExpression");
test("shallowEqual");
test("appendToMemberExpression");
test("prependToMemberExpression");
test("isDynamic");
test("isReferenced");
test("isValidIdentifier");
test("toIdentifier");
test("ensureBlock");
test("toStatement");
test("toBlock");
test("getUid");
test("getIds");
test("isLet");
test("isVar");
test("removeComments");
test("inheritsComments");
test("inherits");
test("getSpecifierName");
test("isSpecifierDefault");
});