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

Commit 90dd3f4

Browse files
devversionadidahiya
authored andcommitted
fix(ruleLoader): resolve rule files using node path resolution (#3108)
1 parent 5732e7d commit 90dd3f4

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/ruleLoader.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,30 @@ function transformName(name: string): string {
107107
* @param ruleName - A name of a rule in filename format. ex) "someLintRule"
108108
*/
109109
function loadRule(directory: string, ruleName: string): RuleConstructor | "not-found" {
110-
const fullPath = path.join(directory, ruleName);
111-
if (fs.existsSync(`${fullPath}.js`)) {
112-
const ruleModule = require(fullPath) as { Rule: RuleConstructor } | undefined;
110+
const ruleFullPath = getRuleFullPath(directory, ruleName);
111+
if (ruleFullPath !== undefined) {
112+
const ruleModule = require(ruleFullPath) as { Rule: RuleConstructor } | undefined;
113113
if (ruleModule !== undefined) {
114114
return ruleModule.Rule;
115115
}
116116
}
117117
return "not-found";
118118
}
119119

120+
/**
121+
* Returns the full path to a rule file. Path to rules are resolved using nodes path resolution.
122+
* This allows developers to write custom rules in TypeScript, which then can be loaded by TS-Node.
123+
* @param directory - An absolute path to a directory of rules
124+
* @param ruleName - A name of a rule in filename format. ex) "someLintRule"
125+
*/
126+
function getRuleFullPath(directory: string, ruleName: string): string | undefined {
127+
try {
128+
return require.resolve(path.join(directory, ruleName));
129+
} catch (e) {
130+
return undefined;
131+
}
132+
}
133+
120134
function loadCachedRule(directory: string, ruleName: string, isCustomPath?: boolean): RuleConstructor | undefined {
121135
// use cached value if available
122136
const fullPath = path.join(directory, ruleName);

0 commit comments

Comments
 (0)