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

Commit

Permalink
Resolve custom paths when loading rules (#3640)
Browse files Browse the repository at this point in the history
  • Loading branch information
adidahiya authored Jan 10, 2018
1 parent 2b8a737 commit a4dc874
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
9 changes: 5 additions & 4 deletions src/ruleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export function loadRules(ruleOptionsList: IOptions[],
return rules;
}

/** @internal private API */
export function findRule(name: string, rulesDirectories?: string | string[]): RuleConstructor | undefined {
const camelizedName = transformName(name);
// first check for core rules
Expand Down Expand Up @@ -123,13 +124,13 @@ function loadCachedRule(directory: string, ruleName: string, isCustomPath?: bool
return cachedRule === "not-found" ? undefined : cachedRule;
}

// get absolute path
// treat directory as a relative path (which needs to be resolved) if it's a custom rule directory
let absolutePath: string = directory;
if (isCustomPath) {
if (!fs.existsSync(directory)) {
throw new FatalError(`Could not find custom rule directory: ${directory}`);
}
absolutePath = path.resolve(directory);
if (!fs.existsSync(absolutePath)) {
throw new FatalError(`Could not find custom rule directory: ${absolutePath}`);
}
}

const Rule = loadRule(absolutePath, ruleName);
Expand Down
27 changes: 24 additions & 3 deletions test/ruleLoaderTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as path from "path";
import { rules as allRules, RULES_EXCLUDED_FROM_ALL_CONFIG } from "../src/configs/all";
import { camelize } from "../src/utils";
import { IOptions } from "./../src/language/rule/rule";
import { loadRules } from "./lint";
import { findRule, loadRules, RuleConstructor } from "./lint";

const builtRulesDir = "build/src/rules";
const srcRulesDir = "src/rules";
Expand Down Expand Up @@ -85,7 +85,7 @@ describe("Rule Loader", () => {
assert.equal(rules.length, 5);
});

it("loads js rules", () => {
it("loads rules for JS files, excluding typescript-only ones", () => {
const validConfiguration: IOptions[] = [
{ ruleName: "class-name", ruleArguments: [], ruleSeverity: "error", disabledIntervals: [] },
{ ruleName: "await-promise", ruleArguments: [], ruleSeverity: "error", disabledIntervals: [] },
Expand All @@ -95,7 +95,7 @@ describe("Rule Loader", () => {
assert.equal(rules.length, 1);
});

it("tests every rule", () => {
it("tests exist for every rule", () => {
const tests = fs.readdirSync(testRulesDir)
.filter((file) => !file.startsWith("_") && fs.statSync(path.join(testRulesDir, file)).isDirectory())
.map(camelize)
Expand All @@ -110,6 +110,27 @@ describe("Rule Loader", () => {

assert.deepEqual(expectedAllRules, tslintAllRules, "rule is missing in tslint:all");
});

it("resolves custom rule directories as relative paths", () => {
let rule: RuleConstructor | undefined;
assert.doesNotThrow(() => {
rule = findRule("always-fail", "test/files/custom-rules");
});
assert.isDefined(rule);
});

it("supports rulesDirectory set to empty string", () => {
// see https://github.com/palantir/tslint/issues/3638
assert.doesNotThrow(() => {
findRule("always-fail", "");
});
});

it("throws an error for invalid rulesDirectories", () => {
assert.throws(() => {
findRule("always-fail", "some/invalid/dir");
});
});
});

function everyRule(): string[] {
Expand Down

0 comments on commit a4dc874

Please sign in to comment.