Skip to content

Commit 188b88a

Browse files
committed
feat: better expression function parsing and other improvements
1 parent f240b7e commit 188b88a

File tree

3 files changed

+51
-77
lines changed

3 files changed

+51
-77
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1╭─ tag a = function (x) { console.log("y") } b
2+
│ │ │ │ │ ╰─ attrName
3+
│ │ │ │ ╰─ attrValue.value "function (x) { console.log(\"y\") }"
4+
│ │ │ ╰─ attrValue "= function (x) { console.log(\"y\") }"
5+
│ │ ╰─ attrName
6+
╰─ ╰─ tagName "tag"
7+
2╭─ tag a = (x) => { console.log("y") } b
8+
│ │ │ │ │ ╰─ attrName
9+
│ │ │ │ ╰─ attrValue.value "(x) => { console.log(\"y\") }"
10+
│ │ │ ╰─ attrValue "= (x) => { console.log(\"y\") }"
11+
│ │ ╰─ attrName
12+
│ ├─ closeTag(tag)
13+
│ ├─ openTagEnd(tag)
14+
╰─ ╰─ tagName "tag"
15+
3╭─ tag a = x => { console.log("y") } b
16+
│ │ │ │ │ ╰─ attrName
17+
│ │ │ │ ╰─ attrValue.value "x => { console.log(\"y\") }"
18+
│ │ │ ╰─ attrValue "= x => { console.log(\"y\") }"
19+
│ │ ╰─ attrName
20+
│ ├─ closeTag(tag)
21+
│ ├─ openTagEnd(tag)
22+
╰─ ╰─ tagName "tag"
23+
4╭─ tag a = async x => { console.log("y") } b
24+
│ │ │ │ │ ╰─ attrName
25+
│ │ │ │ ╰─ attrValue.value "async x => { console.log(\"y\") }"
26+
│ │ │ ╰─ attrValue "= async x => { console.log(\"y\") }"
27+
│ │ ╰─ attrName
28+
│ ├─ closeTag(tag)
29+
│ ├─ openTagEnd(tag)
30+
╰─ ╰─ tagName "tag"
31+
5╭─ tag a = async function (x) { console.log("y") } b
32+
│ │ │ │ │ │├─ closeTag(tag)
33+
│ │ │ │ │ │╰─ openTagEnd(tag)
34+
│ │ │ │ │ ╰─ attrName
35+
│ │ │ │ ╰─ attrValue.value "async function (x) { console.log(\"y\") }"
36+
│ │ │ ╰─ attrValue "= async function (x) { console.log(\"y\") }"
37+
│ │ ╰─ attrName
38+
│ ├─ closeTag(tag)
39+
│ ├─ openTagEnd(tag)
40+
╰─ ╰─ tagName "tag"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tag a = function (x) { console.log("y") } b
2+
tag a = (x) => { console.log("y") } b
3+
tag a = x => { console.log("y") } b
4+
tag a = async x => { console.log("y") } b
5+
tag a = async function (x) { console.log("y") } b

src/states/EXPRESSION.ts

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -223,65 +223,12 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
223223
};
224224

225225
function buildOperatorPattern(isConcise: boolean) {
226-
const unary = ["typeof", "new", "void"];
227-
const operators = [
228-
//Multiplicative Operators
229-
"*",
230-
"/",
231-
"%",
232-
233-
//Additive Operators
234-
"+",
235-
"-",
236-
237-
//Bitwise Shift Operators
238-
"<<",
239-
">>",
240-
">>>",
241-
242-
//Relational Operators
243-
"<",
244-
"<=",
245-
">=",
246-
247-
// Readable Operators
248-
// NOTE: These become reserved words and cannot be used as attribute names
249-
"instanceof",
250-
"in",
251-
252-
// Equality Operators
253-
"==",
254-
"!=",
255-
"===",
256-
"!==",
257-
258-
// Binary Bitwise Operators
259-
"&",
260-
"^",
261-
"|",
262-
263-
// Binary Logical Operators
264-
"&&",
265-
"||",
266-
267-
// Ternary Operator
268-
"?",
269-
":",
270-
271-
// Special
272-
// In concise mode we can support >, and in html mode we can support [
273-
isConcise ? ">" : "[",
274-
];
275-
const lookAheadPattern = `\\s*(${operators
276-
.sort(byLength)
277-
.map(escapeOperator)
278-
.join("|")})\\s*(?!-)`;
279-
const lookBehindPattern = `(?<=[^-+](?:${operators
280-
.concat(unary)
281-
.sort(byLength)
282-
.map(escapeOperator)
283-
.join("|")}))`;
284-
226+
const binary = `[*%<&^|?:]|=[=<>]|/[^*/>]|\\.(?=\\s)|\\bin(?:stanceof)(?=\\s+[^=/,;:>])`;
227+
const unary = `!|a(?:sync|wait)|class|function|new|typeof|void`;
228+
const lookAheadPattern = `\\s*(?:${binary}|\\+|${
229+
isConcise ? "-[^-]" : "-"
230+
}${`|>${isConcise ? "" : "[>=]"}`})\\s*|\\s+(?=[${isConcise ? "" : "["}{(])`;
231+
const lookBehindPattern = `(?<=${unary}|${binary}|[^-]-|[^+]\\+)`;
285232
return new RegExp(`${lookAheadPattern}|${lookBehindPattern}`, "y");
286233
}
287234

@@ -319,21 +266,3 @@ function canCharCodeBeFollowedByDivision(code: number) {
319266
code === CODE.CLOSE_CURLY_BRACE
320267
);
321268
}
322-
323-
function escapeOperator(str: string) {
324-
if (/^[A-Z]+$/i.test(str)) {
325-
return "\\b" + escapeNonAlphaNumeric(str) + "(?=\\s+[^=/,;:>])";
326-
}
327-
if (str === "/") {
328-
return "\\/(?:\\b|\\s)"; //make sure this isn't a comment
329-
}
330-
return escapeNonAlphaNumeric(str);
331-
}
332-
333-
function escapeNonAlphaNumeric(str: string) {
334-
return str.replace(/([^\w\d])/g, "\\$1");
335-
}
336-
337-
function byLength(a: string, b: string) {
338-
return b.length - a.length;
339-
}

0 commit comments

Comments
 (0)