Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Oct 15, 2019
1 parent 59b8673 commit 8949aaa
Show file tree
Hide file tree
Showing 27 changed files with 178 additions and 107 deletions.
2 changes: 1 addition & 1 deletion src/ast/nodes/ForStatement.ts
Expand Up @@ -42,7 +42,7 @@ export default class ForStatement extends StatementBase {
if (this.test) this.test.include(context, includeChildrenRecursively);
const { breakFlow } = context;
if (this.update) this.update.include(context, includeChildrenRecursively);
if (this.body) this.body.include(context, includeChildrenRecursively);
this.body.include(context, includeChildrenRecursively);
context.breakFlow = breakFlow;
}

Expand Down
7 changes: 1 addition & 6 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -19,16 +19,11 @@ export default class IfStatement extends StatementBase implements DeoptimizableE
test!: ExpressionNode;
type!: NodeType.tIfStatement;

private isTestValueAnalysed = false;
private testValue: LiteralValueOrUnknown;

bind() {
super.bind();
if (!this.isTestValueAnalysed) {
this.testValue = UnknownValue;
this.isTestValueAnalysed = true;
this.testValue = this.test.getLiteralValueAtPath(EMPTY_PATH, EMPTY_IMMUTABLE_TRACKER, this);
}
this.testValue = this.test.getLiteralValueAtPath(EMPTY_PATH, EMPTY_IMMUTABLE_TRACKER, this);
}

