@next/codemod 15.5.2 → 15.6.0-canary.0
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/package.json
CHANGED
|
@@ -17,19 +17,21 @@ function transformer(file, _api) {
|
|
|
17
17
|
},
|
|
18
18
|
});
|
|
19
19
|
if (metadataExport.size() !== 1) {
|
|
20
|
-
return;
|
|
20
|
+
return file.source;
|
|
21
21
|
}
|
|
22
22
|
const metadataObject = metadataExport.find(j.ObjectExpression).get(0).node;
|
|
23
23
|
if (!metadataObject) {
|
|
24
24
|
console.error('Could not find metadata object');
|
|
25
|
-
return;
|
|
25
|
+
return file.source;
|
|
26
26
|
}
|
|
27
27
|
let metadataProperties = metadataObject.properties;
|
|
28
28
|
let viewportProperties;
|
|
29
|
+
let hasChanges = false;
|
|
29
30
|
const viewport = metadataProperties.find((prop) => prop.key.name === 'viewport');
|
|
30
31
|
if (viewport) {
|
|
31
32
|
viewportProperties = viewport.value.properties;
|
|
32
33
|
metadataProperties = metadataProperties.filter((prop) => prop.key.name !== 'viewport');
|
|
34
|
+
hasChanges = true;
|
|
33
35
|
}
|
|
34
36
|
else {
|
|
35
37
|
viewportProperties = [];
|
|
@@ -38,11 +40,17 @@ function transformer(file, _api) {
|
|
|
38
40
|
if (colorScheme) {
|
|
39
41
|
viewportProperties.push(colorScheme);
|
|
40
42
|
metadataProperties = metadataProperties.filter((prop) => prop.key.name !== 'colorScheme');
|
|
43
|
+
hasChanges = true;
|
|
41
44
|
}
|
|
42
45
|
const themeColor = metadataProperties.find((prop) => prop.key.name === 'themeColor');
|
|
43
46
|
if (themeColor) {
|
|
44
47
|
viewportProperties.push(themeColor);
|
|
45
48
|
metadataProperties = metadataProperties.filter((prop) => prop.key.name !== 'themeColor');
|
|
49
|
+
hasChanges = true;
|
|
50
|
+
}
|
|
51
|
+
// Only apply changes if there were actual modifications
|
|
52
|
+
if (!hasChanges) {
|
|
53
|
+
return file.source;
|
|
46
54
|
}
|
|
47
55
|
// Update the metadata export
|
|
48
56
|
metadataExport
|
package/transforms/new-link.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// It might insert extra
|
|
2
|
+
// It might insert extra parens for JSX components
|
|
3
3
|
// x-ref: https://github.com/facebook/jscodeshift/issues/534
|
|
4
4
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
5
|
exports.default = transformer;
|
|
6
6
|
const parser_1 = require("../lib/parser");
|
|
7
|
+
const utils_1 = require("./lib/async-request-api/utils");
|
|
7
8
|
function transformer(file, _api) {
|
|
8
9
|
const j = (0, parser_1.createParserFromPath)(file.path);
|
|
9
10
|
const $j = j(file.source);
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
.forEach((path) => {
|
|
11
|
+
let hasChanges = false;
|
|
12
|
+
$j.find(j.ImportDeclaration, { source: { value: 'next/link' } }).forEach((path) => {
|
|
13
13
|
const defaultImport = j(path).find(j.ImportDefaultSpecifier);
|
|
14
14
|
if (defaultImport.size() === 0) {
|
|
15
15
|
return;
|
|
@@ -22,29 +22,21 @@ function transformer(file, _api) {
|
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
24
|
const linkElements = $j.findJSXElements(variableName);
|
|
25
|
-
const hasStylesJSX = $j.findJSXElements('style').some((stylePath) => {
|
|
26
|
-
const $style = j(stylePath);
|
|
27
|
-
const hasJSXProp = $style.find(j.JSXAttribute, { name: { name: 'jsx' } }).size() !== 0;
|
|
28
|
-
return hasJSXProp;
|
|
29
|
-
});
|
|
30
25
|
linkElements.forEach((linkPath) => {
|
|
31
|
-
const $link = j(linkPath)
|
|
32
|
-
// Exclude links with `legacybehavior` prop from modification
|
|
33
|
-
return (j(childPath)
|
|
34
|
-
.find(j.JSXAttribute, { name: { name: 'legacyBehavior' } })
|
|
35
|
-
.size() === 0);
|
|
36
|
-
});
|
|
26
|
+
const $link = j(linkPath);
|
|
37
27
|
if ($link.size() === 0) {
|
|
38
28
|
return;
|
|
39
29
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
30
|
+
const $legacyBehaviorProps = $link.find(j.JSXAttribute, {
|
|
31
|
+
name: { type: 'JSXIdentifier', name: 'legacyBehavior' },
|
|
32
|
+
});
|
|
33
|
+
$legacyBehaviorProps.remove();
|
|
34
|
+
hasChanges ||= $legacyBehaviorProps.size() > 0;
|
|
35
|
+
const $passHrefProps = $link.find(j.JSXAttribute, {
|
|
36
|
+
name: { type: 'JSXIdentifier', name: 'passHref' },
|
|
37
|
+
});
|
|
38
|
+
$passHrefProps.remove();
|
|
39
|
+
hasChanges ||= $passHrefProps.size() > 0;
|
|
48
40
|
const linkChildrenNodes = $link.get('children');
|
|
49
41
|
// Text-only link children are already correct with the new behavior
|
|
50
42
|
// `next/link` would previously auto-wrap typeof 'string' children already
|
|
@@ -59,34 +51,48 @@ function transformer(file, _api) {
|
|
|
59
51
|
return (j(childPath).find(j.JSXOpeningElement).get('name').get('name')
|
|
60
52
|
.value === 'a');
|
|
61
53
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
54
|
+
if ($childrenWithA.length === 0) {
|
|
55
|
+
if ($legacyBehaviorProps.length > 0) {
|
|
56
|
+
linkPath.node.children.unshift(j.jsxText('\n'), j.jsxExpressionContainer.from({
|
|
57
|
+
expression: j.jsxEmptyExpression.from({
|
|
58
|
+
comments: [
|
|
59
|
+
j.commentBlock.from({
|
|
60
|
+
value: ` ${utils_1.NEXT_CODEMOD_ERROR_PREFIX} This Link previously used the now removed \`legacyBehavior\` prop, and has a child that might not be an anchor. The codemod bailed out of lifting the child props to the Link. Check that the child component does not render an anchor, and potentially move the props manually to Link. `,
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
63
|
+
}),
|
|
64
|
+
}));
|
|
65
|
+
hasChanges = true;
|
|
66
|
+
}
|
|
68
67
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
68
|
+
else {
|
|
69
|
+
const props = $childrenWithA.get('attributes').value;
|
|
70
|
+
const hasProps = props.length > 0;
|
|
71
|
+
if (hasProps) {
|
|
72
|
+
// Add only unique props to <Link> (skip duplicate props)
|
|
73
|
+
const linkPropNames = $link
|
|
74
|
+
.get('attributes')
|
|
75
|
+
.value.map((linkProp) => linkProp?.name?.name);
|
|
76
|
+
const uniqueProps = [];
|
|
77
|
+
props.forEach((anchorProp) => {
|
|
78
|
+
if (!linkPropNames.includes(anchorProp?.name?.name)) {
|
|
79
|
+
uniqueProps.push(anchorProp);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
$link.get('attributes').value.push(...uniqueProps);
|
|
83
|
+
// Remove props from <a>
|
|
84
|
+
props.length = 0;
|
|
85
|
+
hasChanges = true;
|
|
86
|
+
}
|
|
87
|
+
const childrenProps = $childrenWithA.get('children');
|
|
88
|
+
$childrenWithA.replaceWith(childrenProps.value);
|
|
89
|
+
hasChanges = true;
|
|
85
90
|
}
|
|
86
|
-
const childrenProps = $childrenWithA.get('children');
|
|
87
|
-
$childrenWithA.replaceWith(childrenProps.value);
|
|
88
91
|
});
|
|
89
|
-
})
|
|
90
|
-
|
|
92
|
+
});
|
|
93
|
+
if (hasChanges) {
|
|
94
|
+
return $j.toSource();
|
|
95
|
+
}
|
|
96
|
+
return file.source;
|
|
91
97
|
}
|
|
92
98
|
//# sourceMappingURL=new-link.js.map
|