ts-codemod-lib 3.5.1 → 4.0.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.
Files changed (59) hide show
  1. package/README.md +7 -3
  2. package/dist/functions/ast-transformers/append-as-const.d.mts +20 -12
  3. package/dist/functions/ast-transformers/append-as-const.d.mts.map +1 -1
  4. package/dist/functions/ast-transformers/append-as-const.mjs +170 -88
  5. package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
  6. package/dist/functions/ast-transformers/convert-to-readonly.d.mts +25 -6
  7. package/dist/functions/ast-transformers/convert-to-readonly.d.mts.map +1 -1
  8. package/dist/functions/ast-transformers/convert-to-readonly.mjs +272 -255
  9. package/dist/functions/ast-transformers/convert-to-readonly.mjs.map +1 -1
  10. package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.d.mts +8 -8
  11. package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.mjs +103 -104
  12. package/dist/functions/ast-transformers/enable-no-unchecked-indexed-access.mjs.map +1 -1
  13. package/dist/functions/ast-transformers/replace-record-with-unknown-record.d.mts +2 -2
  14. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs +51 -51
  15. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs.map +1 -1
  16. package/dist/functions/ast-transformers/transform-source-code.d.mts.map +1 -1
  17. package/dist/functions/ast-transformers/transform-source-code.mjs +69 -69
  18. package/dist/functions/ast-transformers/transform-source-code.mjs.map +1 -1
  19. package/dist/functions/constants/index.d.mts +1 -0
  20. package/dist/functions/constants/index.d.mts.map +1 -1
  21. package/dist/functions/constants/index.mjs +1 -0
  22. package/dist/functions/constants/index.mjs.map +1 -1
  23. package/dist/functions/constants/mutable-prefixes.d.mts +23 -0
  24. package/dist/functions/constants/mutable-prefixes.d.mts.map +1 -0
  25. package/dist/functions/constants/mutable-prefixes.mjs +28 -0
  26. package/dist/functions/constants/mutable-prefixes.mjs.map +1 -0
  27. package/dist/functions/functions/has-disable-next-line-comment.mjs +23 -23
  28. package/dist/functions/functions/has-disable-next-line-comment.mjs.map +1 -1
  29. package/dist/functions/functions/is-readonly-node.d.mts +2 -2
  30. package/dist/functions/functions/is-readonly-node.d.mts.map +1 -1
  31. package/dist/functions/functions/is-readonly-node.mjs +13 -13
  32. package/dist/functions/functions/is-readonly-node.mjs.map +1 -1
  33. package/dist/functions/functions/wrap-with-parentheses.d.mts +2 -2
  34. package/dist/functions/functions/wrap-with-parentheses.d.mts.map +1 -1
  35. package/dist/functions/functions/wrap-with-parentheses.mjs +87 -32
  36. package/dist/functions/functions/wrap-with-parentheses.mjs.map +1 -1
  37. package/dist/functions/utils/replace-node-with-text.d.mts +10 -11
  38. package/dist/functions/utils/replace-node-with-text.d.mts.map +1 -1
  39. package/dist/functions/utils/replace-node-with-text.mjs +0 -1
  40. package/dist/functions/utils/replace-node-with-text.mjs.map +1 -1
  41. package/package.json +6 -6
  42. package/src/functions/ast-transformers/append-as-const.mts +319 -174
  43. package/src/functions/ast-transformers/append-as-const.test.mts +118 -7
  44. package/src/functions/ast-transformers/convert-to-readonly.mts +568 -489
  45. package/src/functions/ast-transformers/convert-to-readonly.test.mts +381 -74
  46. package/src/functions/ast-transformers/enable-no-unchecked-indexed-access.mts +178 -179
  47. package/src/functions/ast-transformers/enable-no-unchecked-indexed-access.test.mts +29 -29
  48. package/src/functions/ast-transformers/replace-any-with-unknown.test.mts +6 -6
  49. package/src/functions/ast-transformers/replace-record-with-unknown-record.mts +75 -75
  50. package/src/functions/ast-transformers/replace-record-with-unknown-record.test.mts +6 -6
  51. package/src/functions/ast-transformers/transform-source-code.mts +110 -110
  52. package/src/functions/ast-transformers/transformer-specific-ignore.test.mts +6 -6
  53. package/src/functions/constants/index.mts +1 -0
  54. package/src/functions/constants/mutable-prefixes.mts +28 -0
  55. package/src/functions/functions/has-disable-next-line-comment.mts +35 -35
  56. package/src/functions/functions/is-readonly-node.mts +25 -25
  57. package/src/functions/functions/wrap-with-parentheses.mts +138 -52
  58. package/src/functions/functions/wrap-with-parentheses.test.mts +115 -0
  59. package/src/functions/utils/replace-node-with-text.mts +10 -11
