@navikt/aksel 7.14.2 → 7.15.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/dist/codemod/migrations.js +17 -0
- package/dist/codemod/transforms/darkside/darkside.utils.js +120 -0
- package/dist/codemod/transforms/darkside/primitives-spacing/spacing.js +100 -0
- package/dist/codemod/transforms/darkside/token-spacing/spacing.js +19 -0
- package/dist/codemod/transforms/darkside/token-spacing-js/spacing.js +28 -0
- package/package.json +2 -2
|
@@ -89,6 +89,23 @@ exports.migrations = {
|
|
|
89
89
|
warning: "Remember to update use of `variant`-prop to match previous use of colors. If needed the component exposes css-variables for custom overrides",
|
|
90
90
|
},
|
|
91
91
|
],
|
|
92
|
+
darkside: [
|
|
93
|
+
{
|
|
94
|
+
description: "Updates all Primitives to use new `space`-tokens. (Works with old and new system)",
|
|
95
|
+
value: "primitive-spacing",
|
|
96
|
+
path: "darkside/primitives-spacing/spacing",
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
description: "Updates css, scss and less-variables to use new `space`-tokens. (Works with old and new system)",
|
|
100
|
+
value: "token-spacing",
|
|
101
|
+
path: "darkside/token-spacing/spacing",
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
description: "Updates js-tokens to use new `space`-tokens. (Works with old and new system)",
|
|
105
|
+
value: "token-spacing-js",
|
|
106
|
+
path: "darkside/token-spacing-js/spacing",
|
|
107
|
+
},
|
|
108
|
+
],
|
|
92
109
|
};
|
|
93
110
|
function getMigrationPath(str) {
|
|
94
111
|
var _a;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.legacySpacingTokenMap = void 0;
|
|
4
|
+
exports.findComponentImport = findComponentImport;
|
|
5
|
+
exports.findJSXElement = findJSXElement;
|
|
6
|
+
exports.findProp = findProp;
|
|
7
|
+
exports.convertSpacingToSpace = convertSpacingToSpace;
|
|
8
|
+
/**
|
|
9
|
+
* Finds a component import, accounting for sub-components and aliases.
|
|
10
|
+
* Returns the local name of the component. If the component is not found, returns null.
|
|
11
|
+
*/
|
|
12
|
+
function findComponentImport(input) {
|
|
13
|
+
const { j, file, name: _name, packageType = "react" } = input;
|
|
14
|
+
/* Account for sub-components */
|
|
15
|
+
const name = _name.includes(".") ? _name.split(".")[0] : _name;
|
|
16
|
+
const root = j(file.source);
|
|
17
|
+
let foundName = null;
|
|
18
|
+
root.find(j.ImportDeclaration).forEach((path) => {
|
|
19
|
+
if (packageType === "react" && !isAkselReactImport(path)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (packageType === "tokens" && !isAkselTokensImport(path)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
path.node.specifiers.forEach((specifier) => {
|
|
26
|
+
if (specifier.type === "ImportSpecifier" &&
|
|
27
|
+
specifier.imported.name === name) {
|
|
28
|
+
foundName = specifier.local
|
|
29
|
+
? specifier.local.name
|
|
30
|
+
: specifier.imported.name;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return foundName;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Finds a JSX element in the AST, accounting for sub-components.
|
|
38
|
+
*/
|
|
39
|
+
function findJSXElement(input) {
|
|
40
|
+
const { root, j, name, originalName } = input;
|
|
41
|
+
const isSubComponent = originalName.includes(".");
|
|
42
|
+
const openingElement = (isSubComponent
|
|
43
|
+
? {
|
|
44
|
+
name: {
|
|
45
|
+
type: "JSXMemberExpression",
|
|
46
|
+
object: {
|
|
47
|
+
name,
|
|
48
|
+
},
|
|
49
|
+
property: {
|
|
50
|
+
name: originalName.split(".")[1],
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
: {
|
|
55
|
+
name: {
|
|
56
|
+
type: "JSXIdentifier",
|
|
57
|
+
name,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
return root.find(j.JSXElement, {
|
|
61
|
+
openingElement,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Finds a prop in a JSX element.
|
|
66
|
+
*/
|
|
67
|
+
function findProp(input) {
|
|
68
|
+
const { j, path, name } = input;
|
|
69
|
+
return j(path).find(j.JSXAttribute, {
|
|
70
|
+
name: {
|
|
71
|
+
name,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Checks if an import is from @navikt/ds-react.
|
|
77
|
+
*/
|
|
78
|
+
function isAkselReactImport(path) {
|
|
79
|
+
const importSource = path.node.source.value;
|
|
80
|
+
return (typeof importSource === "string" &&
|
|
81
|
+
importSource.startsWith("@navikt/ds-react"));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Checks if an import is from @navikt/ds-tokens.
|
|
85
|
+
*/
|
|
86
|
+
function isAkselTokensImport(path) {
|
|
87
|
+
const importSource = path.node.source.value;
|
|
88
|
+
return (typeof importSource === "string" &&
|
|
89
|
+
importSource.startsWith("@navikt/ds-tokens"));
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Maps old spacing-token values to new space-tokens
|
|
93
|
+
*/
|
|
94
|
+
const legacySpacingTokenMap = new Map([
|
|
95
|
+
["32", "128"],
|
|
96
|
+
["24", "96"],
|
|
97
|
+
["20", "80"],
|
|
98
|
+
["18", "72"],
|
|
99
|
+
["16", "64"],
|
|
100
|
+
["14", "56"],
|
|
101
|
+
["12", "48"],
|
|
102
|
+
["11", "44"],
|
|
103
|
+
["10", "40"],
|
|
104
|
+
["9", "36"],
|
|
105
|
+
["8", "32"],
|
|
106
|
+
["7", "28"],
|
|
107
|
+
["6", "24"],
|
|
108
|
+
["5", "20"],
|
|
109
|
+
["4", "16"],
|
|
110
|
+
["3", "12"],
|
|
111
|
+
["2", "8"],
|
|
112
|
+
["1-alt", "6"],
|
|
113
|
+
["1", "4"],
|
|
114
|
+
["05", "2"],
|
|
115
|
+
["0", "0"],
|
|
116
|
+
]);
|
|
117
|
+
exports.legacySpacingTokenMap = legacySpacingTokenMap;
|
|
118
|
+
function convertSpacingToSpace(spacing) {
|
|
119
|
+
return legacySpacingTokenMap.get(spacing) || null;
|
|
120
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = transformer;
|
|
4
|
+
const lineterminator_1 = require("../../../utils/lineterminator");
|
|
5
|
+
const darkside_utils_1 = require("../darkside.utils");
|
|
6
|
+
function transformer(file, api) {
|
|
7
|
+
const j = api.jscodeshift;
|
|
8
|
+
const root = j(file.source);
|
|
9
|
+
/**
|
|
10
|
+
* Migrate Primitives to `space` from `spacing`
|
|
11
|
+
*/
|
|
12
|
+
const primitives = [
|
|
13
|
+
"Box",
|
|
14
|
+
"Box.New",
|
|
15
|
+
"BoxNew",
|
|
16
|
+
"Hgrid",
|
|
17
|
+
"Stack",
|
|
18
|
+
"HStack",
|
|
19
|
+
"VStack",
|
|
20
|
+
"Bleed",
|
|
21
|
+
];
|
|
22
|
+
const affectedProps = [
|
|
23
|
+
"padding",
|
|
24
|
+
"paddingInline",
|
|
25
|
+
"paddingBlock",
|
|
26
|
+
"margin",
|
|
27
|
+
"marginInline",
|
|
28
|
+
"marginBlock",
|
|
29
|
+
"inset",
|
|
30
|
+
"top",
|
|
31
|
+
"right",
|
|
32
|
+
"bottom",
|
|
33
|
+
"left",
|
|
34
|
+
"gap",
|
|
35
|
+
];
|
|
36
|
+
for (const primitive of primitives) {
|
|
37
|
+
const name = (0, darkside_utils_1.findComponentImport)({ file, j, name: primitive });
|
|
38
|
+
if (!name) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
(0, darkside_utils_1.findJSXElement)({
|
|
42
|
+
root,
|
|
43
|
+
j,
|
|
44
|
+
name,
|
|
45
|
+
originalName: primitive,
|
|
46
|
+
}).forEach((path) => {
|
|
47
|
+
for (const prop of affectedProps) {
|
|
48
|
+
(0, darkside_utils_1.findProp)({ j, path, name: prop }).forEach((attr) => {
|
|
49
|
+
const attrValue = attr.value.value;
|
|
50
|
+
if (attrValue.type === "StringLiteral") {
|
|
51
|
+
/* padding="32" */
|
|
52
|
+
attrValue.value = convertSpacingToSpace(attrValue.value);
|
|
53
|
+
}
|
|
54
|
+
else if (attrValue.type === "JSXExpressionContainer") {
|
|
55
|
+
/* padding={{xs: "16", sm: "32"}} */
|
|
56
|
+
const expression = attrValue.expression;
|
|
57
|
+
if (expression.type === "ObjectExpression") {
|
|
58
|
+
/* xs, md, sm */
|
|
59
|
+
expression.properties.forEach((property) => {
|
|
60
|
+
if (property.type === "ObjectProperty") {
|
|
61
|
+
if (property.value.type === "StringLiteral") {
|
|
62
|
+
property.value.value = convertSpacingToSpace(property.value.value);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return root.toSource((0, lineterminator_1.getLineTerminator)(file.source));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Takes an old valid spacing-token and returns the new converted space-token
|
|
76
|
+
* oldValue: "8", "8 10", "8 auto", "auto auto", "full px"
|
|
77
|
+
* @returns "space-32", "space-32 space-40"
|
|
78
|
+
*/
|
|
79
|
+
function convertSpacingToSpace(oldValue) {
|
|
80
|
+
const spacingTokens = oldValue.split(" ");
|
|
81
|
+
const newSpacing = [];
|
|
82
|
+
for (const spacingToken of spacingTokens) {
|
|
83
|
+
if (spacingToken === "px") {
|
|
84
|
+
/* We replace "px" with new `space-1` */
|
|
85
|
+
newSpacing.push(`space-1`);
|
|
86
|
+
}
|
|
87
|
+
else if (["auto", "full", "px"].includes(spacingToken) ||
|
|
88
|
+
spacingToken.startsWith("space-")) {
|
|
89
|
+
newSpacing.push(spacingToken);
|
|
90
|
+
}
|
|
91
|
+
else if (!darkside_utils_1.legacySpacingTokenMap.get(spacingToken)) {
|
|
92
|
+
console.warn(`Possibly invalid spacing token found: ${spacingToken}\n`);
|
|
93
|
+
newSpacing.push(spacingToken);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
newSpacing.push(`space-${darkside_utils_1.legacySpacingTokenMap.get(spacingToken)}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return newSpacing.join(" ");
|
|
100
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = transformer;
|
|
4
|
+
const translate_token_1 = require("../../../utils/translate-token");
|
|
5
|
+
const darkside_utils_1 = require("../darkside.utils");
|
|
6
|
+
function transformer(file) {
|
|
7
|
+
let src = file.source;
|
|
8
|
+
darkside_utils_1.legacySpacingTokenMap.forEach((newVar, oldVar) => {
|
|
9
|
+
const oldCSSVar = `--a-spacing-${oldVar}`;
|
|
10
|
+
const newCSSVar = `--a-space-${newVar}`;
|
|
11
|
+
const CSSRgx = new RegExp("(" + oldCSSVar + ")", "gm");
|
|
12
|
+
const SCSSRgx = new RegExp("(\\" + (0, translate_token_1.translateToken)(oldCSSVar, "scss") + ")", "gm");
|
|
13
|
+
const LESSRgx = new RegExp("(" + (0, translate_token_1.translateToken)(oldCSSVar, "less") + ")", "gm");
|
|
14
|
+
src = src.replace(CSSRgx, newCSSVar);
|
|
15
|
+
src = src.replace(SCSSRgx, (0, translate_token_1.translateToken)(newCSSVar, "scss"));
|
|
16
|
+
src = src.replace(LESSRgx, (0, translate_token_1.translateToken)(newCSSVar, "less"));
|
|
17
|
+
});
|
|
18
|
+
return src;
|
|
19
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = transformer;
|
|
4
|
+
const lineterminator_1 = require("../../../utils/lineterminator");
|
|
5
|
+
const translate_token_1 = require("../../../utils/translate-token");
|
|
6
|
+
const darkside_utils_1 = require("../darkside.utils");
|
|
7
|
+
function transformer(file, api) {
|
|
8
|
+
const j = api.jscodeshift;
|
|
9
|
+
let root = j(file.source);
|
|
10
|
+
darkside_utils_1.legacySpacingTokenMap.forEach((newVar, oldVar) => {
|
|
11
|
+
const oldCSSVar = (0, translate_token_1.translateToken)(`--a-spacing-${oldVar}`, "js");
|
|
12
|
+
const newCSSVar = (0, translate_token_1.translateToken)(`--a-space-${newVar}`, "js");
|
|
13
|
+
const name = (0, darkside_utils_1.findComponentImport)({
|
|
14
|
+
file,
|
|
15
|
+
j,
|
|
16
|
+
name: oldCSSVar,
|
|
17
|
+
packageType: "tokens",
|
|
18
|
+
});
|
|
19
|
+
if (!name) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
let code = root.toSource((0, lineterminator_1.getLineTerminator)(file.source));
|
|
23
|
+
const rgx = new RegExp("(" + oldCSSVar + ")", "gm");
|
|
24
|
+
code = code.replace(rgx, newCSSVar);
|
|
25
|
+
root = j(code);
|
|
26
|
+
});
|
|
27
|
+
return root.toSource((0, lineterminator_1.getLineTerminator)(file.source));
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@navikt/aksel",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.15.0",
|
|
4
4
|
"description": "Aksel command line interface. Handles css-imports, codemods and more",
|
|
5
5
|
"author": "Aksel | Nav designsystem team",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://aksel.nav.no/grunnleggende/kode/kommandolinje",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@navikt/ds-css": "^7.
|
|
32
|
+
"@navikt/ds-css": "^7.15.0",
|
|
33
33
|
"axios": "1.7.4",
|
|
34
34
|
"chalk": "4.1.0",
|
|
35
35
|
"clipboardy": "^2.3.0",
|