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

inherit defaultSeverity onto child configurations #3240

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ function findup(filename: string, directory: string): string | undefined {
* @param originalFilePath The entry point configuration file
* @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, parentConfig?: RawConfigFile) {
if (configFilePath == undefined) {
return DEFAULT_CONFIG;
} else {
Expand All @@ -224,14 +224,19 @@ export function loadConfigurationFromPath(configFilePath?: string, originalFileP
delete (require.cache as { [key: string]: any })[resolvedConfigFilePath];
}

// assign defaultSeverity of parent if child configuration has no defaultSeverity set:
const parentDefaultSeverity = parentConfig !== undefined ? parentConfig.defaultSeverity : undefined;
rawConfigFile.defaultSeverity =
rawConfigFile.defaultSeverity !== undefined ? rawConfigFile.defaultSeverity : parentDefaultSeverity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO a defaultSeverity specified in the extending config file should always override the defaultSeverity in all extended configs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that's should still be the case with my additions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's consider the following:

  • a.json has no defaultSeverity
  • b.json has defaultSeverity "warning"
  • c.json extends a.json and b.json and has defaultSeverity "error"

The result is:

  • rules specified in c.json get a default of "error"
  • rules from b.json get a default of "warning", because b.json specifies its own defaultSeverity
  • rules from a.json get a default of "error" from c.json, because a.json has no defaultSeverity.

To override the defaultSeverity in b.json, you'd need to adjust this line to

rawConfigFile.defaultSeverity = parentDefaultSeverity !== undefined ? parentDefaultSeverity : rawConfigFile.defaultSeverity;

To make it clearer what's happening here, I suggest to rename parentConfig to extendingConfig


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