From 26632f9ad1efe920fcfd3f2cf475ab97f746340c Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Thu, 28 Apr 2016 11:55:23 -0700 Subject: [PATCH] Restricted Linter options type (#1168) Also provide sane defaults for all linter configuration options. --- src/lint.ts | 10 ++++++++-- src/tslint.ts | 25 +++++++++++++++---------- 2 files changed, 23 insertions(+), 12 deletions(-) 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)), + }; } }