This repository was archived by the owner on Mar 25, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -107,16 +107,30 @@ function transformName(name: string): string {
107
107
* @param ruleName - A name of a rule in filename format. ex) "someLintRule"
108
108
*/
109
109
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 ;
113
113
if ( ruleModule !== undefined ) {
114
114
return ruleModule . Rule ;
115
115
}
116
116
}
117
117
return "not-found" ;
118
118
}
119
119
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
+
120
134
function loadCachedRule ( directory : string , ruleName : string , isCustomPath ?: boolean ) : RuleConstructor | undefined {
121
135
// use cached value if available
122
136
const fullPath = path . join ( directory , ruleName ) ;
You can’t perform that action at this time.
0 commit comments