add basic naive constant folding of assignment expressions
This commit is contained in:
parent
355ffbdaf8
commit
0f13097f59
@ -1,4 +1,7 @@
|
||||
export default {
|
||||
//- builtin-prepass
|
||||
"minification.constantFolding": require("./minification/constant-folding"),
|
||||
|
||||
//- builtin-setup
|
||||
strict: require("./other/strict"),
|
||||
_explode: require("./internal/explode"),
|
||||
@ -7,7 +10,6 @@ export default {
|
||||
"minification.removeDebugger": require("./minification/remove-debugger"),
|
||||
"minification.removeConsole": require("./minification/remove-console"),
|
||||
"utility.inlineEnvironmentVariables": require("./utility/inline-environment-variables"),
|
||||
"minification.constantFolding": require("./minification/constant-folding"),
|
||||
"minification.deadCodeElimination": require("./minification/dead-code-elimination"),
|
||||
_modules: require("./internal/modules"),
|
||||
"spec.functionName": require("./spec/function-name"),
|
||||
|
||||
@ -2,7 +2,28 @@ import * as t from "../../../types";
|
||||
|
||||
export var metadata = {
|
||||
optional: true,
|
||||
group: "builtin-setup"
|
||||
group: "builtin-prepass"
|
||||
};
|
||||
|
||||
export function AssignmentExpression() {
|
||||
var left = this.get("left");
|
||||
if (!left.isIdentifier()) return;
|
||||
|
||||
var binding = this.scope.getBinding(left.node.name);
|
||||
if (!binding) return;
|
||||
|
||||
var right = this.get("right");
|
||||
var evaluated = right.evaluate();
|
||||
//if (evaluated.confident) binding.setValue(evaluated.value);
|
||||
}
|
||||
|
||||
export var Scopable = {
|
||||
exit() {
|
||||
for (var name in this.scope.bindings) {
|
||||
var binding = this.scope.bindings[name];
|
||||
binding.clearValue();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export var Expression = {
|
||||
|
||||
@ -100,6 +100,11 @@ export function evaluate(): { confident: boolean; value: any } {
|
||||
}
|
||||
}
|
||||
|
||||
if (path.isReferencedIdentifier()) {
|
||||
var binding = path.scope.getBinding(node.name);
|
||||
if (binding && binding.hasValue) return binding.value;
|
||||
}
|
||||
|
||||
if ((path.isIdentifier() || path.isMemberExpression()) && path.isReferenced()) {
|
||||
path = path.resolve();
|
||||
if (path) {
|
||||
|
||||
@ -10,6 +10,26 @@ export default class Binding {
|
||||
this.scope = scope;
|
||||
this.path = path;
|
||||
this.kind = kind;
|
||||
|
||||
this.clearValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
setValue(value) {
|
||||
this.hasValue = true;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Description
|
||||
*/
|
||||
|
||||
clearValue() {
|
||||
this.hasValue = false;
|
||||
this.value = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user