diff --git a/src/lib/sonar.ts b/src/lib/sonar.ts index 1b6a446fe80..5383ed04c5d 100644 --- a/src/lib/sonar.ts +++ b/src/lib/sonar.ts @@ -148,15 +148,20 @@ export class Sonar extends EventEmitter { const ruleOptions = config.rules[id]; const ruleWorksWithLocalFiles = rule.meta.worksWithLocalFiles; + const severity = getSeverity(ruleOptions); - const context = new RuleContext(id, this, getSeverity(ruleOptions), ruleOptions, rule.meta); - const instance = rule.create(context); + if (severity) { + const context = new RuleContext(id, this, severity, ruleOptions, rule.meta); + const instance = rule.create(context); - Object.keys(instance).forEach((eventName) => { - this.on(eventName, createEventHandler(instance[eventName], ruleWorksWithLocalFiles, id)); - }); + Object.keys(instance).forEach((eventName) => { + this.on(eventName, createEventHandler(instance[eventName], ruleWorksWithLocalFiles, id)); + }); - this.rules.set(id, instance); + this.rules.set(id, instance); + } else { + debug(`Rule "${id}" is disabled`); + } }); debug(`Rules loaded: ${this.rules.size}`); diff --git a/tests/lib/sonar.ts b/tests/lib/sonar.ts index 83bc40c3e7b..7f5a45bc189 100644 --- a/tests/lib/sonar.ts +++ b/tests/lib/sonar.ts @@ -97,7 +97,7 @@ test.serial('If config.plugins is an array we should create just those plugins', t.context.eventemitter.prototype.on.restore(); }); -test.serial('If config.rules is an array of ids we should create just those rules', (t) => { +test.serial('If config.rules is an object with rules, we should create just those rules', (t) => { const rule = { create() { return {}; @@ -134,6 +134,36 @@ test.serial('If config.rules is an array of ids we should create just those rule t.context.eventemitter.prototype.on.restore(); }); +test.serial(`If config.rules has some rules "off", we shouldn't create those rules`, (t) => { + const rule = { + create() { + return {}; + }, + meta: {} + }; + + sinon.spy(eventEmitter.EventEmitter2.prototype, 'on'); + t.context.rule = rule; + sinon.stub(t.context.resourceLoader, 'getRules').returns(new Map([ + ['disallowed-headers', rule], + ['lang-attribute', rule], + ['manifest-exists', rule] + ])); + sinon.stub(rule, 'create').returns({ 'fetch::end': () => { } }); + + const sonarObject = new sonar.Sonar({ //eslint-disable-line no-unused-vars + rules: { + 'disallowed-headers': 'warning', + 'manifest-exists': 'off' + } + }); + + t.true(t.context.resourceLoader.getRules.called); + t.true(t.context.rule.create.calledOnce); + + t.context.eventemitter.prototype.on.restore(); +}); + test.serial(`If an event is emitted for a local file and the rule doesn't work with those then the handler should be null`, (t) => { const rule = { create() {