Skip to content

Commit d90c6e9

Browse files
committed
docs: better document the expression operator regexp
1 parent 188b88a commit d90c6e9

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/states/EXPRESSION.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,29 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
223223
};
224224

225225
function buildOperatorPattern(isConcise: boolean) {
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 ? "" : "["}{(])`;
226+
const binary =
227+
"[*%<&^|?:]" + // Any of these characters can always continue an expression
228+
"|=[=>]" + // We only continue after an equals if it is => or ==
229+
"|/[^*/>]" + // We only continue after a forward slash if it isn't //, /* or />
230+
"|\\.(?=\\s)" + // We only continue after a period if it's followed by a space
231+
"|\\bin(?:stanceof)(?=\\s+[^=/,;:>])"; // We only continue after word operators (instanceof/in) when they are not followed by a terminator
232+
const unary =
233+
"!" +
234+
"|a(?:sync|wait)" +
235+
"|class" +
236+
"|function" +
237+
"|new" +
238+
"|typeof" +
239+
"|void";
240+
const lookAheadPattern =
241+
"\\s*(?:" +
242+
binary +
243+
"|\\+" + // any number of plus signs are ok.
244+
`|-${isConcise ? "[^-]" : ""}` + // in concise mode only consume minus signs if not --
245+
`|>${isConcise ? "" : "[>=]"}` + // in html mode only consume closing angle brackets if it is >= or >>
246+
")\\s*" +
247+
`|\\s+(?=[${isConcise ? "" : "["}{(])`; // if we have spaces followed by an opening bracket, we'll consume the spaces and let the expression state handle the brackets
248+
231249
const lookBehindPattern = `(?<=${unary}|${binary}|[^-]-|[^+]\\+)`;
232250
return new RegExp(`${lookAheadPattern}|${lookBehindPattern}`, "y");
233251
}

0 commit comments

Comments
 (0)