bestax-migrate 2.32.0 → 2.33.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.
Files changed (28) hide show
  1. package/dist/sources/_shared/html/parse.d.ts +10 -0
  2. package/dist/sources/_shared/html/parse.d.ts.map +1 -1
  3. package/dist/sources/_shared/html/parse.js +13 -1
  4. package/dist/sources/bulma-classes/class-map.d.ts +41 -11
  5. package/dist/sources/bulma-classes/class-map.d.ts.map +1 -1
  6. package/dist/sources/bulma-classes/class-map.js +101 -4
  7. package/dist/sources/bulma-classes/glyph.d.ts +22 -7
  8. package/dist/sources/bulma-classes/glyph.d.ts.map +1 -1
  9. package/dist/sources/bulma-classes/glyph.js +50 -20
  10. package/dist/sources/bulma-classes/html-assets.d.ts +8 -1
  11. package/dist/sources/bulma-classes/html-assets.d.ts.map +1 -1
  12. package/dist/sources/bulma-classes/html-assets.js +35 -8
  13. package/dist/sources/bulma-classes/html-extensions.d.ts +69 -0
  14. package/dist/sources/bulma-classes/html-extensions.d.ts.map +1 -0
  15. package/dist/sources/bulma-classes/html-extensions.js +146 -0
  16. package/dist/sources/bulma-classes/html-rules.d.ts +5 -0
  17. package/dist/sources/bulma-classes/html-rules.d.ts.map +1 -1
  18. package/dist/sources/bulma-classes/html-rules.js +6 -0
  19. package/dist/sources/bulma-classes/html.d.ts +3 -2
  20. package/dist/sources/bulma-classes/html.d.ts.map +1 -1
  21. package/dist/sources/bulma-classes/html.js +143 -33
  22. package/dist/sources/bulma-classes/plan.d.ts +25 -2
  23. package/dist/sources/bulma-classes/plan.d.ts.map +1 -1
  24. package/dist/sources/bulma-classes/plan.js +108 -11
  25. package/dist/sources/bulma-classes/transform.d.ts +20 -2
  26. package/dist/sources/bulma-classes/transform.d.ts.map +1 -1
  27. package/dist/sources/bulma-classes/transform.js +113 -11
  28. package/package.json +2 -2
@@ -5,8 +5,9 @@
5
5
  * scope when a lowercase JSX element carries a `className` with a Bulma class
6
6
  * `class-map.ts` knows. Each such element goes through `plan()`, which
7
7
  * converts it only when the component renders exactly the same markup, but
8
- * for the exceptions it states (Font Awesome's hidden `<i>`, and a Bulma 0.9
9
- * helper v1 removed).
8
+ * for the exceptions it states (Font Awesome's hidden `<i>`, an icon in a
9
+ * link or button its own label names, Font Awesome 6's long name for a
10
+ * style, and a Bulma 0.9 helper v1 removed).
10
11
  *
11
12
  * Passes, in order:
12
13
  * 1. the shared stylesheet-import pass (a no-op under the default `keep`)
