@next/eslint-plugin-next 16.3.0-canary.1 → 16.3.0-canary.100
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.
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ declare const plugin: {
|
|
|
18
18
|
'no-head-import-in-document': Rule.RuleModule;
|
|
19
19
|
'no-html-link-for-pages': Rule.RuleModule;
|
|
20
20
|
'no-img-element': Rule.RuleModule;
|
|
21
|
+
'no-location-assign-relative-destination': Rule.RuleModule;
|
|
21
22
|
'no-page-custom-font': Rule.RuleModule;
|
|
22
23
|
'no-script-component-in-head': Rule.RuleModule;
|
|
23
24
|
'no-styled-jsx-in-document': Rule.RuleModule;
|
|
@@ -50,6 +51,7 @@ export declare const rules: {
|
|
|
50
51
|
'no-head-import-in-document': Rule.RuleModule;
|
|
51
52
|
'no-html-link-for-pages': Rule.RuleModule;
|
|
52
53
|
'no-img-element': Rule.RuleModule;
|
|
54
|
+
'no-location-assign-relative-destination': Rule.RuleModule;
|
|
53
55
|
'no-page-custom-font': Rule.RuleModule;
|
|
54
56
|
'no-script-component-in-head': Rule.RuleModule;
|
|
55
57
|
'no-styled-jsx-in-document': Rule.RuleModule;
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,7 @@ var _noheadelement = /*#__PURE__*/ _interop_require_default(require("./rules/no-
|
|
|
33
33
|
var _noheadimportindocument = /*#__PURE__*/ _interop_require_default(require("./rules/no-head-import-in-document"));
|
|
34
34
|
var _nohtmllinkforpages = /*#__PURE__*/ _interop_require_default(require("./rules/no-html-link-for-pages"));
|
|
35
35
|
var _noimgelement = /*#__PURE__*/ _interop_require_default(require("./rules/no-img-element"));
|
|
36
|
+
var _nolocationassignrelativedestination = /*#__PURE__*/ _interop_require_default(require("./rules/no-location-assign-relative-destination"));
|
|
36
37
|
var _nopagecustomfont = /*#__PURE__*/ _interop_require_default(require("./rules/no-page-custom-font"));
|
|
37
38
|
var _noscriptcomponentinhead = /*#__PURE__*/ _interop_require_default(require("./rules/no-script-component-in-head"));
|
|
38
39
|
var _nostyledjsxindocument = /*#__PURE__*/ _interop_require_default(require("./rules/no-styled-jsx-in-document"));
|
|
@@ -84,6 +85,7 @@ var recommendedRules = {
|
|
|
84
85
|
'@next/next/no-head-element': 'warn',
|
|
85
86
|
'@next/next/no-html-link-for-pages': 'warn',
|
|
86
87
|
'@next/next/no-img-element': 'warn',
|
|
88
|
+
'@next/next/no-location-assign-relative-destination': 'warn',
|
|
87
89
|
'@next/next/no-page-custom-font': 'warn',
|
|
88
90
|
'@next/next/no-styled-jsx-in-document': 'warn',
|
|
89
91
|
'@next/next/no-sync-scripts': 'warn',
|
|
@@ -121,6 +123,7 @@ var plugin = {
|
|
|
121
123
|
'no-head-import-in-document': _noheadimportindocument.default,
|
|
122
124
|
'no-html-link-for-pages': _nohtmllinkforpages.default,
|
|
123
125
|
'no-img-element': _noimgelement.default,
|
|
126
|
+
'no-location-assign-relative-destination': _nolocationassignrelativedestination.default,
|
|
124
127
|
'no-page-custom-font': _nopagecustomfont.default,
|
|
125
128
|
'no-script-component-in-head': _noscriptcomponentinhead.default,
|
|
126
129
|
'no-styled-jsx-in-document': _nostyledjsxindocument.default,
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
var _definerule = require("../utils/define-rule");
|
|
12
|
+
var _eslintutils = require("@eslint-community/eslint-utils");
|
|
13
|
+
var url = 'https://nextjs.org/docs/messages/no-location-assign-relative-destination';
|
|
14
|
+
var _default = (0, _definerule.defineRule)({
|
|
15
|
+
meta: {
|
|
16
|
+
docs: {
|
|
17
|
+
description: 'Prevent usage of `location.assign` or `location.href` assignment to navigate to internal Next.js pages.',
|
|
18
|
+
recommended: true,
|
|
19
|
+
url: url
|
|
20
|
+
},
|
|
21
|
+
type: 'problem',
|
|
22
|
+
schema: [],
|
|
23
|
+
messages: {
|
|
24
|
+
noLocationAssign: "Do not use `{{expression}}` to navigate to internal Next.js pages. Use `redirect()` in the render phase, or `useRouter().push()` in Client Components' event handlers instead. See: " + url
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
create: function create(context) {
|
|
28
|
+
var sourceCode = context.sourceCode;
|
|
29
|
+
var scopeManager = sourceCode.scopeManager;
|
|
30
|
+
if (!scopeManager) return {};
|
|
31
|
+
return {
|
|
32
|
+
// location.assign(...) / location['assign'](...)
|
|
33
|
+
// window.location.assign(...) / window.location['assign'](...)
|
|
34
|
+
// globalThis.location.assign(...) / globalThis.location['assign'](...)
|
|
35
|
+
CallExpression: function CallExpression(node) {
|
|
36
|
+
var callee = node.callee, args = node.arguments;
|
|
37
|
+
if (!isMemberExprWithNamedProperty(callee, 'assign')) return;
|
|
38
|
+
var rootIdentifier = getLocationRootIdentifier(callee.object);
|
|
39
|
+
if (!rootIdentifier) return;
|
|
40
|
+
if (!isGlobalReference(sourceCode, rootIdentifier)) return;
|
|
41
|
+
if (args.length < 1) return;
|
|
42
|
+
var firstArg = args[0];
|
|
43
|
+
if (firstArg.type === 'SpreadElement') return;
|
|
44
|
+
var value = getStaticStringPrefix(firstArg, sourceCode);
|
|
45
|
+
if (value !== null && isRelativeUrl(value)) {
|
|
46
|
+
context.report({
|
|
47
|
+
node: node,
|
|
48
|
+
messageId: 'noLocationAssign',
|
|
49
|
+
data: {
|
|
50
|
+
expression: sourceCode.getText(callee) + '()'
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
// location.href = '/path'
|
|
56
|
+
// window.location.href = '/path'
|
|
57
|
+
// globalThis.location.href = '/path'
|
|
58
|
+
AssignmentExpression: function AssignmentExpression(node) {
|
|
59
|
+
var left = node.left, right = node.right;
|
|
60
|
+
if (!isMemberExprWithNamedProperty(left, 'href')) return;
|
|
61
|
+
var rootIdentifier = getLocationRootIdentifier(left.object);
|
|
62
|
+
if (!rootIdentifier) return;
|
|
63
|
+
if (!isGlobalReference(sourceCode, rootIdentifier)) return;
|
|
64
|
+
var value = getStaticStringPrefix(right, sourceCode);
|
|
65
|
+
if (value !== null && isRelativeUrl(value)) {
|
|
66
|
+
context.report({
|
|
67
|
+
node: node,
|
|
68
|
+
messageId: 'noLocationAssign',
|
|
69
|
+
data: {
|
|
70
|
+
expression: sourceCode.getText(left)
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
var ABSOLUTE_URL_RE = /^(?:[a-z][\d+.a-z-]*:|\/\/)/i;
|
|
79
|
+
var GLOBAL_PREFIXES = new Set([
|
|
80
|
+
'window',
|
|
81
|
+
'globalThis',
|
|
82
|
+
'document',
|
|
83
|
+
'self'
|
|
84
|
+
]);
|
|
85
|
+
function isMemberExprWithNamedProperty(expr, name) {
|
|
86
|
+
if (expr.type !== 'MemberExpression') return false;
|
|
87
|
+
return expr.computed ? expr.property.type === 'Literal' && expr.property.value === name : expr.property.type === 'Identifier' && expr.property.name === name;
|
|
88
|
+
}
|
|
89
|
+
function isRelativeUrl(value) {
|
|
90
|
+
return !ABSOLUTE_URL_RE.test(value);
|
|
91
|
+
}
|
|
92
|
+
function getLocationRootIdentifier(node) {
|
|
93
|
+
if (node.type === 'Identifier' && node.name === 'location') {
|
|
94
|
+
return node;
|
|
95
|
+
}
|
|
96
|
+
if (node.type === 'MemberExpression' && node.object.type === 'Identifier' && GLOBAL_PREFIXES.has(node.object.name) && isMemberExprWithNamedProperty(node, 'location')) {
|
|
97
|
+
return node.object;
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
function isGlobalReference(sourceCode, node) {
|
|
102
|
+
if (!node) return false;
|
|
103
|
+
if (node.type !== 'Identifier') return false;
|
|
104
|
+
var variable = sourceCode.scopeManager.scopes[0].set.get(node.name);
|
|
105
|
+
if (!variable || variable.defs.length > 0) return false;
|
|
106
|
+
return variable.references.some(function(param) {
|
|
107
|
+
var identifier = param.identifier;
|
|
108
|
+
return identifier === node;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
function getStaticStringPrefix(node, sourceCode) {
|
|
112
|
+
var constantValue = (0, _eslintutils.getStringIfConstant)(node, sourceCode.getScope(node));
|
|
113
|
+
if (constantValue !== null) {
|
|
114
|
+
return constantValue;
|
|
115
|
+
}
|
|
116
|
+
if (node.type === 'TemplateLiteral' && node.quasis.length > 0) {
|
|
117
|
+
var _node_quasis__value_cooked;
|
|
118
|
+
return (_node_quasis__value_cooked = node.quasis[0].value.cooked) !== null && _node_quasis__value_cooked !== void 0 ? _node_quasis__value_cooked : node.quasis[0].value.raw;
|
|
119
|
+
}
|
|
120
|
+
if (node.type === 'BinaryExpression' && node.operator === '+') {
|
|
121
|
+
return getStaticStringPrefix(node.left, sourceCode);
|
|
122
|
+
}
|
|
123
|
+
if (node.type === 'Identifier') {
|
|
124
|
+
var variable = (0, _eslintutils.findVariable)(sourceCode.getScope(node), node);
|
|
125
|
+
if (!variable || variable.defs.length < 1) return null;
|
|
126
|
+
var def = variable.defs[variable.defs.length - 1];
|
|
127
|
+
if (def.type !== 'Variable') return null;
|
|
128
|
+
var readPos = node.range[0];
|
|
129
|
+
var _def_node_init;
|
|
130
|
+
var lastWriteExpr = (_def_node_init = def.node.init) !== null && _def_node_init !== void 0 ? _def_node_init : null;
|
|
131
|
+
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
132
|
+
try {
|
|
133
|
+
for(var _iterator = variable.references[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
134
|
+
var ref = _step.value;
|
|
135
|
+
if (ref.identifier.range[0] >= readPos) break;
|
|
136
|
+
if (ref.isWrite() && ref.writeExpr && ref.writeExpr !== def.node.init) {
|
|
137
|
+
lastWriteExpr = ref.writeExpr;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} catch (err) {
|
|
141
|
+
_didIteratorError = true;
|
|
142
|
+
_iteratorError = err;
|
|
143
|
+
} finally{
|
|
144
|
+
try {
|
|
145
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
146
|
+
_iterator.return();
|
|
147
|
+
}
|
|
148
|
+
} finally{
|
|
149
|
+
if (_didIteratorError) {
|
|
150
|
+
throw _iteratorError;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (!lastWriteExpr) return null;
|
|
155
|
+
return getStaticStringPrefix(lastWriteExpr, sourceCode);
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next/eslint-plugin-next",
|
|
3
|
-
"version": "16.3.0-canary.
|
|
3
|
+
"version": "16.3.0-canary.100",
|
|
4
4
|
"description": "ESLint plugin for Next.js.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,16 +13,17 @@
|
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
+
"@eslint-community/eslint-utils": "4.9.1",
|
|
16
17
|
"fast-glob": "3.3.1"
|
|
17
18
|
},
|
|
18
19
|
"devDependencies": {
|
|
19
20
|
"@types/eslint": "9.6.1",
|
|
21
|
+
"@types/estree": "1.0.8",
|
|
20
22
|
"eslint": "9.37.0"
|
|
21
23
|
},
|
|
22
24
|
"scripts": {
|
|
23
25
|
"dev": "pnpm build",
|
|
24
26
|
"build": "swc -d dist src && pnpm types",
|
|
25
|
-
"types": "tsc --project tsconfig.json --skipLibCheck --declaration --emitDeclarationOnly --esModuleInterop --declarationDir dist"
|
|
26
|
-
"prepublishOnly": "cd ../../ && turbo run build"
|
|
27
|
+
"types": "tsc --project tsconfig.json --skipLibCheck --declaration --emitDeclarationOnly --esModuleInterop --declarationDir dist"
|
|
27
28
|
}
|
|
28
|
-
}
|
|
29
|
+
}
|