Skip to content

Commit 85c8579

Browse files
committed
Replace this in object property function exprs
1 parent ad3b458 commit 85c8579

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

src/processScript/transform.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
/* eslint-disable jsdoc/check-param-names */
2-
import type { NodePath, Scope } from "@babel/traverse"
3-
import type { ArrayExpression, Block, BlockStatement, CallExpression, File, FunctionDeclaration, Identifier, Node, ObjectExpression } from "@babel/types"
4-
import type { LaxPartial } from "@samual/lib"
5-
import traverse from "@babel/traverse"
6-
import t from "@babel/types"
7-
import { assert } from "@samual/lib/assert"
8-
import { clearObject } from "@samual/lib/clearObject"
9-
import { validDBMethods } from "../constants"
10-
import { getReferencePathsToGlobal } from "./shared"
2+
import type { NodePath, Scope } from '@babel/traverse';
3+
import traverse from '@babel/traverse';
4+
import type {
5+
ArrayExpression,
6+
Block,
7+
BlockStatement,
8+
CallExpression,
9+
File,
10+
FunctionDeclaration,
11+
Identifier,
12+
Node,
13+
ObjectExpression
14+
} from '@babel/types';
15+
import t from '@babel/types';
16+
import type { LaxPartial } from '@samual/lib';
17+
import { assert } from '@samual/lib/assert';
18+
import { clearObject } from '@samual/lib/clearObject';
19+
import { validDBMethods } from '../constants';
20+
import { getReferencePathsToGlobal } from './shared';
1121

1222
export type TransformOptions = LaxPartial<{
1323
/** 11 a-z 0-9 characters */ uniqueId: string
@@ -749,10 +759,19 @@ export function transform(
749759

750760
if (object.type == `ObjectExpression`) {
751761
for (const property of (object as ObjectExpression).properties) {
752-
if (property.type != `ObjectMethod`)
762+
if (property.type == `ObjectMethod`) {
763+
thisIsReferenced ||= replaceAllThisWith(property, scope, thisId)
753764
continue
765+
}
766+
767+
if (property.type == `ObjectProperty` && property.value.type == `FunctionExpression`) {
768+
// Replace in things like this: function (foo = this.a)
769+
for (const param of property.value.params) {
770+
thisIsReferenced ||= replaceAllThisWith(param, scope, thisId)
771+
}
754772

755-
thisIsReferenced ||= replaceAllThisWith(property, scope, thisId)
773+
thisIsReferenced ||= replaceAllThisWith(property.value.body, scope, thisId)
774+
}
756775
}
757776
} else {
758777
for (const element of (object as ArrayExpression).elements) {

0 commit comments

Comments
 (0)