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

Commit

Permalink
Don't exit on missing rules (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
nchen63 authored Nov 22, 2016
1 parent 716e1e7 commit 3531563
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 44 deletions.
23 changes: 13 additions & 10 deletions src/ruleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {camelize} from "underscore.string";

import {getRulesDirectories} from "./configuration";
import {IDisabledInterval, IRule} from "./language/rule/rule";
import {dedent} from "./utils";

const moduleDirectory = path.dirname(module.filename);
const CORE_RULES_DIRECTORY = path.resolve(moduleDirectory, ".", "rules");
Expand Down Expand Up @@ -65,26 +66,28 @@ export function loadRules(ruleConfiguration: {[name: string]: any},
}

if (notFoundRules.length > 0) {
const ERROR_MESSAGE = `
const warning = dedent`
Could not find implementations for the following rules specified in the configuration:
${notFoundRules.join("\n")}
${notFoundRules.join("\n ")}
Try upgrading TSLint and/or ensuring that you have all necessary custom rules installed.
If TSLint was recently upgraded, you may have old rules configured which need to be cleaned up.
`;

throw new Error(ERROR_MESSAGE);
} else if (notAllowedInJsRules.length > 0) {
const JS_ERROR_MESSAGE = `
console.warn(warning);
}
if (notAllowedInJsRules.length > 0) {
const warning = dedent`
Following rules specified in configuration couldn't be applied to .js or .jsx files:
${notAllowedInJsRules.join("\n")}
${notAllowedInJsRules.join("\n ")}
Make sure to exclude them from "jsRules" section of your tslint.json.
`;

throw new Error(JS_ERROR_MESSAGE);
} else {
return rules;
console.warn(warning);
}
if (rules.length === 0) {
console.warn("No valid rules have been specified");
}
return rules;
}

export function findRule(name: string, rulesDirectories?: string | string[]) {
Expand Down
40 changes: 6 additions & 34 deletions test/ruleLoaderTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,15 @@ describe("Rule Loader", () => {
assert.equal(rules.length, 5);
});

it("throws if an invalid rule is found", () => {
it("ignores invalid rules", () => {
const invalidConfiguration: {[name: string]: any} = {
invalidConfig1: true,
invalidConfig2: false,
};

assert.throws(
() => loadRules(invalidConfiguration, {}, RULES_DIRECTORY),
/invalidConfig1\ninvalidConfig2/,
);
});

it("doesn't ignore leading or trailing underscores or dashes", () => {
/* tslint:disable:object-literal-sort-keys */
const invalidConfiguration: {[name: string]: any} = {
"_indent": 6,
"forin_": true,
"-quotemark": "single",
"eofline-": true,
"class-name": true,
"invalidConfig1": true,
"invalidConfig2": false,
};
/* tslint:enable:object-literal-sort-keys */

assert.throws(
() => loadRules(invalidConfiguration, {}, RULES_DIRECTORY),
/_indent\nforin_\n-quotemark\neofline-/,
);
const rules = loadRules(invalidConfiguration, {}, [RULES_DIRECTORY]);
assert.equal(rules.length, 1);
});

it("works with rulesDirectory argument as an Array", () => {
Expand All @@ -82,15 +65,4 @@ describe("Rule Loader", () => {
const rules = loadRules(validConfiguration, {}, RULES_DIRECTORY, true);
assert.equal(rules.length, 1);
});

it("throws if an invalid rule is adopted", () => {
const invalidConfiguration: {[name: string]: any} = {
"array-type": [true, "array"],
};

assert.throws(
() => loadRules(invalidConfiguration, {}, RULES_DIRECTORY, true),
/array-type/,
);
});
});

0 comments on commit 3531563

Please sign in to comment.