Compare commits

...

21 Commits

Author SHA1 Message Date
Sebastian McKenzie
234414c2f2 v3.3.0 2015-02-01 16:49:18 +11:00
Sebastian McKenzie
3ff544bbab fix indentation detection 2015-02-01 16:47:28 +11:00
Sebastian McKenzie
416c4cbb84 fix linting errors 2015-02-01 16:44:47 +11:00
Sebastian McKenzie
db5bf1749b fix linting errors 2015-02-01 16:43:42 +11:00
Sebastian McKenzie
41349afea3 add 3.3.0 changelog 2015-02-01 16:43:05 +11:00
Sebastian McKenzie
27da6de723 add back runtime - fixes #651 2015-02-01 16:38:13 +11:00
Sebastian McKenzie
2cdb4e3343 fix linting errors 2015-02-01 16:21:13 +11:00
Sebastian McKenzie
981d3e40f8 add canRun check for playground transformers 2015-02-01 16:20:32 +11:00
Sebastian McKenzie
c7a616730c add levenshtein suggestions to undeclared variable transformer 2015-02-01 16:20:18 +11:00
Sebastian McKenzie
5aa8ece242 don't run playground transformers at all if playground isn't enabled 2015-02-01 16:19:49 +11:00
Sebastian McKenzie
8c7ba20f86 fix regenerator transformer order - fixes #617 2015-02-01 16:19:35 +11:00
Sebastian McKenzie
1cc9027fcf 3.2.1 2015-02-01 16:19:09 +11:00
Sebastian McKenzie
e63dbaa646 v3.2.1 2015-01-31 21:33:26 +11:00
Sebastian McKenzie
4a720625d9 fix linting errors 2015-01-31 21:29:44 +11:00
Sebastian McKenzie
ad428b107a add 3.2.1 changelog 2015-01-31 21:28:14 +11:00
Sebastian McKenzie
c0299320f0 avoid transforming of inner labels and propagation of maps in block scoping transformer - #644 2015-01-31 21:27:20 +11:00
Sebastian McKenzie
efaee3d5d9 remove pending tests 2015-01-31 18:11:54 +11:00
Sebastian McKenzie
12bee73070 restructure internal indexes a bit 2015-01-31 18:04:57 +11:00
Sebastian McKenzie
82c18a837d add detection skeleton #631 2015-01-31 17:59:30 +11:00
Sebastian McKenzie
fed51e8246 3.2.0 2015-01-31 17:59:08 +11:00
Sebastian McKenzie
71f17e464f update 3.2.0 changelog 2015-01-31 17:59:02 +11:00
45 changed files with 581 additions and 137 deletions

View File

@@ -11,11 +11,24 @@
_Note: Gaps between patch versions are faulty/broken releases._
## 3.3.0
* **Bug Fix**
* Do all transforms before the regenerator transform is ran.
* **New Feature**
* Added back the 2.x optional runtime.
## 3.2.1
* **Bug Fix**
* Fix block scoping transformer rewriting breaks and continues to inner labels.
## 3.2.0
* **Bug Fix**
* Fixed scope tracking for default parameters IIFE.
* Fixed block scoped functions.
* Improved `bin/6to5` path resolution.
* **New Feature**
* You can now trigger super setters in classes. Thanks [@kruppel](https://github.com/kruppel)!
* Add `resolveSourceMap` option.

View File

@@ -21,6 +21,9 @@ build:
node $(BROWSERIFY_CMD) lib/6to5/browser.js -s to5 >dist/6to5.js
node $(UGLIFY_CMD) dist/6to5.js >dist/6to5.min.js
node bin/6to5-runtime >dist/runtime.js
node $(UGLIFY_CMD) dist/runtime.js >dist/runtime.min.js
rm -rf templates.json
clean:
@@ -77,6 +80,7 @@ publish:
make build
cp dist/6to5.min.js browser.js
cp dist/polyfill.min.js browser-polyfill.js
cp dist/runtime.min.js runtime.js
node tools/cache-templates
test -f templates.json

4
bin/6to5-runtime Executable file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/env node
var runtime = require("../lib/6to5/build-runtime");
console.log(runtime());

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env node
var commander = require("commander");
var transform = require("../../lib/6to5/transformation/transform");
var transform = require("../../lib/6to5/transformation");
var util = require("../../lib/6to5/util");
var fs = require("fs");
var each = require("lodash/collection/each");
@@ -11,6 +11,7 @@ commander.option("-t, --source-maps-inline", "Append sourceMappingURL comment to
commander.option("-s, --source-maps", "Save source map alongside the compiled code");
commander.option("-f, --filename [filename]", "Filename to use when reading from stdin - this will be used in source-maps, errors etc [stdin]", "stdin");
commander.option("-w, --watch", "Recompile files on changes");
commander.option("-r, --runtime", "Replace 6to5 declarations with references to a runtime");
commander.option("-e, --experimental", "Enable experimental support for proposed ES7 features");
commander.option("-p, --playground", "Enable playground support");
@@ -108,6 +109,7 @@ exports.opts = {
sourceMap: commander.sourceMaps || commander.sourceMapsInline,
optional: commander.optional,
comments: !commander.removeComments,
runtime: commander.runtime,
modules: commander.modules,
loose: commander.loose
};

View File

@@ -1,6 +1,6 @@
"use strict";
var transform = module.exports = require("./transformation/transform");
var transform = module.exports = require("./transformation");
transform.version = require("../../package").version;

13
lib/6to5/build-helpers.js Normal file
View File

@@ -0,0 +1,13 @@
var File = require("./file");
var util = require("./util");
var each = require("lodash/collection/each");
var t = require("./types");
module.exports = function (body, namespace) {
each(File.helpers, function (name) {
var key = t.identifier(t.toIdentifier(name));
body.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
));
});
};