@@ -8,6 +8,12 @@ import * as prettier from 'prettier/standalone';
8
8
  import { appendAsConstTransformer } from './append-as-const.mjs';
9
9
  import { transformSourceCode } from './transform-source-code.mjs';
10
10
 
11
+ const formatter = (code: string): Promise<string> =>
12
+ prettier.format(code, {
13
+ parser: 'typescript',
14
+ plugins: [prettierPluginTypeScript, prettierPluginEstree],
15
+ });
16
+
11
17
  const testFn = async ({
12
18
  source,
13
19
  expected,
@@ -38,12 +44,6 @@ const testFn = async ({
38
44
  expect(transformed).toBe(expectedFormatted);
39
45
  };
40
46
 
41
- const formatter = (code: string): Promise<string> =>
42
- prettier.format(code, {
43
- parser: 'typescript',
44
- plugins: [prettierPluginTypeScript, prettierPluginEstree],
45
- });
46
-
47
47
  describe(appendAsConstTransformer, () => {
48
48
  describe('Primitive types (should not add as const)', () => {
49
49
  test.each([
@@ -794,6 +794,39 @@ describe(appendAsConstTransformer, () => {
794
794
  const m = h([1], [2] as const);
795
795
  `,
796
796
  },
797
+ {
798
+ name: 'a declared this parameter should not shift arguments onto the parameter to their left',
799
+ source: dedent`
800
+ declare function f<const T, U>(this: void, a: T, b: U): U;
801
+ const r = f(1, [1, 2] as const);
802
+ `,
803
+ expected: dedent`
804
+ declare function f<const T, U>(this: void, a: T, b: U): U;
805
+ const r = f(1, [1, 2] as const);
806
+ `,
807
+ },
808
+ {
809
+ name: 'a declared this parameter should still let the matching argument lose as const',
810
+ source: dedent`
811
+ declare function f<const T, U>(this: void, a: T, b: U): T;
812
+ const r = f([1, 2] as const, [3] as const);
813
+ `,
814
+ expected: dedent`
815
+ declare function f<const T, U>(this: void, a: T, b: U): T;
816
+ const r = f([1, 2], [3] as const);
817
+ `,
818
+ },
819
+ {
820
+ name: 'a this parameter in a function type should be skipped too',
821
+ source: dedent`
822
+ declare const g: <const T>(this: void, x: T) => T;
823
+ const r = g([1] as const);
824
+ `,
825
+ expected: dedent`
826
+ declare const g: <const T>(this: void, x: T) => T;
827
+ const r = g([1]);
828
+ `,
829
+ },
797
830
  {
798
831
  name: 'non-const type parameter should keep as const',
799
832
  source: dedent`
@@ -1013,7 +1046,25 @@ describe(appendAsConstTransformer, () => {
1013
1046
  ])('$name', testFn);
1014
1047
  });
1015
1048
 
1016
- describe('ignorePrefixes option (mut_ prefix)', () => {
1049
+ describe('mutableVariablePrefixes option (mut_ prefix)', () => {
1050
+ test('the removed ignorePrefixes option is refused', () => {
1051
+ expect(() =>
1052
+ appendAsConstTransformer(
1053
+ // @ts-expect-error removed in 4.0.0
1054
+ { ignorePrefixes: ['mut_'] },
1055
+ ),
1056
+ ).toThrow('mutableVariablePrefixes');
1057
+ });
1058
+
1059
+ test('an unset ignorePrefixes option is accepted', () => {
1060
+ expect(() =>
1061
+ appendAsConstTransformer(
1062
+ // @ts-expect-error removed in 4.0.0
1063
+ { ignorePrefixes: undefined },
1064
+ ),
1065
+ ).not.toThrow();
1066
+ });
1067
+
1017
1068
  test.each([
1018
1069
  {
1019
1070
  name: 'single variable with mut_ prefix',
@@ -1025,6 +1076,66 @@ describe(appendAsConstTransformer, () => {
1025
1076
  source: 'const mut_a = [1, 2], mut_b = { c: 3 };',
1026
1077
  expected: 'const mut_a = [1, 2], mut_b = { c: 3 };',
1027
1078
  },
1079
+ {
1080
+ name: '"draft" and "_mut_" are not default prefixes',
1081
+ source: 'const draft = { a: [1] }, _mut_b = [2];',
1082
+ expected: 'const draft = { a: [1] } as const, _mut_b = [2] as const;',
1083
+ },
1084
+ {
1085
+ name: 'a variable annotated with a mutable type gets no as const',
1086
+ source: dedent`
1087
+ const a: MutableFoo = { b: [1] };
1088
+ const c: ns.MutableFoo | undefined = { b: [1] };
1089
+ const d: readonly MutableFoo[] = [{ b: [1] }];
1090
+ const f: mut_Foo = { b: [1] };
1091
+ const g: import('./m').MutableFoo = { b: [1] };
1092
+ const h = { b: [1] } satisfies MutableFoo;
1093
+ const i = (): MutableFoo => ({ b: [1] });
1094
+ const j: typeof mut_x = [1];
1095
+ const k: typeof mut_y.items = [1];
1096
+ const l: typeof import('./m').mut_z = [1];
1097
+ const m: InstanceType<typeof MutableStore> = { items: [] };
1098
+ const n: InstanceType<typeof import('./m').MutableStore> = { items: [] };
1099
+ const o = ((): MutableFoo => ({ b: [1] }));
1100
+ const p = cond ? ({ b: [1] } satisfies MutableFoo) : q;
1101
+ const e: Foo = { b: [1] };
1102
+ `,
1103
+ expected: dedent`
1104
+ const a: MutableFoo = { b: [1] };
1105
+ const c: ns.MutableFoo | undefined = { b: [1] };
1106
+ const d: readonly MutableFoo[] = [{ b: [1] }];
1107
+ const f: mut_Foo = { b: [1] };
1108
+ const g: import('./m').MutableFoo = { b: [1] };
1109
+ const h = { b: [1] } satisfies MutableFoo;
1110
+ const i = (): MutableFoo => ({ b: [1] });
1111
+ const j: typeof mut_x = [1];
1112
+ const k: typeof mut_y.items = [1];
1113
+ const l: typeof import('./m').mut_z = [1];
1114
+ const m: InstanceType<typeof MutableStore> = { items: [] };
1115
+ const n: InstanceType<typeof import('./m').MutableStore> = { items: [] };
1116
+ const o = ((): MutableFoo => ({ b: [1] }));
1117
+ const p = cond ? ({ b: [1] } satisfies MutableFoo) : q;
1118
+ const e: Foo = { b: [1] } as const;
1119
+ `,
1120
+ },
1121
+ {
1122
+ name: 'mutableTypePrefixes replaces the default',
1123
+ source: dedent`
1124
+ const a: MutableFoo = { b: [1] };
1125
+ const c: MyFoo = { b: [1] };
1126
+ `,
1127
+ expected: dedent`
1128
+ const a: MutableFoo = { b: [1] } as const;
1129
+ const c: MyFoo = { b: [1] };
1130
+ `,
1131
+ options: { mutableTypePrefixes: ['My'] },
1132
+ },
1133
+ {
1134
+ name: 'the prefixes named in the option replace the default',
1135
+ source: 'const draft = { a: [1] }, mut_b = [2];',
1136
+ expected: 'const draft = { a: [1] }, mut_b = [2] as const;',
1137
+ options: { mutableVariablePrefixes: ['draft'] },
1138
+ },
1028
1139
  ])('$name', testFn);
1029
1140
  });
1030
1141