Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 22, 2021
1 parent 90b3fcd commit 2bc3bc5
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/ast/nodes/AssignmentExpression.ts
Expand Up @@ -15,7 +15,7 @@ import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';

export default class AssignmentExpression extends NodeBase {
left!: PatternNode;
left!: ExpressionNode | PatternNode;
operator!:
| '='
| '+='
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ForInStatement.ts
Expand Up @@ -49,7 +49,7 @@ export default class ForInStatement extends StatementBase {

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
this.left.includeWithAllDeclaredVariables(context, includeChildrenRecursively);
this.left.includeAllDeclaredVariables(context, includeChildrenRecursively);
this.left.deoptimizePath(EMPTY_PATH);
this.right.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ForOfStatement.ts
Expand Up @@ -34,7 +34,7 @@ export default class ForOfStatement extends StatementBase {

include(context: InclusionContext, includeChildrenRecursively: IncludeChildren) {
this.included = true;
this.left.includeWithAllDeclaredVariables(context, includeChildrenRecursively);
this.left.includeAllDeclaredVariables(context, includeChildrenRecursively);
this.left.deoptimizePath(EMPTY_PATH);
this.right.include(context, includeChildrenRecursively);
const { brokenFlow } = context;
Expand Down
5 changes: 1 addition & 4 deletions src/ast/nodes/MemberExpression.ts
Expand Up @@ -23,7 +23,6 @@ import Identifier from './Identifier';
import Literal from './Literal';
import * as NodeType from './NodeType';
import { ExpressionNode, IncludeChildren, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';
import SpreadElement from './SpreadElement';
import Super from './Super';

Expand Down Expand Up @@ -70,7 +69,7 @@ function getStringFromPath(path: PathWithPositions): string {
return pathString;
}

export default class MemberExpression extends NodeBase implements DeoptimizableEntity, PatternNode {
export default class MemberExpression extends NodeBase implements DeoptimizableEntity {
computed!: boolean;
object!: ExpressionNode | Super;
optional!: boolean;
Expand All @@ -84,8 +83,6 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
private replacement: string | null = null;
private wasPathDeoptimizedWhileOptimized = false;

addExportedVariables(): void {}

bind() {
if (this.bound) return;
this.bound = true;
Expand Down
7 changes: 4 additions & 3 deletions src/ast/nodes/Property.ts
Expand Up @@ -14,15 +14,16 @@ import { LiteralValueOrUnknown, UNKNOWN_EXPRESSION } from '../values';
import * as NodeType from './NodeType';
import { ExpressionEntity } from './shared/Expression';
import { ExpressionNode, NodeBase } from './shared/Node';
import { PatternNode } from './shared/Pattern';

export default class Property extends NodeBase implements DeoptimizableEntity {
export default class Property extends NodeBase implements DeoptimizableEntity, PatternNode {
computed!: boolean;
key!: ExpressionNode;
kind!: 'init' | 'get' | 'set';
method!: boolean;
shorthand!: boolean;
type!: NodeType.tProperty;
value!: ExpressionNode;
value!: ExpressionNode | (ExpressionNode & PatternNode);

private accessorCallOptions!: CallOptions;
private declarationInit: ExpressionEntity | null = null;
Expand All @@ -41,7 +42,7 @@ export default class Property extends NodeBase implements DeoptimizableEntity {

declare(kind: string, init: ExpressionEntity) {
this.declarationInit = init;
return this.value.declare(kind, UNKNOWN_EXPRESSION);
return (this.value as PatternNode).declare(kind, UNKNOWN_EXPRESSION);
}

// As getter properties directly receive their values from function expressions that always
Expand Down
12 changes: 4 additions & 8 deletions src/ast/nodes/VariableDeclaration.ts
Expand Up @@ -68,14 +68,14 @@ export default class VariableDeclaration extends NodeBase {
}
}

includeWithAllDeclaredVariables(
includeAllDeclaredVariables(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren
): void {
this.included = true;
for (const declarator of this.declarations) {
declarator.id.included = true;
declarator.includeWithAllDeclaredVariables(context, includeChildrenRecursively);
declarator.includeAllDeclaredVariables(context, includeChildrenRecursively);
}
}

Expand Down Expand Up @@ -109,16 +109,13 @@ export default class VariableDeclaration extends NodeBase {
lastSeparatorPos: number | null,
actualContentEnd: number,
renderedContentEnd: number,
addSemicolon: boolean,
systemPatternExports: Variable[],
options: RenderOptions
): void {
if (code.original.charCodeAt(this.end - 1) === 59 /*";"*/) {
code.remove(this.end - 1, this.end);
}
if (addSemicolon) {
separatorString += ';';
}
separatorString += ';';
if (lastSeparatorPos !== null) {
if (
code.original.charCodeAt(actualContentEnd - 1) === 10 /*"\n"*/ &&
Expand Down Expand Up @@ -150,7 +147,7 @@ export default class VariableDeclaration extends NodeBase {
private renderReplacedDeclarations(
code: MagicString,
options: RenderOptions,
{ start = this.start, end = this.end, isNoStatement }: NodeRenderOptions
{ start = this.start, end = this.end }: NodeRenderOptions
): void {
const separatedNodes = getCommaSeparatedNodesWithBoundaries(
this.declarations,
Expand Down Expand Up @@ -248,7 +245,6 @@ export default class VariableDeclaration extends NodeBase {
lastSeparatorPos,
actualContentEnd!,
renderedContentEnd,
!isNoStatement,
systemPatternExports,
options
);
Expand Down
5 changes: 1 addition & 4 deletions src/ast/nodes/VariableDeclarator.ts
Expand Up @@ -39,15 +39,12 @@ export default class VariableDeclarator extends NodeBase {
}
}

includeWithAllDeclaredVariables(
includeAllDeclaredVariables(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren
): void {
this.included = true;
this.id.include(context, includeChildrenRecursively);
if (this.init) {
this.init.include(context, includeChildrenRecursively);
}
}

render(code: MagicString, options: RenderOptions) {
Expand Down
42 changes: 20 additions & 22 deletions src/ast/nodes/shared/Node.ts
Expand Up @@ -15,7 +15,6 @@ import { getAndCreateKeys, keys } from '../../keys';
import ChildScope from '../../scopes/ChildScope';
import { ObjectPath, PathTracker } from '../../utils/PathTracker';
import { LiteralValueOrUnknown, UnknownValue, UNKNOWN_EXPRESSION } from '../../values';
import LocalVariable from '../../variables/LocalVariable';
import Variable from '../../variables/Variable';
import * as NodeType from '../NodeType';
import SpreadElement from '../SpreadElement';
Expand All @@ -42,16 +41,13 @@ export interface Node extends Entity {
type: string;
variable?: Variable | null;

addExportedVariables(variables: Variable[], exportNamesByVariable: Map<Variable, string[]>): void;

/**
* Called once all nodes have been initialised and the scopes have been populated.
*/
bind(): void;

/**
* Declare a new variable with the optional initialisation.
*/
declare(kind: string, init: ExpressionEntity | null): LocalVariable[];

/**
* Determine if this Node would have an effect on the bundle.
* This is usually true for already included nodes. Exceptions are e.g. break statements
Expand All @@ -72,10 +68,11 @@ export interface Node extends Entity {
* declarations to only include nodes for declarators that have an effect. Necessary
* for for-loops that do not use a declared loop variable.
*/
includeWithAllDeclaredVariables(
includeAllDeclaredVariables(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren
): void;

render(code: MagicString, options: RenderOptions, nodeRenderOptions?: NodeRenderOptions): void;

/**
Expand Down Expand Up @@ -118,6 +115,11 @@ export class NodeBase implements ExpressionNode {
this.context.magicString.addSourcemapLocation(this.end);
}

addExportedVariables(
_variables: Variable[],
_exportNamesByVariable: Map<Variable, string[]>
): void {}

/**
* Override this to bind assignments to variables and do any initialisations that
* require the scopes to be populated with variables.
Expand All @@ -143,10 +145,6 @@ export class NodeBase implements ExpressionNode {
this.scope = parentScope;
}

declare(_kind: string, _init: ExpressionEntity | null): LocalVariable[] {
return [];
}

deoptimizePath(_path: ObjectPath) {}

getLiteralValueAtPath(
Expand Down Expand Up @@ -209,19 +207,19 @@ export class NodeBase implements ExpressionNode {
}
}

includeCallArguments(context: InclusionContext, args: (ExpressionNode | SpreadElement)[]): void {
for (const arg of args) {
arg.include(context, false);
}
}

includeWithAllDeclaredVariables(
includeAllDeclaredVariables(
context: InclusionContext,
includeChildrenRecursively: IncludeChildren
): void {
this.include(context, includeChildrenRecursively);
}

includeCallArguments(context: InclusionContext, args: (ExpressionNode | SpreadElement)[]): void {
for (const arg of args) {
arg.include(context, false);
}
}

/**
* Override to perform special initialisation steps after the scope is initialised
*/
Expand Down Expand Up @@ -274,10 +272,6 @@ export class NodeBase implements ExpressionNode {
shouldBeIncluded(context: InclusionContext): boolean {
return this.included || (!context.brokenFlow && this.hasEffects(createHasEffectsContext()));
}

toString() {
return this.context.code.slice(this.start, this.end);
}
}

export { NodeBase as StatementBase };
Expand All @@ -290,3 +284,7 @@ export function locateNode(node: Node) {

return location;
}

export function logNode(node: Node) {
console.log(node.context.code.slice(node.start, node.end));
}
5 changes: 3 additions & 2 deletions src/ast/nodes/shared/Pattern.ts
@@ -1,7 +1,8 @@
import { WritableEntity } from '../../Entity';
import Variable from '../../variables/Variable';
import LocalVariable from '../../variables/LocalVariable';
import { ExpressionEntity } from './Expression';
import { Node } from './Node';

export interface PatternNode extends WritableEntity, Node {
addExportedVariables(variables: Variable[], exportNamesByVariable: Map<Variable, string[]>): void;
declare(kind: string, init: ExpressionEntity | null): LocalVariable[];
}

0 comments on commit 2bc3bc5

Please sign in to comment.