25
lib/6to5/build-runtime.js Normal file
View File

@@ -0,0 +1,25 @@
"use strict";
var buildHelpers = require("./build-helpers");
var generator = require("./generation");
var util = require("./util");
var t = require("./types");
module.exports = function () {
var namespace = t.identifier("to5Runtime");
var body = [];
var container = t.functionExpression(null, [t.identifier("global")], t.blockStatement(body));
var tree = t.program([t.expressionStatement(t.callExpression(container, [util.template("self-global")]))]);
body.push(t.variableDeclaration("var", [
t.variableDeclarator(
namespace,
t.assignmentExpression("=", t.memberExpression(t.identifier("global"), namespace), t.objectExpression([]))
)
]));
buildHelpers(body, namespace);
return generator(tree).code;
};

View File

@@ -1,7 +1,29 @@
//exports.canRun = function (ast, userAgent) {
module.exports = detect;
//};
var SYNTAX_KEYS = require("./syntax-keys");
var traverse = require("../traverse");
var visitors = traverse.explode(require("./visitors"));
//exports.whoCanRun = function (ast, userAgent) {
function detect(ast) {
var stats = {
syntax: {},
builtins: {}
};
//};
var detectedSyntax = function (name) {
stats.syntax[name] = true;
};
traverse(ast, {
enter: function (node, parent) {
if (SYNTAX_KEYS[node.type]) {
detectedSyntax(SYNTAX_KEYS[node.type]);
}
var visitor = visitors[node.type];
if (visitor) visitor(node, parent, detectedSyntax);
}
});
return stats;
}

View File

