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

Commit

Permalink
feat(configuration): set js rules to all valid active rules
Browse files Browse the repository at this point in the history
jsRules can now be a boolean. If it is set to true then parseConfig file, will copy over all active rules that can be applied to js to the jsRules configuration.
  • Loading branch information
Michael A. Lavina committed Jan 11, 2018
1 parent b5ebb9f commit b0a4364
Show file tree
Hide file tree
Showing 3 changed files with 68 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 @@ -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 you can set to true to copy over 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
25 changes: 20 additions & 5 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,21 +511,35 @@ export function parseConfigFile(
}

function parse(config: RawConfigFile, dir?: string): IConfigurationFile {
const rulesDirectory: string | string[] = getRulesDirectories(config.rulesDirectory, dir);
const jsRules = typeof config.jsRules === "boolean" ?
parseRules(config.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),
rulesDirectory,
};
}

function parseRules(config: RawRulesConfig | undefined): Map<string, Partial<IOptions>> {
function parseRules(config: RawRulesConfig | undefined,
copyRulestoJsRules = false,
rulesDirectory?: string | string[]): Map<string, Partial<IOptions>> {
const map = new Map<string, Partial<IOptions>>();
if (config !== undefined) {
for (const ruleName in config) {
if (hasOwnProperty(config, ruleName)) {
map.set(ruleName, parseRuleOptions(config[ruleName], defaultSeverity));
const ruleOptions = parseRuleOptions(config[ruleName], defaultSeverity);
if (copyRulestoJsRules && ruleOptions.ruleSeverity !== "off") {
const Rule = findRule(ruleName, rulesDirectory);
if (Rule === undefined || (Rule.metadata !== undefined && Rule.metadata.typescriptOnly)) {
continue;
}
}
map.set(ruleName, ruleOptions);
}
}
}
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

0 comments on commit b0a4364

Please sign in to comment.