Skip to content

Commit 8f1f80a

Browse files
Fix issue found while testing
1 parent 2d4ecb2 commit 8f1f80a

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ export class ESLintEngine extends Engine {
137137
const lintResults: ESLint.LintResult[] = await this._runESLintWorkerTask.run(runTaskInput);
138138

139139
const engineResults: EngineRunResults = {
140-
violations: this.toViolations(lintResults)
140+
violations: this.toViolations(lintResults, new Set(ruleNames))
141141
};
142142
this.emitRunRulesProgressEvent(100);
143143
return engineResults;
144144
}
145145

146-
private toViolations(eslintResults: ESLint.LintResult[]): Violation[] {
146+
private toViolations(eslintResults: ESLint.LintResult[], specifiedRules: Set<string>): Violation[] {
147147
const violations: Violation[] = [];
148148
for (const eslintResult of eslintResults) {
149149
for (const resultMsg of eslintResult.messages) {
@@ -152,7 +152,13 @@ export class ESLintEngine extends Engine {
152152
continue;
153153
}
154154
const violation: Violation = toViolation(eslintResult.filePath, resultMsg);
155-
violations.push(violation);
155+
156+
if (specifiedRules.has(violation.ruleName)) {
157+
violations.push(violation);
158+
} else {
159+
// This may be possible if a user tries to suppress an eslint rule in their code that isn't available. We just ignore it but debug it just in case.
160+
this.emitLogEvent(LogLevel.Debug, getMessage('ViolationFoundFromUnregisteredRule', violation.ruleName, JSON.stringify(violation,null,2)))
161+
}
156162
}
157163
}
158164
return violations;

packages/code-analyzer-eslint-engine/src/messages.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ const MESSAGE_CATALOG : { [key: string]: string } = {
6666
`The eslint engine encountered an unexpected error thrown from '%s':\n%s\n\n` +
6767
'ESLint options used:\n%s',
6868

69+
ViolationFoundFromUnregisteredRule:
70+
`A rule with name '%s' produced a violation, but this rule was not registered with the 'eslint' engine so it will not be included in the results.\n` +
71+
`This may occur if in your file you are using inline comments to attempt to disable or configure this rule even though it is unknown to ESLint and Code Analyzer.\n` +
72+
`Ignored Violation:\n%s`,
73+
6974
UnusedESLintConfigFile:
7075
`The ESLint configuration file '%s' was found but not applied.\n` +
7176
`To apply this configuration file, set it as the eslint_config_file value in your Code Analyzer configuration. For example:\n` +

packages/code-analyzer-eslint-engine/test/engine.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,41 @@ describe('Typical tests for the runRules method of ESLintEngine', () => {
651651
const relevantDebugEvents: LogEvent[] = logEvents.filter(e => e.logLevel === LogLevel.Debug && e.message.includes("has been replaced with"));
652652
expect(relevantDebugEvents.length).toBeGreaterThan(0);
653653
});
654+
655+
it('When scanning a file that has a directive for a unknown rule, we should not error but issue debug log for it', async () => {
656+
const engine: Engine = await createEngineFromPlugin({
657+
...DEFAULT_CONFIG_FOR_TESTING
658+
});
659+
const logEvents: LogEvent[] = [];
660+
engine.onEvent(EventType.LogEvent, (event: LogEvent) => logEvents.push(event));
661+
662+
const runOptions: RunOptions = createRunOptions(new Workspace('id', [
663+
path.join(testDataFolder, 'workspaceWithDirectiveAboutUnknownRule')]));
664+
665+
const results: EngineRunResults = await engine.runRules(['no-unused-vars'], runOptions);
666+
667+
expect(results.violations).toHaveLength(1);
668+
expect(results.violations[0].ruleName).toEqual('no-unused-vars');
669+
670+
const filteredViolation: Violation = {
671+
ruleName: "oops-rule",
672+
message: "Definition for rule 'oops-rule' was not found.",
673+
codeLocations: [{
674+
file: path.join(testDataFolder, 'workspaceWithDirectiveAboutUnknownRule', 'containsDirectiveForUnknownRule.js'),
675+
startLine: 4,
676+
startColumn: 1,
677+
endLine: 4,
678+
endColumn: 31
679+
}],
680+
primaryLocationIndex: 0
681+
};
682+
683+
expect(logEvents).toContainEqual({
684+
type: EventType.LogEvent,
685+
logLevel: LogLevel.Debug,
686+
message: getMessage('ViolationFoundFromUnregisteredRule', 'oops-rule', JSON.stringify(filteredViolation, null, 2))
687+
});
688+
});
654689
});
655690

656691
describe('Tests for the getEngineVersion method of ESLint Engine', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// The "oops-rule" is unknown to our base configuration and so we shouldn't report any issues here, but instead
2+
// issue debug log.
3+
4+
/* eslint-disable oops-rule */
5+
function helloWorld() {
6+
}

0 commit comments

Comments
 (0)