@atlaskit/css 0.10.2 → 0.10.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @atlaskit/css
2
2
 
3
+ ## 0.10.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#133515](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/133515)
8
+ [`3e092526fea27`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/3e092526fea27) -
9
+ Fix codemod config.
10
+
11
+ ## 0.10.3
12
+
13
+ ### Patch Changes
14
+
15
+ - [#127093](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/127093)
16
+ [`1378ea7a99ce1`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/1378ea7a99ce1) -
17
+ Upgrades `jscodeshift` to handle generics properly.
18
+ - Updated dependencies
19
+
3
20
  ## 0.10.2
4
21
 
5
22
  ### Patch Changes
@@ -4,6 +4,7 @@ import type {
4
4
  default as core,
5
5
  FileInfo,
6
6
  ImportDeclaration,
7
+ Node,
7
8
  Options,
8
9
  } from 'jscodeshift';
9
10
 
@@ -92,6 +93,9 @@ function replaceXcssWithCssMap(
92
93
  specifier: string,
93
94
  ) {
94
95
  const cssMapProperties: core.ObjectProperty[] = [];
96
+ let firstXcssPath: ASTPath<core.CallExpression> | null = null;
97
+ let firstUsagePath: ASTPath<core.Node> | null = null;
98
+ const styleVariables = new Set<string>();
95
99
 
96
100
  source
97
101
  .find(j.CallExpression, {
@@ -114,15 +118,68 @@ function replaceXcssWithCssMap(
114
118
 
115
119
  if (variableDeclarator && variableDeclarator.type === 'VariableDeclarator') {
116
120
  const variableName = variableDeclarator.id.name; // e.g. buttonStyles
117
- const key = getCssMapKey(variableName); // buttonStyles -> button to put in cssMap as the key e.g. styles = cssMap({ button: { color: 'red' } });
121
+ styleVariables.add(variableName);
122
+ }
123
+ }
124
+ }
125
+ });
118
126
 
127
+ // find the first usage of any style variable
128
+ source.find(j.Identifier).forEach((path) => {
129
+ if (
130
+ styleVariables.has(path.node.name) &&
131
+ (!firstUsagePath ||
132
+ (path.node.loc?.start &&
133
+ firstUsagePath.node.loc?.start &&
134
+ (path.node.loc.start.line < firstUsagePath.node.loc.start.line ||
135
+ (path.node.loc.start.line === firstUsagePath.node.loc.start.line &&
136
+ path.node.loc.start.column < firstUsagePath.node.loc.start.column))))
137
+ ) {
138
+ firstUsagePath = path;
139
+ }
140
+ });
141
+
142
+ // find all xcss function calls
143
+ source
144
+ .find(j.CallExpression, {
145
+ callee: {
146
+ type: 'Identifier',
147
+ name: specifier,
148
+ },
149
+ })
150
+ .forEach((path) => {
151
+ const args = path.node.arguments;
152
+ // only process xcss calls that have a single object argument
153
+ if (args.length === 1 && args[0].type === 'ObjectExpression') {
154
+ // get the parent variable declaration that contains this xcss call
155
+ // e.g. const buttonStyles = xcss({...})
156
+ const parentVariableDeclaration = path.parentPath?.parentPath?.parentPath?.node;
157
+ if (parentVariableDeclaration && parentVariableDeclaration.type === 'VariableDeclaration') {
158
+ // find the variable declarator that initialises with this xcss call
159
+ // e.g. const buttonStyles = xcss({ color: 'red' });
160
+ const variableDeclarator = parentVariableDeclaration.declarations.find(
161
+ (declaration: core.VariableDeclarator) => declaration.init === path.node,
162
+ );
163
+
164
+ if (variableDeclarator && variableDeclarator.type === 'VariableDeclarator') {
165
+ // convert the variable name to a cssMap key (e.g. myStyles -> root)
166
+ const variableName = variableDeclarator.id.name;
167
+ const key = getCssMapKey(variableName);
168
+
169
+ // create a new cssMap property with the key and the processed styles
119
170
  const cssMapObject = j.objectProperty(
120
171
  j.identifier(key),
121
172
  ensureSelectorAmpersand(j, args[0]),
122
173
  );
123
174
  cssMapProperties.push(cssMapObject);
124
175
 
125
- j(path.parentPath.parentPath.parentPath).remove(); // remove original xcss object
176
+ // track the first xcss call
177
+ if (!firstXcssPath) {
178
+ firstXcssPath = path;
179
+ }
180
+
181
+ // remove the original xcss variable declaration since we'll replace it with cssMap
182
+ j(path.parentPath.parentPath.parentPath).remove();
126
183
  }
127
184
  }
128
185
  }
@@ -138,9 +195,41 @@ function replaceXcssWithCssMap(
138
195
  ),
139
196
  ]);
140
197
 
141
- // insert the cssMap var after all imports
142
- const lastImportIndex = source.find(j.ImportDeclaration).size();
143
- source.get().node.program.body.splice(lastImportIndex, 0, cssMapVariableDeclaration);
198
+ // insert the cssMap declaration before its first usage
199
+ if (firstUsagePath) {
200
+ const programBody = source.get().node.program.body;
201
+ const firstUsageIndex = programBody.findIndex((node: Node) => {
202
+ const nodeStart = node.loc?.start;
203
+ const usageStart = firstUsagePath?.node.loc?.start;
204
+
205
+ if (!nodeStart || !usageStart) {
206
+ return false;
207
+ }
208
+
209
+ return (
210
+ nodeStart.line > usageStart.line ||
211
+ (nodeStart.line === usageStart.line && nodeStart.column > usageStart.column)
212
+ );
213
+ });
214
+
215
+ if (firstUsageIndex !== -1) {
216
+ // find the last import or variable declaration before the first usage
217
+ let insertIndex = firstUsageIndex;
218
+ while (insertIndex > 0) {
219
+ const node = programBody[insertIndex - 1];
220
+ if (
221
+ node.type === 'ImportDeclaration' ||
222
+ node.type === 'VariableDeclaration' ||
223
+ node.type === 'TypeAlias' ||
224
+ node.type === 'InterfaceDeclaration'
225
+ ) {
226
+ break;
227
+ }
228
+ insertIndex--;
229
+ }
230
+ programBody.splice(insertIndex, 0, cssMapVariableDeclaration);
231
+ }
232
+ }
144
233
  }
