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

feat(configuration): set js rules to all valid active rules #3641

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/usage/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ A path to a directory or an array of paths to directories of [custom rules][2].
- A boolean value may be specified instead of the above object, and is equivalent to setting no options with default severity.
- 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.
* `jsRules?: any | boolean`: Same format as `rules` or explicit `true` to copy all valid active rules from rules. These rules are applied to `.js` and `.jsx` files.
* `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
33 changes: 29 additions & 4 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as resolve from "resolve";
import { FatalError, showWarningOnce } from "./error";

import { IOptions, RuleSeverity } from "./language/rule/rule";
import { findRule } from "./ruleLoader";
import { arrayify, hasOwnProperty, stripComments } from "./utils";

export interface IConfigurationFile {
Expand Down Expand Up @@ -462,7 +463,7 @@ export interface RawConfigFile {
rulesDirectory?: string | string[];
defaultSeverity?: string;
rules?: RawRulesConfig;
jsRules?: RawRulesConfig;
jsRules?: RawRulesConfig | boolean;
}
export interface RawRulesConfig {
[key: string]: RawRuleConfig;
Expand Down Expand Up @@ -510,12 +511,18 @@ export function parseConfigFile(
}

function parse(config: RawConfigFile, dir?: string): IConfigurationFile {
const rulesDirectory: string | string[] = getRulesDirectories(config.rulesDirectory, dir);
const rules = parseRules(config.rules);
const jsRules = typeof config.jsRules === "boolean" ?
filterValidJsRules(rules, config.jsRules, rulesDirectory) :
parseRules(config.jsRules);

return {
extends: arrayify(config.extends),
jsRules: parseRules(config.jsRules),
jsRules,
linterOptions: parseLinterOptions(config.linterOptions, dir),
rules: parseRules(config.rules),
rulesDirectory: getRulesDirectories(config.rulesDirectory, dir),
rules,
rulesDirectory,
};
}

Expand All @@ -531,6 +538,24 @@ export function parseConfigFile(
return map;
}

function filterValidJsRules(rules: Map<string, Partial<IOptions>>,
copyRulestoJsRules = false,
rulesDirectory?: string | string[]): Map<string, Partial<IOptions>> {
const validJsRules = new Map<string, Partial<IOptions>>();
if (copyRulestoJsRules) {
rules.forEach((ruleOptions, ruleName) => {
if (ruleOptions.ruleSeverity !== "off") {
const Rule = findRule(ruleName, rulesDirectory);
if ((Rule !== undefined && (Rule.metadata === undefined || !Rule.metadata.typescriptOnly))) {
validJsRules.set(ruleName, ruleOptions);
}
}
});
}

return validJsRules;
}

function parseLinterOptions(raw: RawConfigFile["linterOptions"], dir?: string): IConfigurationFile["linterOptions"] {
if (raw === undefined || raw.exclude === undefined) {
return {};
Expand Down
47 changes: 47 additions & 0 deletions test/configurationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,53 @@ describe("Configuration", () => {
},
);
});

it("parses jsRules when jsRules is a config", () => {
const rawConfig: RawConfigFile = {
jsRules: {
a: true,
},
};

const expected = getEmptyConfig();
expected.jsRules.set("a", { ruleArguments: [], ruleSeverity: "error" });
assertConfigEquals(parseConfigFile(rawConfig), expected);
});

it("copies valid rules to jsRules when jsRules is a boolean", () => {
let rawConfig: RawConfigFile = {
jsRules: true,
rules: {},
};

const expected = getEmptyConfig();
assertConfigEquals(parseConfigFile(rawConfig), expected);

rawConfig = {
jsRules: true,
rules: {
eofline: true,
},
};

let {rules, jsRules} = parseConfigFile(rawConfig);
assert.deepEqual(demap(rules), demap(jsRules));

rawConfig = {
jsRules: true,
rules: {
eofline: true,
typedef: true,
},
};

({rules, jsRules} = parseConfigFile(rawConfig));
assert(jsRules.has("eofline"));
assert(!jsRules.has("typedef"));

rules.delete("typedef");
assert.deepEqual(demap(rules), demap(jsRules));
});
});

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