@next/eslint-plugin-next 16.5.0-canary.47 → 16.5.0-canary.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -12,6 +12,7 @@ var _definerule = require("../utils/define-rule");
|
|
|
12
12
|
var _path = /*#__PURE__*/ _interop_require_wildcard(require("path"));
|
|
13
13
|
var _fs = /*#__PURE__*/ _interop_require_wildcard(require("fs"));
|
|
14
14
|
var _getrootdirs = require("../utils/get-root-dirs");
|
|
15
|
+
var _eslintutils = require("@eslint-community/eslint-utils");
|
|
15
16
|
var _url = require("../utils/url");
|
|
16
17
|
function _array_like_to_array(arr, len) {
|
|
17
18
|
if (len == null || len > arr.length) len = arr.length;
|
|
@@ -134,6 +135,45 @@ var memoize = function(fn) {
|
|
|
134
135
|
var cachedGetUrlFromPagesDirectories = memoize(_url.getUrlFromPagesDirectories);
|
|
135
136
|
var cachedGetUrlFromAppDirectory = memoize(_url.getUrlFromAppDirectory);
|
|
136
137
|
var url = 'https://nextjs.org/docs/messages/no-html-link-for-pages';
|
|
138
|
+
// Resolve direct constants first, then the first parameter of an inline map
|
|
139
|
+
// callback over a static array. Unknown values are left to runtime.
|
|
140
|
+
function getHrefValues(node, sourceCode) {
|
|
141
|
+
var scope = sourceCode.getScope(node);
|
|
142
|
+
var constant = (0, _eslintutils.getStaticValue)(node, scope);
|
|
143
|
+
if (constant) return [
|
|
144
|
+
constant.value
|
|
145
|
+
];
|
|
146
|
+
var identifier = node.type === 'MemberExpression' ? node.object : node;
|
|
147
|
+
if (identifier.type !== 'Identifier') return [];
|
|
148
|
+
var variable = (0, _eslintutils.findVariable)(scope, identifier);
|
|
149
|
+
if (!variable || variable.defs.length !== 1) return [];
|
|
150
|
+
var definition = variable.defs[0];
|
|
151
|
+
if (definition.type !== 'Parameter') return [];
|
|
152
|
+
var callback = definition.node;
|
|
153
|
+
var call = callback.parent;
|
|
154
|
+
if (callback.type !== 'ArrowFunctionExpression' && callback.type !== 'FunctionExpression' || callback.params[0] !== definition.name || call.type !== 'CallExpression' || call.arguments[0] !== callback || call.callee.type !== 'MemberExpression' || (0, _eslintutils.getPropertyName)(call.callee, sourceCode.getScope(call)) !== 'map') {
|
|
155
|
+
return [];
|
|
156
|
+
}
|
|
157
|
+
// Do not infer values when the callback reassigns, mutates, or passes its
|
|
158
|
+
// parameter to another function. Only direct reads in JSX are supported.
|
|
159
|
+
if (variable.references.some(function(reference) {
|
|
160
|
+
if (!reference.isReadOnly()) return true;
|
|
161
|
+
var parent = reference.identifier.parent;
|
|
162
|
+
var container = parent.type === 'MemberExpression' && parent.object === reference.identifier ? parent.parent : parent;
|
|
163
|
+
return container.type !== 'JSXExpressionContainer';
|
|
164
|
+
})) {
|
|
165
|
+
return [];
|
|
166
|
+
}
|
|
167
|
+
var array = (0, _eslintutils.getStaticValue)(call.callee.object, sourceCode.getScope(call));
|
|
168
|
+
if (!array || !Array.isArray(array.value)) return [];
|
|
169
|
+
if (node.type === 'Identifier') return array.value;
|
|
170
|
+
if (node.type !== 'MemberExpression') return [];
|
|
171
|
+
var property = (0, _eslintutils.getPropertyName)(node, scope);
|
|
172
|
+
if (property == null) return [];
|
|
173
|
+
return array.value.map(function(item) {
|
|
174
|
+
return item != null && Object.prototype.hasOwnProperty.call(item, property) ? item[property] : undefined;
|
|
175
|
+
});
|
|
176
|
+
}
|
|
137
177
|
var _default = (0, _definerule.defineRule)({
|
|
138
178
|
meta: {
|
|
139
179
|
docs: {
|
|
@@ -202,6 +242,7 @@ var _default = (0, _definerule.defineRule)({
|
|
|
202
242
|
var allUrlRegex = _to_consumable_array(pageUrls).concat(_to_consumable_array(appDirUrls));
|
|
203
243
|
return {
|
|
204
244
|
JSXOpeningElement: function JSXOpeningElement(node) {
|
|
245
|
+
var _getStaticValue;
|
|
205
246
|
if (node.name.name !== 'a') {
|
|
206
247
|
return;
|
|
207
248
|
}
|
|
@@ -211,13 +252,13 @@ var _default = (0, _definerule.defineRule)({
|
|
|
211
252
|
var target = node.attributes.find(function(attr) {
|
|
212
253
|
return attr.type === 'JSXAttribute' && attr.name.name === 'target';
|
|
213
254
|
});
|
|
214
|
-
if (target && target.value.value === '_blank') {
|
|
255
|
+
if ((target === null || target === void 0 ? void 0 : target.value) && (target.value.type === 'Literal' ? target.value.value === '_blank' : target.value.type === 'JSXExpressionContainer' && ((_getStaticValue = (0, _eslintutils.getStaticValue)(target.value.expression, context.sourceCode.getScope(node))) === null || _getStaticValue === void 0 ? void 0 : _getStaticValue.value) === '_blank')) {
|
|
215
256
|
return;
|
|
216
257
|
}
|
|
217
258
|
var href = node.attributes.find(function(attr) {
|
|
218
259
|
return attr.type === 'JSXAttribute' && attr.name.name === 'href';
|
|
219
260
|
});
|
|
220
|
-
if (!href || href
|
|
261
|
+
if (!(href === null || href === void 0 ? void 0 : href.value)) {
|
|
221
262
|
return;
|
|
222
263
|
}
|
|
223
264
|
var hasDownloadAttr = node.attributes.find(function(attr) {
|
|
@@ -226,19 +267,46 @@ var _default = (0, _definerule.defineRule)({
|
|
|
226
267
|
if (hasDownloadAttr) {
|
|
227
268
|
return;
|
|
228
269
|
}
|
|
229
|
-
var
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
270
|
+
var expression = href.value.type === 'JSXExpressionContainer' ? href.value.expression : href.value;
|
|
271
|
+
var hrefPaths = getHrefValues(expression, context.sourceCode);
|
|
272
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
273
|
+
try {
|
|
274
|
+
var _loop = function() {
|
|
275
|
+
var value = _step.value;
|
|
276
|
+
if (typeof value !== 'string') return "continue";
|
|
277
|
+
var hrefPath = (0, _url.normalizeURL)(value);
|
|
278
|
+
// Outgoing links are ignored.
|
|
279
|
+
if (/^(https?:\/\/|\/\/)/.test(hrefPath)) return "continue";
|
|
280
|
+
if (allUrlRegex.some(function(foundUrl) {
|
|
281
|
+
return foundUrl.test(hrefPath);
|
|
282
|
+
})) {
|
|
283
|
+
context.report({
|
|
284
|
+
node: node,
|
|
285
|
+
message: "Do not use an `<a>` element to navigate to `".concat(hrefPath, "`. Use `<Link />` from `next/link` instead. See: ").concat(url)
|
|
286
|
+
});
|
|
287
|
+
// A mapped anchor can have several internal destinations.
|
|
288
|
+
// Report the element once, using the first matching destination.
|
|
289
|
+
return "break";
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
for(var _iterator = hrefPaths[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
293
|
+
var _ret = _loop();
|
|
294
|
+
if (_ret === "break") break;
|
|
240
295
|
}
|
|
241
|
-
})
|
|
296
|
+
} catch (err) {
|
|
297
|
+
_didIteratorError = true;
|
|
298
|
+
_iteratorError = err;
|
|
299
|
+
} finally{
|
|
300
|
+
try {
|
|
301
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
302
|
+
_iterator.return();
|
|
303
|
+
}
|
|
304
|
+
} finally{
|
|
305
|
+
if (_didIteratorError) {
|
|
306
|
+
throw _iteratorError;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
242
310
|
}
|
|
243
311
|
};
|
|
244
312
|
}
|