145
234
 
146
235
  // update the xcss prop references to use the new cssMap object
@@ -4,6 +4,7 @@ const config = {
4
4
  presets: {
5
5
  'primitives-emotion-to-compiled': primitivesEmotionToCompiled,
6
6
  },
7
+ dependencies: ['@emotion/react'],
7
8
  };
8
9
 
9
10
  export default config;
@@ -25,7 +25,7 @@ Emotion and Compiled, such as dynamic styles or imports. Please use the
25
25
  We have a codemod to assist in migrations from `xcss()` to `@atlaskit/css`.
26
26
 
27
27
  ```sh
28
- npx @hypermod/cli --packages @atlaskit/css@primitives-emotion-to-compiled ./path/to/folder
28
+ npx @hypermod/cli --packages @atlaskit/css#primitives-emotion-to-compiled ./path/to/folder
29
29
  ```
30
30
 
31
31
  The codemod should migrate something like this:
@@ -18,7 +18,7 @@ import UnboundedExample from '../../examples/constellation/unbounded';
18
18
 
19
19
  `@atlaskit/css` is the replacement for `@atlaskit/primitives.xcss`. It serves as a bounded styling
20
20
  library for use with native HTML elements and the Atlassian Design System, including
21
- [primitive components](/components/primitives).
21
+ [primitive components](/components/primitives/overview).
22
22
 
23
23
  Built on [Compiled CSS-in-JS](https://compiledcssinjs.com/), it provides a performant, static
24
24
  styling solution with some syntax changes. Notably, dynamic styles and certain imports/exports may
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/css",
3
- "version": "0.10.2",
3
+ "version": "0.10.4",
4
4
  "description": "Style components backed by Atlassian Design System design tokens powered by Compiled CSS-in-JS.",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -57,13 +57,13 @@
57
57
  },
58
58
  "devDependencies": {
59
59
  "@af/visual-regression": "^1.3.0",
60
- "@atlaskit/button": "^21.1.0",
60
+ "@atlaskit/button": "^22.0.0",
61
61
  "@atlaskit/ds-lib": "^4.0.0",
62
62
  "@atlaskit/primitives": "^14.2.0",
63
63
  "@emotion/react": "^11.7.1",
64
64
  "@testing-library/react": "^13.4.0",
65
65
  "@types/jscodeshift": "^0.11.0",
66
- "jscodeshift": "^0.13.0",
66
+ "jscodeshift": "^17.0.0",
67
67
  "react-dom": "^18.2.0",
68
68
  "typescript": "~5.4.2"
69
69
  },