ts-codemod-lib 1.1.3 → 1.3.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 (58) hide show
  1. package/README.md +0 -4
  2. package/dist/cmd/append-as-const.mjs +1 -1
  3. package/dist/cmd/convert-interface-to-type.mjs +1 -1
  4. package/dist/cmd/convert-to-readonly.mjs +1 -1
  5. package/dist/cmd/replace-any-with-unknown.mjs +1 -1
  6. package/dist/cmd/replace-record-with-unknown-record.mjs +1 -1
  7. package/dist/entry-point.mjs +1 -1
  8. package/dist/functions/ast-transformers/append-as-const.d.mts.map +1 -1
  9. package/dist/functions/ast-transformers/append-as-const.mjs +11 -5
  10. package/dist/functions/ast-transformers/append-as-const.mjs.map +1 -1
  11. package/dist/functions/ast-transformers/convert-interface-to-type.d.mts.map +1 -1
  12. package/dist/functions/ast-transformers/convert-interface-to-type.mjs +71 -66
  13. package/dist/functions/ast-transformers/convert-interface-to-type.mjs.map +1 -1
  14. package/dist/functions/ast-transformers/convert-to-readonly-type.d.mts +1 -1
  15. package/dist/functions/ast-transformers/convert-to-readonly-type.d.mts.map +1 -1
  16. package/dist/functions/ast-transformers/convert-to-readonly-type.mjs +13 -7
  17. package/dist/functions/ast-transformers/convert-to-readonly-type.mjs.map +1 -1
  18. package/dist/functions/ast-transformers/replace-any-with-unknown.d.mts.map +1 -1
  19. package/dist/functions/ast-transformers/replace-any-with-unknown.mjs +35 -26
  20. package/dist/functions/ast-transformers/replace-any-with-unknown.mjs.map +1 -1
  21. package/dist/functions/ast-transformers/replace-record-with-unknown-record.d.mts.map +1 -1
  22. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs +141 -135
  23. package/dist/functions/ast-transformers/replace-record-with-unknown-record.mjs.map +1 -1
  24. package/dist/functions/ast-transformers/transform-source-code.d.mts.map +1 -1
  25. package/dist/functions/ast-transformers/transform-source-code.mjs +40 -7
  26. package/dist/functions/ast-transformers/transform-source-code.mjs.map +1 -1
  27. package/dist/functions/ast-transformers/types.d.mts +9 -1
  28. package/dist/functions/ast-transformers/types.d.mts.map +1 -1
  29. package/dist/functions/constants/ignore-comment-text.d.mts +2 -2
  30. package/dist/functions/constants/ignore-comment-text.d.mts.map +1 -1
  31. package/dist/functions/constants/ignore-comment-text.mjs +3 -3
  32. package/dist/functions/constants/ignore-comment-text.mjs.map +1 -1
  33. package/dist/functions/constants/index.mjs +1 -1
  34. package/dist/functions/functions/has-disable-next-line-comment.d.mts +8 -2
  35. package/dist/functions/functions/has-disable-next-line-comment.d.mts.map +1 -1
  36. package/dist/functions/functions/has-disable-next-line-comment.mjs +31 -6
  37. package/dist/functions/functions/has-disable-next-line-comment.mjs.map +1 -1
  38. package/dist/functions/index.mjs +1 -1
  39. package/dist/index.mjs +1 -1
  40. package/package.json +9 -9
  41. package/src/cmd/append-as-const.mts +1 -1
  42. package/src/cmd/convert-interface-to-type.mts +1 -1
  43. package/src/cmd/convert-to-readonly.mts +1 -1
  44. package/src/cmd/replace-any-with-unknown.mts +1 -1
  45. package/src/cmd/replace-record-with-unknown-record.mts +1 -1
  46. package/src/functions/ast-transformers/append-as-const.mts +18 -9
  47. package/src/functions/ast-transformers/append-as-const.test.mts +58 -0
  48. package/src/functions/ast-transformers/convert-interface-to-type.mts +8 -2
  49. package/src/functions/ast-transformers/convert-to-readonly-type.mts +41 -32
  50. package/src/functions/ast-transformers/convert-to-readonly-type.test.mts +13 -0
  51. package/src/functions/ast-transformers/replace-any-with-unknown.mts +41 -28
  52. package/src/functions/ast-transformers/replace-any-with-unknown.test.mts +58 -0
  53. package/src/functions/ast-transformers/replace-record-with-unknown-record.mts +175 -167
  54. package/src/functions/ast-transformers/transform-source-code.mts +59 -9
  55. package/src/functions/ast-transformers/transformer-specific-ignore.test.mts +190 -0
  56. package/src/functions/ast-transformers/types.mts +10 -3
  57. package/src/functions/constants/ignore-comment-text.mts +2 -2
  58. package/src/functions/functions/has-disable-next-line-comment.mts +39 -8
