Skip to content

Commit

Permalink
NEW (Extension) @W-16002080@ Add eslint support and additional parame…
Browse files Browse the repository at this point in the history
…ters for the scan (#95)
  • Loading branch information
jag-j authored Jun 20, 2024
1 parent 89f1944 commit 65596aa
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 5 deletions.
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@
"type": "boolean",
"default": false,
"description": "Scan files on open automatically."
},
"codeAnalyzer.scanner.engines": {
"type": "string",
"default": "pmd,retire-js,eslint-lwc",
"description": "***Specifies the engines to run. Submit multiple values as a comma-separated list. Possible values are pmd, retire-js, eslint, eslint-lwc and eslint-typescript***"
},
"codeAnalyzer.normalizeSeverity.enabled": {
"type": "boolean",
"default": false,
"description": "***Enable this flag to output normalized severity and engine-specific severity across all engines.***"
},
"codeAnalyzer.rules.category": {
"type": "string",
"description": "***One or more categories of rules to run. Specify multiple values as a comma-separated list.***"
}
}
},
Expand Down
17 changes: 16 additions & 1 deletion src/lib/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,16 @@ export class ScanRunner {
* @param targets The files to be scanned.
*/
private async createPathlessArgArray(targets: string[]): Promise<string[]> {
const engines = SettingsManager.getEnginesToRun();

if (!engines || engines.length === 0) {
throw new Error('***Engines cannot be empty. Please set one or more engines in the VS Code Settings***');
}

const args: string[] = [
'scanner', 'run',
'--target', `${targets.join(',')}`,
'--engine', 'pmd,retire-js',
'--engine', engines,
'--json'
];
const customPmdConfig: string = SettingsManager.getPmdCustomConfigFile();
Expand All @@ -115,6 +121,15 @@ export class ScanRunner {
}
args.push('--pmdconfig', customPmdConfig);
}

const rulesCategory = SettingsManager.getRulesCategory();
if (rulesCategory) {
args.push('--category', rulesCategory);
}

if (SettingsManager.getNormalizeSeverityEnabled()) {
args.push('--normalize-severity')
}
return args;
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ export class SettingsManager {
public static getAnalyzeOnOpen(): boolean {
return vscode.workspace.getConfiguration('codeAnalyzer.analyzeOnOpen').get('enabled');
}

public static getEnginesToRun(): string {
return vscode.workspace.getConfiguration('codeAnalyzer.scanner').get('engines');
}

public static getNormalizeSeverityEnabled(): boolean {
return vscode.workspace.getConfiguration('codeAnalyzer.normalizeSeverity').get('enabled');
}

public static getRulesCategory(): string {
return vscode.workspace.getConfiguration('codeAnalyzer.rules').get('category');
}
}
3 changes: 3 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ suite('Extension Test Suite', () => {
for (const [uri, diagnostics] of diagnosticsArrays) {
expect(diagnostics, `${uri.toString()} should start without diagnostics`).to.be.empty;
}
// Set custom settings
const configuration = vscode.workspace.getConfiguration();
configuration.update('codeAnalyzer.scanner.engines', 'pmd,retire-js,eslint-lwc', vscode.ConfigurationTarget.Global);
});

teardown(async () => {
Expand Down
89 changes: 85 additions & 4 deletions src/test/suite/scanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ suite('ScanRunner', () => {
return (scanner as any).createPathlessArgArray(targets);
}

suite('Simple cases', () => {
suite('Non Custom PMD Config, with various parameter options', () => {
teardown(() => {
// Revert any stubbing we did with Sinon.
Sinon.restore();
});

test('Creates array-ified sfdx-scanner command', async () => {
// ===== SETUP =====
// Stub out the appropriate SettingsManager methods.
Sinon.stub(SettingsManager, 'getEnginesToRun').returns("retire-js,pmd");
Sinon.stub(SettingsManager, 'getRulesCategory').returns("");
Sinon.stub(SettingsManager, 'getNormalizeSeverityEnabled').returns(false);

// ===== TEST =====
// Call the test method helper.
const args: string[] = await invokeTestedMethod();
Expand All @@ -50,9 +61,77 @@ suite('ScanRunner', () => {
expect(args[2]).to.equal('--target', 'Wrong arg');
expect(args[3]).to.equal(targets.join(','), 'Wrong arg');
expect(args[4]).to.equal('--engine', 'Wrong arg');
expect(args[5]).to.equal('pmd,retire-js', 'Wrong arg');
expect(args[5]).to.equal('retire-js,pmd', 'Wrong arg');
expect(args[6]).to.equal('--json', 'Wrong arg');
});

test('Includes rulesCategory in the --category parameter if provided', async () => {
// ===== SETUP =====
// Stub out the appropriate SettingsManager methods.
const ruleCategory = 'security';
Sinon.stub(SettingsManager, 'getRulesCategory').returns(ruleCategory);
Sinon.stub(SettingsManager, 'getNormalizeSeverityEnabled').returns(false);

// ===== TEST =====
// Call the test method helper.
const args: string[] = await invokeTestedMethod();

// ===== ASSERTIONS =====
// Verify that the right arguments were created.
// expect(args).to.have.lengthOf(9, 'Wrong number of args');
expect(args[7]).to.equal('--category', 'Wrong arg');
expect(args[8]).to.equal(ruleCategory, 'Wrong arg');
});

test('Includes --normalize-severity parameter if flag enabled', async () => {
// ===== SETUP =====
// Stub out the appropriate SettingsManager methods.
Sinon.stub(SettingsManager, 'getNormalizeSeverityEnabled').returns(true);

// ===== TEST =====
// Call the test method helper.
const args: string[] = await invokeTestedMethod();

// ===== ASSERTIONS =====
// Verify that the right arguments were created.
expect(args[7]).to.equal('--normalize-severity', 'Wrong arg');
});

test('Error thrown when codeAnalyzer.scanner.engines is empty', async () => {
// ===== SETUP =====
// Stub out the appropriate SettingsManager methods.
Sinon.stub(SettingsManager, 'getEnginesToRun').returns('');

// ===== TEST =====
let err:Error = null;
try {
await invokeTestedMethod();
} catch (e) {
err = e;
}

// ===== ASSERTIONS =====
expect(err).to.exist;
expect(err.message).to.equal('***Engines cannot be empty. Please set one or more engines in the VS Code Settings***');
});

test('Error thrown when codeAnalyzer.scanner.engines is undefined', async () => {
// ===== SETUP =====
// Stub out the appropriate SettingsManager methods.
Sinon.stub(SettingsManager, 'getEnginesToRun').returns(undefined);

// ===== TEST =====
let err:Error = null;
try {
await invokeTestedMethod();
} catch (e) {
err = e;
}

// ===== ASSERTIONS =====
expect(err).to.exist;
expect(err.message).to.equal('***Engines cannot be empty. Please set one or more engines in the VS Code Settings***');
});
});

suite('Custom PMD config', () => {
Expand All @@ -68,6 +147,7 @@ suite('ScanRunner', () => {
const dummyConfigPath: string = '/Users/me/someconfig.xml';
Sinon.stub(SettingsManager, 'getPmdCustomConfigFile').returns(dummyConfigPath);
Sinon.stub(File, 'exists').resolves(true);
Sinon.stub(SettingsManager, 'getEnginesToRun').returns('retire-js,pmd');

// ===== TEST =====
// Call the test method helper.
Expand All @@ -81,7 +161,7 @@ suite('ScanRunner', () => {
expect(args[2]).to.equal('--target', 'Wrong arg');
expect(args[3]).to.equal(targets.join(','), 'Wrong arg');
expect(args[4]).to.equal('--engine', 'Wrong arg');
expect(args[5]).to.equal('pmd,retire-js', 'Wrong arg');
expect(args[5]).to.equal('retire-js,pmd', 'Wrong arg');
expect(args[6]).to.equal('--json', 'Wrong arg');
expect(args[7]).to.equal('--pmdconfig', 'Wrong arg');
expect(args[8]).to.equal(dummyConfigPath, 'Wrong arg');
Expand Down Expand Up @@ -114,6 +194,7 @@ suite('ScanRunner', () => {
// ===== SETUP =====
// Stub out the appropriate SettingsManager method to return an empty string.
Sinon.stub(SettingsManager, 'getPmdCustomConfigFile').returns("");
Sinon.stub(SettingsManager, 'getEnginesToRun').returns('retire-js,pmd');

// ===== TEST =====
// Call the test method helper.
Expand All @@ -127,7 +208,7 @@ suite('ScanRunner', () => {
expect(args[2]).to.equal('--target', 'Wrong arg');
expect(args[3]).to.equal(targets.join(','), 'Wrong arg');
expect(args[4]).to.equal('--engine', 'Wrong arg');
expect(args[5]).to.equal('pmd,retire-js', 'Wrong arg');
expect(args[5]).to.equal('retire-js,pmd', 'Wrong arg');
expect(args[6]).to.equal('--json', 'Wrong arg');
});
});
Expand Down

0 comments on commit 65596aa

Please sign in to comment.