diff --git a/src/configuration.ts b/src/configuration.ts index 0302cc405d9..d0a314fb469 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -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[] = []; diff --git a/src/ruleLoader.ts b/src/ruleLoader.ts index 36d0c35de99..17de44951be 100644 --- a/src/ruleLoader.ts +++ b/src/ruleLoader.ts @@ -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"; @@ -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; - } - } } } @@ -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); diff --git a/test/check-bin.sh b/test/check-bin.sh index 6cde1e6c991..b323582aeef 100755 --- a/test/check-bin.sh +++ b/test/check-bin.sh @@ -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 diff --git a/test/config/tslint-custom-rules-with-two-dirs.json b/test/config/tslint-custom-rules-with-two-dirs.json new file mode 100644 index 00000000000..f02d738225e --- /dev/null +++ b/test/config/tslint-custom-rules-with-two-dirs.json @@ -0,0 +1,7 @@ +{ + "rulesDirectory": ["../files/custom-rules-2", "../files/custom-rules/"], + "rules": { + "always-fail": true, + "no-fail": true + } +} diff --git a/test/files/custom-rules-2/noFail.js b/test/files/custom-rules-2/noFail.js new file mode 100644 index 00000000000..1d2ff62d1af --- /dev/null +++ b/test/files/custom-rules-2/noFail.js @@ -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);