Skip to content

Commit

Permalink
Trace primitive values through variables
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 2, 2018
1 parent 0a5d4cc commit 187a256
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 22 deletions.
9 changes: 8 additions & 1 deletion src/ast/nodes/Identifier.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Node, NodeBase } from './shared/Node';
import isReference from 'is-reference';
import { ObjectPath, UNKNOWN_EXPRESSION } from '../values';
import { ObjectPath, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import Variable from '../variables/Variable';
import CallOptions from '../CallOptions';
Expand Down Expand Up @@ -72,6 +72,13 @@ export default class Identifier extends NodeBase {
}
}

getPrimitiveValue() {
if (this.variable !== null) {
return this.variable.getPrimitiveValue();
}
return UNKNOWN_VALUE;
}

hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions): boolean {
return this.variable && this.variable.hasEffectsWhenAccessedAtPath(path, options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/IfStatement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class IfStatement extends StatementBase {

render(code: MagicString, options: RenderOptions) {
// Note that unknown test values are always included
const testValue = this.hasUnknownTestValue ? UNKNOWN_VALUE : this.test.getPrimitiveValue([]);
const testValue = this.hasUnknownTestValue ? UNKNOWN_VALUE : this.test.getPrimitiveValue();
if (
!this.test.included &&
(testValue ? this.alternate === null || !this.alternate.included : !this.consequent.included)
Expand Down
9 changes: 8 additions & 1 deletion src/ast/variables/LocalVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ForEachReturnExpressionCallback,
SomeReturnExpressionCallback
} from '../nodes/shared/Expression';
import { ObjectPath } from '../values';
import { ObjectPath, UNKNOWN_VALUE } from '../values';
import { Node } from '../nodes/shared/Node';
import { NodeType } from '../nodes/NodeType';

Expand Down Expand Up @@ -57,6 +57,13 @@ export default class LocalVariable extends Variable {
}
}

getPrimitiveValue() {
if (!this.init || this.reassignments.isPathReassigned([])) {
return UNKNOWN_VALUE;
}
return this.init.getPrimitiveValue();
}

hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions) {
if (path.length === 0) return false;
return (
Expand Down
6 changes: 5 additions & 1 deletion src/ast/variables/ReplaceableInitializationVariable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import LocalVariable from './LocalVariable';
import { ObjectPath, UNKNOWN_EXPRESSION } from '../values';
import { ObjectPath, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../values';
import ExecutionPathOptions from '../ExecutionPathOptions';
import CallOptions from '../CallOptions';
import Identifier from '../nodes/Identifier';
Expand All @@ -10,6 +10,10 @@ export default class ReplaceableInitializationVariable extends LocalVariable {
super(name, declarator, null);
}

getPrimitiveValue() {
return UNKNOWN_VALUE;
}

hasEffectsWhenAccessedAtPath(path: ObjectPath, options: ExecutionPathOptions) {
return (
this._getInit(options).hasEffectsWhenAccessedAtPath(path, options) ||
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/dynamic-resolvable-if-statements/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'allows using variables to resolve conditionals'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function () { 'use strict';

{
console.log('retained');
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

{
console.log('retained');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
console.log('retained');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(function () {
'use strict';

{
console.log('retained');
}

}());
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
System.register([], function (exports, module) {
'use strict';
return {
execute: function () {

{
console.log('retained');
}

}
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

{
console.log('retained');
}

})));
7 changes: 7 additions & 0 deletions test/form/samples/dynamic-resolvable-if-statements/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const value = true;

if (value) {
console.log('retained');
} else {
console.log('removed');
}
4 changes: 1 addition & 3 deletions test/form/samples/side-effect-p/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
define(function () { 'use strict';

var bool = true;

const hs = document.documentElement.style;

if ( bool ) {
{
hs.color = "#222";
}

Expand Down
4 changes: 1 addition & 3 deletions test/form/samples/side-effect-p/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';

var bool = true;

const hs = document.documentElement.style;

if ( bool ) {
{
hs.color = "#222";
}
4 changes: 1 addition & 3 deletions test/form/samples/side-effect-p/_expected/es.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var bool = true;

const hs = document.documentElement.style;

if ( bool ) {
{
hs.color = "#222";
}
4 changes: 1 addition & 3 deletions test/form/samples/side-effect-p/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
(function () {
'use strict';

var bool = true;

const hs = document.documentElement.style;

if ( bool ) {
{
hs.color = "#222";
}

Expand Down
4 changes: 1 addition & 3 deletions test/form/samples/side-effect-p/_expected/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ System.register([], function (exports, module) {
return {
execute: function () {

var bool = true;

const hs = document.documentElement.style;

if ( bool ) {
{
hs.color = "#222";
}

Expand Down
4 changes: 1 addition & 3 deletions test/form/samples/side-effect-p/_expected/umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
(factory());
}(this, (function () { 'use strict';

var bool = true;

const hs = document.documentElement.style;

if ( bool ) {
{
hs.color = "#222";
}

Expand Down

0 comments on commit 187a256

Please sign in to comment.