@@ -1,6 +1,13 @@
1
1
  import type * as tsm from 'ts-morph';
2
2
 
3
- export type TsMorphTransformer = (
3
+ export type TsMorphTransformer = {
4
4
  // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
5
- sourceFile: tsm.SourceFile,
6
- ) => void;
5
+ (sourceFile: tsm.SourceFile): void;
6
+
7
+ /**
8
+ * Optional name identifier for this transformer.
9
+ * Used to enable transformer-specific ignore comments.
10
+ * Example: 'append-as-const', 'replace-any-with-unknown'
11
+ */
12
+ transformerName?: string;
13
+ };
@@ -1,3 +1,3 @@
1
- export const IGNORE_LINE_COMMENT_TEXT = 'transformer-ignore-next-line';
1
+ export const IGNORE_LINE_COMMENT_PREFIX = 'transformer-ignore-next-line';
2
2
 
3
- export const IGNORE_FILE_COMMENT_TEXT = '/* transformer-ignore */';
3
+ export const IGNORE_FILE_COMMENT_PREFIX = 'transformer-ignore';
@@ -1,14 +1,23 @@
1
1
  import * as tsm from 'ts-morph';
2
- import { IGNORE_LINE_COMMENT_TEXT } from '../constants/index.mjs';
2
+ import { IGNORE_LINE_COMMENT_PREFIX } from '../constants/index.mjs';
3
3
 
4
4
  /**
5
5
  * Checks if a given ts-morph Node is immediately preceded by a
6
- * '// transformer-ignore-next-line' comment.
6
+ * '// transformer-ignore-next-line' comment, optionally filtered by transformer name(s).
7
7
  *
8
8
  * @param node - The ts-morph Node to check.
9
+ * @param transformerName - Optional transformer name to check for specific ignore directive.
10
+ * If provided, only ignores if the comment specifies this transformer.
11
+ * Supports ESLint-style syntax like:
12
+ * - `// transformer-ignore-next-line` (ignores all transformers)
13
+ * - `// transformer-ignore-next-line append-as-const` (specific transformer)
14
+ * - `// transformer-ignore-next-line append-as-const, replace-any-with-unknown` (multiple transformers)
9
15
  * @returns True if the node is preceded by the ignore comment on the immediately previous line, false otherwise.
10
16
  */
11
- export const hasDisableNextLineComment = (node: tsm.Node): boolean => {
17
+ export const hasDisableNextLineComment = (
18
+ node: tsm.Node,
19
+ transformerName?: string,
20
+ ): boolean => {
12
21
  const nodeStartLine = node.getStartLineNumber();
13
22
 
14
23
  // Cannot be ignored if it's on the first line
@@ -33,11 +42,33 @@ export const hasDisableNextLineComment = (node: tsm.Node): boolean => {
33
42
  // Check if the comment is on the immediately preceding line
34
43
  if (nodeStartLine === commentEndLine + 1) {
35
44
  // Check if it's a single-line comment containing the specific ignore text
36
- if (
37
- commentRange.getKind() === tsm.SyntaxKind.SingleLineCommentTrivia &&
38
- commentRange.getText().trim().includes(IGNORE_LINE_COMMENT_TEXT)
39
- ) {
40
- return true;
45
+ if (commentRange.getKind() === tsm.SyntaxKind.SingleLineCommentTrivia) {
46
+ const commentText = commentRange.getText().trim();
47
+
48
+ if (commentText.includes(IGNORE_LINE_COMMENT_PREFIX)) {
49
+ // Extract the part after the prefix
50
+ const afterPrefix = commentText
51
+ .slice(commentText.indexOf(IGNORE_LINE_COMMENT_PREFIX))
52
+ .replace(IGNORE_LINE_COMMENT_PREFIX, '')
53
+ .trim();
54
+
55
+ // If no transformer name specified, check if comment applies to all transformers
56
+ if (transformerName === undefined) {
57
+ return true;
58
+ }
59
+
60
+ // If no specific transformers listed in comment, it applies to all
61
+ if (afterPrefix === '') {
62
+ return true;
63
+ }
64
+
65
+ // Check if the transformer name is in the comma-separated list
66
+ const targetTransformers = afterPrefix
67
+ .split(',')
68
+ .map((name) => name.trim());
69
+
70
+ return targetTransformers.includes(transformerName);
71
+ }
41
72
  }
42
73
 
43
74
  // If we found *any* comment on the preceding line, but it wasn't