Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Override defaultSeverity defined in extended configs (#3449)
Browse files Browse the repository at this point in the history
  • Loading branch information
M0ns1gn0r authored and adidahiya committed Jan 10, 2018
1 parent 82bf779 commit ef28083
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 6 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 that is applied to rules in this config file as well as rules in any inherited config files which have their severity set to "default". If undefined, "error" is used as the defaultSeverity.
* `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
15 changes: 12 additions & 3 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import { arrayify, hasOwnProperty, stripComments } from "./utils";

export interface IConfigurationFile {
/**
* The severity that is applied to rules in _this_ config with `severity === "default"`.
* @deprecated property is never set
*
* The severity that is applied to rules in this config file as well as rules
* in any inherited config files which have their severity set to "default".
* Not inherited.
*/
defaultSeverity?: RuleSeverity;
Expand Down Expand Up @@ -211,9 +214,10 @@ function findup(filenames: 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 Down Expand Up @@ -246,14 +250,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 !== undefined && extendingConfig.defaultSeverity !== undefined) {
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" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tslint-extends-default-severity.json",
"rules": {
"default-severity-only-in-extended": { "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" }
}
}
24 changes: 22 additions & 2 deletions test/configurationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,26 @@ 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-only-in-extended.json");
assert.equal<RuleSeverity | undefined>(
"warning",
config.rules.get("default-severity-unspecified")!.ruleSeverity,
"should apply defaultSeverity to base config with no defaultSeverity");
assert.equal<RuleSeverity | undefined>(
"warning",
config.rules.get("default-severity-error")!.ruleSeverity,
"should override defaultSeverity defined in base config");
assert.equal<RuleSeverity | undefined>(
"warning",
config.rules.get("default-severity-warning")!.ruleSeverity,
"should apply defaultSeverity to extending config");
assert.equal<RuleSeverity | undefined>(
"error",
config.rules.get("default-severity-only-in-extended")!.ruleSeverity,
"should not inherit defaultSeverity from base configs");
});
});

describe("findConfigurationPath", () => {
Expand Down Expand Up @@ -293,11 +313,11 @@ describe("Configuration", () => {
assert.equal<RuleSeverity | undefined>(
"error",
config.rules.get("no-fail")!.ruleSeverity,
"did not pick up 'no-fail' in base config");
"should pick up 'no-fail' in base config");
assert.equal<RuleSeverity | undefined>(
"off",
config.rules.get("always-fail")!.ruleSeverity,
"did not set 'always-fail' in top config");
"should set 'always-fail' in top config");
assert.equal<RuleSeverity | undefined>("error", config.jsRules.get("no-fail")!.ruleSeverity);
assert.equal<RuleSeverity | undefined>("off", config.jsRules.get("always-fail")!.ruleSeverity);
});
Expand Down

0 comments on commit ef28083

Please sign in to comment.