diff --git a/src/lint.ts b/src/lint.ts index b50d31e8ee3..1416c6a83d9 100644 --- a/src/lint.ts +++ b/src/lint.ts @@ -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[]; } diff --git a/src/tslint.ts b/src/tslint.ts index 80db3a1d442..cbd21885f62 100644 --- a/src/tslint.ts +++ b/src/tslint.ts @@ -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"; @@ -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 { @@ -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)), + }; } }