@@ -0,0 +1,84 @@
{
"ArrowFunctionExpression": "es6.arrowFunctions",
"AwaitExpression": "es7.asyncFunctions",
"ClassBody": "es6.classes",
"ClassDeclaration": "es6.classes",
"ClassExpression": "es6.classes",
"MethodDefinition": "es6.classes",
"ComprehensionBlock": "es7.comprehensions",
"ComprehensionExpression": "es7.comprehensions",
"ForOfStatement": "es6.forOf",
"ExportBatchSpecifier": "es6.modules",
"ExportDeclaration": "es6.modules",
"ExportSpecifier": "es6.modules",
"ImportBatchSpecifier": "es6.modules",
"ImportDeclaration": "es6.modules",
"ImportSpecifier": "es6.modules",
"ArrayPattern": "es6.destructuring",
"AssignmentPattern": "es6.destructuring",
"ObjectPattern": "es6.destructuring",
"RestElement": "es6.parameters.rest",
"SpreadElement": "es6.spread",
"SpreadProperty": "es7.objectSpread",
"TaggedTemplateExpression": "es6.templateLiterals",
"TemplateElement": "es6.templateLiterals",
"TemplateLiteral": "es6.templateLiterals",
"VirtualPropertyExpression": "es7.abstractReferences",
"PrivateDeclaration": "es7.abstractReferences",
"YieldExpression": "es6.generators",
"AnyTypeAnnotation": "flow",
"ArrayTypeAnnotation": "flow",
"BooleanTypeAnnotation": "flow",
"ClassProperty": "flow",
"DeclareClass": "flow",
"DeclareFunction": "flow",
"DeclareModule": "flow",
"DeclareVariable": "flow",
"FunctionTypeAnnotation": "flow",
"FunctionTypeParam": "flow",
"GenericTypeAnnotation": "flow",
"InterfaceExtends": "flow",
"InterfaceDeclaration": "flow",
"IntersectionTypeAnnotation": "flow",
"NullableTypeAnnotation": "flow",
"NumberTypeAnnotation": "flow",
"StringLiteralTypeAnnotation": "flow",
"StringTypeAnnotation": "flow",
"TupleTypeAnnotation": "flow",
"TypeofTypeAnnotation": "flow",
"TypeAlias": "flow",
"TypeAnnotation": "flow",
"TypeParameterDeclaration": "flow",
"TypeParameterInstantiation": "flow",
"ObjectTypeAnnotation": "flow",
"ObjectTypeCallProperty": "flow",
"ObjectTypeIndexer": "flow",
"ObjectTypeProperty": "flow",
"QualifiedTypeIdentifier": "flow",
"UnionTypeAnnotation": "flow",
"VoidTypeAnnotation": "flow",
"JSXAttribute": "jsx",
"JSXClosingElement": "jsx",
"JSXElement": "jsx",
"JSXEmptyExpression": "jsx",
"JSXExpressionContainer": "jsx",
"JSXIdentifier": "jsx",
"JSXMemberExpression": "jsx",
"JSXNamespacedName": "jsx",
"JSXOpeningElement": "jsx",
"JSXSpreadAttribute": "jsx"
}

View File

@@ -0,0 +1,54 @@
var t = require("../types");
var _ = require("lodash");
exports.AssignmentExpression = function (node, parent, detected) {
if (node.operator === "**=") {
detected("es6.exponentation");
}
};
exports.BinaryExpression = function (node, parent, detected) {
if (node.operator === "**") {
detected("es6.exponentation");
}
};
exports.VariableDeclaration = function (node, parent, detected) {
if (node.kind === "let" || node.kind === "const") {
detected("es6.blockScoping");
}
if (node.kind === "const") {
detected("es6.constants");
}
};
exports.Property = function (node, parent, detected) {
if (node.shorthand || node.method) {
detected("es6.properties.shorthand");
}
if (node.kind === "set" || node.kind === "get") {
detected("es5.properties.mutators");
}
if (node.computed) {
detected("es6.properties.computed");
}
};
exports.AssignmentPattern = function (node, parent, detected) {
if (t.isFunction(parent) && _.contains(parent.params, node)) {
detected("es6.parameters.default");
}
};
exports.Function = function (node, parent, detected) {
if (node.generator) {
detected("es6.generators");
}
if (node.async) {
detected("es7.asyncFunctions");
}
};

View File