@@ -17,6 +18,9 @@
17
18
  * Next.js server component (bestax's components are client components)
18
19
  * 4. TODOs in reading order; conversions innermost first
19
20
  * 5. write the bestax import, after the file's last import
21
+ * 6. settle the TODOs an earlier run wrote: one this run writes again keeps
22
+ * its earlier copy, and one that asked for a re-run it no longer writes
23
+ * goes (`settleTodos`)
20
24
  */
21
25
  import path from 'node:path';
22
26
  import { addTodo, findAttr, forgetJsxParens, jsxNameParts, literalValueOf, makeAttr, removeAttr, renameElement, reprintDirectives, } from '../_shared/jsx-utils.js';
@@ -837,6 +841,22 @@ function labelOf(clickable, self, kindOf) {
837
841
  return 'text';
838
842
  return unsure ? undefined : 'none';
839
843
  }
844
+ /**
845
+ * Whether an <a> is a link, as `ElementFacts.clickableAround` says: true with
846
+ * an `href` written out as a string, false with neither an `href` nor a
847
+ * `role`, and undefined when that can't be told: an `href` written as an
848
+ * expression, which may render as none, a `role` of its own, or a spread,
849
+ * which may carry either.
850
+ */
851
+ function linkOf(clickable) {
852
+ if ((clickable.openingElement.attributes ?? []).some((attr) => attr.type === 'JSXSpreadAttribute')) {
853
+ return undefined;
854
+ }
855
+ const href = findAttr(clickable, 'href');
856
+ if (href)
857
+ return typeof attributeValue(href) === 'string' ? true : undefined;
858
+ return findAttr(clickable, 'role') ? undefined : false;
859
+ }
840
860
  /**
841
861
  * An element's content as one prop value: its one element, or its text, or
842
862
  * a fragment of them all, each text written as the string it renders.
@@ -892,6 +912,30 @@ function onlyChildOf(elementPath, bestaxLocals) {
892
912
  * the file, though it sits on the first element that would convert, so no
893
913
  * line of the file is where it came from.
894
914
  */
915
+ /**
916
+ * A `TODO(bestax-migrate)` line comment's message as a run compares it: the
917
+ * page's lines an HTML page's module adds after it left out, and a re-run it
918
+ * asks for on a named file read as any re-run. So a re-run on that module
919
+ * finds the TODOs it writes again in the ones the page's run wrote.
920
+ */
921
+ export function todoKey(text) {
922
+ return text
923
+ .replace(/^\s*TODO\(bestax-migrate\):\s*/, '')
924
+ .replace(/\s+\([^()]+\.html?:[\d, -]+\)$/, '')
925
+ .replace(/\bre-run on `[^`]+`/g, 're-run')
926
+ .trim();
927
+ }
928
+ function isTodoComment(comment) {
929
+ return ((comment.type === 'CommentLine' || comment.type === 'Line') &&
930
+ /^\s*TODO\(bestax-migrate\):/.test(comment.value));
931
+ }
932
+ /**
933
+ * The packages the library sources migrate from. A file that still imports
934
+ * one may hold that source's TODOs, which ask for a re-run of it, not of
935
+ * this one, so this one leaves every TODO there as it is. A new library
936
+ * source joins this list.
937
+ */
938
+ export const LIBRARY_SOURCES = /^(?:react-bulma-components|rbx|bloomer)(?:\/|$)/;
895
939
  export const FILE_RULES = [
896
940
  'jsx-runtime',
897
941
  'imports',
@@ -916,7 +960,60 @@ export default function transform(fileInfo, api, options = {}) {
916
960
  overrides: new WeakMap(),
917
961
  dirty: false,
918
962
  };
963
+ // The TODOs the file holds from an earlier run, and the messages this run
964
+ // writes, so a re-run can tell the ones it no longer writes.
965
+ const earlier = new Set();
966
+ const written = new Set();
967
+ root.find(j.Node).forEach((nodePath) => {
968
+ for (const comment of nodePath.node.comments ?? []) {
969
+ if (isTodoComment(comment))
970
+ earlier.add(comment);
971
+ }
972
+ });
973
+ const todo = (path, rule, message) => {
974
+ written.add(todoKey(message));
975
+ addTodo(ctx, path, rule, message);
976
+ };
977
+ /**
978
+ * A re-run's TODOs against the earlier run's: one it writes again keeps
979
+ * the earlier copy, which carries the page's lines when the file is a
980
+ * page's module. One it no longer writes that asked for a re-run, above
981
+ * JSX, is done (the element converts now, or changed), so it goes, unless
982
+ * the file still imports a library source, whose TODOs this one can't tell
983
+ * from its own.
984
+ */
985
+ const settleTodos = () => {
986
+ if (earlier.size === 0)
987
+ return;
988
+ const library = root
989
+ .find(j.ImportDeclaration)
990
+ .some((importPath) => LIBRARY_SOURCES.test(String(importPath.node.source.value)));
991
+ root.find(j.Node).forEach((nodePath) => {
992
+ const node = nodePath.node;
993
+ const comments = node.comments;
994
+ if (!comments?.some(isTodoComment))
995
+ return;
996
+ const holdsJsx = () => j(node).find(j.JSXElement).size() > 0 ||
997
+ j(node).find(j.JSXFragment).size() > 0;
998
+ const kept = comments.filter(comment => {
999
+ if (!isTodoComment(comment))
1000
+ return true;
1001
+ const key = todoKey(comment.value);
1002
+ if (!earlier.has(comment)) {
1003
+ return !comments.some(other => earlier.has(other) && todoKey(other.value) === key);
1004
+ }
1005
+ if (written.has(key) || library)
1006
+ return true;
1007
+ return !(/\bre-run\b/.test(key) && holdsJsx());
1008
+ });
1009
+ if (kept.length !== comments.length) {
1010
+ node.comments = kept;
1011
+ ctx.dirty = true;
1012
+ }
1013
+ });
1014
+ };
919
1015
  const print = () => {
1016
+ settleTodos();
920
1017
  if (!ctx.dirty)
921
1018
  return undefined;
922
1019
  forgetJsxParens(j, root);
@@ -1182,11 +1279,11 @@ export default function transform(fileInfo, api, options = {}) {
1182
1279
  '');
1183
1280
  };
1184
1281
  /**
1185
- * Where an element whose target renders a glyph sits, for its TODO: the
1186
- * classes the element directly around it renders, and the nearest <a> or
1187
- * <button> around it, reached through markup and bestax components only,
1188
- * since any other component between could render anything, with what
1189
- * names that.
1282
+ * Where an element whose target renders a glyph sits: the classes the
1283
+ * element directly around it renders, and the nearest <a> or <button>
1284
+ * around it, reached through markup and bestax components only, since any
1285
+ * other component between could render anything, with what names that and,
1286
+ * for an <a>, whether it is a link.
1190
1287
  */
1191
1288
  const glyphPlace = (elementPath) => {
1192
1289
  const parent = elementPath.parent;
@@ -1214,10 +1311,15 @@ export default function transform(fileInfo, api, options = {}) {
1214
1311
  return entries.some(buildsContent) ? 'opaque' : 'open';
1215
1312
  };
1216
1313
  const label = clickable && labelOf(clickable.node, elementPath.node, kindOf);
1314
+ const link = tag === 'a' && clickable ? linkOf(clickable.node) : undefined;
1217
1315
  return {
1218
1316
  ...(parentTokens && { parentTokens }),
1219
1317
  ...(clickable && {
1220
- clickableAround: { tag: tag, ...(label && { label }) },
1318
+ clickableAround: {
1319
+ tag: tag,
1320
+ ...(label && { label }),
1321
+ ...(link !== undefined && { link }),
1322
+ },
1221
1323
  }),
1222
1324
  };
1223
1325
  };
@@ -1441,7 +1543,7 @@ export default function transform(fileInfo, api, options = {}) {
1441
1543
  const converting = elements.some(element => element.converts);
1442
1544
  const block = (rule, message) => {
1443
1545
  if (converting)
1444
- addTodo(ctx, elements[0].path, rule, message);
1546
+ todo(elements[0].path, rule, message);
1445
1547
  for (const element of elements) {
1446
1548
  element.plan = { ...element.plan, conversion: null, fold: undefined };
1447
1549
  }
@@ -1484,8 +1586,8 @@ export default function transform(fileInfo, api, options = {}) {
1484
1586
  ctx.reserve = makeReserve(ctx, bound);
1485
1587
  const bestaxImportState = seedBestaxImport(ctx, root);
1486
1588
  for (const { path: elementPath, plan: result } of elements) {
1487
- for (const todo of result.todos) {
1488
- addTodo(ctx, elementPath, todo.rule, todo.message);
1589
+ for (const { rule, message } of result.todos) {
1590
+ todo(elementPath, rule, message);
1489
1591
  }
1490
1592
  }
1491
1593
  for (const { path: elementPath, plan: result } of [...elements].reverse()) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bestax-migrate",
3
- "version": "2.32.0",
3
+ "version": "2.33.0",
4
4
  "description": "Codemods for migrating existing React apps from other Bulma libraries to @allxsmith/bestax-bulma",
5
5
  "type": "module",
6
6
  "bin": {
@@ -40,7 +40,7 @@
40
40
  "jscodeshift": "^17.3.0",
41
41
  "parse5": "^7.3.0",
42
42
  "property-information": "^7.2.0",
43
- "@allxsmith/bestax-bulma": "^5.44.0"
43
+ "@allxsmith/bestax-bulma": "^5.45.1"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@jest/globals": "^30.0.0",