Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Refactor Apex parser #158

Merged
merged 2 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions packages/core/src/parser/ApexTypeFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import fs from "fs-extra";
const path = require("path");
const glob = require("glob");

import { CommonTokenStream, ANTLRInputStream } from 'antlr4ts';
import { ParseTreeWalker } from "antlr4ts/tree/ParseTreeWalker";

import ApexTypeListener from "./listeners/ApexTypeListener";

import {
ApexLexer,
ApexParser,
ApexParserListener,
ThrowingErrorListener
} from "apex-parser";

export default class ApexTypeFetcher {


/**
* Get Apex type of cls files in a search directory.
* Sorts files into classes, test classes and interfaces.
* @param searchDir
*/
public getApexTypeOfClsFiles(searchDir: string): ApexSortedByType {
const apexSortedByType: ApexSortedByType = {
class: [],
testClass: [],
interface: [],
parseError: []
};

let clsFiles: string[];
if (fs.existsSync(searchDir)) {
clsFiles = glob.sync(`*.cls`, {
cwd: searchDir,
absolute: true
});
} else {
throw new Error(`Search directory ${searchDir} does not exist`);
}


for (let clsFile of clsFiles) {

let clsPayload: string = fs.readFileSync(clsFile, 'utf8');
let fileDescriptor: FileDescriptor = {name: path.basename(clsFile, ".cls"), filepath: clsFile};

// Parse cls file
let compilationUnitContext;
try {
let lexer = new ApexLexer(new ANTLRInputStream(clsPayload));
let tokens: CommonTokenStream = new CommonTokenStream(lexer);

let parser = new ApexParser(tokens);
parser.removeErrorListeners()
parser.addErrorListener(new ThrowingErrorListener());

compilationUnitContext = parser.compilationUnit();

} catch (err) {
console.log(`Failed to parse ${clsFile}`);
console.log(err);

// Manually parse class if error is caused by System.runAs() or testMethod modifier
if (
this.parseSystemRunAs(err, clsPayload) ||
this.parseTestMethod(err, clsPayload)
) {
console.log(`Manually identified test class ${clsFile}`)
apexSortedByType["testClass"].push(fileDescriptor);
} else {
fileDescriptor["error"] = err;
apexSortedByType["parseError"].push(fileDescriptor);
}
continue;
}

let apexTypeListener: ApexTypeListener = new ApexTypeListener();

// Walk parse tree to determine Apex type
ParseTreeWalker.DEFAULT.walk(apexTypeListener as ApexParserListener, compilationUnitContext);

let apexType = apexTypeListener.getApexType();

if (apexType.class) {
apexSortedByType["class"].push(fileDescriptor);
if (apexType.testClass) {
apexSortedByType["testClass"].push(fileDescriptor);
}
} else if (apexType.interface) {
apexSortedByType["interface"].push(fileDescriptor);
} else {
fileDescriptor["error"] = {message: "Unknown Apex Type"};
apexSortedByType["parseError"].push(fileDescriptor);
}
}

return apexSortedByType;
}

/**
* Bypass error parsing System.runAs()
* @param error
* @param clsPayload
*/
private parseSystemRunAs(error, clsPayload: string): boolean {
return (
error["message"].includes("missing ';' at '{'") &&
/System.runAs/i.test(clsPayload) &&
/@isTest/i.test(clsPayload)
);
}

/**
* Bypass error parsing testMethod modifier
* @param error
* @param clsPayload
*/
private parseTestMethod(error, clsPayload: string): boolean {
return (
error["message"].includes("no viable alternative at input") &&
/testMethod/i.test(error["message"]) &&
/testMethod/i.test(clsPayload)
);
}
}

interface ApexSortedByType {
class: FileDescriptor[],
testClass: FileDescriptor[],
interface: FileDescriptor[],
parseError: FileDescriptor[]
}

interface FileDescriptor {
name: string
filepath: string,
error?: any
}
76 changes: 0 additions & 76 deletions packages/core/src/parser/InterfaceFetcher.ts

This file was deleted.

76 changes: 0 additions & 76 deletions packages/core/src/parser/TestClassFetcher.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/core/src/parser/listeners/AnnotationListener.ts

This file was deleted.

39 changes: 39 additions & 0 deletions packages/core/src/parser/listeners/ApexTypeListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
ApexParserListener,
AnnotationContext,
InterfaceDeclarationContext,
ClassDeclarationContext
} from "apex-parser";


export default class ApexTypeListener implements ApexParserListener{
private apexType: ApexType = {
class: false,
testClass: false,
interface: false
}

protected enterAnnotation(ctx: AnnotationContext): void {
if (ctx._stop.text.toUpperCase() === "ISTEST") {
this.apexType["testClass"] = true;
}
}

private enterInterfaceDeclaration(ctx: InterfaceDeclarationContext): void {
this.apexType["interface"] = true;
}

private enterClassDeclaration(ctx: ClassDeclarationContext): void {
this.apexType["class"] = true;
}

public getApexType(): ApexType {
return this.apexType;
}
}

interface ApexType {
class: boolean,
testClass: boolean,
interface: boolean
}
18 changes: 0 additions & 18 deletions packages/core/src/parser/listeners/InterfaceDeclarationListener.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/core/src/parser/listeners/TestAnnotationListener.ts

This file was deleted.

Loading