Skip to content

Commit

Permalink
Initialise variables that are not initialised during construction
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 16, 2018
1 parent b6d7682 commit 339f5ab
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 24 deletions.
10 changes: 8 additions & 2 deletions src/ast/nodes/ClassBody.ts
Expand Up @@ -20,12 +20,18 @@ export default class ClassBody extends NodeBase {
return true;
}
return (
this.classConstructor &&
this.classConstructor !== null &&
this.classConstructor.hasEffectsWhenCalledAtPath([], callOptions, options)
);
}

initialise() {
this.classConstructor = this.body.find(method => method.kind === 'constructor');
for (const method of this.body) {
if (method.kind === 'constructor') {
this.classConstructor = method;
return;
}
}
this.classConstructor = null;
}
}
1 change: 1 addition & 0 deletions src/ast/nodes/ConditionalExpression.ts
Expand Up @@ -15,6 +15,7 @@ export default class ConditionalExpression extends NodeBase {
alternate: ExpressionNode;
consequent: ExpressionNode;

// Not initialised during construction
private hasUnknownTestValue = false;

forEachReturnExpressionWhenCalledAtPath(
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/IfStatement.ts
Expand Up @@ -11,6 +11,7 @@ export default class IfStatement extends StatementBase {
consequent: StatementNode;
alternate: StatementNode | null;

// Not initialised during construction
private hasUnknownTestValue = false;

hasEffects(options: ExecutionPathOptions): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/Import.ts
Expand Up @@ -8,6 +8,7 @@ export default class Import extends NodeBase {
type: NodeType.Import;
parent: CallExpression;

// Not initialised during construction
private resolutionNamespace: string = undefined;
private resolutionInterop: boolean = false;
private rendered: boolean = false;
Expand Down
5 changes: 1 addition & 4 deletions src/ast/nodes/Literal.ts
Expand Up @@ -57,10 +57,7 @@ export default class Literal<T = LiteralValueTypes> extends NodeBase {

render(code: MagicString, _options: RenderOptions) {
if (typeof this.value === 'string') {
(<[number, number][]>code.indentExclusionRanges).push(<[number, number]>[
this.start + 1,
this.end - 1
]);
(<[number, number][]>code.indentExclusionRanges).push([this.start + 1, this.end - 1]);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/ast/nodes/LogicalExpression.ts
Expand Up @@ -21,6 +21,7 @@ export default class LogicalExpression extends NodeBase {
left: ExpressionNode;
right: ExpressionNode;

// Not initialised during construction
private hasUnknownLeftValue = false;

forEachReturnExpressionWhenCalledAtPath(
Expand Down
32 changes: 15 additions & 17 deletions src/ast/nodes/ThisExpression.ts
Expand Up @@ -9,8 +9,10 @@ import { ObjectPath } from '../values';
export default class ThisExpression extends NodeBase {
type: NodeType.ThisExpression;

variable: ThisVariable;
alias: string;
private alias: string | null;

// Not initialised during construction
variable: ThisVariable = null;

bind() {
super.bind();
Expand All @@ -26,25 +28,21 @@ export default class ThisExpression extends NodeBase {
}

initialise() {
const lexicalBoundary = this.scope.findLexicalBoundary();

if (lexicalBoundary.isModuleScope) {
this.alias = this.module.context;
if (this.alias === 'undefined') {
this.module.warn(
{
code: 'THIS_IS_UNDEFINED',
message: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined`
},
this.start
);
}
this.alias = this.scope.findLexicalBoundary().isModuleScope ? this.module.context : null;
if (this.alias === 'undefined') {
this.module.warn(
{
code: 'THIS_IS_UNDEFINED',
message: `The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten`,
url: `https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined`
},
this.start
);
}
}

render(code: MagicString, _options: RenderOptions) {
if (this.alias) {
if (this.alias !== null) {
code.overwrite(this.start, this.end, this.alias, {
storeName: true,
contentOnly: false
Expand Down
4 changes: 3 additions & 1 deletion src/ast/nodes/shared/Node.ts
Expand Up @@ -81,13 +81,15 @@ export interface ExpressionNode extends ExpressionEntity, Node {}
export class NodeBase implements ExpressionNode {
type: string;
keys: string[];
included: boolean = false;
scope: Scope;
start: number;
end: number;
module: Module;
parent: Node | { type?: string };

// Not initialised during construction
included: boolean = false;

constructor(
esTreeNode: GenericEsTreeNode,
// we need to pass down the node constructors to avoid a circular dependency
Expand Down

0 comments on commit 339f5ab

Please sign in to comment.