All files / src/compiler/phases/3-transform/client/visitors MemberExpression.js

100% Statements 29/29
100% Branches 12/12
100% Functions 1/1
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 292x 2x 2x 2x 2x 2x 2x 2x 2x 3467x 3467x 24x 24x 22x 22x 3467x 74x 74x 72x 72x 72x 13x 13x 72x 74x 3432x 3432x 3432x  
/** @import { MemberExpression } from 'estree' */
/** @import { Context } from '../types' */
import * as b from '../../../../utils/builders.js';
 
/**
 * @param {MemberExpression} node
 * @param {Context} context
 */
export function MemberExpression(node, context) {
	// rewrite `this.#foo` as `this.#foo.v` inside a constructor
	if (node.property.type === 'PrivateIdentifier') {
		const field = context.state.private_state.get(node.property.name);
		if (field) {
			return context.state.in_constructor ? b.member(node, 'v') : b.call('$.get', node);
		}
	} else if (node.object.type === 'ThisExpression') {
		// rewrite `this.foo` as `this.#foo.v` inside a constructor
		if (node.property.type === 'Identifier' && !node.computed) {
			const field = context.state.public_state.get(node.property.name);
 
			if (field && context.state.in_constructor) {
				return b.member(b.member(b.this, field.id), 'v');
			}
		}
	}
 
	context.next();
}