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

Commit

Permalink
Fix bug where only one directory of custom rules was considered
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Killian committed Jan 22, 2016
1 parent c25d084 commit 7391dde
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export function getRelativePath(directory: string, relativeTo?: string): string
}
}

/**
* @return An array of absolute paths to directories potentially containing rules
*/
export function getRulesDirectories(directories: string | string[], relativeTo?: string): string[] {
let rulesDirectories: string[] = [];

Expand Down
23 changes: 7 additions & 16 deletions src/ruleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import * as fs from "fs";
import * as path from "path";
import {camelize, strLeft, strRight} from "underscore.string";
import {camelize} from "underscore.string";
import {getRulesDirectories} from "./configuration";
import {IRule, IDisabledInterval} from "./language/rule/rule";

Expand Down Expand Up @@ -68,18 +68,6 @@ export function findRule(name: string, rulesDirectories?: string | string[]) {
if (Rule != null) {
return Rule;
}

// finally check for rules within the first level of directories,
// using dash prefixes as the sub-directory names
const subDirectory = strLeft(rulesDirectory, "-");
const ruleName = strRight(rulesDirectory, "-");
if (subDirectory !== rulesDirectory && ruleName !== rulesDirectory) {
camelizedName = transformName(ruleName);
Rule = loadRule(rulesDirectory, subDirectory, camelizedName);
if (Rule != null) {
return Rule;
}
}
}
}

Expand All @@ -96,9 +84,12 @@ function transformName(name: string) {
return nameMatch[1] + camelize(nameMatch[2]) + nameMatch[3] + "Rule";
}

function loadRule(...paths: string[]) {
const rulePath = paths.reduce((p, c) => path.join(p, c), "");
const fullPath = path.resolve(moduleDirectory, rulePath);
/**
* @param directory - An absolute path to a directory of rules
* @param ruleName - A name of a rule in filename format. ex) "someLintRule"
*/
function loadRule(directory: string, ruleName: string) {
const fullPath = path.join(directory, ruleName);

if (fs.existsSync(fullPath + ".js")) {
const ruleModule = require(fullPath);
Expand Down
4 changes: 3 additions & 1 deletion test/check-bin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ expectOut $? 2 "tslint with with -r pointing to custom rules did not find lint f
./bin/tslint -c ./test/config/tslint-custom-rules-with-dir.json src/tslint.ts
expectOut $? 2 "tslint with with JSON pointing to custom rules did not find lint failures"


# make sure calling tslint with an array as rulesDirectory in a config file works
./bin/tslint -c ./test/config/tslint-custom-rules-with-two-dirs.json src/tslint.ts
expectOut $? 2 "tslint with with JSON pointing to two custom rules did not find lint failures from second directory"

# make sure tslint --init generates a file
cd ./bin
Expand Down
7 changes: 7 additions & 0 deletions test/config/tslint-custom-rules-with-two-dirs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"rulesDirectory": ["../files/custom-rules-2", "../files/custom-rules/"],
"rules": {
"always-fail": true,
"no-fail": true
}
}
27 changes: 27 additions & 0 deletions test/files/custom-rules-2/noFail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Lint = require("tslint/lib/lint");
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
_super.apply(this, arguments);
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithWalker(new NoFailWalker(sourceFile, this.getOptions()));
};
return Rule;
})(Lint.Rules.AbstractRule);
exports.Rule = Rule;
var NoFailWalker = (function (_super) {
__extends(NoFailWalker, _super);
function NoFailWalker() {
_super.apply(this, arguments);
}
NoFailWalker.prototype.visitSourceFile = function (node) {
// yay, no failures!
};
return NoFailWalker;
})(Lint.RuleWalker);

0 comments on commit 7391dde

Please sign in to comment.