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

Commit

Permalink
Merge branch 'master' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Killian committed Jan 22, 2016
2 parents 810fdc1 + bb58eeb commit e3f35d8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Change Log
===

v3.2.2
---
* Stable release containing changes from the last dev release

v3.2.2-dev.1
---
* [enhancement] Throw an error if a path to a directory of custom rules is invalid (#910)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ A sample configuration file with all options is available [here](https://github.
Each rule option requires a value of `"always"` or `"never"`. Rule options:
* `"multiline"` checks multi-line object literals.
* `"singleline"` checks single-line object literals.
* `triple-equals` enforces === and !== in favor of == and !=.
* `triple-equals` enforces `===` and `!==` in favor of `==` and `!=`.
* `"allow-null-check"` allows `==` and `!=` when comparing to `null`.
* `typedef` enforces type definitions to exist. Rule options:
* `"call-signature"` checks return type of functions.
* `"parameter"` checks type specifier of function parameters.
Expand Down
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 e3f35d8

Please sign in to comment.