Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable rules with severity "off" #197

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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