Compare commits

...

8 Commits

Author SHA1 Message Date
Sebastian McKenzie
c9d9a085f1 v1.12.13 2014-11-15 03:08:35 +11:00
Sebastian McKenzie
6d1953d9c3 fix constants transformer not accurately checking nodes 2014-11-15 03:07:33 +11:00
Sebastian McKenzie
92621d71c7 remove unused variable 2014-11-15 03:01:58 +11:00
Sebastian McKenzie
dc01731c25 add changelog for 1.12.13 2014-11-15 03:01:01 +11:00
Sebastian McKenzie
9fb8a80f60 support raw property on tagged template literals - closes #164 2014-11-15 03:00:53 +11:00
Sebastian McKenzie
85c2de57e4 fix for-head duplication testing and replacement - fixes #162 2014-11-15 02:50:05 +11:00
Sebastian McKenzie
58fac2e2be support duplicate constants within different block scopes - fixes #161 2014-11-15 02:49:49 +11:00
Sebastian McKenzie
682806f1ca instead of ignoring dot tests, add them as pending 2014-11-15 02:49:28 +11:00
15 changed files with 93 additions and 35 deletions

View File

@@ -1,3 +1,9 @@
# 1.12.13
* Support duplicate constants within different block scopes.
* Fix for-head duplication testing and replacement.
* Support `raw` property on tagged template literals.
# 1.12.12
* Make scope tracker more reliable to handle all edgecases.

View File

