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

added a set rules function for more clarity #33

Merged
merged 5 commits into from
Oct 8, 2018
Merged
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
28 changes: 18 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ export class Spectral {
private _rules: IRuleStore = {};

// the initial rule config, set on initialization
private readonly _rulesets: types.IRuleset[];
// @ts-ignore
private _rulesets: types.IRuleset[] = [];
casserni marked this conversation as resolved.
Show resolved Hide resolved

private readonly _functions: IFunctionStore;
private _functions: IFunctionStore = {};

constructor(opts: ISpectralOpts) {
this._rulesets = opts.rulesets;
this._functions = this._rulesetsToFunctions(this._rulesets);
this._rules = this._rulesetsToRules(this._rulesets);
this.setRules(opts.rulesets);
}

// TODO needs better pattern matching
Expand All @@ -66,17 +65,26 @@ export class Spectral {
return rules;
}

public setRules(rulesets: types.IRuleset[]) {
this._rulesets = rulesets;
this._functions = this._rulesetsToFunctions(this._rulesets);
this._rules = this._rulesetsToRules(this._rulesets);
}

public run({ target, spec, rulesets = [], type }: IRunOpts): types.IRuleResult[] {
const results: types.IRuleResult[] = [];

let runRules: IRuleStore;
if (rulesets.length) {
runRules = this._rulesetsToRules(rulesets);
} else {
// create a shallow copy of rule configuration for this run
runRules = { ...this._rules };
this.setRules(rulesets);
}

if (!target) {
return results;
}

// create a shallow copy of rule configuration for this run
const runRules: IRuleStore = { ...this._rules };

for (const path in this._paths) {
if (!this._paths.hasOwnProperty(path)) continue;

Expand Down