Skip to content

Commit

Permalink
Add cliOptions section to configuration files.
Browse files Browse the repository at this point in the history
This allows users to define default cli options in tslint.json.

e.g. if you want to exclude the "bin" directory from linting, you could add this to your tslint.json:
"cliOptions": {
    "exclude": "bin"
}
  • Loading branch information
mmkal authored and mmkal committed Mar 25, 2017
1 parent da6ce1b commit 4449db0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ The configuration file specifies which rules are enabled and their options. Thes
* A list of relative or absolute paths to directories that contain custom rules.
* See the Custom Rules documentation below for more details.
*/
]
],
"cliOptions": {
/*
* Any options from the [command-line options section](####CLI) can be set here.
* They will be overriden by options passed in from the CLI.
*/
"exclude": [
"test",
"bin"
]
}
}
```

Expand Down
10 changes: 10 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { IOptions, RuleSeverity } from "./language/rule/rule";
import { arrayify, objectify, stripComments } from "./utils";

export interface IConfigurationFile {
cliOptions: { [index: string]: any };
extends: string[];
jsRules: Map<string, Partial<IOptions>>;
linterOptions?: {
Expand All @@ -42,13 +43,15 @@ export interface IConfigurationLoadResult {
export const CONFIG_FILENAME = "tslint.json";

export const DEFAULT_CONFIG: IConfigurationFile = {
cliOptions: {},
extends: ["tslint:recommended"],
jsRules: new Map<string, Partial<IOptions>>(),
rules: new Map<string, Partial<IOptions>>(),
rulesDirectory: [],
};

export const EMPTY_CONFIG: IConfigurationFile = {
cliOptions: {},
extends: [],
jsRules: new Map<string, Partial<IOptions>>(),
rules: new Map<string, Partial<IOptions>>(),
Expand Down Expand Up @@ -216,7 +219,13 @@ export function extendConfigurationFile(targetConfig: IConfigurationFile,
const combinedRulesDirs = targetConfig.rulesDirectory.concat(nextConfigSource.rulesDirectory);
const dedupedRulesDirs = Array.from(new Set(combinedRulesDirs));

const cliOptions = {
...targetConfig.cliOptions,
...nextConfigSource.cliOptions,
};

return {
cliOptions,
extends: [],
jsRules: combineMaps(targetConfig.jsRules, nextConfigSource.jsRules),
linterOptions: combineProperties(targetConfig.linterOptions, nextConfigSource.linterOptions),
Expand Down Expand Up @@ -346,6 +355,7 @@ export function parseConfigFile(configFile: any, configFileDir?: string): IConfi
}

return {
cliOptions: configFile.cliOptions || {},
extends: arrayify(configFile.extends),
jsRules,
linterOptions: configFile.linterOptions || {},
Expand Down
5 changes: 5 additions & 0 deletions src/tslint-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
import * as fs from "fs";
import * as optimist from "optimist";

import { findConfigurationPath, loadConfigurationFromPath } from "./configuration";
import { IRunnerOptions, Runner } from "./runner";

const configPath = findConfigurationPath(optimist.argv.c || optimist.argv.config, __filename);
const configuration = loadConfigurationFromPath(configPath);

const processed = optimist
.usage("Usage: $0 [options] file ...")
.default(configuration.cliOptions || {})
.check((argv: any) => {
// at least one of file, help, version, project or unqualified argument must be present
if (!(argv.h || argv.i || argv.test || argv.v || argv.project || argv._.length > 0)) {
Expand Down
23 changes: 23 additions & 0 deletions test/configurationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ describe.only("Configuration", () => {
const actualConfig = extendConfigurationFile(baseConfig, extendingConfig);
assertConfigEquals(actualConfig, expectedConfig);
});

it("merges cli options", () => {
const baseConfig = getEmptyConfig();
baseConfig.cliOptions.exclude = "src";
baseConfig.cliOptions.fix = true;

const extendingConfig = getEmptyConfig();
extendingConfig.cliOptions.exclude = [
"lib",
"bin",
];

const expectedConfig = getEmptyConfig();
expectedConfig.cliOptions.exclude = [
"lib",
"bin",
];
expectedConfig.cliOptions.fix = true;

const actualConfig = extendConfigurationFile(baseConfig, extendingConfig);
assertConfigEquals(actualConfig, expectedConfig);
});
});

describe("loadConfigurationFromPath", () => {
Expand Down Expand Up @@ -315,6 +337,7 @@ describe.only("Configuration", () => {

function getEmptyConfig(): IConfigurationFile {
return {
cliOptions: {},
extends: [],
jsRules: new Map<string, Partial<IOptions>>(),
linterOptions: {},
Expand Down

0 comments on commit 4449db0

Please sign in to comment.