Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Only start another tree-shaking pass if a variable was included;
saves one pass
  • Loading branch information
lukastaegert committed Apr 16, 2018
1 parent 52fb2d7 commit 67e4080
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 64 deletions.
6 changes: 3 additions & 3 deletions src/ast/nodes/BlockStatement.ts
Expand Up @@ -31,14 +31,14 @@ export default class BlockStatement extends StatementBase {
}

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
for (const node of this.body) {
if (node.shouldBeIncluded() && node.include()) {
addedNewNodes = true;
anotherPassNeeded = true;
}
}
return addedNewNodes;
return anotherPassNeeded;
}

render(code: MagicString, options: RenderOptions) {
Expand Down
12 changes: 6 additions & 6 deletions src/ast/nodes/ConditionalExpression.ts
Expand Up @@ -83,17 +83,17 @@ export default class ConditionalExpression extends NodeBase {
}

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
const testValue = this.test.getValue();
if (testValue === UNKNOWN_VALUE || this.test.shouldBeIncluded()) {
if (this.test.include()) addedNewNodes = true;
if (this.consequent.include()) addedNewNodes = true;
if (this.alternate.include()) addedNewNodes = true;
if (this.test.include()) anotherPassNeeded = true;
if (this.consequent.include()) anotherPassNeeded = true;
if (this.alternate.include()) anotherPassNeeded = true;
} else if (testValue ? this.consequent.include() : this.alternate.include()) {
addedNewNodes = true;
anotherPassNeeded = true;
}
return addedNewNodes;
return anotherPassNeeded;
}

