Skip to content

Commit

Permalink
Override defaultSeverity of extended configs (palantir#2569).
Browse files Browse the repository at this point in the history
  • Loading branch information
M0ns1gn0r committed Nov 3, 2017
1 parent d1caf11 commit 251640b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/usage/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ A path to a directory or an array of paths to directories of [custom rules][2].
- Any rules specified in this block will override those configured in any base configuration being extended.
- [Check out the full rules list here][3].
* `jsRules?: any`: Same format as `rules`. These rules are applied to `.js` and `.jsx` files.
* `defaultSeverity?: "error" | "warning" | "off"`: The severity level used when a rule specifies a default warning level. If undefined, "error" is used. This value is not inherited and is only applied to rules in this file.
* `defaultSeverity?: "error" | "warning" | "off"`: The severity level used when a rule specifies a default warning level. If undefined, "error" is used. If specified, overrides the values defined in the configuration files which are extended by this configuration.
* `linterOptions?: { exclude?: string[] }`:
- `exclude: string[]`: An array of globs. Any file matching these globs will not be linted. All exclude patterns are relative to the configuration file they were specified in.

Expand Down
12 changes: 9 additions & 3 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { arrayify, hasOwnProperty, stripComments } from "./utils";

export interface IConfigurationFile {
/**
* The severity that is applied to rules in _this_ config with `severity === "default"`.
* The severity that is applied to rules in this and _extended_ configs with `severity === "default"`.
* Not inherited.
*/
defaultSeverity?: RuleSeverity;
Expand Down Expand Up @@ -200,9 +200,10 @@ function findup(filename: string, directory: string): string | undefined {
* 'path/to/config' will attempt to load a to/config file inside a node module named path
* @param configFilePath The configuration to load
* @param originalFilePath The entry point configuration file
* @param extendingConfig The configuration which extends this one
* @returns a configuration object for TSLint loaded from the file at configFilePath
*/
export function loadConfigurationFromPath(configFilePath?: string, originalFilePath = configFilePath) {
export function loadConfigurationFromPath(configFilePath?: string, originalFilePath = configFilePath, extendingConfig?: RawConfigFile) {
if (configFilePath == undefined) {
return DEFAULT_CONFIG;
} else {
Expand All @@ -224,14 +225,19 @@ export function loadConfigurationFromPath(configFilePath?: string, originalFileP
delete (require.cache as { [key: string]: any })[resolvedConfigFilePath];
}

// defaultSeverity defined in the config which extends this one wins.
if (extendingConfig && extendingConfig.defaultSeverity) {
rawConfigFile.defaultSeverity = extendingConfig.defaultSeverity;
}

const configFileDir = path.dirname(resolvedConfigFilePath);
const configFile = parseConfigFile(rawConfigFile, configFileDir);

// load configurations, in order, using their identifiers or relative paths
// apply the current configuration last by placing it last in this array
const configs: IConfigurationFile[] = configFile.extends.map((name) => {
const nextConfigFilePath = resolveConfigurationPath(name, configFileDir);
return loadConfigurationFromPath(nextConfigFilePath, originalFilePath);
return loadConfigurationFromPath(nextConfigFilePath, originalFilePath, rawConfigFile);
}).concat([configFile]);

return configs.reduce(extendConfigurationFile, EMPTY_CONFIG);
Expand Down
6 changes: 6 additions & 0 deletions test/config/tslint-default-severity-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"defaultSeverity": "error",
"rules": {
"default-severity-error": { "severity": "default" }
}
}
5 changes: 5 additions & 0 deletions test/config/tslint-default-severity-unspecified.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"default-severity-unspecified": { "severity": "default" }
}
}
7 changes: 7 additions & 0 deletions test/config/tslint-extends-default-severity.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": [ "./tslint-default-severity-unspecified.json", "./tslint-default-severity-error.json" ],
"defaultSeverity": "warning",
"rules": {
"default-severity-warning": { "severity": "default" }
}
}
16 changes: 16 additions & 0 deletions test/configurationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,22 @@ describe("Configuration", () => {
const actualConfig = extendConfigurationFile(baseConfig, extendingConfig);
assertConfigEquals(actualConfig, expectedConfig);
});

it("overrides defaultSeverity of base configs", () => {
const config = loadConfigurationFromPath("./test/config/tslint-extends-default-severity.json");
assert.equal<RuleSeverity | undefined>(
"warning",
config.rules.get("default-severity-unspecified")!.ruleSeverity,
"did not apply defaultSeverity to base config with no defaultSeverity");
assert.equal<RuleSeverity | undefined>(
"warning",
config.rules.get("default-severity-error")!.ruleSeverity,
"did not override defaultSeverity defined in base config");
assert.equal<RuleSeverity | undefined>(
"warning",
config.rules.get("default-severity-warning")!.ruleSeverity,
"did not apply defaultSeverity to extending config");
});
});

describe("findConfigurationPath", () => {
Expand Down

0 comments on commit 251640b

Please sign in to comment.