Skip to content

Commit

Permalink
Disable rules with severity "off"
Browse files Browse the repository at this point in the history
Fix #191
  • Loading branch information
sarvaje authored and molant committed May 10, 2017
1 parent 38c5577 commit 5ace5aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/lib/sonar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
32 changes: 31 additions & 1 deletion tests/lib/sonar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5ace5aa

Please sign in to comment.