reassignPath(path: ObjectPath, options: ExecutionPathOptions) {
Expand Down
11 changes: 6 additions & 5 deletions src/ast/nodes/ForInStatement.ts
Expand Up @@ -32,11 +32,12 @@ export default class ForInStatement extends StatementBase {
}

include() {
let addedNewNodes = super.include();
if (this.left.includeWithAllDeclaredVariables()) {
addedNewNodes = true;
}
return addedNewNodes;
let anotherPassNeeded = false;
this.included = true;
if (this.left.includeWithAllDeclaredVariables()) anotherPassNeeded = true;
if (this.right.include()) anotherPassNeeded = true;
if (this.body.include()) anotherPassNeeded = true;
return anotherPassNeeded;
}

render(code: MagicString, options: RenderOptions) {
Expand Down
11 changes: 6 additions & 5 deletions src/ast/nodes/ForOfStatement.ts
Expand Up @@ -37,11 +37,12 @@ export default class ForOfStatement extends StatementBase {
}

include() {
let addedNewNodes = super.include();
if (this.left.includeWithAllDeclaredVariables()) {
addedNewNodes = true;
}
return addedNewNodes;
let anotherPassNeeded = false;
this.included = true;
if (this.left.includeWithAllDeclaredVariables()) anotherPassNeeded = true;
if (this.right.include()) anotherPassNeeded = true;
if (this.body.include()) anotherPassNeeded = true;
return anotherPassNeeded;
}

render(code: MagicString, options: RenderOptions) {
Expand Down
7 changes: 5 additions & 2 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -93,8 +93,11 @@ export default class Identifier extends NodeBase {
include() {
if (this.included) return false;
this.included = true;
if (this.variable !== null && !this.variable.included) this.variable.include();
return true;
if (this.variable !== null && !this.variable.included) {
this.variable.include();
return true;
}
return false;
}

initialise() {
Expand Down
12 changes: 6 additions & 6 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -25,21 +25,21 @@ export default class IfStatement extends StatementBase {
}

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
const testValue = this.test.getValue();
if ((testValue === UNKNOWN_VALUE || this.test.shouldBeIncluded()) && this.test.include()) {
addedNewNodes = true;
anotherPassNeeded = true;
}
if (testValue === UNKNOWN_VALUE) {
if (this.consequent.include()) addedNewNodes = true;
if (this.alternate !== null && this.alternate.include()) addedNewNodes = true;
if (this.consequent.include()) anotherPassNeeded = true;
if (this.alternate !== null && this.alternate.include()) anotherPassNeeded = true;
} else if (
testValue ? this.consequent.include() : this.alternate !== null && this.alternate.include()
) {
addedNewNodes = true;
anotherPassNeeded = true;
}
return addedNewNodes;
return anotherPassNeeded;
}

render(code: MagicString, options: RenderOptions) {
Expand Down
8 changes: 4 additions & 4 deletions src/ast/nodes/LogicalExpression.ts
Expand Up @@ -90,7 +90,7 @@ export default class LogicalExpression extends NodeBase {
}

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
const leftValue = this.left.getValue();
if (
Expand All @@ -99,13 +99,13 @@ export default class LogicalExpression extends NodeBase {
this.left.shouldBeIncluded()) &&
this.left.include()
)
addedNewNodes = true;
anotherPassNeeded = true;
if (
(leftValue === UNKNOWN_VALUE || !leftValue === (this.operator === '||')) &&
this.right.include()
)
addedNewNodes = true;
return addedNewNodes;
anotherPassNeeded = true;
return anotherPassNeeded;
}

reassignPath(path: ObjectPath, options: ExecutionPathOptions) {
Expand Down
15 changes: 10 additions & 5 deletions src/ast/nodes/MemberExpression.ts
Expand Up @@ -152,12 +152,17 @@ export default class MemberExpression extends NodeBase {
}

include() {
let addedNewNodes = super.include();
if (this.variable !== null && !this.variable.included) {
this.variable.include();
addedNewNodes = true;
let anotherPassNeeded = false;
if (!this.included) {
this.included = true;
if (this.variable !== null && !this.variable.included) {
this.variable.include();
anotherPassNeeded = true;
}
}
return addedNewNodes;
if (this.object.include()) anotherPassNeeded = true;
if (this.property.include()) anotherPassNeeded = true;
return anotherPassNeeded;
}

initialise() {
Expand Down
6 changes: 3 additions & 3 deletions src/ast/nodes/Program.ts
Expand Up @@ -9,14 +9,14 @@ export default class Program extends NodeBase {
sourceType: 'module';

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
for (const node of this.body) {
if (node.shouldBeIncluded() && node.include()) {
addedNewNodes = true;
anotherPassNeeded = true;
}
}
return addedNewNodes;
return anotherPassNeeded;
}

render(code: MagicString, options: RenderOptions) {
Expand Down
8 changes: 4 additions & 4 deletions src/ast/nodes/SequenceExpression.ts
Expand Up @@ -66,14 +66,14 @@ export default class SequenceExpression extends NodeBase {
}

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
for (let i = 0; i < this.expressions.length - 1; i++) {
const node = this.expressions[i];
if (node.shouldBeIncluded() && node.include()) addedNewNodes = true;
if (node.shouldBeIncluded() && node.include()) anotherPassNeeded = true;
}
if (this.expressions[this.expressions.length - 1].include()) addedNewNodes = true;
return addedNewNodes;
if (this.expressions[this.expressions.length - 1].include()) anotherPassNeeded = true;
return anotherPassNeeded;
}

reassignPath(path: ObjectPath, options: ExecutionPathOptions) {
Expand Down
12 changes: 4 additions & 8 deletions src/ast/nodes/SwitchCase.ts
Expand Up @@ -13,17 +13,13 @@ export default class SwitchCase extends NodeBase {
consequent: StatementNode[];

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
if (this.test && this.test.include()) {
addedNewNodes = true;
}
if (this.test && this.test.include()) anotherPassNeeded = true;
for (const node of this.consequent) {
if (node.shouldBeIncluded() && node.include()) {
addedNewNodes = true;
}
if (node.shouldBeIncluded() && node.include()) anotherPassNeeded = true;
}
return addedNewNodes;
return anotherPassNeeded;
}

render(code: MagicString, options: RenderOptions) {
Expand Down
8 changes: 3 additions & 5 deletions src/ast/nodes/VariableDeclaration.ts
Expand Up @@ -36,14 +36,12 @@ export default class VariableDeclaration extends NodeBase {
}

includeWithAllDeclaredVariables() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
for (const declarator of this.declarations) {
if (declarator.include()) {
addedNewNodes = true;
}
if (declarator.include()) anotherPassNeeded = true;
}
return addedNewNodes;
return anotherPassNeeded;
}

include() {
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/shared/FunctionNode.ts
Expand Up @@ -14,6 +14,7 @@ export default class FunctionNode extends NodeBase {
id: Identifier | null;
body: BlockStatement;
params: PatternNode[];

scope: BlockScope;

bind() {
Expand Down
12 changes: 4 additions & 8 deletions src/ast/nodes/shared/Node.ts
Expand Up @@ -190,22 +190,18 @@ export class NodeBase implements ExpressionNode {
}

include() {
let addedNewNodes = !this.included;
let anotherPassNeeded = false;
this.included = true;
for (const key of this.keys) {
const value = (<GenericEsTreeNode>this)[key];
if (value === null) continue;
if (Array.isArray(value)) {
for (const child of value) {
if (child !== null && child.include()) {
addedNewNodes = true;
}
if (child !== null && child.include()) anotherPassNeeded = true;
}
} else if (value.include()) {
addedNewNodes = true;
}
} else if (value.include()) anotherPassNeeded = true;
}
return addedNewNodes;
return anotherPassNeeded;
}

includeWithAllDeclaredVariables() {
Expand Down

0 comments on commit 67e4080

Please sign in to comment.