Skip to content

Commit

Permalink
Restricted Linter options type (palantir#1168)
Browse files Browse the repository at this point in the history
Also provide sane defaults for all linter configuration options.
  • Loading branch information
Josh Goldberg authored and tomduncalf committed Jun 14, 2016
1 parent 70cb301 commit 26632f9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ export interface LintResult {
output: string;
}

export interface ILinterOptions {
export interface ILinterOptionsRaw {
configuration?: any;
formatter?: string;
formattersDirectory?: string;
rulesDirectory?: string | string[];
}

export interface ILinterOptions extends ILinterOptionsRaw {
configuration: any;
formatter: string;
formattersDirectory: string;
rulesDirectory: string | string[];
}
25 changes: 15 additions & 10 deletions src/tslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
} from "./configuration";
import {EnableDisableRulesWalker} from "./enableDisableRules";
import {findFormatter} from "./formatterLoader";
import {ILinterOptions, LintResult} from "./lint";
import {ILinterOptionsRaw, ILinterOptions, LintResult} from "./lint";
import {loadRules} from "./ruleLoader";
import {arrayify} from "./utils";

Expand All @@ -44,11 +44,10 @@ class Linter {
private source: string;
private options: ILinterOptions;

constructor(fileName: string, source: string, options: ILinterOptions) {
constructor(fileName: string, source: string, options: ILinterOptionsRaw) {
this.fileName = fileName;
this.source = source;
this.options = options;
this.computeFullOptions();
this.options = this.computeFullOptions(options);
}

public lint(): LintResult {
Expand Down Expand Up @@ -99,13 +98,19 @@ class Linter {
return rules.some((r) => r.equals(rule));
}

private computeFullOptions() {
let {configuration, rulesDirectory} = this.options;
if (configuration == null) {
configuration = DEFAULT_CONFIG;
private computeFullOptions(options: ILinterOptionsRaw = {}): ILinterOptions {
if (typeof options !== "object") {
throw new Error("Unknown Linter options type: " + typeof options);
}
this.options.rulesDirectory = arrayify(rulesDirectory).concat(arrayify(configuration.rulesDirectory));
this.options.configuration = configuration;

let { configuration, formatter, formattersDirectory, rulesDirectory } = options;

return {
configuration: configuration || DEFAULT_CONFIG,
formatter: formatter || "prose",
formattersDirectory: formattersDirectory,
rulesDirectory: arrayify(rulesDirectory).concat(arrayify(configuration.rulesDirectory)),
};
}
}

Expand Down

0 comments on commit 26632f9

Please sign in to comment.