-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrule-link.ts
More file actions
137 lines (123 loc) · 3.93 KB
/
Copy pathrule-link.ts
File metadata and controls
137 lines (123 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { join, sep, relative, dirname } from 'node:path';
import {
PathRuleDocFunction,
Plugin,
RULE_SOURCE,
UrlRuleDocFunction,
} from './types.js';
import { getPluginRoot } from './package-json.js';
export function replaceRulePlaceholder(
pathOrUrl: string | PathRuleDocFunction,
ruleName: string,
) {
if (typeof pathOrUrl === 'function') {
return pathOrUrl(ruleName);
}
return pathOrUrl.replaceAll('{name}', ruleName);
}
/**
* Account for how Windows paths use backslashes instead of the forward slashes that URLs require.
*/
function pathToUrl(path: string): string {
return path.split(sep).join('/');
}
/**
* Get the link to a rule's documentation page.
* Will be relative to the current page.
*/
export function getUrlToRule(
ruleName: string,
ruleSource: RULE_SOURCE,
pluginPrefix: string | undefined,
pathPlugin: string,
pathRuleDoc: string | PathRuleDocFunction,
pathCurrentPage: string,
urlRuleDoc?: string | UrlRuleDocFunction,
) {
switch (ruleSource) {
case RULE_SOURCE.eslintCore: {
return `https://eslint.org/docs/latest/rules/${ruleName}`;
}
case RULE_SOURCE.thirdPartyPlugin: {
// We don't know the documentation URL to third-party plugins.
return undefined;
}
default: {
// Fallthrough to remaining logic in function.
break;
}
}
const prefix = pluginPrefix ? `${pluginPrefix}/` : '';
// Ignore plugin prefix if it's included in rule name.
// While we could display the prefix if we wanted, it definitely cannot be part of the link.
const ruleNameWithoutPluginPrefix = ruleName.startsWith(prefix)
? ruleName.slice(prefix.length + 1)
: ruleName;
// If the URL is a function, evaluate it.
const urlRuleDocFunctionEvaluated =
typeof urlRuleDoc === 'function'
? urlRuleDoc(ruleName, pathToUrl(relative(pathPlugin, pathCurrentPage)))
: undefined;
const pathRuleDocEvaluated = join(
getPluginRoot(pathPlugin),
replaceRulePlaceholder(pathRuleDoc, ruleNameWithoutPluginPrefix),
);
return (
// If the function returned a URL, use it.
urlRuleDocFunctionEvaluated ??
(typeof urlRuleDoc === 'string'
? // Otherwise, use the URL if it's a string.
replaceRulePlaceholder(urlRuleDoc, ruleNameWithoutPluginPrefix)
: // Finally, fallback to the relative path.
pathToUrl(relative(dirname(pathCurrentPage), pathRuleDocEvaluated)))
);
}
/**
* Get the markdown link (title and URL) to the rule's documentation.
*/
export function getLinkToRule(
ruleName: string,
plugin: Plugin,
pluginPrefix: string | undefined,
pathPlugin: string,
pathRuleDoc: string | PathRuleDocFunction,
pathCurrentPage: string,
includeBackticks: boolean,
includePrefix: boolean,
urlRuleDoc?: string | UrlRuleDocFunction,
) {
const prefix = pluginPrefix ? `${pluginPrefix}/` : '';
const ruleNameWithoutPluginPrefix = ruleName.startsWith(prefix)
? ruleName.slice(prefix.length + 1)
: ruleName;
// Determine what plugin this rule comes from.
let ruleSource: RULE_SOURCE;
if (plugin.rules?.[ruleNameWithoutPluginPrefix]) {
ruleSource = RULE_SOURCE.self;
} else if (ruleName.includes('/')) {
// Assume a slash is for the plugin prefix (ESLint core doesn't have any nested rules).
ruleSource = RULE_SOURCE.thirdPartyPlugin;
} else {
ruleSource = RULE_SOURCE.eslintCore;
}
const ruleNameWithPluginPrefix = ruleName.startsWith(prefix)
? ruleName
: ruleSource === RULE_SOURCE.self
? `${prefix}${ruleName}`
: undefined;
const urlToRule = getUrlToRule(
ruleName,
ruleSource,
pluginPrefix,
pathPlugin,
pathRuleDoc,
pathCurrentPage,
urlRuleDoc,
);
const ruleNameToDisplay = `${includeBackticks ? '`' : ''}${
includePrefix && ruleNameWithPluginPrefix
? ruleNameWithPluginPrefix
: ruleNameWithoutPluginPrefix
}${includeBackticks ? '`' : ''}`;
return urlToRule ? `[${ruleNameToDisplay}](${urlToRule})` : ruleNameToDisplay;
}