add basic naive constant folding of assignment expressions
This commit is contained in:
parent
355ffbdaf8
commit
0f13097f59
@ -1,4 +1,7 @@
|
|||||||
export default {
|
export default {
|
||||||
|
//- builtin-prepass
|
||||||
|
"minification.constantFolding": require("./minification/constant-folding"),
|
||||||
|
|
||||||
//- builtin-setup
|
//- builtin-setup
|
||||||
strict: require("./other/strict"),
|
strict: require("./other/strict"),
|
||||||
_explode: require("./internal/explode"),
|
_explode: require("./internal/explode"),
|
||||||
@ -7,7 +10,6 @@ export default {
|
|||||||
"minification.removeDebugger": require("./minification/remove-debugger"),
|
"minification.removeDebugger": require("./minification/remove-debugger"),
|
||||||
"minification.removeConsole": require("./minification/remove-console"),
|
"minification.removeConsole": require("./minification/remove-console"),
|
||||||
"utility.inlineEnvironmentVariables": require("./utility/inline-environment-variables"),
|
"utility.inlineEnvironmentVariables": require("./utility/inline-environment-variables"),
|
||||||
"minification.constantFolding": require("./minification/constant-folding"),
|
|
||||||
"minification.deadCodeElimination": require("./minification/dead-code-elimination"),
|
"minification.deadCodeElimination": require("./minification/dead-code-elimination"),
|
||||||
_modules: require("./internal/modules"),
|
_modules: require("./internal/modules"),
|
||||||
"spec.functionName": require("./spec/function-name"),
|
"spec.functionName": require("./spec/function-name"),
|
||||||
|
|||||||
@ -2,7 +2,28 @@ import * as t from "../../../types";
|
|||||||
|
|
||||||
export var metadata = {
|
export var metadata = {
|
||||||
optional: true,
|
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 = {
|
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()) {
|
if ((path.isIdentifier() || path.isMemberExpression()) && path.isReferenced()) {
|
||||||
path = path.resolve();
|
path = path.resolve();
|
||||||
if (path) {
|
if (path) {
|
||||||
|
|||||||
@ -7,9 +7,29 @@ export default class Binding {
|
|||||||
this.references = 0;
|
this.references = 0;
|
||||||
this.referenced = false;
|
this.referenced = false;
|
||||||
|
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.kind = kind;
|
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