remove declarations transformer and instead do it whenever we push a generated declaration, better for perf and removes shitty bugs - fixes #1268

This commit is contained in:
Sebastian McKenzie
2015-04-26 15:46:06 +01:00
parent 68bfafe745
commit f4611469b4
20 changed files with 88 additions and 107 deletions

View File

@@ -41,10 +41,11 @@ export default class File {
this.dynamicImportIds = {};
this.dynamicImports = [];
this.usedHelpers = {};
this.dynamicData = {};
this.data = {};
this.uids = {};
this.declarations = {};
this.usedHelpers = {};
this.dynamicData = {};
this.data = {};
this.uids = {};
this.log = new Logger(this, opts.filename || "unknown");
this.opts = this.normalizeOptions(opts);
@@ -395,8 +396,8 @@ export default class File {
var program = this.ast.program;
var declar = program._declarations && program._declarations[name];
if (declar) return declar.id;
var declar = this.declarations[name];
if (declar) return declar;
this.usedHelpers[name] = true;
@@ -413,11 +414,11 @@ export default class File {
var ref = util.template("helper-" + name);
ref._compact = true;
var uid = this.scope.generateUidIdentifier(name);
var uid = this.declarations[name] = this.scope.generateUidIdentifier(name);
this.scope.push({
key: name,
id: uid,
init: ref
init: ref,
unique: true
});
return uid;
}

View File

@@ -21,3 +21,7 @@ export function UnaryExpression(node, parent, scope, file) {
}
}
}
export function VariableDeclaration(node) {
if (node._generated) this.skip();
}

View File

@@ -94,8 +94,6 @@ export default {
"spec.protoToAssign": require("./spec/proto-to-assign"),
_declarations: require("./internal/declarations"),
_shadowFunctions: require("./internal/shadow-functions"),
"es7.doExpressions": require("./es7/do-expressions"),

View File

@@ -1,37 +0,0 @@
import * as strict from "../../helpers/strict";
import * as t from "../../../types";
export var metadata = {
secondPass: true
};
export function BlockStatement(node, parent, scope, file) {
if (!node._declarations) return;
strict.wrap(node, function () {
var kinds = {};
var kind;
for (var i in node._declarations) {
var declar = node._declarations[i];
kind = declar.kind || "var";
var declarNode = t.variableDeclarator(declar.id, declar.init);
if (declar.init) {
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, [declarNode])));
} else {
kinds[kind] ||= [];
kinds[kind].push(declarNode);
}
}
for (kind in kinds) {
node.body.unshift(file.attachAuxiliaryComment(t.variableDeclaration(kind, kinds[kind])));
}
node._declarations = null;
});
}
export { BlockStatement as Program };

View File

@@ -76,7 +76,7 @@ traverse.node = function (node, opts, scope, state, parentPath) {
const CLEAR_KEYS = [
"trailingComments", "leadingComments", "extendedRange",
"_declarations", "_scopeInfo" ,"_paths",
"_scopeInfo" ,"_paths",
"tokens", "range", "start", "end", "loc", "raw"
];

View File

@@ -516,23 +516,35 @@ export default class Scope {
*/
push(opts: Object) {
var block = this.block;
var path = this.path;
if (t.isLoop(block) || t.isCatchClause(block) || t.isFunction(block)) {
t.ensureBlock(block);
block = block.body;
if (path.isLoop() || path.isCatchClause() || path.isFunction()) {
t.ensureBlock(path.node);
path = path.get("body");
}
if (!t.isBlockStatement(block) && !t.isProgram(block)) {
block = this.getBlockParent().block;
if (!path.isBlockStatement() && !path.isProgram()) {
path = this.getBlockParent().path;
}
block._declarations ||= {};
block._declarations[opts.key || opts.id.name] = {
kind: opts.kind || "var",
id: opts.id,
init: opts.init
};
var unique = opts.unique;
var kind = opts.kind || "var";
var dataKey = `declaration:${kind}`;
var declar = !unique && path.getData(dataKey);
if (!declar) {
declar = t.variableDeclaration(opts.kind || "var", []);
declar._generated = true;
declar._blockHoist = 2;
this.file.attachAuxiliaryComment(declar);
path.get("body")[0]._containerInsertBefore([declar]);
if (!unique) path.setData(dataKey, declar);
}
declar.declarations.push(t.variableDeclarator(opts.id, opts.init));
}
/**

View File

@@ -282,12 +282,11 @@ export function inheritsComments(child: Object, parent: Object): Object {
export function inherits(child: Object, parent: Object): Object {
if (!child || !parent) return child;
child._declarations = parent._declarations;
child._scopeInfo = parent._scopeInfo;
child.range = parent.range;
child.start = parent.start;
child.loc = parent.loc;
child.end = parent.end;
child._scopeInfo = parent._scopeInfo;
child.range = parent.range;
child.start = parent.start;
child.loc = parent.loc;
child.end = parent.end;
child.typeAnnotation = parent.typeAnnotation;
child.returnType = parent.returnType;