Skip to content

Commit

Permalink
perf: code splitting on the log-viewer @apexdevtools/apex-parser
Browse files Browse the repository at this point in the history
Uses code splitting to speed up perf by reducing the
time needed to initialially load and parse js (by having less).
The Dynamic imports will be loaded later.
  • Loading branch information
lukecotter committed Feb 27, 2024
1 parent 1d32017 commit e54a8b3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions log-viewer/modules/components/SOQLLinterIssues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export class SOQLLinterIssues extends LitElement {
`,
];

updated(changedProperties: PropertyValues): void {
async updated(changedProperties: PropertyValues): Promise<void> {
if (changedProperties.has('soql')) {
const stack = DatabaseAccess.instance()?.getStack(this.timestamp).reverse() || [];
const soqlLine = stack[0] as SOQLExecuteBeginLine;
this.issues = this.getIssuesFromSOQLLine(soqlLine);
this.issues = this.issues.concat(new SOQLLinter().lint(this.soql, stack));
this.issues = this.issues.concat(await new SOQLLinter().lint(this.soql, stack));
this.issues.sort((a, b) => {
return SEVERITY_TYPES.indexOf(a.severity) - SEVERITY_TYPES.indexOf(b.severity);
});
Expand Down
4 changes: 2 additions & 2 deletions log-viewer/modules/soql/SOQLLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { SOQLParser, SOQLTree } from './SOQLParser.js';

//todo : need a general concept of stack (to pull out linter)
export class SOQLLinter {
lint(soql: string, stack?: Stack): SOQLLinterRule[] {
async lint(soql: string, stack?: Stack): Promise<SOQLLinterRule[]> {
const results: SOQLLinterRule[] = [];

const tree = new SOQLParser().parse(soql);
const tree = await new SOQLParser().parse(soql);
const rules = [
new UnboundedSOQLRule(),
new LeadingPercentWildcardRule(),
Expand Down
26 changes: 14 additions & 12 deletions log-viewer/modules/soql/SOQLParser.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
/*
* Copyright (c) 2022 Certinia Inc. All rights reserved.
*/
import { type QueryContext } from '@apexdevtools/apex-parser';
import {
ApexLexer,
ApexParser,
CaseInsensitiveInputStream,
QueryContext,
} from '@apexdevtools/apex-parser';
import {
CharStreams,
CommonTokenStream,
RecognitionException,
Recognizer,
Token,
type ANTLRErrorListener,
type RecognitionException,
type Recognizer,
type Token,
} from 'antlr4ts';

// To understand the parser AST see https://github.com/nawforce/apex-parser/blob/master/antlr/ApexParser.g4
Expand Down Expand Up @@ -77,7 +70,16 @@ export class SOQLTree {
}

export class SOQLParser {
parse(query: string): SOQLTree {
async parse(query: string): Promise<SOQLTree> {
// Dynamic import for code splitting. Improves performance by reducing the amount of JS that is loaded and parsed at the start.
// eslint-disable-next-line @typescript-eslint/naming-convention
const { ApexLexer, ApexParser, CaseInsensitiveInputStream } = await import(
'@apexdevtools/apex-parser'
);
// Dynamic import for code splitting. Improves performance by reducing the amount of JS that is loaded and parsed at the start.
// eslint-disable-next-line @typescript-eslint/naming-convention
const { CommonTokenStream, CharStreams } = await import('antlr4ts');

const lexer = new ApexLexer(new CaseInsensitiveInputStream(CharStreams.fromString(query)));
const tokens = new CommonTokenStream(lexer);
const parser = new ApexParser(tokens);
Expand Down

0 comments on commit e54a8b3

Please sign in to comment.