-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathexpression.ts
More file actions
191 lines (167 loc) · 7.21 KB
/
expression.ts
File metadata and controls
191 lines (167 loc) · 7.21 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2018, 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 { parseExpressionAt, isIdentifierStart, isIdentifierChar } from 'acorn';
import { ParserDiagnostics, invariant } from '@lwc/errors';
import { APIFeature, minApiVersion } from '@lwc/shared';
import * as t from '../shared/estree';
import { isReservedES6Keyword } from './utils/javascript';
import { isComplexTemplateExpressionEnabled } from './expression-complex';
import type { Expression, Identifier, SourceLocation } from '../shared/types';
import type ParserCtx from './parser';
import type { Node } from 'acorn';
export const EXPRESSION_SYMBOL_START = '{';
export const EXPRESSION_SYMBOL_END = '}';
const POTENTIAL_EXPRESSION_RE = /^.?{.+}.*$/;
const WHITESPACES_RE = /\s/;
export function isExpression(source: string): boolean {
// Issue #3418: Legacy behavior, previous regex treated "{}" attribute value as non expression
return source[0] === '{' && source.slice(-1) === '}' && source.length > 2;
}
export function isPotentialExpression(source: string): boolean {
return !!source.match(POTENTIAL_EXPRESSION_RE);
}
const minCteApiVersion = minApiVersion(APIFeature.ENABLE_COMPLEX_TEMPLATE_EXPRESSIONS);
function validateExpression(
source: string,
node: t.BaseNode,
ctx: ParserCtx,
unquotedAttributeExpression: boolean
): asserts node is Expression {
const cteOnlyNode = !t.isIdentifier(node) && !t.isMemberExpression(node);
// If this node is not an identifier or a member expression (the only two nodes allowed if complexTemplateExpressions are disabled),
// then we throw if the following invariants do not hold true.
if (cteOnlyNode) {
// complexTemplateExpressions must be enabled if this is a cteOnlyNode.
invariant(ctx.config.experimentalComplexExpressions, ParserDiagnostics.INVALID_NODE, [
node.type,
]);
// complexTemplateExpressions must be enabled and the component API version must be sufficient.
invariant(
isComplexTemplateExpressionEnabled(ctx),
ParserDiagnostics.INVALID_NODE_CTE_API_VERSION,
[node.type, ctx.apiVersion, minCteApiVersion]
);
// complexTemplateExpressions must be enabled, the component API version must be sufficient and the expression should not be
// an unquoted attribute expression.
invariant(
isComplexTemplateExpressionEnabled(ctx) && !unquotedAttributeExpression,
ParserDiagnostics.INVALID_NODE_CTE_UNQUOTED,
[node.type, source]
);
}
if (t.isMemberExpression(node)) {
// If this is a computed node and experimentalComputedMemberExpressions is not enabled,
// then we throw if the following invariants do not hold true.
if (!ctx.config.experimentalComputedMemberExpression && node.computed) {
// complexTemplateExpressions must be enabled.
invariant(
ctx.config.experimentalComplexExpressions,
ParserDiagnostics.COMPUTED_PROPERTY_ACCESS_NOT_ALLOWED,
[source]
);
// complexTemplateExpressions must be enabled and the component API version must be sufficient.
invariant(
isComplexTemplateExpressionEnabled(ctx),
ParserDiagnostics.COMPUTED_PROPERTY_ACCESS_NOT_ALLOWED_CTE_API_VERSION,
[source, ctx.apiVersion, minCteApiVersion]
);
// complexTemplateExpressions must be enabled, the component API version must be sufficient and the expression
// should not be an unquoted attribute expression.
invariant(
isComplexTemplateExpressionEnabled(ctx) && !unquotedAttributeExpression,
ParserDiagnostics.COMPUTED_PROPERTY_ACCESS_NOT_ALLOWED_CTE_UNQUOTED,
[source]
);
}
const { object, property } = node;
if (!t.isIdentifier(object)) {
validateExpression(source, object, ctx, unquotedAttributeExpression);
}
if (!t.isIdentifier(property)) {
validateExpression(source, property, ctx, unquotedAttributeExpression);
}
}
}
export function validateSourceIsParsedExpression(source: string, parsedExpression: Node) {
if (parsedExpression.end === source.length - 1) {
return;
}
let unclosedParenthesisCount = 0;
for (let i = 0, n = parsedExpression.start; i < n; i++) {
if (source[i] === '(') {
unclosedParenthesisCount++;
}
}
// source[source.length - 1] === '}', n = source.length - 1 is to avoid processing '}'.
for (let i = parsedExpression.end, n = source.length - 1; i < n; i++) {
const character = source[i];
if (character === ')') {
unclosedParenthesisCount--;
} else if (character === ';') {
// acorn parseExpressionAt will stop at the first ";", it may be that the expression is not
// a multiple expression ({foo;}), but this is a case that we explicitly do not want to support.
// in such case, let's fail with the same error as if it were a multiple expression.
invariant(false, ParserDiagnostics.MULTIPLE_EXPRESSIONS);
} else {
invariant(
WHITESPACES_RE.test(character),
ParserDiagnostics.TEMPLATE_EXPRESSION_PARSING_ERROR,
['Unexpected end of expression']
);
}
}
invariant(unclosedParenthesisCount === 0, ParserDiagnostics.TEMPLATE_EXPRESSION_PARSING_ERROR, [
'Unexpected end of expression',
]);
}
export function parseExpression(
ctx: ParserCtx,
source: string,
location: SourceLocation,
unquotedAttributeExpression: boolean
): Expression {
const { ecmaVersion } = ctx;
return ctx.withErrorWrapping(
() => {
const parsed = parseExpressionAt(source, 1, {
ecmaVersion,
allowAwaitOutsideFunction: false,
onComment: () =>
invariant(false, ParserDiagnostics.INVALID_EXPR_COMMENTS_DISALLOWED),
});
validateSourceIsParsedExpression(source, parsed);
validateExpression(source, parsed, ctx, unquotedAttributeExpression);
return { ...parsed, location };
},
ParserDiagnostics.TEMPLATE_EXPRESSION_PARSING_ERROR,
location,
(err) => `Invalid expression ${source} - ${err.message}`
);
}
export function parseIdentifier(
ctx: ParserCtx,
source: string,
location: SourceLocation
): Identifier {
let isValid = isIdentifierStart(source.charCodeAt(0));
for (let i = 1; i < source.length && isValid; i++) {
isValid = isIdentifierChar(source.charCodeAt(i));
}
if (isValid) {
if (isReservedES6Keyword(source)) {
ctx.throwAtLocation(ParserDiagnostics.RESERVED_KEYWORD_AS_IDENTIFIER, location, [
source,
]);
}
return {
...t.identifier(source),
location,
};
} else {
ctx.throwAtLocation(ParserDiagnostics.INVALID_IDENTIFIER, location, [source]);
}
}