-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
relative-url-style.js
167 lines (139 loc) · 3.84 KB
/
relative-url-style.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
const {getStaticValue} = require('eslint-utils');
const {newExpressionSelector} = require('./selectors/index.js');
const {replaceStringLiteral} = require('./fix/index.js');
const MESSAGE_ID_NEVER = 'never';
const MESSAGE_ID_ALWAYS = 'always';
const MESSAGE_ID_REMOVE = 'remove';
const messages = {
[MESSAGE_ID_NEVER]: 'Remove the `./` prefix from the relative URL.',
[MESSAGE_ID_ALWAYS]: 'Add a `./` prefix to the relative URL.',
[MESSAGE_ID_REMOVE]: 'Remove leading `./`.',
};
const templateLiteralSelector = [
newExpressionSelector({name: 'URL', argumentsLength: 2}),
' > TemplateLiteral.arguments:first-child',
].join('');
const literalSelector = [
newExpressionSelector({name: 'URL', argumentsLength: 2}),
' > Literal.arguments:first-child',
].join('');
const DOT_SLASH = './';
const TEST_URL_BASES = [
'https://example.com/a/b/',
'https://example.com/a/b.html',
];
const isSafeToAddDotSlashToUrl = (url, base) => {
try {
return new URL(url, base).href === new URL(DOT_SLASH + url, base).href;
} catch {}
return false;
};
const isSafeToAddDotSlash = (url, bases = TEST_URL_BASES) => bases.every(base => isSafeToAddDotSlashToUrl(url, base));
const isSafeToRemoveDotSlash = (url, bases = TEST_URL_BASES) => bases.every(base => isSafeToAddDotSlashToUrl(url.slice(DOT_SLASH.length), base));
function canAddDotSlash(node, context) {
const url = node.value;
if (url.startsWith(DOT_SLASH) || url.startsWith('.') || url.startsWith('/')) {
return false;
}
const baseNode = node.parent.arguments[1];
const staticValueResult = getStaticValue(baseNode, context.getScope());
if (
staticValueResult
&& typeof staticValueResult.value === 'string'
&& isSafeToAddDotSlash(url, [staticValueResult.value])
) {
return true;
}
return isSafeToAddDotSlash(url);
}
function canRemoveDotSlash(node, context) {
const rawValue = node.raw.slice(1, -1);
if (!rawValue.startsWith(DOT_SLASH)) {
return false;
}
const baseNode = node.parent.arguments[1];
const staticValueResult = getStaticValue(baseNode, context.getScope());
if (
staticValueResult
&& typeof staticValueResult.value === 'string'
&& isSafeToRemoveDotSlash(node.value, [staticValueResult.value])
) {
return true;
}
return isSafeToRemoveDotSlash(node.value);
}
function addDotSlash(node, context) {
if (!canAddDotSlash(node, context)) {
return;
}
return fixer => replaceStringLiteral(fixer, node, DOT_SLASH, 0, 0);
}
function removeDotSlash(node, context) {
if (!canRemoveDotSlash(node, context)) {
return;
}
return fixer => replaceStringLiteral(fixer, node, '', 0, 2);
}
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const style = context.options[0] || 'never';
const listeners = {};
// TemplateLiteral are not always safe to remove `./`, but if it's starts with `./` we'll report
if (style === 'never') {
listeners[templateLiteralSelector] = function (node) {
const firstPart = node.quasis[0];
if (!firstPart.value.raw.startsWith(DOT_SLASH)) {
return;
}
return {
node,
messageId: style,
suggest: [
{
messageId: MESSAGE_ID_REMOVE,
fix(fixer) {
const start = firstPart.range[0] + 1;
return fixer.removeRange([start, start + 2]);
},
},
],
};
};
}
listeners[literalSelector] = function (node) {
if (typeof node.value !== 'string') {
return;
}
const fix = (style === 'never' ? removeDotSlash : addDotSlash)(node, context);
if (!fix) {
return;
}
return {
node,
messageId: style,
fix,
};
};
return listeners;
};
const schema = [
{
enum: ['never', 'always'],
default: 'never',
},
];
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce consistent relative URL style.',
},
fixable: 'code',
hasSuggestions: true,
schema,
messages,
},
};