Skip to content

Commit fb8da42

Browse files
fix describeOptions
1 parent cd3feac commit fb8da42

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

packages/code-analyzer-apexguru-engine/src/engine.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ export class ApexGuruEngine extends EngineEventEmitter implements Engine {
5454
async describeRules(describeOptions: DescribeOptions): Promise<RuleDescription[]> {
5555
this.emitDescribeRulesProgressEvent(0);
5656

57+
// Check if workspace has any Apex files (following SFGE pattern)
58+
if (describeOptions.workspace) {
59+
const workspaceFiles = await describeOptions.workspace.getWorkspaceFiles();
60+
const hasApexFiles = workspaceFiles.some(file => this.isApexFile(path.basename(file)));
61+
62+
if (!hasApexFiles) {
63+
this.emitLogEvent(LogLevel.Debug, 'No Apex files found in workspace. Returning no ApexGuru rules.');
64+
this.emitDescribeRulesProgressEvent(100);
65+
return [];
66+
}
67+
}
68+
5769
// ApexGuru is dynamic - new rules can be added by Salesforce at any time.
5870
// We declare known rules explicitly (in apexguru-rules.ts), plus a fallback rule.
5971
// Unknown violations from the API will be mapped to "apexguru-other" by ViolationMapper.

packages/code-analyzer-apexguru-engine/test/ApexGuruEngine.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,66 @@ describe('ApexGuruEngine', () => {
8282
expect(progressSpy).toHaveBeenCalledWith(0);
8383
expect(progressSpy).toHaveBeenCalledWith(100);
8484
});
85+
86+
it('should return empty array when workspace has no Apex files', async () => {
87+
mockWorkspace.getWorkspaceFiles = jest.fn().mockResolvedValue([
88+
'/project/js/app.js',
89+
'/project/js/utils.js',
90+
'/project/css/styles.css'
91+
]);
92+
93+
const rules = await engine.describeRules({
94+
logFolder: '/tmp/logs',
95+
workingFolder: '/tmp/working',
96+
workspace: mockWorkspace
97+
});
98+
99+
expect(rules).toEqual([]);
100+
expect(mockWorkspace.getWorkspaceFiles).toHaveBeenCalled();
101+
});
102+
103+
it('should return all rules when workspace has Apex files', async () => {
104+
mockWorkspace.getWorkspaceFiles = jest.fn().mockResolvedValue([
105+
'/project/classes/Account.cls',
106+
'/project/js/app.js'
107+
]);
108+
109+
const rules = await engine.describeRules({
110+
logFolder: '/tmp/logs',
111+
workingFolder: '/tmp/working',
112+
workspace: mockWorkspace
113+
});
114+
115+
expect(rules.length).toBeGreaterThan(0);
116+
expect(rules.find(r => r.name === 'SoqlInALoop')).toBeDefined();
117+
});
118+
119+
it('should return all rules when workspace has trigger files', async () => {
120+
mockWorkspace.getWorkspaceFiles = jest.fn().mockResolvedValue([
121+
'/project/triggers/AccountTrigger.trigger',
122+
'/project/js/app.js'
123+
]);
124+
125+
const rules = await engine.describeRules({
126+
logFolder: '/tmp/logs',
127+
workingFolder: '/tmp/working',
128+
workspace: mockWorkspace
129+
});
130+
131+
expect(rules.length).toBeGreaterThan(0);
132+
expect(rules.find(r => r.name === 'DmlInALoop')).toBeDefined();
133+
});
134+
135+
it('should return all rules when no workspace provided', async () => {
136+
const rules = await engine.describeRules({
137+
logFolder: '/tmp/logs',
138+
workingFolder: '/tmp/working'
139+
// No workspace
140+
});
141+
142+
expect(rules.length).toBeGreaterThan(0);
143+
expect(rules.find(r => r.name === 'SoqlInALoop')).toBeDefined();
144+
});
85145
});
86146

87147
describe('runRules', () => {

0 commit comments

Comments
 (0)