@pob/codemods 0.0.0 → 0.1.1

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 ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## [0.1.1](https://github.com/christophehurpeau/pob/compare/@pob/codemods@0.0.0...@pob/codemods@0.1.1) (2024-06-07)
7
+
8
+ Note: no notable changes
9
+
10
+
package/bin/migrate.mjs CHANGED
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { readdirSync } from 'node:fs';
4
- import { fileURLToPath } from 'node:url';
5
- import { glob } from 'glob';
6
- import runJscodeshift from '../lib/runJscodeshift.cjs';
3
+ import { readdirSync } from "node:fs";
4
+ import { fileURLToPath } from "node:url";
5
+ import { glob } from "glob";
6
+ import runJscodeshift from "../lib/runJscodeshift.cjs";
7
7
 
8
8
  const argv = process.argv.slice(2);
9
9
 
@@ -12,12 +12,12 @@ const globPaths = argv[1];
12
12
 
13
13
  if (!transformerName || !globPaths) {
14
14
  const transformerList = readdirSync(
15
- new URL('../lib/transforms/', import.meta.url),
15
+ new URL("../lib/transforms/", import.meta.url),
16
16
  );
17
17
  throw new Error(
18
18
  `Usage: pob-migrate <transformer-name> <glob-paths>\n${transformerList
19
19
  .map((t) => `- "${t.slice(0, -4)}"`)
20
- .join('\n')}`,
20
+ .join("\n")}`,
21
21
  );
22
22
  }
23
23
 
@@ -34,4 +34,4 @@ const res = await runJscodeshift(transformPath, paths, {});
34
34
  if (res.errors) {
35
35
  process.exitCode = 1;
36
36
  }
37
- console.log('Done!');
37
+ console.log("Done!");
@@ -1,6 +1,6 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
- const { run: jscodeshift } = require('jscodeshift/src/Runner');
3
+ const { run: jscodeshift } = require("jscodeshift/src/Runner");
4
4
 
5
5
  module.exports = (...args) => {
6
6
  return jscodeshift(...args);
@@ -1,23 +1,23 @@
1
1
  /* eslint-disable unicorn/prefer-array-some */
2
2
  /* eslint-disable unicorn/no-array-method-this-argument */
3
3
 
4
- 'use strict';
4
+ "use strict";
5
5
 
6
- const { prependImport } = require('../utils/prependImport.cjs');
6
+ const { prependImport } = require("../utils/prependImport.cjs");
7
7
 
8
8
  const jestToNodeGlobalMethods = {
9
- test: 'test',
10
- describe: 'describe',
11
- it: 'it',
12
- beforeAll: 'before',
13
- afterAll: 'after',
14
- beforeEach: 'beforeEach',
15
- afterEach: 'afterEach',
9
+ test: "test",
10
+ describe: "describe",
11
+ it: "it",
12
+ beforeAll: "before",
13
+ afterAll: "after",
14
+ beforeEach: "beforeEach",
15
+ afterEach: "afterEach",
16
16
  };
17
17
  const propertiesMap = {
18
- toBe: { default: 'equal', not: 'notEqual' },
19
- toStrictEqual: { default: 'equal', not: 'notEqual' },
20
- toEqual: { default: 'equal', not: 'notEqual' },
18
+ toBe: { default: "equal", not: "notEqual" },
19
+ toStrictEqual: { default: "equal", not: "notEqual" },
20
+ toEqual: { default: "equal", not: "notEqual" },
21
21
  };
22
22
 
23
23
  // const mocks = {
@@ -29,15 +29,15 @@ const propertiesMap = {
29
29
  module.exports = (file, api, options) => {
30
30
  if (
31
31
  !(
32
- file.path.endsWith('.test.js') ||
33
- file.path.endsWith('.test.ts') ||
34
- file.path.endsWith('.test.tsx')
32
+ file.path.endsWith(".test.js") ||
33
+ file.path.endsWith(".test.ts") ||
34
+ file.path.endsWith(".test.tsx")
35
35
  )
36
36
  ) {
37
37
  throw new Error(`Invalid file: "${file.path}"`);
38
38
  }
39
39
 
40
- const j = api.jscodeshift.withParser('tsx');
40
+ const j = api.jscodeshift.withParser("tsx");
41
41
  const root = j(file.source);
42
42
 
43
43
  function updateTopLevelMethods() {
@@ -68,7 +68,7 @@ module.exports = (file, api, options) => {
68
68
  [...topLevelMethodsUsed].map((method) =>
69
69
  j.importSpecifier(j.identifier(method)),
70
70
  ),
71
- j.stringLiteral('node:test'),
71
+ j.stringLiteral("node:test"),
72
72
  ),
73
73
  );
74
74
  }
@@ -80,7 +80,7 @@ module.exports = (file, api, options) => {
80
80
  root
81
81
  .find(j.MemberExpression, {
82
82
  object: {
83
- callee: { name: 'expect' },
83
+ callee: { name: "expect" },
84
84
  },
85
85
  })
86
86
  .forEach((memberExpressionPath) => {
@@ -88,7 +88,7 @@ module.exports = (file, api, options) => {
88
88
  let memberExpressionPathToReplace = memberExpressionPath;
89
89
  let propertyName = memberExpression.property.name;
90
90
  let not = false;
91
- if (propertyName === 'not') {
91
+ if (propertyName === "not") {
92
92
  not = true;
93
93
  memberExpressionPathToReplace = parentPath;
94
94
  propertyName = parentPath.value.property.name;
@@ -100,34 +100,35 @@ module.exports = (file, api, options) => {
100
100
  }
101
101
 
102
102
  const assertPropertyName =
103
- propertiesMap[propertyName][not ? 'not' : 'default'];
103
+ propertiesMap[propertyName][not ? "not" : "default"];
104
104
  const callExpressionPath = memberExpressionPathToReplace.parentPath;
105
105
 
106
- const argument0 = callExpressionPath.value.arguments[0];
107
- const argument1 = memberExpression.object.arguments[0];
106
+ const value = memberExpression.object.arguments[0];
107
+ const expected = callExpressionPath.value.arguments[0];
108
108
 
109
109
  hasAssertions = true;
110
110
  callExpressionPath.value.callee = j.memberExpression(
111
- j.identifier('assert'),
111
+ j.identifier("assert"),
112
112
  j.identifier(assertPropertyName),
113
113
  );
114
- callExpressionPath.value.arguments = [argument0, argument1];
114
+ callExpressionPath.value.arguments = [value, expected];
115
115
  });
116
116
 
117
117
  if (hasAssertions) {
118
118
  root
119
- .find(j.ImportDeclaration, { source: { value: 'node:assert' } })
119
+ .find(j.ImportDeclaration, { source: { value: "node:assert" } })
120
120
  .remove();
121
121
  if (
122
+ // eslint-disable-next-line no-constant-condition
122
123
  (!root.find(j.ImportDeclaration),
123
- { source: { value: 'node:assert/strict' } })
124
+ { source: { value: "node:assert/strict" } })
124
125
  ) {
125
126
  prependImport(
126
127
  j,
127
128
  root,
128
129
  j.importDeclaration(
129
- [j.importDefaultSpecifier(j.identifier('assert'))],
130
- j.stringLiteral('node:assert/strict'),
130
+ [j.importDefaultSpecifier(j.identifier("assert"))],
131
+ j.stringLiteral("node:assert/strict"),
131
132
  ),
132
133
  );
133
134
  }
@@ -138,7 +139,7 @@ module.exports = (file, api, options) => {
138
139
  updateAssertions();
139
140
 
140
141
  root
141
- .find(j.ImportDeclaration, { source: { value: '@jest/globals' } })
142
+ .find(j.ImportDeclaration, { source: { value: "@jest/globals" } })
142
143
  .remove();
143
144
 
144
145
  return root.toSource(options);
@@ -1,8 +1,8 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  /* eslint-disable complexity */
4
4
  module.exports = (file, api, options) => {
5
- const j = api.jscodeshift.withParser('tsx');
5
+ const j = api.jscodeshift.withParser("tsx");
6
6
  const root = j(file.source);
7
7
  let hasModifications = false;
8
8
 
@@ -10,38 +10,38 @@ module.exports = (file, api, options) => {
10
10
  const node = path.value;
11
11
  const { returnType } = node;
12
12
  if (returnType == null) return;
13
- if (returnType.type !== 'TSTypeAnnotation') return;
13
+ if (returnType.type !== "TSTypeAnnotation") return;
14
14
  const { typeAnnotation } = returnType;
15
15
  if (typeAnnotation == null) return;
16
16
 
17
17
  if (
18
- typeAnnotation.type === 'TSTypeReference' &&
18
+ typeAnnotation.type === "TSTypeReference" &&
19
19
  typeAnnotation.typeName &&
20
- typeAnnotation.typeName.type === 'Identifier'
20
+ typeAnnotation.typeName.type === "Identifier"
21
21
  ) {
22
- if (typeAnnotation.typeName.name === 'ReactElement') {
23
- typeAnnotation.typeName.name = 'ReactNode';
22
+ if (typeAnnotation.typeName.name === "ReactElement") {
23
+ typeAnnotation.typeName.name = "ReactNode";
24
24
  hasModifications = true;
25
25
  }
26
26
  } else if (
27
- typeAnnotation.type === 'TSUnionType' &&
27
+ typeAnnotation.type === "TSUnionType" &&
28
28
  typeAnnotation.types.every((unionTypeItem) => {
29
- if (unionTypeItem.type === 'TSTypeReference') {
29
+ if (unionTypeItem.type === "TSTypeReference") {
30
30
  return (
31
31
  unionTypeItem.typeName &&
32
- unionTypeItem.typeName.type === 'Identifier' &&
33
- unionTypeItem.typeName.name === 'ReactElement'
32
+ unionTypeItem.typeName.type === "Identifier" &&
33
+ unionTypeItem.typeName.name === "ReactElement"
34
34
  );
35
35
  }
36
36
 
37
- if (unionTypeItem.type === 'TSNullKeyword') {
37
+ if (unionTypeItem.type === "TSNullKeyword") {
38
38
  return true;
39
39
  }
40
40
 
41
41
  return false;
42
42
  })
43
43
  ) {
44
- returnType.typeAnnotation = j.tsTypeReference(j.identifier('ReactNode'));
44
+ returnType.typeAnnotation = j.tsTypeReference(j.identifier("ReactNode"));
45
45
  hasModifications = true;
46
46
  }
47
47
  }
@@ -51,23 +51,23 @@ module.exports = (file, api, options) => {
51
51
  if (hasModifications) {
52
52
  root.find(j.ImportDeclaration).forEach((importDeclaration) => {
53
53
  const { node } = importDeclaration;
54
- if (node.source.value !== 'react') return;
55
- if (node.importKind !== 'type') return;
54
+ if (node.source.value !== "react") return;
55
+ if (node.importKind !== "type") return;
56
56
 
57
57
  node.specifiers = node.specifiers.filter((importSpecifier) => {
58
- if (importSpecifier.local.name !== 'ReactElement') return true;
59
- if (importSpecifier.importKind !== 'value') return true;
58
+ if (importSpecifier.local.name !== "ReactElement") return true;
59
+ if (importSpecifier.importKind !== "value") return true;
60
60
  return (
61
61
  j(importDeclaration)
62
62
  .closestScope()
63
63
  // eslint-disable-next-line unicorn/no-array-method-this-argument -- not an array
64
- .find(j.Identifier, { name: 'ReactElement' })
64
+ .find(j.Identifier, { name: "ReactElement" })
65
65
  .filter((p) => {
66
66
  if (p.value.start === importSpecifier.local.start) return false;
67
- if (p.parentPath.value.type === 'Property' && p.name === 'key') {
67
+ if (p.parentPath.value.type === "Property" && p.name === "key") {
68
68
  return false;
69
69
  }
70
- if (p.name === 'property') return false;
70
+ if (p.name === "property") return false;
71
71
  return true;
72
72
  })
73
73
  .size() > 0
@@ -76,11 +76,11 @@ module.exports = (file, api, options) => {
76
76
  if (
77
77
  !node.specifiers.some(
78
78
  (specifier) =>
79
- specifier.type === 'ImportSpecifier' &&
80
- specifier.imported.name === 'ReactNode',
79
+ specifier.type === "ImportSpecifier" &&
80
+ specifier.imported.name === "ReactNode",
81
81
  )
82
82
  ) {
83
- node.specifiers.push(j.importSpecifier(j.identifier('ReactNode')));
83
+ node.specifiers.push(j.importSpecifier(j.identifier("ReactNode")));
84
84
  }
85
85
  });
86
86
  }
@@ -1,4 +1,4 @@
1
- 'use strict';
1
+ "use strict";
2
2
 
3
3
  exports.prependImport = (j, source, importDeclaration) => {
4
4
  const existingImports = source.find(j.ImportDeclaration);
@@ -14,7 +14,7 @@ exports.prependImport = (j, source, importDeclaration) => {
14
14
  return;
15
15
  }
16
16
 
17
- const firstNode = source.find(j.Program).get('body', 0).node;
17
+ const firstNode = source.find(j.Program).get("body", 0).node;
18
18
  const { comments } = firstNode;
19
19
  if (comments?.length) {
20
20
  const comment = comments[0];
@@ -23,13 +23,13 @@ exports.prependImport = (j, source, importDeclaration) => {
23
23
  // line-level and JSDoc-style comments because these probably belong
24
24
  // to the first node, rather than the file.
25
25
  if (
26
- (comment.type === 'Block' || comment.type === 'CommentBlock') &&
27
- !comment.value.startsWith('*')
26
+ (comment.type === "Block" || comment.type === "CommentBlock") &&
27
+ !comment.value.startsWith("*")
28
28
  ) {
29
29
  importDeclaration.comments = comments;
30
30
  firstNode.comments = null;
31
31
  }
32
32
  }
33
33
 
34
- source.get('program', 'body').unshift(importDeclaration);
34
+ source.get("program", "body").unshift(importDeclaration);
35
35
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pob/codemods",
3
- "version": "0.0.0",
3
+ "version": "0.1.1",
4
4
  "description": "codemods for pob",
5
5
  "keywords": [],
6
6
  "author": "Christophe Hurpeau <302891+christophehurpeau@users.noreply.github.com> (https://christophe.hurpeau.com)",
@@ -34,8 +34,8 @@
34
34
  "pob": {},
35
35
  "prettier": "@pob/root/prettier-config",
36
36
  "dependencies": {
37
- "glob": "10.3.10",
38
- "jscodeshift": "0.15.1"
37
+ "glob": "10.4.1",
38
+ "jscodeshift": "0.15.2"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/jscodeshift": "0.11.11"