@@ -18,7 +18,8 @@ function File(opts) {
this.ast = {};
}
File.declarations = ["extends", "class-props", "slice", "apply-constructor"];
File.declarations = ["extends", "class-props", "slice", "apply-constructor",
"tagged-template-literal"];
File.normaliseOptions = function (opts) {
opts = _.cloneDeep(opts || {});

View File

@@ -0,0 +1,4 @@
(function (strings, raw) {
return Object.defineProperties(strings, { raw: { value: raw } });
});

View File

@@ -7,13 +7,14 @@ exports.BlockStatement =
exports.ForInStatement =
exports.ForOfStatement =
exports.ForStatement = function (node, parent, file) {
var constants = [];
var constants = {};
var check = function (node, names) {
var check = function (node, names, parent) {
_.each(names, function (name) {
if (constants.indexOf(name) >= 0) {
throw file.errorWithNode(node, name + " is read-only");
}
if (!_.has(constants, name)) return;
if (parent && t.isBlockStatement(parent) && parent !== constants[name]) return;
throw file.errorWithNode(node, name + " is read-only");
});
};
@@ -21,12 +22,12 @@ exports.ForStatement = function (node, parent, file) {
return t.getIds(node, false, ["MemberExpression"]);
};
_.each(node.body, function (child) {
_.each(node.body, function (child, parent) {
if (child && t.isVariableDeclaration(child, { kind: "const" })) {
_.each(child.declarations, function (declar) {
_.each(getIds(declar), function (name) {
check(declar, [name]);
constants.push(name);
check(declar, [name], parent);
constants[name] = parent;
});
declar._ignoreConstant = true;
@@ -37,13 +38,13 @@ exports.ForStatement = function (node, parent, file) {
}
});
if (!constants.length) return;
if (_.isEmpty(constants)) return;
traverse(node, function (child) {
traverse(node, function (child, parent) {
if (child._ignoreConstant) return;
if (t.isVariableDeclarator(child) || t.isDeclaration(child) || t.isAssignmentExpression(child)) {
check(child, getIds(child));
if (t.isDeclaration(child) || t.isAssignmentExpression(child)) {
check(child, getIds(child), parent);
}
});
};

View File

@@ -145,11 +145,25 @@ LetScoping.prototype.noClosure = function () {
if (_.isEmpty(replacements)) return;
traverse(block, function (node, parent) {
var replace = function (node, parent) {
if (!t.isIdentifier(node)) return;
if (!t.isReferenced(node, parent)) return;
node.name = replacements[node.name] || node.name;
});
};
var traverseReplace = function (node, parent) {
replace(node, parent);
traverse(node, replace);
};
var forParent = this.forParent;
if (forParent) {
traverseReplace(forParent.right, forParent);
traverseReplace(forParent.test, forParent);
traverseReplace(forParent.update, forParent);
}
traverse(block, replace);
};
/**
@@ -181,10 +195,23 @@ LetScoping.prototype.getInfo = function () {
keys: []
};
var duplicates = function (id, key) {
var has = scope.parentGet(key);
if (has && has !== id) {
// there's a variable with this exact name in an upper scope so we need
// to generate a new name
opts.duplicates[key] = id.name = file.generateUid(key, scope);
}
};
_.each(opts.declarators, function (declar) {
opts.declarators.push(declar);
var keys = t.getIds(declar);
var keys = t.getIds(declar, true);
_.each(keys, duplicates);
keys = _.keys(keys);
opts.outsideKeys = opts.outsideKeys.concat(keys);
opts.keys = opts.keys.concat(keys);
});
@@ -193,14 +220,7 @@ LetScoping.prototype.getInfo = function () {
if (!isLet(declar)) return;
_.each(t.getIds(declar, true), function (id, key) {
var has = scope.parentGet(key);
if (has && has !== id) {
// there's a variable with this exact name in an upper scope so we need
// to generate a new name
opts.duplicates[key] = id.name = file.generateUid(key, scope);
}
duplicates(id, key);
opts.keys.push(key);
});
});

View File

@@ -5,14 +5,22 @@ var buildBinaryExpression = function (left, right) {
return t.binaryExpression("+", left, right);
};
exports.TaggedTemplateExpression = function (node) {
exports.TaggedTemplateExpression = function (node, parent, file) {
var args = [];
var quasi = node.quasi;
var strings = quasi.quasis.map(function (elem) {
return t.literal(elem.value.raw);
var strings = [];
var raw = [];
_.each(quasi.quasis, function (elem) {
strings.push(t.literal(elem.value.cooked));
raw.push(t.literal(elem.value.raw));
});
args.push(t.arrayExpression(strings));
args.push(t.callExpression(file.addDeclaration("tagged-template-literal"), [
t.arrayExpression(strings),
t.arrayExpression(raw)
]));
_.each(quasi.expressions, function (expr) {
args.push(expr);

View File

@@ -1,7 +1,7 @@
{
"name": "6to5",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "1.12.12",
"version": "1.12.13",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://github.com/6to5/6to5",
"repository": {

View File

@@ -36,8 +36,6 @@ exports.get = function (entryName) {
if (fs.existsSync(suiteOptsLoc)) suite.options = require(suiteOptsLoc);
_.each(fs.readdirSync(suite.filename), function (taskName) {
if (taskName[0] === ".") return;
var taskDir = suite.filename + "/" + taskName;
if (fs.statSync(taskDir).isFile()) return;
@@ -59,6 +57,7 @@ exports.get = function (entryName) {
var test = {
title: humanise(taskName),
disabled: taskName[0] === ".",
options: taskOpts,
exec: {
code: readFile(execLoc),

View File

@@ -0,0 +1,6 @@
let x = 1;
{
let x = 2;
assert.equal(x, 2);
}
assert.equal(x, 1);

View File

@@ -0,0 +1,5 @@
assert.equal((function(){
let a = 1;
for (let a = 0; a < 8; a++) {}
return a;
}()), 1);

View File

@@ -1 +1 @@
var foo = bar`a${ 42 }b ${_.foobar()}`;
var foo = bar`wow\na${ 42 }b ${_.foobar()}`;

View File

@@ -1,3 +1,11 @@
"use strict";
var foo = bar(["a", "b ", ""], 42, _.foobar());
var _taggedTemplateLiteral = function (strings, raw) {
return Object.defineProperties(strings, {
raw: {
value: raw
}
});
};
var foo = bar(_taggedTemplateLiteral(["wow\na", "b ", ""], ["wow\\na", "b ", ""]), 42, _.foobar());

View File

@@ -22,7 +22,7 @@ suite("generation", function () {
_.each(helper.get("generation"), function (testSuite) {
suite("generation/" + testSuite.title, function () {
_.each(testSuite.tests, function (task) {
test(task.title, function () {
test(task.title, !task.disabled && function () {
var expect = task.expect;
var actual = task.actual;

View File

@@ -64,7 +64,7 @@ var run = function (task) {
_.each(helper.get("transformation"), function (testSuite) {
suite("transformation/" + testSuite.title, function () {
_.each(testSuite.tests, function (task) {
test(task.title, function () {
test(task.title, !task.disabled && function () {
var runTask = function () {
run(task);
};