@@ -4,8 +4,8 @@ module.exports = File;
var SHEBANG_REGEX = /^\#\!.*/;
var transform = require("./transformation/transform");
var generate = require("./generation/generator");
var transform = require("./transformation");
var generate = require("./generation");
var clone = require("./helpers/clone");
var Scope = require("./traverse/scope");
var util = require("./util");
@@ -24,7 +24,7 @@ function File(opts) {
this.data = {};
this.lastStatements = [];
this.opts = File.normaliseOptions(opts);
this.opts = this.normaliseOptions(opts);
this.ast = {};
this.buildTransformers();
@@ -76,6 +76,7 @@ File.validOptions = [
"playground",
"experimental",
"resolveModuleSource",
"runtime",
// these are used by plugins
"ignore",
@@ -84,7 +85,7 @@ File.validOptions = [
"accept"
];
File.normaliseOptions = function (opts) {
File.prototype.normaliseOptions = function (opts) {
opts = clone(opts);
for (var key in opts) {
@@ -108,6 +109,7 @@ File.normaliseOptions = function (opts) {
comments: true,
filename: "unknown",
modules: "common",
runtime: false,
loose: [],
code: true,
ast: true
@@ -146,6 +148,10 @@ File.normaliseOptions = function (opts) {
opts.experimental = true;
}
if (opts.runtime) {
this.set("runtimeIdentifier", t.identifier("to5Runtime"));
}
opts.blacklist = transform._ensureTransformerNames("blacklist", opts.blacklist);
opts.whitelist = transform._ensureTransformerNames("whitelist", opts.whitelist);
opts.optional = transform._ensureTransformerNames("optional", opts.optional);

View File

@@ -40,9 +40,11 @@ each(Buffer.prototype, function (fn, key) {
});
CodeGenerator.normaliseOptions = function (code, opts) {
var indent = detectIndent(code);
var style = indent.indent;
if (!style || style === " ") style = " ";
var style = " ";
if (code) {
var indent = detectIndent(code).indent;
if (indent && indent !== " ") style = indent;
}
return merge({
parentheses: true,

View File

@@ -0,0 +1,38 @@
// taken from stackoverflow, it's crap i know.
module.exports = function (a, b) {
if (a.length === 0) return b.length;
if (b.length === 0) return a.length;
var matrix = [];
// increment along the first column of each row
var i;
for (i = 0; i <= b.length; i++) {
matrix[i] = [i];
}
// increment each column in the first row
var j;
for (j = 0; j <= a.length; j++) {
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for (i = 1; i <= b.length; i++) {
for (j = 1; j <= a.length; j++) {
if (b.charAt(i - 1) == a.charAt(j - 1)) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1, // substitution
Math.min(
matrix[i][j - 1] + 1, // insertion
matrix[i - 1][j] + 1) // deletion
);
}
}
}
return matrix[b.length][a.length];
};

View File

@@ -1,12 +1,14 @@
"use strict";
var transform = require("./transformation/transform");
var transform = require("./transformation");
var util = require("./util");
var fs = require("fs");
var isFunction = require("lodash/lang/isFunction");
exports.version = require("../../package").version;
exports.runtime = require("./build-runtime");
exports.types = require("./types");
exports.register = function (opts) {

View File

@@ -3,9 +3,9 @@
module.exports = CommonJSFormatter;
var DefaultFormatter = require("./_default");
var contains = require("lodash/collection/contains");
var util = require("../../util");
var t = require("../../types");
var contains = require("lodash/collection/contains");
function CommonJSFormatter() {
DefaultFormatter.apply(this, arguments);

View File

@@ -41,6 +41,8 @@ TransformerPass.prototype.canRun = function () {
if (transformer.experimental && !opts.experimental) return false;
if (transformer.playground && !opts.playground) return false;
return true;
};

View File

@@ -3,8 +3,8 @@
module.exports = Transformer;
var TransformerPass = require("./transformer-pass");
var t = require("../types");
var isFunction = require("lodash/lang/isFunction");
var traverse = require("../traverse");
var isObject = require("lodash/lang/isObject");
var each = require("lodash/collection/each");
@@ -17,6 +17,7 @@ var each = require("lodash/collection/each");
function Transformer(key, transformer, opts) {
this.manipulateOptions = transformer.manipulateOptions;
this.experimental = !!transformer.experimental;
this.playground = !!transformer.playground;
this.secondPass = !!transformer.secondPass;
this.optional = !!transformer.optional;
this.handlers = this.normalise(transformer);
@@ -31,6 +32,8 @@ Transformer.prototype.normalise = function (transformer) {
transformer = { ast: transformer };
}
traverse.explode(transformer);
each(transformer, function (fns, type) {
// hidden property
if (type[0] === "_") {
@@ -46,13 +49,6 @@ Transformer.prototype.normalise = function (transformer) {
if (!fns.exit) fns.exit = function () { };
transformer[type] = fns;
var aliases = t.FLIPPED_ALIAS_KEYS[type];
if (aliases) {
each(aliases, function (alias) {
transformer[alias] = fns;
});
}
});
return transformer;

View File

@@ -2,7 +2,6 @@
var traverse = require("../../../traverse");
var object = require("../../../helpers/object");
var clone = require("lodash/lang/clone");
var util = require("../../../util");
var t = require("../../../types");
var values = require("lodash/object/values");
@@ -311,9 +310,9 @@ var loopVisitor = {
var replace;
if (t.isLoop(node)) {
state = clone(state);
state.ignoreLabeless = true;
traverse(node, loopVisitor, scope, state);
state.ignoreLabeless = false;
}
if (t.isFunction(node) || t.isLoop(node)) {
@@ -324,9 +323,15 @@ var loopVisitor = {
if (loopText) {
if (node.label) {
// we shouldn't be transforming this because it exists somewhere inside
if (state.innerLabels.indexOf(node.label.name) >= 0) {
return;
}
loopText = loopText + "|" + node.label.name;
} else {
// we shouldn't be dealing with this
// we shouldn't be transforming these statements because
// they don't refer to the actual loop we're scopifying
if (state.ignoreLabeless) return;
// break statements mean something different in this context
@@ -352,6 +357,14 @@ var loopVisitor = {
}
};
var loopLabelVisitor = {
enter: function (node, parent, scope, context, state) {
if (t.isLabeledStatement(node)) {
state.innerLabels.push(node.label.name);
}
}
};
/**
* If we're inside of a loop then traverse it and check if it has one of
* the following node types `ReturnStatement`, `BreakStatement`,
@@ -365,12 +378,15 @@ LetScoping.prototype.checkLoop = function () {
var state = {
hasBreakContinue: false,
ignoreLabeless: false,
innerLabels: [],
hasReturn: false,
isLoop: !!this.loopParent,
map: {}
};
traverse(this.block, loopLabelVisitor, this.scope, state);
traverse(this.block, loopVisitor, this.scope, state);
return state;
};

View File

@@ -31,7 +31,7 @@ var visitor = {
exports.Scope = function (node, parent, scope, context, file) {
traverse(node, visitor, scope, {
constants: scope.getAllOfKind("const"),
constants: scope.getAllDeclarationsOfKind("const"),
file: file
});
};

View File

@@ -58,15 +58,15 @@ exports.Function = function (node, parent, scope) {
node.body.body.unshift(restDeclar);
}
node.body.body.unshift(
util.template("rest", {
ARGUMENTS: argsId,
ARRAY_KEY: arrKey,
ARRAY_LEN: arrLen,
START: start,
ARRAY: rest,
KEY: key,
LEN: len,
})
);
var loop = util.template("rest", {
ARGUMENTS: argsId,
ARRAY_KEY: arrKey,
ARRAY_LEN: arrLen,
START: start,
ARRAY: rest,
KEY: key,
LEN: len,
});
loop._blockHoist = node.params.length + 1;
node.body.body.unshift(loop);
};

View File

@@ -52,14 +52,13 @@ module.exports = {
// needs to be after `es6.blockScoping` due to needing `letReferences` set on blocks
"es6.blockScopingTDZ": require("./es6/block-scoping-tdz"),
// needs to be after block scoping since regenerator doesn't support it
regenerator: require("./other/regenerator"),
"es6.parameters.default": require("./es6/parameters.default"),
"es6.parameters.rest": require("./es6/parameters.rest"),
"es6.destructuring": require("./es6/destructuring"),
regenerator: require("./other/regenerator"),
// needs to be after `regenerator` due to needing `regeneratorRuntime` references
// needs to be after `es6.forOf` due to needing `Symbol.iterator` references
// needs to be before `es6.modules` due to dynamic imports

View File

@@ -1,7 +1,9 @@
"use strict";
var build = require("../../helpers/build-conditional-assignment-operator-transformer");
var t = require("../../../types");
var t = require("../../../types");
exports.playground = true;
build(exports, {
is: function (node, file) {

View File

@@ -3,6 +3,8 @@
var build = require("../../helpers/build-conditional-assignment-operator-transformer");
var t = require("../../../types");
exports.playground = true;
build(exports, {
is: function (node) {
var is = t.isAssignmentExpression(node) && node.operator === "?=";

View File

@@ -2,6 +2,8 @@
var t = require("../../../types");
exports.playground = true;
exports.BindMemberExpression = function (node, parent, scope) {
var object = node.object;
var prop = node.property;

View File

@@ -3,6 +3,8 @@
var traverse = require("../../../traverse");
var t = require("../../../types");
exports.playground = true;
var visitor = {
enter: function (node, parent, scope, context, state) {
if (t.isFunction(node)) return;

View File

@@ -1,9 +1,38 @@
"use strict";
var levenshtein = require("../../../helpers/levenshtein");
var t = require("../../../types");
exports.optional = true;
exports.Identifier = function (node, parent, scope, context, file) {
if (!scope.has(node.name, true)) {
throw file.errorWithNode(node, "Reference to undeclared variable", ReferenceError);
if (!t.isReferenced(node, parent)) return;
if (scope.has(node.name, true)) return;
var msg = "Reference to undeclared variable";
// get the closest declaration to offer as a suggestion
// the variable name may have just been mistyped
var declarations = scope.getAllDeclarations();
var closest;
var shortest = -1;
for (var name in declarations) {
var distance = levenshtein(node.name, name);
if (distance <= 0 || distance > 3) continue;
if (distance <= shortest) continue;
closest = name;
shortest = distance;
}
if (closest) {
msg += " - Did you mean " + closest + "?";
}
//
throw file.errorWithNode(node, msg, ReferenceError);
};

View File

@@ -237,6 +237,20 @@ traverse.removeProperties = function (tree) {
return tree;
};
traverse.explode = function (obj) {
for (var type in obj) {
var fns = obj[type];
var aliases = t.FLIPPED_ALIAS_KEYS[type];
if (aliases) {
for (var i = 0; i < aliases.length; i++) {
obj[aliases[i]] = fns;
}
}
}
return obj;
};
function hasBlacklistedType(node, parent, scope, context, state) {
if (node.type === state.type) {
state.has = true;

View File

@@ -326,12 +326,31 @@ Scope.prototype.getFunctionParent = function () {
};
/**
* Walks the scope tree and gathers all declarations of `kind`.
* Walks the scope tree and gathers **all** declarations.
*
* @returns {Object}
*/
Scope.prototype.getAllOfKind = function (kind) {
Scope.prototype.getAllDeclarations = function () {
var ids = object();
var scope = this;
do {
defaults(ids, scope.declarations);
scope = scope.parent;
} while (scope);
return ids;
};
/**
* Walks the scope tree and gathers all declarations of `kind`.
*
* @param {String} kind
* @returns {Object}
*/
Scope.prototype.getAllDeclarationsOfKind = function (kind) {
var ids = object();
var scope = this;

View File

@@ -1,7 +1,7 @@
{
"name": "6to5",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "3.2.0",
"version": "3.3.0",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://6to5.org/",
"repository": "6to5/6to5",
@@ -9,7 +9,8 @@
"main": "lib/6to5/index.js",
"bin": {
"6to5": "./bin/6to5/index.js",
"6to5-node": "./bin/6to5-node"
"6to5-node": "./bin/6to5-node",
"6to5-runtime": "./bin/6to5-runtime"
},
"browser": {
"./lib/6to5/index.js": "./lib/6to5/browser.js",
@@ -48,7 +49,7 @@
"lodash": "3.0.0",
"output-file-sync": "1.1.0",
"private": "0.1.6",
"regenerator-6to5": "0.8.9-7",
"regenerator-6to5": "0.8.9-8",
"regexpu": "1.1.0",
"roadrunner": "1.0.4",
"source-map": "0.1.43",

View File

@@ -1,7 +1,7 @@
{
"name": "6to5-runtime",
"description": "6to5 selfContained runtime",
"version": "3.1.1",
"version": "3.2.1",
"repository": "6to5/6to5",
"author": "Sebastian McKenzie <sebmck@gmail.com>"
}

View File

@@ -1,11 +1,10 @@
"use strict";
var transform = require("../lib/6to5/transformation/transform");
var File = require("../lib/6to5/file");
var util = require("../lib/6to5/util");
var fs = require("fs");
var t = require("../lib/6to5/types");
var _ = require("lodash");
var buildHelpers = require("../lib/6to5/build-helpers");
var transform = require("../lib/6to5/transformation");
var fs = require("fs");
var t = require("../lib/6to5/types");
var _ = require("lodash");
var relative = function (filename) {
return __dirname + "/6to5-runtime/" + filename;
@@ -35,12 +34,7 @@ var buildHelpers = function () {
var body = [];
var tree = t.program(body);
_.each(File.helpers, function (name) {
var key = t.identifier(t.toIdentifier(name));
body.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(t.identifier("exports"), key), util.template(name))
));
});
buildHelpers(body, t.identifier("exports"));
return transform.fromAst(tree, null, {
optional: ["selfContained"]

View File

@@ -1,5 +1,5 @@
var genHelpers = require("./_generator-helpers");
var transform = require("../lib/6to5/transformation/transform");
var transform = require("../lib/6to5/transformation");
var sourceMap = require("source-map");
var codeFrame = require("../lib/6to5/helpers/code-frame");
var esvalid = require("esvalid");

View File

@@ -1,4 +1,4 @@
var transform = require("../lib/6to5/transformation/transform");
var transform = require("../lib/6to5/transformation");
var assert = require("assert");
var File = require("../lib/6to5/file");

140
test/detection.js Normal file
View File

@@ -0,0 +1,140 @@
var detect = require("../lib/6to5/detection");
var assert = require("assert");
var util = require("../lib/6to5/util");
suite("detection", function () {
var checkSyntax = function (code, name) {
var ast = util.parse({
experimental: true
}, code);
assert.ok(detect(ast).syntax[name]);
};
test("es5.properties.mutators", function () {
checkSyntax("var obj = { get foo() {} };", "es5.properties.mutators");
checkSyntax("var obj = { set foo() {} };", "es5.properties.mutators");
});
test("es6.exponentation", function () {
checkSyntax("x ** 2;", "es6.exponentation");
checkSyntax("x **= 2;", "es6.exponentation");
});
test("es6.blockScoping", function () {
checkSyntax("let foo;", "es6.blockScoping");
checkSyntax("let foo = bar;", "es6.blockScoping");
checkSyntax("const foo = bar;", "es6.blockScoping");
});
test("es6.constants", function () {
checkSyntax("const foo = bar;", "es6.constants");
});
test("es6.properties.shorthand", function () {
checkSyntax("var obj = { foo };", "es6.properties.shorthand");
checkSyntax("var obj = { foo };", "es6.properties.shorthand");
});
test("es6.properties.computed", function () {
checkSyntax("var obj = { [foo]: bar };", "es6.properties.computed");
checkSyntax("var obj = { ['foo']: bar };", "es6.properties.computed");
});
test("es6.parameters.default", function () {
checkSyntax("var obj = (foo = bar) => {};", "es6.parameters.default");
checkSyntax("var obj = function (foo = bar) {};", "es6.parameters.default");
checkSyntax("function foo(foo = bar) {}", "es6.parameters.default");
});
test("es6.arrowFunctions", function () {
checkSyntax("var foo = x => x;", "es6.arrowFunctions");
checkSyntax("var foo = x => { return x * x };", "es6.arrowFunctions");
checkSyntax("var foo = (x) => x;", "es6.arrowFunctions");
checkSyntax("var foo = (a, b) => { return a * b };", "es6.arrowFunctions");
});
test("es6.classes", function () {
checkSyntax("class Foo {}", "es6.classes");
checkSyntax("var Foo = class {};", "es6.classes");
});
test("es6.forOf", function () {
checkSyntax("for (var val of foo);", "es6.forOf");
checkSyntax("for (val of foo);", "es6.forOf");
});
test("es6.modules", function () {
checkSyntax("import 'foo';", "es6.modules");
checkSyntax("import foo from 'foo';", "es6.modules");
checkSyntax("import * as foo from 'foo';", "es6.modules");
checkSyntax("import { foo } from 'foo';", "es6.modules");
checkSyntax("export { foo } from 'foo';", "es6.modules");
checkSyntax("export var foo = 5;", "es6.modules");
checkSyntax("export class Foo {}", "es6.modules");
checkSyntax("export function foo() {}", "es6.modules");
checkSyntax("export default class Foo {}", "es6.modules");
checkSyntax("export default function foo() {}", "es6.modules");
});
test("es6.destructuring", function () {
checkSyntax("[a, b] = [];", "es6.destructuring");
checkSyntax("var [a, b] = [];", "es6.destructuring");
checkSyntax("({ a, b }) = {};", "es6.destructuring");
checkSyntax("var { a, b } = {};", "es6.destructuring");
checkSyntax("function foo(foo = bar) {}", "es6.destructuring");
});
test("es6.parameters.rest", function () {
checkSyntax("function foo(...items) {}", "es6.parameters.rest");
checkSyntax("var foo = (...items) => {}", "es6.parameters.rest");
});
test("es6.spread", function () {
checkSyntax("new Foo(...items);", "es6.spread");
checkSyntax("foo(...items);", "es6.spread");
checkSyntax("[...items];", "es6.spread");
});
test("es6.templateLiterals", function () {
checkSyntax("`foobar`;", "es6.templateLiterals");
checkSyntax("foobar`foobar`;", "es6.templateLiterals");
});
test("es6.generators", function () {
checkSyntax("function* foo() {}", "es6.generators");
checkSyntax("var foo = function* () {};", "es6.generators");
});
test("es7.asyncFunctions", function () {
checkSyntax("async function foo() {}", "es7.asyncFunctions");
checkSyntax("var foo = async function() {};", "es7.asyncFunctions");
checkSyntax("var foo = async () => {};", "es7.asyncFunctions");
});
test("es7.comprehensions", function () {
checkSyntax("[for (i of test) i]", "es7.comprehensions");
checkSyntax("(for (i of test) i)", "es7.comprehensions");
});
test("es7.objectSpread", function () {
checkSyntax("var foo = { ...bar };", "es7.objectSpread");
});
test("es7.abstractReferences", function () {
checkSyntax("class Foo { private A; }", "es7.abstractReferences");
checkSyntax("foo::bar();", "es7.abstractReferences");
checkSyntax("delete foo::bar;", "es7.abstractReferences");
checkSyntax("foo::bar;", "es7.abstractReferences");
checkSyntax("foo::bar = baz;", "es7.abstractReferences");
});
test("flow", function () {
});
test("jsx", function () {
checkSyntax("<div />", "jsx");
checkSyntax("<Element />", "jsx");
});
});

View File

@@ -0,0 +1,14 @@
(function () {
var stack = [];
loop1:
for (let j = 0; j < 10; j++) {
for (let i = 0; i < 10; i++) {
stack.push(() => [i, j]);
break loop1;
}
}
assert.deepEqual(stack.length, 1);
assert.deepEqual(stack[0](), [0, 0]);
})();

View File

@@ -1,11 +0,0 @@
if (true) {
const x = 1;
switch (x) {
case 1: {
function y() {
assert(x, 1);
};
break;
}
}
}

View File

@@ -0,0 +1,5 @@
function* foo({ bar }) {
return bar;
}
assert(foo({ bar: "bar" }).next().value, "bar");

View File

@@ -1,5 +1,6 @@
function* foo({ bar }) {
function* foo() {
var { bar } = { bar: "bar" };
return bar;
}
assert(foo({ bar: "bar" }).next().value, "bar");
assert.deepEqual(foo().next().value, "bar");

View File

@@ -1,4 +1,4 @@
var generate = require("../lib/6to5/generation/generator");
var generate = require("../lib/6to5/generation");
var assert = require("assert");
var helper = require("./_helper");
var util = require("../lib/6to5/util");

View File

@@ -2,7 +2,7 @@ require("./_helper").assertVendor("compat-table");
require("../polyfill");
var transform = require("../lib/6to5/transformation/transform");
var transform = require("../lib/6to5/transformation");
var assert = require("assert");
var data = require("../vendor/compat-table/data-es6");
var _ = require("lodash");

View File

@@ -2,7 +2,7 @@ if (!process.env.ALL_6TO5_TESTS) return;
require("./_helper").assertVendor("regenerator");
var transform = require("../lib/6to5/transformation/transform");
var transform = require("../lib/6to5/transformation");
var fs = require("fs");
var _ = require("lodash");

View File

@@ -2,7 +2,7 @@ if (process.env.SIMPLE_6TO5_TESTS) return;
require("./_helper").assertVendor("test262");
var transform = require("../lib/6to5/transformation/transform");
var transform = require("../lib/6to5/transformation");
var readdir = require("fs-readdir-recursive");
var path = require("path");
var fs = require("fs");

View File

@@ -12,44 +12,4 @@ suite("types", function () {
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");
});

View File

@@ -22,6 +22,10 @@ suite("util", function () {
assert.ok(util.canCompile("/test.es"));
assert.ok(util.canCompile("/scripts/test.es"));
assert.ok(util.canCompile("test.jsx"));
assert.ok(util.canCompile("/test.jsx"));
assert.ok(util.canCompile("/scripts/test.jsx"));
assert.ok(!util.canCompile("test"));
assert.ok(!util.canCompile("test.css"));
assert.ok(!util.canCompile("/test.css"));
@@ -75,24 +79,8 @@ suite("util", function () {
}, /illegal type for regexify/);
});
test("getIds");
test("toStatement");
test("toBlock");
test("toIdentifier", function () {
assert.equal(t.toIdentifier(t.identifier("swag")), "swag");
assert.equal(t.toIdentifier("swag-lord"), "swagLord");
});
test("isDynamic");
test("isReferenced");
test("removeProperties");
test("ensureBlock");
test("pushMutatorMap");
});