Skip to content
Snippets Groups Projects
Unverified Commit 55480a8e authored by Ariya Hidayat's avatar Ariya Hidayat Committed by GitHub
Browse files

Defer the parser and its related construction until it's needed (#14133)

Expression parser construction is expensive (mainly due to grammar validation
via Chevrotain's performSelfAnalysis). Thus, instantiating the parsers at the
initialization time will lead to a signifant delay in DOMContentLoaded,
thereby affecting (among others) the embedding performance.

To avoid this, simply defer that step until parsing is really needed
(which is actually far far later, i.e. when the user is using the
notebook).

* Add a clarifying comment regarding parser lazy loading
parent f29c2117
No related branches found
No related tags found
No related merge requests found
import { parse } from "metabase/lib/expressions/parser";
import { compile } from "metabase/lib/expressions/compile";
import { suggest } from "metabase/lib/expressions/suggest";
import { syntax } from "metabase/lib/expressions/syntax";
// combine compile/suggest/syntax so we only need to parse once
export function processSource(options) {
// Lazily load all these parser-related stuff, because parser construction is expensive
// https://github.com/metabase/metabase/issues/13472
const parse = require("./parser").parse;
const compile = require("./compile").compile;
const suggest = require("./suggest").suggest;
const syntax = require("./syntax").syntax;
const { source, targetOffset } = options;
let expression;
......
import { processSource } from "metabase/lib/expressions/process";
describe("metabase/lib/expressions/process", () => {
describe("processSource", () => {
it("should non throw", () => {
expect(() =>
processSource({ source: "1", targetOffset: null }),
).not.toThrow();
});
it("should handle valid input", () => {
const { compileError, syntaxTree } = processSource({
source: "1",
targetOffset: null,
});
expect(compileError).toBeUndefined();
expect(syntaxTree).toBeDefined();
expect(syntaxTree.children).toBeDefined();
expect(syntaxTree.children.length).toEqual(1);
});
it("should handle invalid input", () => {
const { compileError } = processSource({
source: "1+",
targetOffset: null,
});
expect(compileError.toString()).toEqual(
"NoViableAltException: Expected expression",
);
});
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment