Skip to content

Commit

Permalink
Chore: remove ConfigOps.normalize in favor of ConfigOps.getRuleSeveri…
Browse files Browse the repository at this point in the history
…ty (#9224)
  • Loading branch information
not-an-aardvark committed Sep 4, 2017
1 parent 171962a commit c991630
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 174 deletions.
30 changes: 15 additions & 15 deletions lib/config/config-ops.js
Expand Up @@ -193,25 +193,25 @@ module.exports = {
},

/**
* Converts new-style severity settings (off, warn, error) into old-style
* severity settings (0, 1, 2) for all rules. Assumption is that severity
* values have already been validated as correct.
* @param {Object} config The config object to normalize.
* @returns {void}
* Normalizes the severity value of a rule's configuration to a number
* @param {(number|string|[number, ...*]|[string, ...*])} ruleConfig A rule's configuration value, generally
* received from the user. A valid config value is either 0, 1, 2, the string "off" (treated the same as 0),
* the string "warn" (treated the same as 1), the string "error" (treated the same as 2), or an array
* whose first element is one of the above values. Strings are matched case-insensitively.
* @returns {(0|1|2)} The numeric severity value if the config value was valid, otherwise 0.
*/
normalize(config) {
getRuleSeverity(ruleConfig) {
const severityValue = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;

if (config.rules) {
Object.keys(config.rules).forEach(ruleId => {
const ruleConfig = config.rules[ruleId];
if (severityValue === 0 || severityValue === 1 || severityValue === 2) {
return severityValue;
}

if (typeof ruleConfig === "string") {
config.rules[ruleId] = RULE_SEVERITY[ruleConfig.toLowerCase()] || 0;
} else if (Array.isArray(ruleConfig) && typeof ruleConfig[0] === "string") {
ruleConfig[0] = RULE_SEVERITY[ruleConfig[0].toLowerCase()] || 0;
}
});
if (typeof severityValue === "string") {
return RULE_SEVERITY[severityValue.toLowerCase()] || 0;
}

return 0;
},

/**
Expand Down
21 changes: 6 additions & 15 deletions lib/linter.js
Expand Up @@ -604,16 +604,6 @@ function stripUnicodeBOM(text) {
return text;
}

/**
* Get the severity level of a rule (0 - none, 1 - warning, 2 - error)
* Returns 0 if the rule config is not valid (an Array or a number)
* @param {Array|number} ruleConfig rule configuration
* @returns {number} 0, 1, or 2, indicating rule severity
*/
function getRuleSeverity(ruleConfig) {
return Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig;
}

/**
* Get the options for a rule (not including severity), if any
* @param {Array|number} ruleConfig rule configuration
Expand Down Expand Up @@ -886,9 +876,6 @@ class Linter {
modifyConfigResult.problems.forEach(problem => problems.push(problem));
}

// ensure that severities are normalized in the config
ConfigOps.normalize(config);

const emitter = new EventEmitter().setMaxListeners(Infinity);

/*
Expand Down Expand Up @@ -928,7 +915,12 @@ class Linter {
);

// enable appropriate rules
Object.keys(config.rules).filter(ruleId => getRuleSeverity(config.rules[ruleId]) > 0).forEach(ruleId => {
Object.keys(config.rules).forEach(ruleId => {
const severity = ConfigOps.getRuleSeverity(config.rules[ruleId]);

if (severity === 0) {
return;
}
let ruleCreator = this.rules.get(ruleId);

if (!ruleCreator) {
Expand All @@ -942,7 +934,6 @@ class Linter {
this.rules.define(ruleId, ruleCreator);
}

const severity = getRuleSeverity(config.rules[ruleId]);
const ruleContext = Object.freeze(
Object.assign(
Object.create(sharedTraversalContext),
Expand Down
175 changes: 31 additions & 144 deletions tests/lib/config/config-ops.js
Expand Up @@ -10,6 +10,7 @@

const assert = require("chai").assert,
leche = require("leche"),
util = require("util"),
environments = require("../../../conf/environments"),
Environments = require("../../../lib/config/environments"),
ConfigCache = require("../../../lib/config/config-cache"),
Expand Down Expand Up @@ -550,150 +551,36 @@ describe("ConfigOps", () => {
});
});

describe("normalize()", () => {
it("should convert error rule setting to 2 when rule has just a severity", () => {
const config = {
rules: {
foo: "errOr",
bar: "error"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: 2,
bar: 2
}
});
});

it("should convert error rule setting to 2 when rule has array with severity", () => {
const config = {
rules: {
foo: ["Error", "something"],
bar: "error"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: [2, "something"],
bar: 2
}
});
});

it("should convert warn rule setting to 1 when rule has just a severity", () => {
const config = {
rules: {
foo: "waRn",
bar: "warn"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: 1,
bar: 1
}
});
});

it("should convert warn rule setting to 1 when rule has array with severity", () => {
const config = {
rules: {
foo: ["Warn", "something"],
bar: "warn"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: [1, "something"],
bar: 1
}
});
});

it("should convert off rule setting to 0 when rule has just a severity", () => {
const config = {
rules: {
foo: "ofF",
bar: "off"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: 0,
bar: 0
}
});
});

it("should convert off rule setting to 0 when rule has array with severity", () => {
const config = {
rules: {
foo: ["Off", "something"],
bar: "off"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: [0, "something"],
bar: 0
}
});
});

it("should convert invalid rule setting to 0 when rule has just a severity", () => {
const config = {
rules: {
foo: "invalid",
bar: "invalid"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: 0,
bar: 0
}
});
});

it("should convert invalid rule setting to 0 when rule has array with severity", () => {
const config = {
rules: {
foo: ["invalid", "something"],
bar: "invalid"
}
};

ConfigOps.normalize(config);

assert.deepEqual(config, {
rules: {
foo: [0, "something"],
bar: 0
}
});
});
describe("getRuleSeverity()", () => {
const EXPECTED_RESULTS = new Map([
[0, 0],
[1, 1],
[2, 2],
[[0], 0],
[[1], 1],
[[2], 2],
["off", 0],
["warn", 1],
["error", 2],
[["off"], 0],
[["warn"], 1],
[["error"], 2],
["OFF", 0],
["wArN", 1],
[["ErRoR"], 2],
["invalid config", 0],
[["invalid config"], 0],
[3, 0],
[[3], 0],
[1.5, 0],
[[1.5], 0]
]);

for (const key of EXPECTED_RESULTS.keys()) {
it(`returns ${util.inspect(EXPECTED_RESULTS.get(key))} for ${util.inspect(key)}`, () => {
assert.strictEqual(ConfigOps.getRuleSeverity(key), EXPECTED_RESULTS.get(key));
});
}
});

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

0 comments on commit c991630

Please sign in to comment.