deoptimizeCache() {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/scopes/CatchScope.ts
Expand Up @@ -8,7 +8,7 @@ export default class CatchScope extends ParameterScope {
addDeclaration(
identifier: Identifier,
context: AstContext,
init: ExpressionEntity | null = null,
init: ExpressionEntity | null,
isHoisted: boolean | 'function'
): LocalVariable {
if (isHoisted) {
Expand Down
2 changes: 2 additions & 0 deletions src/ast/values.ts
Expand Up @@ -47,6 +47,7 @@ export const UNKNOWN_EXPRESSION: ExpressionEntity = {
included: true,
toString: () => '[[UNKNOWN]]'
};

export const UNDEFINED_EXPRESSION: ExpressionEntity = {
deoptimizePath: () => {},
getLiteralValueAtPath: () => undefined,
Expand All @@ -59,6 +60,7 @@ export const UNDEFINED_EXPRESSION: ExpressionEntity = {
included: true,
toString: () => 'undefined'
};

const returnsUnknown: RawMemberDescription = {
value: {
callsArgs: null,
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/binary-expressions/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles binary expression side-effects'
};
25 changes: 25 additions & 0 deletions test/form/samples/binary-expressions/_expected.js
@@ -0,0 +1,25 @@
if ((1 + 1).unknown) {
console.log('retained');
} else {
console.log('retained');
}
console.log('retained 1');
console.log('retained 2');
console.log('retained 3');
console.log('retained 4');
console.log('retained 5');
console.log('retained 6');
console.log('retained 7');
console.log('retained 8');
console.log('retained 9');
console.log('retained 10');
console.log('retained 11');
console.log('retained 12');
console.log('retained 13');
console.log('retained 14');
console.log('retained 15');
console.log('retained 16');
console.log('retained 17');
if (1 in 2) console.log('retained 18');
if (1 instanceof 2) console.log('retained 19');
console.log('retained 20');
28 changes: 28 additions & 0 deletions test/form/samples/binary-expressions/main.js
@@ -0,0 +1,28 @@
if ((1 + 1).unknown) {
console.log('retained');
} else {
console.log('retained');
}

if (1 != '1') console.log('removed');
if (1 !== 1) console.log('removed');
if (4 % 2 === 0) console.log('retained 1');
if ((6 & 3) === 2) console.log('retained 2');
if (2 * 3 === 6) console.log('retained 3');
if (2 ** 3 === 8) console.log('retained 4');
if (2 + 3 === 5) console.log('retained 5');
if (3 - 2 === 1) console.log('retained 6');
if (6 / 3 === 2) console.log('retained 7');
if (1 < 2 ) console.log('retained 8');
if (3 << 1 === 6) console.log('retained 9');
if (3 <= 4) console.log('retained 10');
if (1 == '1') console.log('retained 11');
if (1 === 1) console.log('retained 12');
if (3 > 2) console.log('retained 13');
if (3 >= 2) console.log('retained 14');
if (6 >> 1 === 3) console.log('retained 15');
if (-1 >>> 28 === 15) console.log('retained 16');
if (3 ^ 5 === 6) console.log('retained 17');
if (1 in 2) console.log('retained 18');
if (1 instanceof 2) console.log('retained 19');
if (2 | 4 === 6) console.log('retained 20');
Expand Up @@ -31,11 +31,11 @@ outer: {
console.log('retained');
}

function withReturn() {
function withConsequentReturn() {
outer: {
inner: {
if (globalThis.unknown) break inner;
else return;
if (globalThis.unknown) return;
else break inner;
}
console.log('retained');
}
Expand All @@ -46,4 +46,16 @@ function withReturn() {
}
}

withReturn();
withConsequentReturn();

function withAlternateReturn() {
outer: {
inner: {
if (globalThis.unknown) break inner;
else return;
}
console.log('retained');
}
}

withAlternateReturn();
Expand Up @@ -48,11 +48,11 @@ outer: {
console.log('retained');
}

function withReturn() {
function withConsequentReturn() {
outer: {
inner: {
if (globalThis.unknown) break inner;
else return;
if (globalThis.unknown) return;
else break inner;
console.log('removed');
}
console.log('retained');
Expand All @@ -67,4 +67,17 @@ function withReturn() {
}
}

withReturn();
withConsequentReturn();

function withAlternateReturn() {
outer: {
inner: {
if (globalThis.unknown) break inner;
else return;
console.log('removed');
}
console.log('retained');
}
}

withAlternateReturn();
@@ -1,4 +1,3 @@
module.exports = {
description: 'Tree-shake known array prototype functions',
options: { output: { name: 'bundle' } }
description: 'Tree-shake known array prototype functions'
};
Expand Up @@ -3,6 +3,8 @@ const map4 = [ 1 ].map( x => x ).map( x => console.log( 1 ) );
const map5 = [ 1 ].map( x => console.log( 1 ) ).map( x => x );
const map7 = [ 1 ].map( x => x ).map( x => x ).map( x => console.log( 1 ) );
const map8 = [ 1 ].map( x => x ).map( x => console.log( 1 ) ).map( x => x );

[]();
const _everyEffect = [ 1 ].every( () => console.log( 1 ) || true );
const _filterEffect = [ 1 ].filter( () => console.log( 1 ) || true );
const _findEffect = [ 1 ].find( () => console.log( 1 ) || true );
Expand Down
2 changes: 2 additions & 0 deletions test/form/samples/builtin-prototypes/array-expression/main.js
Expand Up @@ -12,6 +12,8 @@ const map6 = [ 1 ].map( x => x ).map( x => x ).map( x => x );
const map7 = [ 1 ].map( x => x ).map( x => x ).map( x => console.log( 1 ) );
const map8 = [ 1 ].map( x => x ).map( x => console.log( 1 ) ).map( x => x );

[]();

// accessor methods
const _includes = [].includes( 1 ).valueOf();
const _indexOf = [].indexOf( 1 ).toPrecision( 1 );
Expand Down
11 changes: 11 additions & 0 deletions test/form/samples/class-constructor-side-effect/_expected.js
@@ -0,0 +1,11 @@
class Effect {
constructor () {
console.log( 'Foo' );
}
}

new Effect();

class Empty {}

new Empty.doesNotExist();
11 changes: 0 additions & 11 deletions test/form/samples/class-constructor-side-effect/_expected/amd.js

This file was deleted.

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions test/form/samples/class-constructor-side-effect/_expected/iife.js

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions test/form/samples/class-constructor-side-effect/_expected/umd.js

This file was deleted.

15 changes: 13 additions & 2 deletions test/form/samples/class-constructor-side-effect/main.js
@@ -1,7 +1,18 @@
class Foo {
class Effect {
constructor () {
console.log( 'Foo' );
}
}

new Foo;
new Effect();

class NoEffect {
constructor () {
}
}

new NoEffect();

class Empty {}

new Empty.doesNotExist();
4 changes: 4 additions & 0 deletions test/form/samples/side-effects-switch-statements/_expected.js
Expand Up @@ -30,3 +30,7 @@ switch ( globalThis.unknown ) {
effect();
}
}());

switch ( globalThis.unknown ) {
case effect():
}
4 changes: 4 additions & 0 deletions test/form/samples/side-effects-switch-statements/main.js
Expand Up @@ -44,3 +44,7 @@ switch ( globalThis.unknown ) {
default:
}
}());

switch ( globalThis.unknown ) {
case effect():
}
3 changes: 3 additions & 0 deletions test/form/samples/unary-expressions/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles unary expression side-effects'
};
13 changes: 13 additions & 0 deletions test/form/samples/unary-expressions/_expected.js
@@ -0,0 +1,13 @@
if ((!true).unknown) {
console.log('retained');
} else {
console.log('retained');
}

console.log('retained 1');
console.log('retained 2');
console.log('retained 3');
if (delete 1) console.log('retained 4');
console.log('retained 5');
console.log('retained 6');
console.log('retained 7');
13 changes: 13 additions & 0 deletions test/form/samples/unary-expressions/main.js
@@ -0,0 +1,13 @@
if ((!true).unknown) {
console.log('retained');
} else {
console.log('retained');
}

if (!false) console.log('retained 1');
if (+'1' === 1) console.log('retained 2');
if (-1 + 2 === 1) console.log('retained 3');
if (delete 1) console.log('retained 4');
if (typeof 1 === 'number') console.log('retained 5');
if (void 1 === undefined) console.log('retained 6');
if (~1 === -2) console.log('retained 7');
14 changes: 8 additions & 6 deletions test/form/samples/undefined-var/_expected.js
@@ -1,8 +1,10 @@
var z;
console.log('no');
console.log('no');
if (z)
console.log('yes');
if (!z)
console.log('no');

console.log('retained');

console.log('retained');

if (z) console.log('retained');
else console.log('retained');

z = 1;

0 comments on commit 8949aaa

Please sign in to comment.