ts-codemod-lib 3.4.4 → 3.5.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 (42) hide show
  1. package/dist/functions/ast-transformers/append-as-const.d.mts.map +1 -1
  2. package/dist/functions/ast-transformers/append-as-const.mjs +3 -3
  3. package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
  4. package/dist/functions/ast-transformers/convert-interface-to-type.d.mts.map +1 -1
  5. package/dist/functions/ast-transformers/convert-interface-to-type.mjs +2 -1
  6. package/dist/functions/ast-transformers/convert-interface-to-type.mjs.map +1 -1
  7. package/dist/functions/ast-transformers/convert-to-readonly.d.mts.map +1 -1
  8. package/dist/functions/ast-transformers/convert-to-readonly.mjs +2 -2
  9. package/dist/functions/ast-transformers/convert-to-readonly.mjs.map +1 -1
  10. package/dist/functions/ast-transformers/replace-any-with-unknown.d.mts.map +1 -1
  11. package/dist/functions/ast-transformers/replace-any-with-unknown.mjs +3 -2
  12. package/dist/functions/ast-transformers/replace-any-with-unknown.mjs.map +1 -1
  13. package/dist/functions/ast-transformers/replace-record-with-unknown-record.d.mts.map +1 -1
  14. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs +6 -5
  15. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs.map +1 -1
  16. package/dist/functions/functions/has-disable-next-line-comment.d.mts.map +1 -1
  17. package/dist/functions/functions/has-disable-next-line-comment.mjs +56 -4
  18. package/dist/functions/functions/has-disable-next-line-comment.mjs.map +1 -1
  19. package/dist/functions/utils/index.d.mts +1 -0
  20. package/dist/functions/utils/index.d.mts.map +1 -1
  21. package/dist/functions/utils/index.mjs +1 -0
  22. package/dist/functions/utils/index.mjs.map +1 -1
  23. package/dist/functions/utils/replace-node-with-text.d.mts +21 -0
  24. package/dist/functions/utils/replace-node-with-text.d.mts.map +1 -0
  25. package/dist/functions/utils/replace-node-with-text.mjs +22 -0
  26. package/dist/functions/utils/replace-node-with-text.mjs.map +1 -0
  27. package/dist/functions/utils/replace-with-debug.d.mts.map +1 -1
  28. package/dist/functions/utils/replace-with-debug.mjs +2 -1
  29. package/dist/functions/utils/replace-with-debug.mjs.map +1 -1
  30. package/package.json +13 -13
  31. package/src/functions/ast-transformers/append-as-const.mts +6 -3
  32. package/src/functions/ast-transformers/convert-interface-to-type.mts +2 -1
  33. package/src/functions/ast-transformers/convert-to-readonly.mts +5 -2
  34. package/src/functions/ast-transformers/convert-to-readonly.test.mts +9 -20
  35. package/src/functions/ast-transformers/replace-any-with-unknown.mts +3 -2
  36. package/src/functions/ast-transformers/replace-record-with-unknown-record.mts +7 -5
  37. package/src/functions/functions/has-disable-next-line-comment.mts +79 -6
  38. package/src/functions/functions/has-disable-next-line-comment.test.mts +96 -0
  39. package/src/functions/utils/index.mts +1 -0
  40. package/src/functions/utils/replace-node-with-text.mts +37 -0
  41. package/src/functions/utils/replace-node-with-text.test.mts +89 -0
  42. package/src/functions/utils/replace-with-debug.mts +2 -1
@@ -0,0 +1,89 @@
1
+ import dedent from 'dedent';
2
+ import * as tsm from 'ts-morph';
3
+ import { replaceNodeWithText } from './replace-node-with-text.mjs';
4
+
5
+ /**
6
+ * Replaces the first type reference named `target` in `source`, once with
7
+ * `replaceNodeWithText` and once with ts-morph's own `replaceWithText`.
8
+ */
9
+ const replaceBothWays = (
10
+ source: string,
11
+ target: string,
12
+ newNodeText: string,
13
+ ): Readonly<{ actual: string; expected: string; replacedKind: string }> => {
14
+ const replace = (
15
+ fn: (nodeToReplace: tsm.Node, text: string) => tsm.Node,
16
+ ): Readonly<{ text: string; replacedKind: string }> => {
17
+ const project = new tsm.Project({ useInMemoryFileSystem: true });
18
+
19
+ const sourceFile = project.createSourceFile('source.ts', source);
20
+
21
+ const node = sourceFile
22
+ .getDescendantsOfKind(tsm.SyntaxKind.TypeReference)
23
+ .find((n) => n.getText() === target);
24
+
25
+ assert.isDefined(node);
26
+
27
+ const replaced = fn(node, newNodeText);
28
+
29
+ return {
30
+ text: sourceFile.getFullText(),
31
+ replacedKind: replaced.getKindName(),
32
+ };
33
+ };
34
+
35
+ const actual = replace(replaceNodeWithText);
36
+
37
+ const expected = replace((node, text) => node.replaceWithText(text));
38
+
39
+ assert.strictEqual(actual.replacedKind, expected.replacedKind);
40
+
41
+ return {
42
+ actual: actual.text,
43
+ expected: expected.text,
44
+ replacedKind: actual.replacedKind,
45
+ };
46
+ };
47
+
48
+ const indentedSource = dedent`
49
+ namespace A {
50
+ namespace B {
51
+ export type C = {
52
+ readonly d: Foo;
53
+ };
54
+ }
55
+ }
56
+ `;
57
+
58
+ describe(replaceNodeWithText, () => {
59
+ test.each([
60
+ { name: 'a one-line text', newNodeText: 'readonly number[]' },
61
+ { name: 'an empty text', newNodeText: '' },
62
+ { name: 'a text with a lone CR', newNodeText: 'Bar\r' },
63
+ { name: 'a text with a trailing space', newNodeText: 'Bar ' },
64
+ { name: 'a text with a string literal', newNodeText: '\'a\' | "b"' },
65
+ { name: 'a text with a line comment', newNodeText: 'Bar /* c */' },
66
+ { name: 'a multi-line text', newNodeText: '{\n a: number;\n}' },
67
+ { name: 'a CRLF text', newNodeText: '{\r\n a: number;\r\n}' },
68
+ ])('writes the text replaceWithText writes: $name', ({ newNodeText }) => {
69
+ const { actual, expected } = replaceBothWays(
70
+ indentedSource,
71
+ 'Foo',
72
+ newNodeText,
73
+ );
74
+
75
+ expect(actual).toBe(expected);
76
+ });
77
+
78
+ test('returns the node that took the place of the replaced one', () => {
79
+ const { actual, replacedKind } = replaceBothWays(
80
+ indentedSource,
81
+ 'Foo',
82
+ 'Readonly<Bar>',
83
+ );
84
+
85
+ expect(replacedKind).toBe('TypeReference');
86
+
87
+ expect(actual).toContain('readonly d: Readonly<Bar>;');
88
+ });
89
+ });
@@ -1,4 +1,5 @@
1
1
  import type * as tsm from 'ts-morph';
2
+ import { replaceNodeWithText } from './replace-node-with-text.mjs';
2
3
 
3
4
  export const replaceNodeWithDebugPrint = (
4
5
  node: tsm.Node,
@@ -6,5 +7,5 @@ export const replaceNodeWithDebugPrint = (
6
7
  ): void => {
7
8
  console.debug(`${node.getText()} -> ${newNodeText}`);
8
9
 
9
- node.replaceWithText(newNodeText);
10
+ replaceNodeWithText(node, newNodeText);
10
11
  };