-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathexpression.ts
More file actions
81 lines (74 loc) · 3.03 KB
/
expression.ts
File metadata and controls
81 lines (74 loc) · 3.03 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { bindExpression } from '@lwc/template-compiler';
import { APIFeature, isAPIFeatureEnabled } from '@lwc/shared';
import type {
ComplexExpression as IrComplexExpression,
Expression as IrExpression,
Identifier as IrIdentifier,
MemberExpression as IrMemberExpression,
} from '@lwc/template-compiler';
import type { Identifier as EsIdentifier, Expression as EsExpression } from 'estree';
import type { TransformerContext } from './types';
export function expressionIrToEs(
node: IrExpression | IrComplexExpression,
cxt: TransformerContext
): EsExpression {
const isComplexTemplateExpressionEnabled =
cxt.templateOptions.experimentalComplexExpressions &&
isAPIFeatureEnabled(
APIFeature.ENABLE_COMPLEX_TEMPLATE_EXPRESSIONS,
cxt.templateOptions.apiVersion
);
return bindExpression(
node as IrComplexExpression,
(n: EsIdentifier) => cxt.isLocalVar(n.name),
'instance',
isComplexTemplateExpressionEnabled
);
}
/**
* Given an expression in a context, return an expression that may be scoped to that context.
* For example, for the expression `foo`, it will typically be `instance.foo`, but if we're
* inside a `for:each` block then the `foo` variable may refer to the scoped `foo`,
* e.g. `<template for:each={foos} for:item="foo">`
* @param expression
* @param cxt
*/
export function getScopedExpression(
expression: IrExpression,
cxt: TransformerContext
): EsExpression {
let scopeReferencedId: IrExpression | null = null;
if (expression.type === 'MemberExpression') {
// e.g. `foo.bar` -> scopeReferencedId is `foo`
scopeReferencedId = getRootIdentifier(expression);
} else if (expression.type === 'Identifier') {
// e.g. `foo` -> scopeReferencedId is `foo`
scopeReferencedId = expression;
}
if (scopeReferencedId === null && !cxt.templateOptions.experimentalComplexExpressions) {
throw new Error(
`Invalid expression, must be a MemberExpression or Identifier, found type="${expression.type}": \`${JSON.stringify(expression)}\``
);
}
return cxt.isLocalVar(scopeReferencedId?.name)
? (expression as EsExpression)
: expressionIrToEs(expression, cxt);
}
function getRootMemberExpression(node: IrMemberExpression): IrMemberExpression {
return node.object.type === 'MemberExpression' ? getRootMemberExpression(node.object) : node;
}
function getRootIdentifier(node: IrMemberExpression): IrIdentifier {
const rootMemberExpression = getRootMemberExpression(node);
if (rootMemberExpression.object.type === 'Identifier') {
return rootMemberExpression.object;
}
throw new Error(
`Invalid expression, must be an Identifier, found type="${rootMemberExpression.type}": \`${JSON.stringify(rootMemberExpression)}\``
);
}